MoochoPack: Miscellaneous Utilities for MOOCHO Version of the Day
OptionsFromStreamPack_CommandLineOptionsFromStreamProcessor.cpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "OptionsFromStreamPack_CommandLineOptionsFromStreamProcessor.hpp"
00043 
00044 // Define this if you want to debug the parser
00045 //#define PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00046 
00047 namespace OptionsFromStreamPack {
00048 
00049 CommandLineOptionsFromStreamProcessor::CommandLineOptionsFromStreamProcessor(
00050   const std::string  &options_file_name_opt_name
00051   ,const std::string &options_file_name_opt_doc
00052   ,const std::string &options_file_name
00053   ,const std::string &extra_options_str_opt_name
00054   ,const std::string &extra_options_str_opt_doc
00055   ,const std::string &extra_options_str
00056   )
00057   :options_file_name_opt_name_(options_file_name_opt_name)
00058   ,options_file_name_opt_doc_(options_file_name_opt_doc)
00059   ,options_file_name_(options_file_name)
00060   ,extra_options_str_opt_name_(extra_options_str_opt_name)
00061   ,extra_options_str_opt_doc_(extra_options_str_opt_doc)
00062   ,extra_options_str_(extra_options_str)
00063 {}
00064 
00065 void CommandLineOptionsFromStreamProcessor::set_options(
00066   Teuchos::RCP<OptionsFromStream> const& options
00067   )
00068 {
00069   options_ = options;
00070 }
00071 
00072 Teuchos::RCP<OptionsFromStream>
00073 CommandLineOptionsFromStreamProcessor::get_options() const
00074 {
00075   return options_;
00076 }
00077 
00078 void CommandLineOptionsFromStreamProcessor::setup_commandline_processor(
00079   Teuchos::CommandLineProcessor *clp
00080   )
00081 {
00082   clp->setOption(options_file_name_opt_name().c_str(),&options_file_name_,options_file_name_opt_doc().c_str());
00083   clp->setOption(extra_options_str_opt_name().c_str(),&extra_options_str_,extra_options_str_opt_doc().c_str());
00084 }
00085 
00086 void CommandLineOptionsFromStreamProcessor::process_options()
00087 {
00088   // Process the file options first
00089   if(options_file_name_.length()) {
00090     std::ifstream options_in(options_file_name_.c_str());
00091     if(options_in) {
00092       if(!options_.get())
00093         options_ = Teuchos::rcp(new OptionsFromStream());
00094       options_->read_options(options_in);
00095     }
00096   }
00097   // Process the extra commandline options
00098   const int len = extra_options_str_.length();
00099   if(len) {
00100     if(!options_.get())
00101       options_ = Teuchos::rcp(new OptionsFromStream());
00102     const char colon = ':';
00103     const std::string::size_type npos = std::string::npos;
00104     std::ostringstream ooptsstream;
00105     ooptsstream << "\nbegin_options\n\n";
00106     std::string::size_type start_i = 0, last_i = 0;
00107     while(true) {
00108       last_i = extra_options_str_.find(colon,start_i);
00109       std::string optgroup = extra_options_str_.substr(start_i,last_i-start_i);
00110 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00111       std::cout << "\nstart_i = " << start_i;
00112       std::cout << "\nlast_i = " << last_i;
00113       std::cout << "\noptgroup (before replacement) = \""<<optgroup<<"\"\n";
00114 #endif
00115       std::replace( optgroup.begin(), optgroup.end(), ',',';' ); // See above!
00116 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00117       std::cout << "\noptgroup (after replacement) = \""<<optgroup<<"\"\n";
00118 #endif
00119       ooptsstream << "options_group " << optgroup << "\n";
00120       if(last_i == npos) break;
00121       start_i = last_i + 1;
00122     }
00123     ooptsstream << "\nend_options\n";
00124     const std::string options_str = ooptsstream.str();
00125 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00126     std::cout << "options_str:\n" << options_str;
00127 #endif
00128     std::istringstream ioptsstream(options_str);
00129     options_->read_options(ioptsstream);
00130   }
00131 }
00132 
00133 Teuchos::RCP<OptionsFromStream>
00134 CommandLineOptionsFromStreamProcessor::process_and_get_options()
00135 {
00136   process_options();
00137   return get_options();
00138 }
00139 
00140 } // end namespace OptionsFromStreamPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends