moab
moab::FileOptions Class Reference

Parse options string passed to file IO routines. More...

#include <FileOptions.hpp>

List of all members.

Public Member Functions

 FileOptions (const char *option_string)
 FileOptions (const FileOptions &copy)
FileOptionsoperator= (const FileOptions &copy)
 ~FileOptions ()
ErrorCode get_null_option (const char *name) const
 Check for option with no value.
ErrorCode get_toggle_option (const char *name, bool default_value, bool &value) const
 Check for option with boolean (true/false, yes/no) value.
ErrorCode get_int_option (const char *name, int &value) const
 Check for option with an integer value.
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.
ErrorCode get_real_option (const char *name, double &value) const
 Check for option with a double value.
ErrorCode get_str_option (const char *name, std::string &value) const
 Check for option with any value.
ErrorCode get_option (const char *name, std::string &value) const
 Check for option.
ErrorCode match_option (const char *name, const char *const *values, int &index) const
 Check the string value of an option.
ErrorCode match_option (const char *name, const char *value) const
 Check if an option matches a string value.
ErrorCode get_ints_option (const char *name, std::vector< int > &values) const
 Check for option for which the value is a list of ints.
ErrorCode get_reals_option (const char *name, std::vector< double > &values) const
 Check for option for which the value is a list of doubles.
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.
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.

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

Detailed Description

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 36 of file FileOptions.hpp.


Constructor & Destructor Documentation

moab::FileOptions::FileOptions ( const char *  option_string)

Definition at line 34 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 );
  }
  
  mSeen.resize( mOptions.size(), false );
}

Definition at line 65 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);
  }
  mSeen = copy.mSeen;
}

Definition at line 100 of file FileOptions.cpp.

{
  free( mData );
}

Member Function Documentation

Check if all options have been looked at

Definition at line 396 of file FileOptions.cpp.

{
  return std::find( mSeen.begin(), mSeen.end(), false ) == mSeen.end();
}
bool moab::FileOptions::compare ( const char *  name,
const char *  option 
) [static, private]

Case-insensitive compare of name with option value.

Definition at line 377 of file FileOptions.cpp.

{
  while (!strempty(name) && toupper(*name) == toupper(*option)) {
    ++name;
    ++option;
  }
   // match if name matched option for length of name,
   // and option either matched entirely or matches up to
   // and equals sign.
  return strempty(name) && (strempty(option) || *option == '=');
}
bool moab::FileOptions::empty ( ) const [inline]

true if no options

Definition at line 211 of file FileOptions.hpp.

    { return mOptions.empty(); }
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

Parameters:
nameThe option name
valueOutput. The value.
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found, but does not have an integer value
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 114 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  
    // empty string
  if (strempty(s))
    return MB_TYPE_OUT_OF_RANGE;
  
    // parse value
  char* endptr;
  long int pval = strtol( s, &endptr, 0 );
  if (!strempty(endptr)) // syntax error
    return MB_TYPE_OUT_OF_RANGE;
  
    // check for overflow (parsing long int, returning int)
  value = pval;
  if (pval != (long int)value)
    return MB_TYPE_OUT_OF_RANGE;
  
  return MB_SUCCESS;
}
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.

: 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.

Parameters:
nameThe option name
default_valThe default value for the option.
valueOutput. The value.
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found but has a value that cannot be parsed as an int
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 139 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  
    // empty string
  if (strempty(s)) {
    value = default_val;
    return MB_SUCCESS;
  }
  
    // parse value
  char* endptr;
  long int pval = strtol( s, &endptr, 0 );
  if (!strempty(endptr)) // syntax error
    return MB_TYPE_OUT_OF_RANGE;
  
    // check for overflow (parsing long int, returning int)
  value = pval;
  if (pval != (long int)value)
    return MB_TYPE_OUT_OF_RANGE;
  
  return MB_SUCCESS;
}
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 ('-').

