MOAB: Mesh Oriented datABase  (version 5.4.1)
moab::FileOptions Class Reference

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

#include <FileOptions.hpp>

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

Examples:
VisTags.cpp.

Definition at line 37 of file FileOptions.hpp.


Constructor & Destructor Documentation

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

Definition at line 38 of file FileOptions.cpp.

References moab::DEFAULT_SEPARATOR, mData, mOptions, mSeen, and moab::strempty().

                                          : 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 67 of file FileOptions.cpp.

References mData, mOptions, and mSeen.

                                                  : 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 106 of file FileOptions.cpp.

References mData.

{
    free( mData );
}

Member Function Documentation

Check if all options have been looked at

Definition at line 377 of file FileOptions.cpp.

References mSeen.

Referenced by moab::AdaptiveKDTree::build_tree(), moab::BVHTree::build_tree(), moab::Core::load_file(), and main().

{
    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 357 of file FileOptions.cpp.

References moab::strempty().

Referenced by get_option(), and match_option().

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

Referenced by main().

    {
        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 119 of file FileOptions.cpp.

References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().

Referenced by moab::NCHelperFV::init_mesh_vals(), moab::NCHelperEuler::init_mesh_vals(), moab::ReadParallel::load_file(), moab::ReadMCNP5::load_file(), moab::Core::load_file(), moab::Tqdcfr::load_file(), main(), moab::WriteHDF5Parallel::parallel_create_file(), moab::Tree::parse_common_options(), moab::BVHTree::parse_options(), moab::AdaptiveKDTree::parse_options(), moab::ReadNC::parse_options(), moab::WriteNC::parse_options(), moab::ReadCGM::set_options(), moab::ReadHDF5::set_up_read(), moab::WriteSmf::write_file(), moab::WriteVtk::write_file(), moab::WriteGmsh::write_file(), moab::WriteSTL::write_file(), and moab::WriteHDF5::write_file().

{
    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 141 of file FileOptions.cpp.

References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().

{
    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 167 of file FileOptions.cpp.

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

{
    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 111 of file FileOptions.cpp.

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(), main(), moab::WriteHDF5Parallel::parallel_create_file(), moab::ReadDamsel::parse_options(), moab::ReadNC::parse_options(), moab::WriteNC::parse_options(), moab::ReadCGM::set_options(), moab::ReadHDF5::set_up_read(), moab::WriteVtk::write_file(), moab::WriteSTL::write_file(), moab::WriteHDF5::write_file(), moab::WriteNCDF::write_file(), and moab::WriteHDF5::write_file_impl().

{
    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 282 of file FileOptions.cpp.

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::ReadVtk::load_file(), moab::ReadSmf::load_file(), moab::Core::load_file(), moab::ReadHDF5::load_file_partial(), main(), match_option(), and moab::ReadTetGen::open_file().

{
    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 292 of file FileOptions.cpp.

References compare(), MB_ENTITY_NOT_FOUND, MB_SUCCESS, mOptions, and mSeen.

{
    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 370 of file FileOptions.cpp.

References mOptions.

{
    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 234 of file FileOptions.cpp.

References ErrorCode, get_option(), MB_SUCCESS, MB_TYPE_OUT_OF_RANGE, and moab::strempty().

Referenced by moab::ReadParallel::load_file(), main(), moab::Tree::parse_common_options(), moab::AdaptiveKDTree::parse_options(), moab::ReadCGM::set_options(), and test_valid_tree().

{
    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 210 of file FileOptions.cpp.

References EATSPACE, 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().

{
    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 272 of file FileOptions.cpp.

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::ReadTetGen::load_file(), moab::ReadNCDF::load_file(), moab::Tqdcfr::load_file(), main(), moab::WriteHDF5Parallel::parallel_create_file(), moab::Tree::parse_common_options(), moab::ReadHDF5::set_up_read(), and moab::ReadNCDF::update().

{
    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.
Examples:
VisTags.cpp.

Definition at line 252 of file FileOptions.cpp.

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

{
    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 333 of file FileOptions.cpp.

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

{
    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 388 of file FileOptions.cpp.

References MB_ENTITY_NOT_FOUND, MB_SUCCESS, mOptions, and mSeen.

Referenced by moab::Core::load_file(), and main().

{
    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 382 of file FileOptions.cpp.

References mOptions, and mSeen.

Referenced by moab::ReadParallel::load_file(), moab::ReadCGNS::process_options(), and moab::ReadTemplate::process_options().

{
    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 320 of file FileOptions.cpp.

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::ReadCGM::set_options(), moab::ReadHDF5::set_up_read(), and moab::WriteHDF5::write_file_impl().

{
    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 313 of file FileOptions.cpp.

References match_option().

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

Definition at line 82 of file FileOptions.cpp.

References mData, mOptions, and mSeen.

{
    // Check for self-assignment
    if( this == &copy ) return *this;

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

Referenced by main().

    {
        return mOptions.size();
    }

Member Data Documentation

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

Definition at line 239 of file FileOptions.hpp.

Referenced by FileOptions(), operator=(), and ~FileOptions().

std::vector< const char* > moab::FileOptions::mOptions [private]
std::vector< bool > moab::FileOptions::mSeen [mutable, private]

List of all members.


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