1 /*
2 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3 * storing and accessing finite element mesh data.
4 *
5 * Copyright 2004 Sandia Corporation. Under the terms of Contract
6 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
7 * retains certain rights in this software.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 */
15
16 /**\file FileOptions.hpp
17 *\author Jason Kraftcheck (kraftche@cae.wisc.edu)
18 *\date 2007-08-21
19 */
20
21 #ifndef FILE_OPTIONS_HPP
22 #define FILE_OPTIONS_HPP
23
24 #include <string>
25 #include <vector>
26 #include "moab/Types.hpp"
27
28 namespace moab
29 {
30
31 /**\brief Parse options string passed to file IO routines
32 *
33 * This is a utility class used by file-IO-related code to
34 * parse the options string passed to Core::load_file and
35 * Core::write_file
36 */
37 class MOAB_EXPORT FileOptions
38 {
39 public:
40 /*\param options_string The concatenation of a list of
41 * options, separated either by the default separator
42 * (semicolon) with a custom separator specified at
43 * the beginning of the string (semicolon followed by
44 * destired separator character.)
45 */
46 FileOptions( const char* option_string );
47
48 FileOptions( const FileOptions& copy );
49 FileOptions& operator=( const FileOptions& copy );
50
51 ~FileOptions();
52
53 /**\brief Check for option with no value
54 *
55 * Check for an option w/out a value.
56 *\param name The option name
57 *\return - MB_SUCCESS if option is found
58 * - MB_TYPE_OUT_OF_RANGE if options is found, but has value
59 * - MB_ENTITY_NOT_FOUND if option is not found.
60 */
61 ErrorCode get_null_option( const char* name ) const;
62
63 /**\brief Check for option with boolean (true/false, yes/no) value.
64 *
65 * Check for an option with a true/false value. Allowable values
66 * are "true", "false", "yes", "no", "1", "0", "on", "off".
67 *\param name The option name
68 *\param default_value The value to return if the option is not specified.
69 *\param value The resulting value. This argument is set to the passed
70 * default value if the option is not found.
71 *\return - MB_TYPE_OUT_OF_RANGE if options is found, but has an invalid value
72 * - MB_SUCCESS otherwise
73 */
74 ErrorCode get_toggle_option( const char* name, bool default_value, bool& value ) const;
75
76 /**\brief Check for option with an integer value.
77 *
78 * Check for an option with an integer value
79 *\param name The option name
80 *\param value Output. The value.
81 *\return - MB_SUCCESS if option is found
82 * - MB_TYPE_OUT_OF_RANGE if options is found, but does not have an integer value
83 * - MB_ENTITY_NOT_FOUND if option is not found.
84 */
85 ErrorCode get_int_option( const char* name, int& value ) const;
86
87 /**\brief Check for option with an integer value. Accept option with no value.
88 *
89 * Check for an option with an integer value.
90 * If the option is found but has no value specified, then
91 * pass back the user-specified default value.
92 *
93 *\NOTE: This function will not pass back the default_val, but will instead
94 * return MB_ENTITY_NOT_FOUND if the option is not specified at all.
95 * The default value is returned only when the option is specified,
96 * but is specified w/out a value.
97 *
98 *\param name The option name
99 *\param default_val The default value for the option.
100 *\param value Output. The value.
101 *\return - MB_SUCCESS if option is found
102 * - MB_TYPE_OUT_OF_RANGE if options is found but has a value that cannot be parsed as an
103 *int
104 * - MB_ENTITY_NOT_FOUND if option is not found.
105 */
106 ErrorCode get_int_option( const char* name, int default_val, int& value ) const;
107
108 /**\brief Check for option with a double value.
109 *
110 * Check for an option with a double value
111 *\param name The option name
112 *\param value Output. The value.
113 *\return - MB_SUCCESS if option is found
114 * - MB_TYPE_OUT_OF_RANGE if options is found, but does not have a double value
115 * - MB_ENTITY_NOT_FOUND if option is not found.
116 */
117 ErrorCode get_real_option( const char* name, double& value ) const;
118
119 /**\brief Check for option with any value.
120 *
121 * Check for an option with any value.
122 *\param name The option name
123 *\param value Output. The value.
124 *\return - MB_SUCCESS if option is found
125 * - MB_TYPE_OUT_OF_RANGE if options is found, but does not have a value
126 * - MB_ENTITY_NOT_FOUND if option is not found.
127 */
128 ErrorCode get_str_option( const char* name, std::string& value ) const;
129
130 /**\brief Check for option
131 *
132 * Check for an option
133 *\param name The option name
134 *\param value The option value, or an empty string if no value.
135 *\return MB_SUCCESS or MB_ENTITY_NOT_FOUND
136 */
137 ErrorCode get_option( const char* name, std::string& value ) const;
138
139 /**\brief Check the string value of an option
140 *
141 * Check which of a list of possible values a string option contains.
142 *\param name The option name
143 *\param values A NULL-terminated array of C-style strings enumerating
144 * the possible option values.
145 *\param index Output: The index into <code>values</code> for the
146 * option value.
147 *\return MB_SUCCESS if matched name and value.
148 * MB_ENTITY_NOT_FOUND if the option was not specified
149 * MB_FAILURE if the option value is not in the input <code>values</code> array.
150 */
151 ErrorCode match_option( const char* name, const char* const* values, int& index ) const;
152
153 /**\brief Check if an option matches a string value
154 *
155 * Check if the value for an option is the passed string.
156 *\param name The option name
157 *\param value The expected value.
158 *\return MB_SUCCESS if matched name and value.
159 * MB_ENTITY_NOT_FOUND if the option was not specified
160 * MB_FAILURE if the option value doesn't match the passed string/
161 */
162 ErrorCode match_option( const char* name, const char* value ) const;
163
164 /**\brief Check for option for which the value is a list of ints
165 *
166 * Check for an option which is an int list. The value is expected to
167 * be a comma-separated list of int ranges, where an int range can be
168 * either a single integer value or a range of integer values separated
169 * by a dash ('-').
170 *
171 *\param name The option name
172 *\param values Output. The list of integer values.
173 *\return - MB_SUCCESS if option is found
174 * - MB_TYPE_OUT_OF_RANGE if options is found, but does not contain an ID list
175 * - MB_ENTITY_NOT_FOUND if option is not found.
176 */
177 ErrorCode get_ints_option( const char* name, std::vector< int >& values ) const;
178
179 /**\brief Check for option for which the value is a list of doubles
180 *
181 * Check for an option which is a double list. The value is expected to
182 * be a comma-separated list of double values
183 *
184 *\param name The option name
185 *\param values Output. The list of double values.
186 *\return - MB_SUCCESS if option is found
187 * - MB_TYPE_OUT_OF_RANGE if options is found, but does not contain an ID list
188 * - MB_ENTITY_NOT_FOUND if option is not found.
189 */
190 ErrorCode get_reals_option( const char* name, std::vector< double >& values ) const;
191
192 /**\brief Check for option for which the value is a list of strings
193 *
194 * Check for an option which is a string list. The value is expected to
195 * be a comma-separated list of string values, with no embedded spaces or commas.
196 *
197 *\param name The option name
198 *\param values Output. The list of string values.
199 *\return - MB_SUCCESS if option is found
200 * - MB_TYPE_OUT_OF_RANGE if options is found, but does not contain a string list
201 * - MB_ENTITY_NOT_FOUND if option is not found.
202 */
203 ErrorCode get_strs_option( const char* name, std::vector< std::string >& values ) const;
204
205 /** number of options */
206 inline unsigned size() const
207 {
208 return mOptions.size();
209 }
210
211 /** true if no options */
212 inline bool empty() const
213 {
214 return mOptions.empty();
215 }
216
217 /** Get list of options */
218 void get_options( std::vector< std::string >& list ) const;
219
220 /** Check if all options have been looked at */
221 bool all_seen() const;
222
223 /** Mark all options as seen. USed for non-root procs during bcast-delete read */
224 void mark_all_seen() const;
225
226 /** Get first unseen option */
227 ErrorCode get_unseen_option( std::string& value ) const;
228
229 private:
230 /**\brief Check for option
231 *
232 * Check for an option
233 *\param name The option name
234 *\param value The option value, or an empty string if no value.
235 *\return MB_SUCCESS or MB_ENTITY_NOT_FOUND
236 */
237 ErrorCode get_option( const char* name, const char*& value ) const;
238
239 char* mData;
240 std::vector< const char* > mOptions;
241 mutable std::vector< bool > mSeen;
242
243 /** Case-insensitive compare of name with option value. */
244 static bool compare( const char* name, const char* option );
245 };
246
247 } // namespace moab
248
249 #endif