Parameters:
nameThe option name
valuesOutput. The list of integer values.
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found, but does not contain an ID list
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 168 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  
    // empty string
  if (strempty(s))
    return MB_TYPE_OUT_OF_RANGE;
  
    // parse values
  while (!strempty(s)) {
    char* endptr;
    long int sval = strtol( s, &endptr, 0 );

#define EATSPACE(a) while ((*a == ' ' ||          \
                            *a == ',') && !strempty(a)) a++;
    EATSPACE(endptr);
    long int eval = sval;
    if (*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 MB_TYPE_OUT_OF_RANGE;
    value = eval;
    if (eval != (long int)value)
      return MB_TYPE_OUT_OF_RANGE;
  
    for (int i = sval; i <= eval; i++)
      values.push_back(i);

    s = endptr;
  }
  
  return MB_SUCCESS;
}
ErrorCode moab::FileOptions::get_null_option ( const char *  name) const

Check for option with no value.

Check for an option w/out a value.

Parameters:
nameThe option name
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found, but has value
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 105 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  return strempty(s) ? MB_SUCCESS : MB_TYPE_OUT_OF_RANGE;
}
ErrorCode moab::FileOptions::get_option ( const char *  name,
std::string &  value 
) const

Check for option.

Check for an option

Parameters:
nameThe option name
valueThe option value, or an empty string if no value.
Returns:
MB_SUCCESS or MB_ENTITY_NOT_FOUND

Definition at line 294 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  
  value = s;
  return MB_SUCCESS;
}  
ErrorCode moab::FileOptions::get_option ( const char *  name,
const char *&  value 
) const [private]

Check for option.

Check for an option

Parameters:
nameThe option name
valueThe option value, or an empty string if no value.
Returns:
MB_SUCCESS or MB_ENTITY_NOT_FOUND

Definition at line 305 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;
      
      mSeen[i - mOptions.begin()] = true;
      return MB_SUCCESS;
    }
  }
  
  return MB_ENTITY_NOT_FOUND;
}
void moab::FileOptions::get_options ( std::vector< std::string > &  list) const

Get list of options

Definition at line 389 of file FileOptions.cpp.

{
  list.clear();
  list.resize( mOptions.size() );
  std::copy( mOptions.begin(), mOptions.end(), list.begin() );
}
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

Parameters:
nameThe option name
valueOutput. The value.
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found, but does not have a double value
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 239 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  
    // empty string
  if (strempty(s))
    return MB_TYPE_OUT_OF_RANGE;
  
    // parse value
  char* endptr;
  value = strtod( s, &endptr );
  if (!strempty(endptr)) // syntax error
    return MB_TYPE_OUT_OF_RANGE;
  
  return MB_SUCCESS;
}
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

Parameters:
nameThe option name
valuesOutput. The list of double values.
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found, but does not contain an ID list
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 213 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  
    // empty string
  if (strempty(s))
    return MB_TYPE_OUT_OF_RANGE;
  
    // parse values
  while (!strempty(s)) {
    char* endptr;
    double sval = strtod( s, &endptr);

    EATSPACE(endptr);
    values.push_back(sval);

    s = endptr;
  }
  
  return MB_SUCCESS;
}
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.

Parameters:
nameThe option name
valueOutput. The value.
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found, but does not have a value
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 282 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  if (strempty(s))
    return MB_TYPE_OUT_OF_RANGE;
  value = s;
  return MB_SUCCESS;
}
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.

Parameters:
nameThe option name
valuesOutput. The list of string values.
Returns:
- MB_SUCCESS if option is found
  • MB_TYPE_OUT_OF_RANGE if options is found, but does not contain a string list
  • MB_ENTITY_NOT_FOUND if option is not found.

Definition at line 259 of file FileOptions.cpp.

