#include "parse.hpp"
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <cstring>
Go to the source code of this file.
|
void | tag_syntax (std::ostream &s) |
|
static bool | is_platform_little_endian () |
|
static void * | parse_values (const char *vals, DataType type, int size) |
|
static unsigned char * | parse_opaque_value (const char *vals, int size) |
|
template<typename T > |
T * | parse_values_typed (const char *vals, int size) |
|
template<typename T > |
int | parse_value (const char *&iter, T &value) |
|
static int | hexdigit (char c) |
|
template<> |
int | parse_value< double > (const char *&iter, double &value) |
|
int | parse_tag_spec (char *name, TagSpec &result, Interface *iface) |
|
int | parse_tag_create (char *name, TagSpec &result, Interface *iface) |
|
◆ hexdigit()
Definition at line 100 of file parse.cpp.
102 if( c >=
'0' && c <=
'9' )
104 else if( c >=
'a' && c <=
'f' )
106 else if( c >=
'A' && c <=
'F' )
Referenced by parse_opaque_value().
◆ is_platform_little_endian()
static bool is_platform_little_endian |
( |
| ) |
|
|
inlinestatic |
Definition at line 65 of file parse.cpp.
67 static const unsigned int one = 1;
68 static const bool little = !*( (
char*)&one );
Referenced by parse_opaque_value().
◆ parse_opaque_value()
unsigned char * parse_opaque_value |
( |
const char * |
vals, |
|
|
int |
size |
|
) |
| |
|
static |
Definition at line 112 of file parse.cpp.
114 unsigned char* data = (
unsigned char*)malloc(
size );
115 if( vals[0] && vals[0] ==
'0' && vals[1] && toupper( vals[1] ) ==
'X' )
117 unsigned char *iter, *end;
127 iter = data +
size - 1;
132 const char* vals_end = vals + 1;
133 const char* vals_iter = vals + strlen( vals ) - 1;
134 for( ; iter != end; iter += step )
138 if( vals_iter != vals_end )
143 if( vals_iter != vals_end )
148 if( less < 0 || most < 0 )
150 std::cerr <<
"Error parsing hex value: " << vals << std::endl;
155 *iter = 16 * most + less;
160 memset( data, 0,
size );
161 strcpy( (
char*)data, vals + 2 );
References hexdigit(), is_platform_little_endian(), and size.
Referenced by parse_values().
◆ parse_tag_create()
Definition at line 275 of file parse.cpp.
279 char* eq1 = strrchr( name,
'=' );
282 std::cerr <<
"Invalid tag specification: " << name << std::endl;
287 char *type_str = eq1, *val = 0;
289 char* eq2 = strrchr( name,
'=' );
294 val = (
'\0' == eq1[0] ) ? 0 : eq1;
299 char* size_str = strchr( type_str,
':' );
302 std::cerr <<
"Invalid tag type specification: " << type_str << std::endl;
308 if( !strcmp( type_str,
"int" ) )
312 else if( !strcmp( type_str,
"double" ) )
316 else if( !strcmp( type_str,
"bit" ) )
320 else if( !strcmp( type_str,
"handle" ) )
324 else if( !strcmp( type_str,
"opaque" ) )
330 std::cerr <<
"Invalid tag type specification: " << type_str << std::endl;
334 int count = (int)strtol( size_str, &end_ptr, 0 );
335 if( !*size_str || *end_ptr || count < 1 )
337 std::cerr <<
"Invalid tag size specification: " << size_str << std::endl;
346 if( !result.
value )
return 1;
358 std::cerr <<
"Error accessing properties of tag: " << name << std::endl;
362 if( etype != type || esize != count )
364 std::cerr <<
"Tag already exists with different type: " << name << std::endl;
368 std::vector< unsigned char > value( esize );
374 std::cerr <<
"Error checking default value of tag: " << name << std::endl;
379 std::cerr <<
"Tag already exists and default value doesn't match: " << name << std::endl;
390 std::cerr <<
"Failed to create tag: " << name << std::endl;
References ErrorCode, TagSpec::handle, iface, MB_ENTITY_NOT_FOUND, MB_SUCCESS, MB_TAG_ANY, MB_TAG_CREAT, MB_TAG_SPARSE, MB_TYPE_BIT, MB_TYPE_DOUBLE, MB_TYPE_HANDLE, MB_TYPE_INTEGER, MB_TYPE_OPAQUE, parse_values(), moab::Interface::tag_get_data_type(), moab::Interface::tag_get_default_value(), moab::Interface::tag_get_handle(), moab::Interface::tag_get_length(), and TagSpec::value.
Referenced by main().
◆ parse_tag_spec()
Definition at line 218 of file parse.cpp.
221 char* val = strrchr( name,
'=' );
227 std::cerr <<
"Cannot create tag w/out name: " << name << std::endl;
239 std::cerr <<
"Tag not found: " << name << std::endl;
244 std::cerr <<
"Error retrieving tag handle: " << name << std::endl;
256 std::cerr <<
"Error retrieving type for tag: " << name << std::endl;
264 std::cerr <<
"Error retrieving size for tag: " << name << std::endl;
269 if( !result.
value )
return 1;
References ErrorCode, TagSpec::handle, iface, MB_SUCCESS, MB_TAG_ANY, MB_TAG_NOT_FOUND, MB_TYPE_OPAQUE, parse_values(), size, moab::Interface::tag_get_data_type(), moab::Interface::tag_get_handle(), moab::Interface::tag_get_length(), and TagSpec::value.
Referenced by main().
◆ parse_value()
template<typename T >
int parse_value |
( |
const char *& |
iter, |
|
|
T & |
value |
|
) |
| |
Definition at line 73 of file parse.cpp.
76 long parsed_val = strtol( iter, &endptr, 0 );
77 if( endptr == iter )
return 1;
80 value = (T)parsed_val;
81 if( (
long)value != parsed_val )
83 std::cerr <<
"Value too large: " << iter << std::endl;
◆ parse_value< double >()
template<>
int parse_value< double > |
( |
const char *& |
iter, |
|
|
double & |
value |
|
) |
| |
Definition at line 91 of file parse.cpp.
94 value = strtod( iter, &endptr );
95 if( endptr == iter )
return 1;
◆ parse_values()
void * parse_values |
( |
const char * |
vals, |
|
|
DataType |
type, |
|
|
int |
size |
|
) |
| |
|
static |
Definition at line 198 of file parse.cpp.
205 return parse_values_typed< int >( vals,
size );
207 return parse_values_typed< double >( vals,
size );
209 return parse_values_typed< bittype >( vals,
size );
211 return parse_values_typed< EntityHandle >( vals,
size );
213 std::cerr <<
"Unknown tag data type: " << (int)type << std::endl;
References MB_TYPE_BIT, MB_TYPE_DOUBLE, MB_TYPE_HANDLE, MB_TYPE_INTEGER, MB_TYPE_OPAQUE, parse_opaque_value(), and size.
Referenced by parse_tag_create(), and parse_tag_spec().
◆ parse_values_typed()
template<typename T >
T * parse_values_typed |
( |
const char * |
vals, |
|
|
int |
size |
|
) |
| |
Definition at line 168 of file parse.cpp.
170 if( !count )
return 0;
172 T* data = (T*)malloc( count *
sizeof( T ) );
173 T* end = data + count;
174 if( parse_value< T >( vals, *data ) )
179 for( T* ptr = data + 1; ptr != end; ++ptr )
183 std::cerr <<
"Expected ',' separating tag values: " << vals << std::endl;
188 if( parse_value< T >( vals, *ptr ) )
◆ tag_syntax()
void tag_syntax |
( |
std::ostream & |
s | ) |
|
Definition at line 10 of file parse.cpp.
12 s <<
"Tags are specified as <name>=[value], where the tag value " << std::endl
13 <<
"is optional." << std::endl
15 <<
"Values of integral types (INTEGER, BIT, and HANDLE) are " << std::endl
16 <<
"specified using standard C integer notation (a 0x prefix " << std::endl
17 <<
"for hexidecimal, 0 prefix for octal, and no prefix for " << std::endl
18 <<
"decimal.) The value of an opaque tag is interpreted as " << std::endl
19 <<
"either a integral value or a character string. If the tag " << std::endl
20 <<
"value begins with the prefix 0x it will be interpreted as a " << std::endl
21 <<
"hexidecimal (base-16) number. If the value does not begin " << std::endl
22 <<
"with the 0x prefix, it is interpreted as a character string." << std::endl
23 <<
"Characater strings will be padded with null characters as" << std::endl
24 <<
"necessary to fill the tag." << std::endl
25 <<
"Floating-point (real) values must be specified in base-10." << std::endl
26 <<
"C exponential notation (e.g. 1e-10) is accepted." << std::endl
28 <<
"If the tag is an array of integral or floating-point values " << std::endl
29 <<
"then the tag value must be specified as a comma-separated " << std::endl
30 <<
"list, with NO spaces." << std::endl
32 <<
"Tags are created with the syntax name=type:size[=default_value]." << std::endl
33 <<
"where type is one of {int,double,opaque,handle,bit} and size is " << std::endl
34 <<
"the number of values of the specified type, or the number of " << std::endl
35 <<
"bytes if the type is 'opaque', A default value for the tag may " << std::endl
36 <<
"be specified." << std::endl;
Referenced by usage().