Parse a file as space-separated tokens. More...
#include <FileTokenizer.hpp>
Public Member Functions | |
FileTokenizer (std::FILE *file_ptr, ReadUtilIface *read_util_ptr) | |
constructor More... | |
~FileTokenizer () | |
destructor : closes file. More... | |
const char * | get_string () |
get next token More... | |
bool | get_newline (bool report_error=true) |
check for newline More... | |
bool | get_doubles (size_t count, double *array) |
Parse a sequence of double values. More... | |
bool | get_floats (size_t count, float *array) |
Parse a sequence of float values. More... | |
bool | get_integers (size_t count, int *array) |
Parse a sequence of integer values. More... | |
bool | get_long_ints (size_t count, long *array) |
Parse a sequence of integer values. More... | |
bool | get_short_ints (size_t count, short *array) |
Parse a sequence of integer values. More... | |
bool | get_bytes (size_t count, unsigned char *array) |
Parse a sequence of integer values. More... | |
bool | get_binary (size_t bytes, void *mem) |
Read binary data (interleaved with ASCII) More... | |
bool | get_booleans (size_t count, bool *array) |
Parse a sequence of bit or boolean values. More... | |
bool | eof () const |
int | line_number () const |
void | unget_token () |
bool | match_token (const char *string, bool print_error=true) |
int | match_token (const char *const *string_list, bool print_error=true) |
Private Member Functions | |
bool | get_double_internal (double &result) |
bool | get_long_int_internal (long &result) |
bool | get_boolean_internal (bool &result) |
bool | get_float_internal (float &result) |
bool | get_integer_internal (int &result) |
bool | get_short_int_internal (short &result) |
bool | get_byte_internal (unsigned char &result) |
Private Attributes | |
std::FILE * | filePtr |
char | buffer [512] |
char * | nextToken |
char * | bufferEnd |
int | lineNumber |
char | lastChar |
Parse a file as space-separated tokens.
Read a file, separating it into space-separated tokens. This is provided in place of using the standard C or C++ file parsing routines because it counts lines, which is useful for error reporting. Also provides some useful utility methods for parsing VTK files (which is the intended use of this implementation.)
Uses raw reads/writes, implementing internal buffering. Token size may not exceed buffer size.
Definition at line 44 of file FileTokenizer.hpp.
moab::FileTokenizer::FileTokenizer | ( | std::FILE * | file_ptr, |
ReadUtilIface * | read_util_ptr | ||
) |
constructor
file_ptr | The file to read from. |
read_util_ptr | Pointer to ReadUtilIface to use for reporting errors. |
Definition at line 30 of file FileTokenizer.cpp.
moab::FileTokenizer::~FileTokenizer | ( | ) |
destructor : closes file.
The destructor closes the passed file handle. This is done as a convenience feature. If the caller creates an instance of this object on the stack, the file will automatically be closed when the caller returns.
Definition at line 35 of file FileTokenizer.cpp.
References filePtr.
bool moab::FileTokenizer::eof | ( | ) | const |
Check for end-of-file condition.
Definition at line 40 of file FileTokenizer.cpp.
References bufferEnd, filePtr, and nextToken.
Referenced by get_newline(), and moab::ReadVtk::load_file().
bool moab::FileTokenizer::get_binary | ( | size_t | bytes, |
void * | mem | ||
) |
Read binary data (interleaved with ASCII)
Read a block of binary data.
bytes | Number of bytes to read |
mem | Memory address at which to store data. |
Definition at line 420 of file FileTokenizer.cpp.
|
private |
Internal implementation of get_Booleans
Definition at line 214 of file FileTokenizer.cpp.
References get_string(), line_number(), and MB_SET_ERR_RET_VAL.
Referenced by get_booleans().
bool moab::FileTokenizer::get_booleans | ( | size_t | count, |
bool * | array | ||
) |
Parse a sequence of bit or boolean values.
Read the specified number of space-delimited values.
count | The number of values to read. |
array | The memory at which to store the values. |
Definition at line 295 of file FileTokenizer.cpp.
References get_boolean_internal().
Referenced by moab::ReadVtk::vtk_read_tag_data().
|
private |
Internal implementation of get_bytes
Definition at line 181 of file FileTokenizer.cpp.
References get_long_int_internal(), line_number(), and MB_SET_ERR_RET_VAL.
Referenced by get_bytes().
bool moab::FileTokenizer::get_bytes | ( | size_t | count, |
unsigned char * | array | ||
) |
Parse a sequence of integer values.
Read the specified number of space-delimited ints.
count | The number of values to read. |
array | The memory at which to store the values. |
Definition at line 251 of file FileTokenizer.cpp.
References get_byte_internal().
|
private |
Internal implementation of get_doubles
Definition at line 126 of file FileTokenizer.cpp.
References get_string(), line_number(), and MB_SET_ERR_RET_VAL.
Referenced by get_doubles(), and get_float_internal().
bool moab::FileTokenizer::get_doubles | ( | size_t | count, |
double * | array | ||
) |
Parse a sequence of double values.
Read the specified number of space-delimited doubles.
count | The number of values to read. |
array | The memory at which to store the values. |
Definition at line 240 of file FileTokenizer.cpp.
References get_double_internal().
Referenced by moab::ReadGmsh::load_file(), moab::ReadVtk::read_vertices(), moab::ReadVtk::vtk_read_field(), moab::ReadVtk::vtk_read_rectilinear_grid(), moab::ReadVtk::vtk_read_structured_points(), and moab::ReadVtk::vtk_read_tag_data().
|
private |
Internal implementation of get_floats
Definition at line 152 of file FileTokenizer.cpp.
References get_double_internal().
Referenced by get_floats().
bool moab::FileTokenizer::get_floats | ( | size_t | count, |
float * | array | ||
) |
Parse a sequence of float values.
Read the specified number of space-delimited doubles.
count | The number of values to read. |
array | The memory at which to store the values. |
Definition at line 229 of file FileTokenizer.cpp.
References get_float_internal().
Referenced by moab::ReadSTL::ascii_read_triangles().
|
private |
Internal implementation of get_integers
Definition at line 203 of file FileTokenizer.cpp.
References get_long_int_internal(), line_number(), and MB_SET_ERR_RET_VAL.
Referenced by get_integers().
bool moab::FileTokenizer::get_integers | ( | size_t | count, |
int * | array | ||
) |
Parse a sequence of integer values.
Read the specified number of space-delimited ints.
count | The number of values to read. |
array | The memory at which to store the values. |
Definition at line 273 of file FileTokenizer.cpp.
References get_integer_internal().
Referenced by moab::ReadGmsh::load_file(), moab::ReadVtk::vtk_read_tag_data(), and moab::ReadVtk::vtk_read_texture_attrib().
|
private |
Internal implementation of get_long_ints
Definition at line 162 of file FileTokenizer.cpp.
References get_string(), line_number(), and MB_SET_ERR_RET_VAL.
Referenced by get_byte_internal(), get_integer_internal(), get_long_ints(), and get_short_int_internal().
bool moab::FileTokenizer::get_long_ints | ( | size_t | count, |
long * | array | ||
) |
Parse a sequence of integer values.
Read the specified number of space-delimited ints.
count | The number of values to read. |
array | The memory at which to store the values. |
Definition at line 284 of file FileTokenizer.cpp.
References get_long_int_internal().
Referenced by moab::ReadGmsh::load_file(), moab::ReadVtk::load_file(), moab::ReadVtk::vtk_read_color_attrib(), moab::ReadVtk::vtk_read_field(), moab::ReadVtk::vtk_read_field_attrib(), moab::ReadVtk::vtk_read_polydata(), moab::ReadVtk::vtk_read_polygons(), moab::ReadVtk::vtk_read_rectilinear_grid(), moab::ReadVtk::vtk_read_structured_grid(), moab::ReadVtk::vtk_read_structured_points(), and moab::ReadVtk::vtk_read_unstructured_grid().
bool moab::FileTokenizer::get_newline | ( | bool | report_error = true | ) |
check for newline
Consume whitespace up to and including the next newline. If a non-space character is found before a newline, the function will stop, set the error message, and return false.
Definition at line 372 of file FileTokenizer.cpp.
References buffer, bufferEnd, eof(), filePtr, lastChar, line_number(), lineNumber, MB_SET_ERR_RET_VAL, and nextToken.
Referenced by moab::ReadGmsh::load_file(), moab::ReadVtk::vtk_read_polydata(), moab::ReadVtk::vtk_read_polygons(), moab::ReadVtk::vtk_read_rectilinear_grid(), moab::ReadVtk::vtk_read_structured_grid(), moab::ReadVtk::vtk_read_structured_points(), and moab::ReadVtk::vtk_read_unstructured_grid().
|
private |
Internal implementation of get_short_ints
Definition at line 192 of file FileTokenizer.cpp.
References get_long_int_internal(), line_number(), and MB_SET_ERR_RET_VAL.
Referenced by get_short_ints().
bool moab::FileTokenizer::get_short_ints | ( | size_t | count, |
short * | array | ||
) |
Parse a sequence of integer values.
Read the specified number of space-delimited ints.
count | The number of values to read. |
array | The memory at which to store the values. |
Definition at line 262 of file FileTokenizer.cpp.
References get_short_int_internal().
const char * moab::FileTokenizer::get_string | ( | ) |
get next token
Get the next whitespace-delimited token from the file. NOTE: The returned string is only valid until the next call to any of the functions in this class that read from the file.
Definition at line 45 of file FileTokenizer.cpp.
References buffer, bufferEnd, filePtr, lastChar, lineNumber, MB_SET_ERR_RET_VAL, and nextToken.
Referenced by get_boolean_internal(), get_double_internal(), get_long_int_internal(), moab::ReadGmsh::load_file(), match_token(), moab::ReadVtk::vtk_read_attrib_data(), moab::ReadVtk::vtk_read_field(), moab::ReadVtk::vtk_read_field_attrib(), and moab::ReadVtk::vtk_read_scalar_attrib().
|
inline |
Get the line number the last token was read from.
Definition at line 175 of file FileTokenizer.hpp.
References lineNumber.
Referenced by get_boolean_internal(), get_byte_internal(), get_double_internal(), get_integer_internal(), get_long_int_internal(), get_newline(), get_short_int_internal(), moab::ReadGmsh::load_file(), moab::ReadVtk::load_file(), match_token(), moab::ReadVtk::vtk_read_field_attrib(), moab::ReadVtk::vtk_read_polydata(), moab::ReadVtk::vtk_read_rectilinear_grid(), moab::ReadVtk::vtk_read_scalar_attrib(), moab::ReadVtk::vtk_read_structured_grid(), moab::ReadVtk::vtk_read_structured_points(), moab::ReadVtk::vtk_read_tag_data(), moab::ReadVtk::vtk_read_texture_attrib(), and moab::ReadVtk::vtk_read_unstructured_grid().
int moab::FileTokenizer::match_token | ( | const char *const * | string_list, |
bool | print_error = true |
||
) |
Match the current token to one of an array of strings. Sets the error message if the current token doesn't match any of the input strings.
string_list | A NULL-terminated array of strings. |
Definition at line 338 of file FileTokenizer.cpp.
References get_string(), line_number(), MB_SET_ERR_CONT, and print_error().
bool moab::FileTokenizer::match_token | ( | const char * | string, |
bool | print_error = true |
||
) |
Match current token to passed string. If token doesn't match, set error message.
Definition at line 321 of file FileTokenizer.cpp.
References get_string(), line_number(), MB_SET_ERR_CONT, and print_error().
Referenced by moab::ReadSTL::ascii_read_triangles(), moab::ReadGmsh::load_file(), moab::ReadVtk::load_file(), moab::ReadVtk::vtk_read_attrib_data(), moab::ReadVtk::vtk_read_dataset(), moab::ReadVtk::vtk_read_field(), moab::ReadVtk::vtk_read_field_attrib(), moab::ReadVtk::vtk_read_polydata(), moab::ReadVtk::vtk_read_rectilinear_grid(), moab::ReadVtk::vtk_read_scalar_attrib(), moab::ReadVtk::vtk_read_structured_grid(), moab::ReadVtk::vtk_read_structured_points(), moab::ReadVtk::vtk_read_tensor_attrib(), moab::ReadVtk::vtk_read_texture_attrib(), moab::ReadVtk::vtk_read_unstructured_grid(), and moab::ReadVtk::vtk_read_vector_attrib().
void moab::FileTokenizer::unget_token | ( | ) |
Put current token back in buffer. Can only unget one token.
Definition at line 306 of file FileTokenizer.cpp.
References buffer, lastChar, and nextToken.
Referenced by moab::ReadVtk::load_file(), and moab::ReadVtk::vtk_read_scalar_attrib().
|
private |
Input buffer
Definition at line 223 of file FileTokenizer.hpp.
Referenced by get_newline(), get_string(), and unget_token().
|
private |
One past the last used byte of the buffer
Definition at line 228 of file FileTokenizer.hpp.
Referenced by eof(), get_binary(), get_newline(), and get_string().
|
private |
Pointer to standard C FILE struct
Definition at line 220 of file FileTokenizer.hpp.
Referenced by eof(), get_binary(), get_newline(), get_string(), and ~FileTokenizer().
|
private |
The whitespace character marking the end of the last returned token. Saved here because if it is a newline, the line count will need to be incremented when the next token is returned.
Definition at line 238 of file FileTokenizer.hpp.
Referenced by get_newline(), get_string(), and unget_token().
|
private |
Line number of last returned token
Definition at line 231 of file FileTokenizer.hpp.
Referenced by get_newline(), get_string(), and line_number().
|
private |
One past the end of the last token returned
Definition at line 226 of file FileTokenizer.hpp.
Referenced by eof(), get_binary(), get_newline(), get_string(), and unget_token().