{
  const char* s;
  ErrorCode rval = get_option( name, s );
  if (MB_SUCCESS != rval)
    return rval;
  
    // empty string
  if (strempty(s))
    return MB_TYPE_OUT_OF_RANGE;
  
    // parse values
  char separator[3] = { ' ', ',', '\0' };
  char *tmp_str = strdup(s);
  for (char* i = strtok( tmp_str, separator ); i; i = strtok( 0, separator )) 
    if (!strempty(i)) // skip empty strings
      values.push_back( std::string(i));
  free(tmp_str);
  
  return MB_SUCCESS;
}
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".

Parameters:
nameThe option name
default_valueThe value to return if the option is not specified.
valueThe resulting value. This argument is set to the passed default value if the option is not found.
Returns:
- MB_TYPE_OUT_OF_RANGE if options is found, but has an invalid value
  • MB_SUCCESS otherwise

Definition at line 350 of file FileOptions.cpp.

{
  static const char* values[] = {
    "true",  "yes", "1", "on",
    "false", "no",  "0", "off",
    0 };
  const int num_true = 4;
  
  int index;
  ErrorCode result = match_option( name, values, index );
  if (result == MB_SUCCESS) {
    value = index < num_true;
  }
  else if (result == MB_ENTITY_NOT_FOUND) {
    value = default_value;
    result = MB_SUCCESS;
  }
  else {
    result = MB_TYPE_OUT_OF_RANGE;
  }
  
  return result;
}
ErrorCode moab::FileOptions::get_unseen_option ( std::string &  value) const

Get first unseen option

Definition at line 407 of file FileOptions.cpp.

{
  std::vector<bool>::iterator i = std::find( mSeen.begin(), mSeen.end(), false );
  if (i == mSeen.end()) {
    name.clear();
    return MB_ENTITY_NOT_FOUND;
  }
  
  const char* opt = mOptions[i - mSeen.begin()];
  const char* end = strchr( opt, '=' );
  name = end ? std::string(opt, end-opt) : std::string(opt);
  return MB_SUCCESS;
}

Mark all options as seen. USed for non-root procs during bcast-delete read

Definition at line 401 of file FileOptions.cpp.

{
  mSeen.clear();
  mSeen.resize( mOptions.size(), true );
}
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.

Parameters:
nameThe option name
valuesA NULL-terminated array of C-style strings enumerating the possible option values.
indexOutput: The index into values for the option value.
Returns:
MB_SUCCESS if matched name and value. MB_ENTITY_NOT_FOUND if the option was not specified MB_FAILURE if the option value is not in the input values array.

Definition at line 333 of file FileOptions.cpp.

{
  const char* optval;
  ErrorCode rval = get_option( name, optval );
  if (MB_SUCCESS != rval)
    return rval;
  
  for (index = 0; values[index]; ++index)
    if (compare( optval, values[index] ))
      return MB_SUCCESS;
  
  index = -1;
  return MB_FAILURE;
}
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.

Parameters:
nameThe option name
valueThe expected value.
Returns:
MB_SUCCESS if matched name and value. MB_ENTITY_NOT_FOUND if the option was not specified MB_FAILURE if the option value doesn't match the passed string/

Definition at line 325 of file FileOptions.cpp.

{
  int idx;
  const char* array[] = { value, NULL };
  return match_option( name, array, idx );
}
FileOptions & moab::FileOptions::operator= ( const FileOptions copy)

Definition at line 80 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);
  }

  mSeen = copy.mSeen;
  return *this;
}
unsigned moab::FileOptions::size ( ) const [inline]

number of options

Definition at line 207 of file FileOptions.hpp.

    { return mOptions.size(); }

Member Data Documentation

char* moab::FileOptions::mData [private]

Definition at line 237 of file FileOptions.hpp.

std::vector<const char*> moab::FileOptions::mOptions [private]

Definition at line 238 of file FileOptions.hpp.

std::vector<bool> moab::FileOptions::mSeen [mutable, private]

Definition at line 239 of file FileOptions.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines