MOOCHO (Single Doxygen Collection) Version of the Day
OptionsFromStreamPack_CommandLineOptionsFromStreamProcessor.cpp
Go to the documentation of this file.
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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "OptionsFromStreamPack_CommandLineOptionsFromStreamProcessor.hpp"
00030 
00031 // Define this if you want to debug the parser
00032 //#define PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00033 
00034 namespace OptionsFromStreamPack {
00035 
00036 CommandLineOptionsFromStreamProcessor::CommandLineOptionsFromStreamProcessor(
00037   const std::string  &options_file_name_opt_name
00038   ,const std::string &options_file_name_opt_doc
00039   ,const std::string &options_file_name
00040   ,const std::string &extra_options_str_opt_name
00041   ,const std::string &extra_options_str_opt_doc
00042   ,const std::string &extra_options_str
00043   )
00044   :options_file_name_opt_name_(options_file_name_opt_name)
00045   ,options_file_name_opt_doc_(options_file_name_opt_doc)
00046   ,options_file_name_(options_file_name)
00047   ,extra_options_str_opt_name_(extra_options_str_opt_name)
00048   ,extra_options_str_opt_doc_(extra_options_str_opt_doc)
00049   ,extra_options_str_(extra_options_str)
00050 {}
00051 
00052 void CommandLineOptionsFromStreamProcessor::set_options(
00053   Teuchos::RCP<OptionsFromStream> const& options
00054   )
00055 {
00056   options_ = options;
00057 }
00058 
00059 Teuchos::RCP<OptionsFromStream>
00060 CommandLineOptionsFromStreamProcessor::get_options() const
00061 {
00062   return options_;
00063 }
00064 
00065 void CommandLineOptionsFromStreamProcessor::setup_commandline_processor(
00066   Teuchos::CommandLineProcessor *clp
00067   )
00068 {
00069   clp->setOption(options_file_name_opt_name().c_str(),&options_file_name_,options_file_name_opt_doc().c_str());
00070   clp->setOption(extra_options_str_opt_name().c_str(),&extra_options_str_,extra_options_str_opt_doc().c_str());
00071 }
00072 
00073 void CommandLineOptionsFromStreamProcessor::process_options()
00074 {
00075   // Process the file options first
00076   if(options_file_name_.length()) {
00077     std::ifstream options_in(options_file_name_.c_str());
00078     if(options_in) {
00079       if(!options_.get())
00080         options_ = Teuchos::rcp(new OptionsFromStream());
00081       options_->read_options(options_in);
00082     }
00083   }
00084   // Process the extra commandline options
00085   const int len = extra_options_str_.length();
00086   if(len) {
00087     if(!options_.get())
00088       options_ = Teuchos::rcp(new OptionsFromStream());
00089     const char colon = ':';
00090     const std::string::size_type npos = std::string::npos;
00091     std::ostringstream ooptsstream;
00092     ooptsstream << "\nbegin_options\n\n";
00093     std::string::size_type start_i = 0, last_i = 0;
00094     while(true) {
00095       last_i = extra_options_str_.find(colon,start_i);
00096       std::string optgroup = extra_options_str_.substr(start_i,last_i-start_i);
00097 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00098       std::cout << "\nstart_i = " << start_i;
00099       std::cout << "\nlast_i = " << last_i;
00100       std::cout << "\noptgroup (before replacement) = \""<<optgroup<<"\"\n";
00101 #endif
00102       std::replace( optgroup.begin(), optgroup.end(), ',',';' ); // See above!
00103 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00104       std::cout << "\noptgroup (after replacement) = \""<<optgroup<<"\"\n";
00105 #endif
00106       ooptsstream << "options_group " << optgroup << "\n";
00107       if(last_i == npos) break;
00108       start_i = last_i + 1;
00109     }
00110     ooptsstream << "\nend_options\n";
00111     const std::string options_str = ooptsstream.str();
00112 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
00113     std::cout << "options_str:\n" << options_str;
00114 #endif
00115     std::istringstream ioptsstream(options_str);
00116     options_->read_options(ioptsstream);
00117   }
00118 }
00119 
00120 Teuchos::RCP<OptionsFromStream>
00121 CommandLineOptionsFromStreamProcessor::process_and_get_options()
00122 {
00123   process_options();
00124   return get_options();
00125 }
00126 
00127 } // end namespace OptionsFromStreamPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines