cgma
|
Parse options string passed to file IO routines. More...
#include <CGMFileOptions.hpp>
Public Member Functions | |
CGMFileOptions (const char *option_string) | |
CGMFileOptions (const CGMFileOptions ©) | |
CGMFileOptions & | operator= (const CGMFileOptions ©) |
~CGMFileOptions () | |
CGMFOErrorCode | get_null_option (const char *name) const |
Check for option with no value. | |
CGMFOErrorCode | get_int_option (const char *name, int &value) const |
Check for option with an integer value. | |
CGMFOErrorCode | get_real_option (const char *name, double &value) const |
Check for option with a double value. | |
CGMFOErrorCode | get_str_option (const char *name, std::string &value) const |
Check for option with any value. | |
CGMFOErrorCode | get_option (const char *name, std::string &value) const |
Check for option. | |
CGMFOErrorCode | match_option (const char *name, const char *const *values, int &index) const |
Check the string value of an option. | |
CGMFOErrorCode | match_option (const char *name, const char *value) const |
Check if an option matches a string value. | |
CGMFOErrorCode | 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 | |
CGMFOErrorCode | 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 CGMFileOptions.hpp.
CGMFileOptions::CGMFileOptions | ( | const char * | option_string | ) |
Definition at line 16 of file CGMFileOptions.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 ); } }
CGMFileOptions::CGMFileOptions | ( | const CGMFileOptions & | copy | ) |
Definition at line 45 of file CGMFileOptions.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 CGMFileOptions.cpp.
{ free( mData ); }
bool CGMFileOptions::compare | ( | const char * | name, |
const char * | option | ||
) | [static, private] |
Case-insensitive compare of name with option value.
Definition at line 256 of file CGMFileOptions.cpp.
bool CGMFileOptions::empty | ( | ) | const [inline] |
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ const char* s; CGMFOErrorCode 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; }
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ const char* s; CGMFOErrorCode 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; }
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ const char* s; CGMFOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; return strempty(s) ? FO_SUCCESS : FO_TYPE_OUT_OF_RANGE; }
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ const char* s; CGMFOErrorCode rval = get_option( name, s ); if (FO_SUCCESS != rval) return rval; value = s; return FO_SUCCESS; }
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.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 CGMFileOptions::get_options | ( | std::vector< std::string > & | list | ) | const |
Get list of options
Definition at line 268 of file CGMFileOptions.cpp.
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ const char* s; CGMFOErrorCode 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; }
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ const char* s; CGMFOErrorCode 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; }
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ const char* optval; CGMFOErrorCode 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; }
CGMFOErrorCode CGMFileOptions::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 CGMFileOptions.cpp.
{ int idx; const char* array[] = { value, NULL }; return match_option( name, array, idx ); }
CGMFileOptions & CGMFileOptions::operator= | ( | const CGMFileOptions & | copy | ) |
Definition at line 59 of file CGMFileOptions.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 CGMFileOptions::size | ( | ) | const [inline] |
char* CGMFileOptions::mData [private] |
Definition at line 157 of file CGMFileOptions.hpp.
std::vector<const char*> CGMFileOptions::mOptions [private] |
Definition at line 158 of file CGMFileOptions.hpp.