Parse options string passed to file IO routines. More...
#include <FileOptions.hpp>
Public Member Functions | |
FileOptions (const char *option_string) | |
FileOptions (const FileOptions ©) | |
FileOptions & | operator= (const FileOptions ©) |
~FileOptions () | |
ErrorCode | get_null_option (const char *name) const |
Check for option with no value. More... | |
ErrorCode | get_toggle_option (const char *name, bool default_value, bool &value) const |
Check for option with boolean (true/false, yes/no) value. More... | |
ErrorCode | get_int_option (const char *name, int &value) const |
Check for option with an integer value. More... | |
ErrorCode | get_int_option (const char *name, int default_val, int &value) const |
Check for option with an integer value. Accept option with no value. More... | |
ErrorCode | get_real_option (const char *name, double &value) const |
Check for option with a double value. More... | |
ErrorCode | get_str_option (const char *name, std::string &value) const |
Check for option with any value. More... | |
ErrorCode | get_option (const char *name, std::string &value) const |
Check for option. More... | |
ErrorCode | match_option (const char *name, const char *const *values, int &index) const |
Check the string value of an option. More... | |
ErrorCode | match_option (const char *name, const char *value) const |
Check if an option matches a string value. More... | |
ErrorCode | get_ints_option (const char *name, std::vector< int > &values) const |
Check for option for which the value is a list of ints. More... | |
ErrorCode | get_reals_option (const char *name, std::vector< double > &values) const |
Check for option for which the value is a list of doubles. More... | |
ErrorCode | get_strs_option (const char *name, std::vector< std::string > &values) const |
Check for option for which the value is a list of strings. More... | |
unsigned | size () const |
bool | empty () const |
void | get_options (std::vector< std::string > &list) const |
bool | all_seen () const |
void | mark_all_seen () const |
ErrorCode | get_unseen_option (std::string &value) const |
Private Member Functions | |
ErrorCode | get_option (const char *name, const char *&value) const |
Check for option. More... | |
Static Private Member Functions | |
static bool | compare (const char *name, const char *option) |
Private Attributes | |
char * | mData |
std::vector< const char * > | mOptions |
std::vector< bool > | mSeen |
Parse options string passed to file IO routines.
This is a utility class used by file-IO-related code to parse the options string passed to Core::load_file and Core::write_file
Definition at line 37 of file FileOptions.hpp.
moab::FileOptions::FileOptions | ( | const char * | option_string | ) |
Definition at line 38 of file FileOptions.cpp.
38 : mData( 0 )
39 {
40 // if option string is null, just return
41 if( !str ) return;
42
43 // check if alternate separator is specified
44 char separator[2] = { DEFAULT_SEPARATOR, '\0' };
45 if( *str == DEFAULT_SEPARATOR )
46 {
47 ++str;
48 if( strempty( str ) ) return;
49 separator[0] = *str;
50 ++str;
51 }
52
53 // don't bother allocating copy of input string if
54 // input string is empty.
55 if( !strempty( str ) )
56 {
57 // tokenize at separator character
58 mData = strdup( str );
59 for( char* i = strtok( mData, separator ); i; i = strtok( 0, separator ) )
60 if( !strempty( i ) ) // skip empty strings
61 mOptions.push_back( i );
62 }
63
64 mSeen.resize( mOptions.size(), false );
65 }
References moab::DEFAULT_SEPARATOR, mData, mOptions, mSeen, and moab::strempty().
moab::FileOptions::FileOptions | ( | const FileOptions & | copy | ) |
Definition at line 67 of file FileOptions.cpp.
67 : mData( 0 ), mOptions( copy.mOptions.size() )
68 {
69 if( !copy.mOptions.empty() )
70 {
71 const char* last = copy.mOptions.back();
72 const char* endptr = last + strlen( last ) + 1;
73 size_t len = endptr - copy.mData;
74 mData = (char*)malloc( len );
75 memcpy( mData, copy.mData, len );
76 for( size_t i = 0; i < mOptions.size(); ++i )
77 mOptions[i] = mData + ( copy.mOptions[i] - copy.mData );
78 }
79 mSeen = copy.mSeen;
80 }
moab::FileOptions::~FileOptions | ( | ) |
bool moab::FileOptions::all_seen | ( | ) | const |
Check if all options have been looked at
Definition at line 377 of file FileOptions.cpp.
378 {
379 return std::find( mSeen.begin(), mSeen.end(), false ) == mSeen.end();
380 }
References mSeen.
Referenced by moab::AdaptiveKDTree::build_tree(), moab::BVHTree::build_tree(), moab::Core::load_file(), and moab::Core::write_file().
|
staticprivate |
Case-insensitive compare of name with option value.
Definition at line 357 of file FileOptions.cpp.
358 {
359 while( !strempty( name ) && toupper( *name ) == toupper( *option ) )
360 {
361 ++name;
362 ++option;
363 }
364 // match if name matched option for length of name,
365 // and option either matched entirely or matches up to
366 // and equals sign.
367 return strempty( name ) && ( strempty( option ) || *option == '=' );
368 }
References moab::strempty().
Referenced by get_option(), and match_option().
|
inline |
true if no options
Definition at line 212 of file FileOptions.hpp.
213 {
214 return mOptions.empty();
215 }
ErrorCode moab::FileOptions::get_int_option | ( | const char * | name, |
int & | value | ||
) | const |
Check for option with an integer value.
Check for an option with an integer value
name | The option name |
value | Output. The value. |
Definition at line 119 of file FileOptions.cpp.
120 {
121 const char* s;
122 ErrorCode rval = get_option( name, s );
123 if( MB_SUCCESS != rval ) return rval;
124
125 // empty string
126 if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;
127
128 // parse value
129 char* endptr;
130 long int pval = strtol( s, &endptr, 0 );
131 if( !strempty( endptr ) ) // syntax error
132 return MB_TYPE_OUT_OF_RANGE;
133
134 // check for overflow (parsing long int, returning int)
135 value = pval;
136 if( pval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;
137
138 return MB_SUCCESS;
139 }
References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
Referenced by moab::NCHelperEuler::init_mesh_vals(), moab::NCHelperFV::init_mesh_vals(), moab::ReadParallel::load_file(), moab::ReadMCNP5::load_file(), moab::Tqdcfr::load_file(), moab::Core::load_file(), moab::WriteHDF5Parallel::parallel_create_file(), moab::Tree::parse_common_options(), moab::ReadNC::parse_options(), moab::WriteNC::parse_options(), moab::AdaptiveKDTree::parse_options(), moab::BVHTree::parse_options(), moab::ReadHDF5::set_up_read(), moab::WriteGmsh::write_file(), moab::WriteSmf::write_file(), moab::WriteSTL::write_file(), moab::WriteVtk::write_file(), and moab::WriteHDF5::write_file().
ErrorCode moab::FileOptions::get_int_option | ( | const char * | name, |
int | default_val, | ||
int & | value | ||
) | const |
Check for option with an integer value. Accept option with no value.
Check for an option with an integer value. If the option is found but has no value specified, then pass back the user-specified default value.
\NOTE: This function will not pass back the default_val, but will instead return MB_ENTITY_NOT_FOUND if the option is not specified at all. The default value is returned only when the option is specified, but is specified w/out a value.
name | The option name |
default_val | The default value for the option. |
value | Output. The value. |
Definition at line 141 of file FileOptions.cpp.
142 {
143 const char* s;
144 ErrorCode rval = get_option( name, s );
145 if( MB_SUCCESS != rval ) return rval;
146
147 // empty string
148 if( strempty( s ) )
149 {
150 value = default_val;
151 return MB_SUCCESS;
152 }
153
154 // parse value
155 char* endptr;
156 long int pval = strtol( s, &endptr, 0 );
157 if( !strempty( endptr ) ) // syntax error
158 return MB_TYPE_OUT_OF_RANGE;
159
160 // check for overflow (parsing long int, returning int)
161 value = pval;
162 if( pval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;
163
164 return MB_SUCCESS;
165 }
References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
ErrorCode moab::FileOptions::get_ints_option | ( | const char * | name, |
std::vector< int > & | values | ||
) | const |
Check for option for which the value is a list of ints.
Check for an option which is an int list. The value is expected to be a comma-separated list of int ranges, where an int range can be either a single integer value or a range of integer values separated by a dash ('-').
name | The option name |
values | Output. The list of integer values. |
Definition at line 167 of file FileOptions.cpp.
168 {
169 const char* s;
170 ErrorCode rval = get_option( name, s );
171 if( MB_SUCCESS != rval ) return rval;
172
173 // empty string
174 if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;
175
176 // parse values
177 while( !strempty( s ) )
178 {
179 char* endptr;
180 long int sval = strtol( s, &endptr, 0 );
181
182 #define EATSPACE( a ) \
183 while( ( *( a ) == ' ' || *( a ) == ',' ) && !strempty( a ) ) \
184 ( a )++;
185 EATSPACE( endptr );
186 long int eval = sval;
187 if( *endptr == '-' )
188 {
189 endptr++;
190 s = endptr;
191 eval = strtol( s, &endptr, 0 );
192 EATSPACE( endptr );
193 }
194
195 // check for overflow (parsing long int, returning int)
196 int value = sval;
197 if( sval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;
198 value = eval;
199 if( eval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;
200
201 for( int i = sval; i <= eval; i++ )
202 values.push_back( i );
203
204 s = endptr;
205 }
206
207 return MB_SUCCESS;
208 }
References EATSPACE, ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
Referenced by moab::ReadParallel::load_file(), main(), moab::ReadNC::parse_options(), and moab::WriteNC::parse_options().
ErrorCode moab::FileOptions::get_null_option | ( | const char * | name | ) | const |
Check for option with no value.
Check for an option w/out a value.
name | The option name |
Definition at line 111 of file FileOptions.cpp.
112 {
113 const char* s;
114 ErrorCode rval = get_option( name, s );
115 if( MB_SUCCESS != rval ) return rval;
116 return strempty( s ) ? MB_SUCCESS : MB_TYPE_OUT_OF_RANGE;
117 }
References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
Referenced by moab::ReadParallel::load_file(), moab::ReadHDF5::load_file(), moab::ReadSTL::load_file(), moab::Tqdcfr::load_file(), moab::ReadMCNP5::load_one_file(), moab::WriteHDF5Parallel::parallel_create_file(), moab::ReadDamsel::parse_options(), moab::ReadNC::parse_options(), moab::WriteNC::parse_options(), moab::ReadHDF5::set_up_read(), moab::WriteNCDF::write_file(), moab::WriteSTL::write_file(), moab::WriteVtk::write_file(), moab::Core::write_file(), moab::WriteHDF5::write_file(), and moab::WriteHDF5::write_file_impl().
|
private |
Check for option.
Check for an option
name | The option name |
value | The option value, or an empty string if no value. |
Definition at line 292 of file FileOptions.cpp.
293 {
294 std::vector< const char* >::const_iterator i;
295 for( i = mOptions.begin(); i != mOptions.end(); ++i )
296 {
297 const char* opt = *i;
298 if( compare( name, opt ) )
299 {
300 value = opt + strlen( name );
301 // if compare returned true, next char after option
302 // name must be either the null char or an equals symbol.
303 if( *value == '=' ) ++value;
304
305 mSeen[i - mOptions.begin()] = true;
306 return MB_SUCCESS;
307 }
308 }
309
310 return MB_ENTITY_NOT_FOUND;
311 }
References compare(), MB_ENTITY_NOT_FOUND, MB_SUCCESS, mOptions, and mSeen.
ErrorCode moab::FileOptions::get_option | ( | const char * | name, |
std::string & | value | ||
) | const |
Check for option.
Check for an option
name | The option name |
value | The option value, or an empty string if no value. |
Definition at line 282 of file FileOptions.cpp.
283 {
284 const char* s;
285 ErrorCode rval = get_option( name, s );
286 if( MB_SUCCESS != rval ) return rval;
287
288 value = s;
289 return MB_SUCCESS;
290 }
References ErrorCode, and MB_SUCCESS.
Referenced by get_int_option(), get_ints_option(), get_null_option(), get_real_option(), get_reals_option(), get_str_option(), get_strs_option(), moab::ReadParallel::load_file(), moab::ReadSmf::load_file(), moab::ReadVtk::load_file(), moab::Core::load_file(), moab::ReadHDF5::load_file_partial(), match_option(), and moab::ReadTetGen::open_file().
void moab::FileOptions::get_options | ( | std::vector< std::string > & | list | ) | const |
Get list of options
Definition at line 370 of file FileOptions.cpp.
371 {
372 list.clear();
373 list.resize( mOptions.size() );
374 std::copy( mOptions.begin(), mOptions.end(), list.begin() );
375 }
References mOptions.
ErrorCode moab::FileOptions::get_real_option | ( | const char * | name, |
double & | value | ||
) | const |
Check for option with a double value.
Check for an option with a double value
name | The option name |
value | Output. The value. |
Definition at line 234 of file FileOptions.cpp.
235 {
236 const char* s;
237 ErrorCode rval = get_option( name, s );
238 if( MB_SUCCESS != rval ) return rval;
239
240 // empty string
241 if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;
242
243 // parse value
244 char* endptr;
245 value = strtod( s, &endptr );
246 if( !strempty( endptr ) ) // syntax error
247 return MB_TYPE_OUT_OF_RANGE;
248
249 return MB_SUCCESS;
250 }
References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
Referenced by moab::ReadParallel::load_file(), moab::Tree::parse_common_options(), and moab::AdaptiveKDTree::parse_options().
ErrorCode moab::FileOptions::get_reals_option | ( | const char * | name, |
std::vector< double > & | values | ||
) | const |
Check for option for which the value is a list of doubles.
Check for an option which is a double list. The value is expected to be a comma-separated list of double values
name | The option name |
values | Output. The list of double values. |
Definition at line 210 of file FileOptions.cpp.
211 {
212 const char* s;
213 ErrorCode rval = get_option( name, s );
214 if( MB_SUCCESS != rval ) return rval;
215
216 // empty string
217 if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;
218
219 // parse values
220 while( !strempty( s ) )
221 {
222 char* endptr;
223 double sval = strtod( s, &endptr );
224
225 EATSPACE( endptr );
226 values.push_back( sval );
227
228 s = endptr;
229 }
230
231 return MB_SUCCESS;
232 }
References EATSPACE, ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
Referenced by moab::ReadNC::parse_options(), and moab::WriteNC::parse_options().
ErrorCode moab::FileOptions::get_str_option | ( | const char * | name, |
std::string & | value | ||
) | const |
Check for option with any value.
Check for an option with any value.
name | The option name |
value | Output. The value. |
Definition at line 272 of file FileOptions.cpp.
273 {
274 const char* s;
275 ErrorCode rval = get_option( name, s );
276 if( MB_SUCCESS != rval ) return rval;
277 if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;
278 value = s;
279 return MB_SUCCESS;
280 }
References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
Referenced by moab::NCHelperHOMME::create_mesh(), moab::ReadParallel::load_file(), moab::ReadNCDF::load_file(), moab::ReadTetGen::load_file(), moab::Tqdcfr::load_file(), moab::WriteHDF5Parallel::parallel_create_file(), moab::Tree::parse_common_options(), moab::ReadHDF5::set_up_read(), and moab::ReadNCDF::update().
ErrorCode moab::FileOptions::get_strs_option | ( | const char * | name, |
std::vector< std::string > & | values | ||
) | const |
Check for option for which the value is a list of strings.
Check for an option which is a string list. The value is expected to be a comma-separated list of string values, with no embedded spaces or commas.
name | The option name |
values | Output. The list of string values. |
Definition at line 252 of file FileOptions.cpp.
253 {
254 const char* s;
255 ErrorCode rval = get_option( name, s );
256 if( MB_SUCCESS != rval ) return rval;
257
258 // empty string
259 if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;
260
261 // parse values
262 char separator[3] = { ' ', ',', '\0' };
263 char* tmp_str = strdup( s );
264 for( char* i = strtok( tmp_str, separator ); i; i = strtok( 0, separator ) )
265 if( !strempty( i ) ) // skip empty strings
266 values.push_back( std::string( i ) );
267 free( tmp_str );
268
269 return MB_SUCCESS;
270 }
References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().
Referenced by main(), moab::ReadNC::parse_options(), and moab::WriteNC::parse_options().
ErrorCode moab::FileOptions::get_toggle_option | ( | const char * | name, |
bool | default_value, | ||
bool & | value | ||
) | const |
Check for option with boolean (true/false, yes/no) value.
Check for an option with a true/false value. Allowable values are "true", "false", "yes", "no", "1", "0", "on", "off".
name | The option name |
default_value | The value to return if the option is not specified. |
value | The resulting value. This argument is set to the passed default value if the option is not found. |
Definition at line 333 of file FileOptions.cpp.
334 {
335 static const char* values[] = { "true", "yes", "1", "on", "false", "no", "0", "off", 0 };
336 const int num_true = 4;
337
338 int index;
339 ErrorCode result = match_option( name, values, index );
340 if( result == MB_SUCCESS )
341 {
342 value = index < num_true;
343 }
344 else if( result == MB_ENTITY_NOT_FOUND )
345 {
346 value = default_value;
347 result = MB_SUCCESS;
348 }
349 else
350 {
351 result = MB_TYPE_OUT_OF_RANGE;
352 }
353
354 return result;
355 }
References ErrorCode, match_option(), MB_ENTITY_NOT_FOUND, MB_SUCCESS, and MB_TYPE_OUT_OF_RANGE.
Referenced by moab::Tree::parse_common_options(), moab::AdaptiveKDTree::parse_options(), and moab::ReadHDF5::set_up_read().
ErrorCode moab::FileOptions::get_unseen_option | ( | std::string & | value | ) | const |
Get first unseen option
Definition at line 388 of file FileOptions.cpp.
389 {
390 std::vector< bool >::iterator i = std::find( mSeen.begin(), mSeen.end(), false );
391 if( i == mSeen.end() )
392 {
393 name.clear();
394 return MB_ENTITY_NOT_FOUND;
395 }
396
397 const char* opt = mOptions[i - mSeen.begin()];
398 const char* end = strchr( opt, '=' );
399 name = end ? std::string( opt, end - opt ) : std::string( opt );
400 return MB_SUCCESS;
401 }
References MB_ENTITY_NOT_FOUND, MB_SUCCESS, mOptions, and mSeen.
Referenced by moab::Core::load_file(), and moab::Core::write_file().
void moab::FileOptions::mark_all_seen | ( | ) | const |
Mark all options as seen. USed for non-root procs during bcast-delete read
Definition at line 382 of file FileOptions.cpp.
383 {
384 mSeen.clear();
385 mSeen.resize( mOptions.size(), true );
386 }
References mOptions, and mSeen.
Referenced by moab::ReadParallel::load_file(), moab::ReadCGNS::process_options(), and moab::ReadTemplate::process_options().
ErrorCode moab::FileOptions::match_option | ( | const char * | name, |
const char *const * | values, | ||
int & | index | ||
) | const |
Check the string value of an option.
Check which of a list of possible values a string option contains.
name | The option name |
values | A NULL-terminated array of C-style strings enumerating the possible option values. |
index | Output: The index into values for the option value. |
values
array. Definition at line 320 of file FileOptions.cpp.
321 {
322 const char* optval;
323 ErrorCode rval = get_option( name, optval );
324 if( MB_SUCCESS != rval ) return rval;
325
326 for( index = 0; values[index]; ++index )
327 if( compare( optval, values[index] ) ) return MB_SUCCESS;
328
329 index = -1;
330 return MB_FAILURE;
331 }
References compare(), ErrorCode, get_option(), and MB_SUCCESS.
Referenced by get_toggle_option(), moab::ReadParallel::load_file(), moab::ReadHDF5::load_file_partial(), match_option(), moab::ReadDamsel::parse_options(), moab::ReadNC::parse_options(), moab::WriteNC::parse_options(), moab::ReadHDF5::set_up_read(), and moab::WriteHDF5::write_file_impl().
ErrorCode moab::FileOptions::match_option | ( | const char * | name, |
const char * | value | ||
) | const |
Check if an option matches a string value.
Check if the value for an option is the passed string.
name | The option name |
value | The expected value. |
Definition at line 313 of file FileOptions.cpp.
314 {
315 int idx;
316 const char* array[] = { value, NULL };
317 return match_option( name, array, idx );
318 }
References match_option().
FileOptions & moab::FileOptions::operator= | ( | const FileOptions & | copy | ) |
Definition at line 82 of file FileOptions.cpp.
83 {
84 // Check for self-assignment
85 if( this == © ) return *this;
86
87 free( mData );
88 mData = 0;
89 mOptions.resize( copy.mOptions.size() );
90
91 if( !copy.mOptions.empty() )
92 {
93 const char* last = copy.mOptions.back();
94 const char* endptr = last + strlen( last ) + 1;
95 size_t len = endptr - copy.mData;
96 mData = (char*)malloc( len );
97 memcpy( mData, copy.mData, len );
98 for( size_t i = 0; i < mOptions.size(); ++i )
99 mOptions[i] = mData + ( copy.mOptions[i] - copy.mData );
100 }
101
102 mSeen = copy.mSeen;
103 return *this;
104 }
|
inline |
number of options
Definition at line 206 of file FileOptions.hpp.
207 {
208 return mOptions.size();
209 }
Referenced by moab::Core::write_file().
|
private |
Definition at line 239 of file FileOptions.hpp.
Referenced by FileOptions(), operator=(), and ~FileOptions().
|
private |
Definition at line 240 of file FileOptions.hpp.
Referenced by FileOptions(), get_option(), get_options(), get_unseen_option(), mark_all_seen(), and operator=().
|
mutableprivate |
Definition at line 241 of file FileOptions.hpp.
Referenced by all_seen(), FileOptions(), get_option(), get_unseen_option(), mark_all_seen(), and operator=().