Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
FileOptions.hpp
Go to the documentation of this file.
00001 /*
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 /**\file FileOptions.hpp
00017  *\author Jason Kraftcheck ([email protected])
00018  *\date 2007-08-21
00019  */
00020 
00021 #ifndef FILE_OPTIONS_HPP
00022 #define FILE_OPTIONS_HPP
00023 
00024 #include <string>
00025 #include <vector>
00026 #include "moab/Types.hpp"
00027 
00028 namespace moab
00029 {
00030 
00031 /**\brief Parse options string passed to file IO routines
00032  *
00033  * This is a utility class used by file-IO-related code to
00034  * parse the options string passed to Core::load_file and
00035  * Core::write_file
00036  */
00037 class MOAB_EXPORT FileOptions
00038 {
00039   public:
00040     /*\param options_string The concatenation of a list of
00041      *          options, separated either by the default separator
00042      *          (semicolon) with a custom separator specified at
00043      *          the beginning of the string (semicolon followed by
00044      *          destired separator character.)
00045      */
00046     FileOptions( const char* option_string );
00047 
00048     FileOptions( const FileOptions& copy );
00049     FileOptions& operator=( const FileOptions& copy );
00050 
00051     ~FileOptions();
00052 
00053     /**\brief Check for option with no value
00054      *
00055      * Check for an option w/out a value.
00056      *\param name The option name
00057      *\return - MB_SUCCESS if option is found
00058      *        - MB_TYPE_OUT_OF_RANGE if options is found, but has value
00059      *        - MB_ENTITY_NOT_FOUND if option is not found.
00060      */
00061     ErrorCode get_null_option( const char* name ) const;
00062 
00063     /**\brief Check for option with boolean (true/false, yes/no) value.
00064      *
00065      * Check for an option with a true/false value.  Allowable values
00066      * are "true", "false", "yes", "no", "1", "0", "on", "off".
00067      *\param name The option name
00068      *\param default_value The value to return if the option is not specified.
00069      *\param value The resulting value.  This argument is set to the passed
00070      *            default value if the option is not found.
00071      *\return - MB_TYPE_OUT_OF_RANGE if options is found, but has an invalid value
00072      *        - MB_SUCCESS otherwise
00073      */
00074     ErrorCode get_toggle_option( const char* name, bool default_value, bool& value ) const;
00075 
00076     /**\brief Check for option with an integer value.
00077      *
00078      * Check for an option with an integer value
00079      *\param name The option name
00080      *\param value Output. The value.
00081      *\return - MB_SUCCESS if option is found
00082      *        - MB_TYPE_OUT_OF_RANGE if options is found, but does not have an integer value
00083      *        - MB_ENTITY_NOT_FOUND if option is not found.
00084      */
00085     ErrorCode get_int_option( const char* name, int& value ) const;
00086 
00087     /**\brief Check for option with an integer value.  Accept option with no value.
00088      *
00089      * Check for an option with an integer value.
00090      * If the option is found but has no value specified, then
00091      * pass back the user-specified default value.
00092      *
00093      *\NOTE:  This function will not pass back the default_val, but will instead
00094      *        return MB_ENTITY_NOT_FOUND if the option is not specified at all.
00095      *        The default value is returned only when the option is specified,
00096      *        but is specified w/out a value.
00097      *
00098      *\param name The option name
00099      *\param default_val The default value for the option.
00100      *\param value Output. The value.
00101      *\return - MB_SUCCESS if option is found
00102      *        - MB_TYPE_OUT_OF_RANGE if options is found but has a value that cannot be parsed as an
00103      *int
00104      *        - MB_ENTITY_NOT_FOUND if option is not found.
00105      */
00106     ErrorCode get_int_option( const char* name, int default_val, int& value ) const;
00107 
00108     /**\brief Check for option with a double value.
00109      *
00110      * Check for an option with a double value
00111      *\param name The option name
00112      *\param value Output. The value.
00113      *\return - MB_SUCCESS if option is found
00114      *        - MB_TYPE_OUT_OF_RANGE if options is found, but does not have a double value
00115      *        - MB_ENTITY_NOT_FOUND if option is not found.
00116      */
00117     ErrorCode get_real_option( const char* name, double& value ) const;
00118 
00119     /**\brief Check for option with any value.
00120      *
00121      * Check for an option with any value.
00122      *\param name The option name
00123      *\param value Output. The value.
00124      *\return - MB_SUCCESS if option is found
00125      *        - MB_TYPE_OUT_OF_RANGE if options is found, but does not have a value
00126      *        - MB_ENTITY_NOT_FOUND if option is not found.
00127      */
00128     ErrorCode get_str_option( const char* name, std::string& value ) const;
00129 
00130     /**\brief Check for option
00131      *
00132      * Check for an option
00133      *\param name The option name
00134      *\param value The option value, or an empty string if no value.
00135      *\return MB_SUCCESS or MB_ENTITY_NOT_FOUND
00136      */
00137     ErrorCode get_option( const char* name, std::string& value ) const;
00138 
00139     /**\brief Check the string value of an option
00140      *
00141      * Check which of a list of possible values a string option contains.
00142      *\param name The option name
00143      *\param values A NULL-terminated array of C-style strings enumerating
00144      *              the possible option values.
00145      *\param index  Output: The index into <code>values</code> for the
00146      *              option value.
00147      *\return MB_SUCCESS if matched name and value.
00148      *        MB_ENTITY_NOT_FOUND if the option was not specified
00149      *        MB_FAILURE if the option value is not in the input <code>values</code> array.
00150      */
00151     ErrorCode match_option( const char* name, const char* const* values, int& index ) const;
00152 
00153     /**\brief Check if an option matches a string value
00154      *
00155      * Check if the value for an option is the passed string.
00156      *\param name The option name
00157      *\param value The expected value.
00158      *\return MB_SUCCESS if matched name and value.
00159      *        MB_ENTITY_NOT_FOUND if the option was not specified
00160      *        MB_FAILURE if the option value doesn't match the passed string/
00161      */
00162     ErrorCode match_option( const char* name, const char* value ) const;
00163 
00164     /**\brief Check for option for which the value is a list of ints
00165      *
00166      * Check for an option which is an int list.  The value is expected to
00167      * be a comma-separated list of int ranges, where an int range can be
00168      * either a single integer value or a range of integer values separated
00169      * by a dash ('-').
00170      *
00171      *\param name The option name
00172      *\param values Output. The list of integer values.
00173      *\return - MB_SUCCESS if option is found
00174      *        - MB_TYPE_OUT_OF_RANGE if options is found, but does not contain an ID list
00175      *        - MB_ENTITY_NOT_FOUND if option is not found.
00176      */
00177     ErrorCode get_ints_option( const char* name, std::vector< int >& values ) const;
00178 
00179     /**\brief Check for option for which the value is a list of doubles
00180      *
00181      * Check for an option which is a double list.  The value is expected to
00182      * be a comma-separated list of double values
00183      *
00184      *\param name The option name
00185      *\param values Output. The list of double values.
00186      *\return - MB_SUCCESS if option is found
00187      *        - MB_TYPE_OUT_OF_RANGE if options is found, but does not contain an ID list
00188      *        - MB_ENTITY_NOT_FOUND if option is not found.
00189      */
00190     ErrorCode get_reals_option( const char* name, std::vector< double >& values ) const;
00191 
00192     /**\brief Check for option for which the value is a list of strings
00193      *
00194      * Check for an option which is a string list.  The value is expected to
00195      * be a comma-separated list of string values, with no embedded spaces or commas.
00196      *
00197      *\param name The option name
00198      *\param values Output. The list of string values.
00199      *\return - MB_SUCCESS if option is found
00200      *        - MB_TYPE_OUT_OF_RANGE if options is found, but does not contain a string list
00201      *        - MB_ENTITY_NOT_FOUND if option is not found.
00202      */
00203     ErrorCode get_strs_option( const char* name, std::vector< std::string >& values ) const;
00204 
00205     /** number of options */
00206     inline unsigned size() const
00207     {
00208         return mOptions.size();
00209     }
00210 
00211     /** true if no options */
00212     inline bool empty() const
00213     {
00214         return mOptions.empty();
00215     }
00216 
00217     /** Get list of options */
00218     void get_options( std::vector< std::string >& list ) const;
00219 
00220     /** Check if all options have been looked at */
00221     bool all_seen() const;
00222 
00223     /** Mark all options as seen.  USed for non-root procs during bcast-delete read */
00224     void mark_all_seen() const;
00225 
00226     /** Get first unseen option */
00227     ErrorCode get_unseen_option( std::string& value ) const;
00228 
00229   private:
00230     /**\brief Check for option
00231      *
00232      * Check for an option
00233      *\param name The option name
00234      *\param value The option value, or an empty string if no value.
00235      *\return MB_SUCCESS or MB_ENTITY_NOT_FOUND
00236      */
00237     ErrorCode get_option( const char* name, const char*& value ) const;
00238 
00239     char* mData;
00240     std::vector< const char* > mOptions;
00241     mutable std::vector< bool > mSeen;
00242 
00243     /** Case-insensitive compare of name with option value. */
00244     static bool compare( const char* name, const char* option );
00245 };
00246 
00247 }  // namespace moab
00248 
00249 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines