cgma
|
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 () | |
FOErrorCode | get_null_option (const char *name) const |
Check for option with no value. | |
FOErrorCode | get_int_option (const char *name, int &value) const |
Check for option with an integer value. | |
FOErrorCode | get_real_option (const char *name, double &value) const |
Check for option with a double value. | |
FOErrorCode | get_str_option (const char *name, std::string &value) const |
Check for option with any value. | |
FOErrorCode | get_option (const char *name, std::string &value) const |
Check for option. | |
FOErrorCode | match_option (const char *name, const char *const *values, int &index) const |
Check the string value of an option. | |
FOErrorCode | match_option (const char *name, const char *value) const |
Check if an option matches a string value. | |
FOErrorCode | get_ints_option (const char *name, std::vector< int > &values) const |
Check for option for which the value is a list of ints. | |
unsigned | size () const |
bool | empty () const |
void | get_options (std::vector< std::string > &list) const |
Private Member Functions | |
FOErrorCode | get_option (const char *name, const char *&value) const |
Check for option. | |
Static Private Member Functions | |
static bool | compare (const char *name, const char *option) |
Private Attributes | |
char * | mData |
std::vector< const char * > | mOptions |
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 ParallelMeshTool::load_file
Definition at line 27 of file FileOptions.hpp.
FileOptions::FileOptions | ( | const char * | option_string | ) |
Definition at line 16 of file FileOptions.cpp.
: mData(0) { // if option string is null, just return if (!str) return; // check if alternate separator is specified char separator[2] = { DEFAULT_SEPARATOR, '\0' }; if (*str == DEFAULT_SEPARATOR) { ++str; if (strempty(str)) return; separator[0] = *str; ++str; } // don't bother allocating copy of input string if // input string is empty. if (!strempty(str)) { // tokenize at separator character mData = strdup( str ); for (char* i = strtok( mData, separator ); i; i = strtok( 0, separator )) if (!strempty(i)) // skip empty strings mOptions.push_back( i ); } }
FileOptions::FileOptions | ( | const FileOptions & | copy | ) |
Definition at line 45 of file FileOptions.cpp.
: mData(0), mOptions( copy.mOptions.size() ) { if (!copy.mOptions.empty()) { const char* last = copy.mOptions.back(); const char* endptr = last + strlen(last) + 1; size_t len = endptr - copy.mData; mData = (char*)malloc( len ); memcpy( mData, copy.mData, len ); for (size_t i = 0; i < mOptions.size(); ++i) mOptions[i] = mData + (copy.mOptions[i] - copy.mData); } }
Definition at line 78 of file FileOptions.cpp.
{ free( mData ); }
bool FileOptions::compare | ( | const char * | name, |
const char * | option | ||
) | [static, private] |
Case-insensitive compare of name with option value.
Definition at line 256 of file FileOptions.cpp.
bool FileOptions::empty | ( | ) | const [inline] |
FOErrorCode 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 92 of file FileOptions.cpp.
{ const char* s; FOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; // empty string if (strempty(s)) return FO_TYPE_OUT_OF_RANGE; // parse value char* endptr; long int pval = strtol( s, &endptr, 0 ); if (!strempty(endptr)) // syntax error return FO_TYPE_OUT_OF_RANGE; // check for overflow (parsing long int, returning int) value = pval; if (pval != (long int)value) return FO_TYPE_OUT_OF_RANGE; return FO_SUCCESS; }
FOErrorCode 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 117 of file FileOptions.cpp.
{ const char* s; FOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; // empty string if (strempty(s)) return FO_TYPE_OUT_OF_RANGE; // parse values while (!strempty(s)) { char* endptr; long int sval = strtol( s, &endptr, 0 ); #define EATSPACE(a) while ((!strncmp(a, " ", 1) || \ !strncmp(a, ",", 1)) && !strempty(a)) a++; //EATSPACE(endptr); while ((!strncmp(endptr, " ", 1) || !strncmp(endptr, ",", 1)) && !strempty(endptr)) { endptr++; } long int eval = sval; if (!strcmp(endptr, "-")) { endptr++; s = endptr; eval = strtol(s, &endptr, 0); EATSPACE(endptr); } // check for overflow (parsing long int, returning int) int value = sval; if (sval != (long int)value) return FO_TYPE_OUT_OF_RANGE; value = eval; if (eval != (long int)value) return FO_TYPE_OUT_OF_RANGE; for (int i = sval; i <= eval; i++) values.push_back(i); s = endptr; } return FO_SUCCESS; }
FOErrorCode 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 83 of file FileOptions.cpp.
{ const char* s; FOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; return strempty(s) ? FO_SUCCESS : FO_TYPE_OUT_OF_RANGE; }
FOErrorCode 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 200 of file FileOptions.cpp.
{ const char* s; FOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; value = s; return FO_SUCCESS; }
FOErrorCode FileOptions::get_option | ( | const char * | name, |
const char *& | value | ||
) | const [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 211 of file FileOptions.cpp.
{ std::vector<const char*>::const_iterator i; for (i = mOptions.begin(); i != mOptions.end(); ++i) { const char* opt = *i; if (compare( name, opt )) { value = opt + strlen(name); // if compare returned true, next char after option // name must be either the null char or an equals symbol. if (*value == '=') ++value; return FO_SUCCESS; } } return FO_ENTITY_NOT_FOUND; }
void FileOptions::get_options | ( | std::vector< std::string > & | list | ) | const |
Get list of options
Definition at line 268 of file FileOptions.cpp.
FOErrorCode 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 168 of file FileOptions.cpp.
{ const char* s; FOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; // empty string if (strempty(s)) return FO_TYPE_OUT_OF_RANGE; // parse value char* endptr; value = strtod( s, &endptr ); if (!strempty(endptr)) // syntax error return FO_TYPE_OUT_OF_RANGE; return FO_SUCCESS; }
FOErrorCode 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 188 of file FileOptions.cpp.
{ const char* s; FOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; if (strempty(s)) return FO_TYPE_OUT_OF_RANGE; value = s; return FO_SUCCESS; }
FOErrorCode 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 238 of file FileOptions.cpp.
{ const char* optval; FOErrorCode rval = get_option( name, optval ); if (FO_SUCCESS != rval) return rval; for (index = 0; values[index]; ++index) if (compare( optval, values[index] )) return FO_SUCCESS; index = -1; return FO_FAILURE; }
FOErrorCode 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 230 of file FileOptions.cpp.
{ int idx; const char* array[] = { value, NULL }; return match_option( name, array, idx ); }
FileOptions & FileOptions::operator= | ( | const FileOptions & | copy | ) |
Definition at line 59 of file FileOptions.cpp.
{ free( mData ); mData = 0; mOptions.resize( copy.mOptions.size() ); if (!copy.mOptions.empty()) { const char* last = copy.mOptions.back(); const char* endptr = last + strlen(last) + 1; size_t len = endptr - copy.mData; mData = (char*)malloc( len ); memcpy( mData, copy.mData, len ); for (size_t i = 0; i < mOptions.size(); ++i) mOptions[i] = mData + (copy.mOptions[i] - copy.mData); } return *this; }
unsigned FileOptions::size | ( | ) | const [inline] |
char* FileOptions::mData [private] |
Definition at line 157 of file FileOptions.hpp.
std::vector<const char*> FileOptions::mOptions [private] |
Definition at line 158 of file FileOptions.hpp.