cgma
CGMFileOptions Class Reference

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

#include <CGMFileOptions.hpp>

List of all members.

Public Member Functions

 CGMFileOptions (const char *option_string)
 CGMFileOptions (const CGMFileOptions &copy)
CGMFileOptionsoperator= (const CGMFileOptions &copy)
 ~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

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 ParallelMeshTool::load_file

Definition at line 27 of file CGMFileOptions.hpp.


Constructor & Destructor Documentation

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 );
  }
}

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 );
}

Member Function Documentation

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.

{
  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 CGMFileOptions::empty ( ) const [inline]

true if no options

Definition at line 140 of file CGMFileOptions.hpp.

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

Parameters:
nameThe option name
valueOutput. The value.
Returns:
- CUBIT_SUCCESS if option is found
  • CUBIT_TYPE_OUT_OF_RANGE if options is found, but does not have an integer value
  • CUBIT_ENTITY_NOT_FOUND if option is not found.

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 ('-').

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

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.

Parameters:
nameThe option name
Returns:
- CUBIT_SUCCESS if option is found
  • CUBIT_TYPE_OUT_OF_RANGE if options is found, but has value
  • CUBIT_ENTITY_NOT_FOUND if option is not found.

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

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

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

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

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.

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

Parameters:
nameThe option name
valueOutput. The value.
Returns:
- CUBIT_SUCCESS if option is found
  • CUBIT_TYPE_OUT_OF_RANGE if options is found, but does not have a double value
  • CUBIT_ENTITY_NOT_FOUND if option is not found.

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.

Parameters:
nameThe option name
valueOutput. The value.
Returns:
- CUBIT_SUCCESS if option is found
  • CUBIT_TYPE_OUT_OF_RANGE if options is found, but does not have a value
  • CUBIT_ENTITY_NOT_FOUND if option is not found.

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.

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:
CUBIT_SUCCESS if matched name and value. CUBIT_ENTITY_NOT_FOUND if the option was not specified CUBIT_FAILURE if the option value is not in the input 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.

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

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]

number of options

Definition at line 136 of file CGMFileOptions.hpp.

    { return mOptions.size(); }

Member Data Documentation

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.


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