MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionsFromStreamPack_OptionsFromStream.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <string>
43 #include <ostream>
44 #include <istream>
45 
48 #include "Teuchos_Assert.hpp"
49 
50 // Define this if you want to debug the parser
51 //#define PRINT_OPTIONS_FROM_STREAM_TRACE
52 
53 namespace {
54 
55 // Remove white space from beginning and end.
56 inline
57 void clip_ws( std::string* str ) {
58  // Remove the first non ' ' characters
59  {
60  std::string::iterator itr;
61  for( itr = str->begin(); itr != str->end(); ++itr )
62  if( *itr != ' ' ) break;
63  str->erase( str->begin(), itr );
64  }
65  // Remove the last non ' ' characters
66  {
67  std::string::iterator itr;
68  for( itr = str->end() - 1; itr > str->begin() - 1; --itr )
69  if( *itr != ' ' ) break;
70  str->erase( itr + 1, str->end() );
71  }
72 }
73 
74 } // end namespace
75 
76 namespace OptionsFromStreamPack {
77 
78 namespace OptionsFromStreamUtilityPack {
79 
80 // class OptionsGroup
81 
83 
84 } // end namespace OptionsFromStreamUtilityPack
85 
86 // class OptionsFromStream
87 
90 {
91  options_group_map_t::iterator itr = options_group_map_.find( options_group_name );
92  if( itr != options_group_map_.end() ) {
93  (*itr).second.second.set(true); // flag that we have accessed this options group
94  return options_group_t(&(*itr).second.first);
95  }
96  return options_group_t(NULL);
97 }
98 
101 {
102  options_group_map_t::const_iterator itr = options_group_map_.find( options_group_name );
103  if( itr != options_group_map_.end() ) {
104  const_cast<false_bool_t&>((*itr).second.second).set(true); // flag that we have accessed this options group
105  return options_group_t(const_cast<option_to_value_map_t*>(&(*itr).second.first));
106  }
107  return options_group_t(NULL);
108 }
109 
111 {
112  for( iterator og_itr = begin() ; og_itr != end(); ++og_itr ) {
113  (*og_itr).second.second.set(false); // rest to not accessed yet.
114  }
115 }
116 
118 {
119  const_iterator og_itr = begin();
120  for( ; og_itr != end(); ++og_itr ) {
121  if( (*og_itr).second.second == false ) // if options group was not accessed
122  out << "options_group " << options_group_name( og_itr ) << " {}\n";
123  }
124 }
125 
126 void OptionsFromStream::read_options( std::istream& in )
127 {
128  using std::getline;
129 
131 
132  std::string curr_word;
133 
134  if(!in)
135  return; // No options to read!
136 
137 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
138  std::cout << "\n*** Entering OptionsFromStream::read_options(...)!\n\n";
139 #endif
140 
141  // Eat words until you get to begin_options.
142  while( curr_word != "begin_options" && !in.eof() )
143  in >> curr_word;
144 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
145  std::cout << "Found begin_options, start parsing options!\n";
146 #endif
147  // Loop through each options group.
148  while( !in.eof() ) {
149  eat_comment_lines(in,'*');
150  // read options_group
151  in >> curr_word;
152 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
153  std::cout << "\ncurr_word = \""<<curr_word<<"\"\n";
154 #endif
155  if( curr_word == "}" ) {
156 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
157  std::cout << "Found \'}\', Moving on to the next options group or the end!\n";
158 #endif
159  break;
160  }
161  if( curr_word == "end_options" ) {
162 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
163  std::cout << "Found \'end_options\', stoping parsing options!\n";
164 #endif
165  break;
166  }
168  curr_word != "options_group", InputStreamError
169  ,"OptionsFromStream::read_options(...) : "
170  "Error, curr_word = \'" << curr_word << " != \'options_group\'" );
171  // read the name of the options group up to {
172  std::getline(in,curr_word,'{');
173  clip_ws( &curr_word );
174  const std::string optgroup_name = curr_word;
175 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
176  std::cout << "\noptgroup_name = \"" << optgroup_name << "\"\n";
177 #endif
178  // Access the options and values map for this options group.
179  option_to_value_map_t& optval = options_group_map_[optgroup_name].first;
180  // Grap all of the options for this options group
181  eat_comment_lines(in,'*');
182  std::string optgroup_options;
183  getline(in,optgroup_options,'}');
184 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
185  std::cout << "optgroup_options = \"" << optgroup_options << "\"\n";
186 #endif
187  std::istringstream optgroup_options_in(optgroup_options);
188  // Loop through and add the options.
189  while(true) {
190  eat_comment_lines(optgroup_options_in,'*');
191  // Get an option and its value
192  std::string option_and_value;
193  getline(optgroup_options_in,option_and_value,';');
194  // Note: above If ';' is missing it will take the rest of the string to
195  // the end of '}' for the end of the options group. These means that if
196  // there is no comments after the last option=value pair then the last
197  // semicolon is optional! This turns out to work nicely for the
198  // CommandLineOptionsFromStreamProcessor class so this is good behavior!
199  clip_ws(&option_and_value);
200 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
201  std::cout << " option_and_value = \"" << option_and_value << "\"\n";
202 #endif
203  if(!option_and_value.length())
204  break;
205  // Process the option and value
206  const std::string::size_type equal_idx = option_and_value.find('=',0);
208  equal_idx==std::string::npos, std::logic_error,
209  "Error, for the option group \"" << optgroup_name << "\""
210  << " the option value string \"" << option_and_value << "\" is missing the \"=\" separator!"
211  );
212  std::string option = option_and_value.substr(0,equal_idx);
213  std::string value = option_and_value.substr(equal_idx+1);
214  clip_ws(&option);
215  clip_ws(&value);
216 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
217  std::cout << " option = \"" << option << "\"\n";
218  std::cout << " value = \"" << value << "\"\n";
219 #endif
220  optval[option] = value;
221  }
222  }
223 #ifdef PRINT_OPTIONS_FROM_STREAM_TRACE
224  std::cout << "\n*** Leaving OptionsFromStream::read_options(...)!\n\n";
225 #endif
226 }
227 
228 void OptionsFromStream::print_options( std::ostream& out ) const {
229  out << "\nbegin_options\n";
230  const_iterator og_itr = begin();
231  for( ; og_itr != end(); ++og_itr ) {
232  const options_group_t optgrp = OptionsFromStreamPack::options_group( og_itr );
233  options_group_t::const_iterator itr = optgrp.begin();
234  if(itr == optgrp.end()) continue;
235  out << "\noptions_group " << options_group_name( og_itr ) << " {\n";
236  for( ; itr != optgrp.end(); ++itr ) {
237  const std::string
238  &name = option_name(itr),
239  &value = option_value(itr);
240  out << " " << name << " = " << value << ";\n";
241  }
242  out << "}\n";
243  }
244  out << "\nend_options\n\n";
245 }
246 
247 } // end namespace OptionsFromStreamPack
AbstractLinAlgPack::size_type size_type
void print_options(std::ostream &out) const
Print the options to an output stream.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
const std::string & option_name(OptionsGroup::const_iterator &itr)
myDepList set(dependee1,"val1")
OptionsFromStreamUtilityPack::options_group_map_t::const_iterator const_iterator
non-const iterator through options group access options
std::istream & eat_comment_lines(std::istream &is, char comment_identifier)
Discards comment lines from an input stream.
OptionsFromStreamUtilityPack::OptionsGroup options_group_t
{OptionsGroup} typedef
void read_options(std::istream &in)
Add / modify options read in from a text stream.
OptionsFromStreamUtilityPack::option_to_value_map_t option_to_value_map_t
std::ostream * out
const OptionsFromStream::options_group_t options_group(OptionsFromStream::const_iterator &itr)
const std::string & options_group_name(OptionsFromStream::const_iterator &itr)
void reset_unaccessed_options_groups()
Reset the flags to false for if the options groups was accessed.
options_group_t options_group(const std::string &options_group_name)
void print_unaccessed_options_groups(std::ostream &out) const
Print a list of options groups never accessed (accessed flag is falsed).
OptionsFromStreamUtilityPack::options_group_map_t::iterator iterator
const iterator through options group access options
const std::string & option_value(OptionsGroup::const_iterator &itr)