Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileTokenizer Class Reference

Parse a file as space-separated tokens. More...

#include <FileTokenizer.hpp>

Public Member Functions

 FileTokenizer (msq_stdc::FILE *file_ptr)
 constructor More...
 
 ~FileTokenizer ()
 destructor : closes file. More...
 
const char * get_string (MsqError &err)
 get next token More...
 
bool get_newline (MsqError &err)
 check for newline More...
 
bool get_doubles (size_t count, double *array, MsqError &err)
 Parse a sequence of double values. More...
 
bool get_floats (size_t count, float *array, MsqError &err)
 Parse a sequence of float values. More...
 
bool get_integers (size_t count, int *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_long_ints (size_t count, long *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_short_ints (size_t count, short *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_bytes (size_t count, unsigned char *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_booleans (size_t count, bool *array, MsqError &err)
 Parse a sequence of bit or boolean values. More...
 
bool eof () const
 Check for end-of-file condition. More...
 
int line_number () const
 Get the line number the last token was read from. More...
 
void unget_token ()
 Put current token back in buffer. More...
 
bool match_token (const char *string, MsqError &err)
 Match current token to passed string. More...
 
int match_token (const char *const *string_list, MsqError &err)
 Match the current token to one of an array of strings. More...
 
 FileTokenizer (msq_stdc::FILE *file_ptr)
 constructor More...
 
 ~FileTokenizer ()
 destructor : closes file. More...
 
const char * get_string (MsqError &err)
 get next token More...
 
bool get_newline (MsqError &err)
 check for newline More...
 
bool get_doubles (size_t count, double *array, MsqError &err)
 Parse a sequence of double values. More...
 
bool get_floats (size_t count, float *array, MsqError &err)
 Parse a sequence of float values. More...
 
bool get_integers (size_t count, int *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_long_ints (size_t count, long *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_short_ints (size_t count, short *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_bytes (size_t count, unsigned char *array, MsqError &err)
 Parse a sequence of integer values. More...
 
bool get_booleans (size_t count, bool *array, MsqError &err)
 Parse a sequence of bit or boolean values. More...
 
bool eof () const
 Check for end-of-file condition. More...
 
int line_number () const
 Get the line number the last token was read from. More...
 
void unget_token ()
 Put current token back in buffer. More...
 
bool match_token (const char *string, MsqError &err)
 Match current token to passed string. More...
 
int match_token (const char *const *string_list, MsqError &err)
 Match the current token to one of an array of strings. More...
 

Private Member Functions

bool get_double_internal (double &result, MsqError &err)
 Internal implementation of get_doubles. More...
 
bool get_long_int_internal (long &result, MsqError &err)
 Internal implementation of get_long_ints. More...
 
bool get_boolean_internal (bool &result, MsqError &err)
 Internal implementation of get_Booleans. More...
 
bool get_float_internal (float &result, MsqError &err)
 Internal implementation of get_floats. More...
 
bool get_integer_internal (int &result, MsqError &err)
 Internal implementation of get_integers. More...
 
bool get_short_int_internal (short &result, MsqError &err)
 Internal implementation of get_short_ints. More...
 
bool get_byte_internal (unsigned char &result, MsqError &err)
 Internal implementation of get_bytes. More...
 
bool get_double_internal (double &result, MsqError &err)
 Internal implementation of get_doubles. More...
 
bool get_long_int_internal (long &result, MsqError &err)
 Internal implementation of get_long_ints. More...
 
bool get_boolean_internal (bool &result, MsqError &err)
 Internal implementation of get_Booleans. More...
 
bool get_float_internal (float &result, MsqError &err)
 Internal implementation of get_floats. More...
 
bool get_integer_internal (int &result, MsqError &err)
 Internal implementation of get_integers. More...
 
bool get_short_int_internal (short &result, MsqError &err)
 Internal implementation of get_short_ints. More...
 
bool get_byte_internal (unsigned char &result, MsqError &err)
 Internal implementation of get_bytes. More...
 

Private Attributes

msq_stdc::FILE * filePtr
 Pointer to standard C FILE struct. More...
 
char buffer [512]
 Input buffer. More...
 
char * nextToken
 One past the end of the last token returned. More...
 
char * bufferEnd
 One past the last used byte of the buffer. More...
 
int lineNumber
 Line number of last returned token. More...
 
char lastChar
 The whitespace character marking the end of the last returned token. More...
 

Detailed Description

Parse a file as space-separated tokens.

Author
Jason Kraftcheck
Date
30 Sept 2004

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 60 of file includeLinks/FileTokenizer.hpp.

Constructor & Destructor Documentation

FileTokenizer ( msq_stdc::FILE *  file_ptr)

constructor

Parameters
file_ptrThe file to read from.

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 50 of file Misc/FileTokenizer.cpp.

References cimg_library::cimg::fclose().

51  { fclose( filePtr ); }
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
msq_stdc::FILE * filePtr
Pointer to standard C FILE struct.

Here is the call graph for this function:

FileTokenizer ( msq_stdc::FILE *  file_ptr)

constructor

Parameters
file_ptrThe file to read from.

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.

Member Function Documentation

bool eof ( ) const

Check for end-of-file condition.

Definition at line 53 of file Misc/FileTokenizer.cpp.

Referenced by MeshImpl::read_vtk().

54  { return nextToken == bufferEnd && feof(filePtr); }
char * bufferEnd
One past the last used byte of the buffer.
char * nextToken
One past the end of the last token returned.
msq_stdc::FILE * filePtr
Pointer to standard C FILE struct.

Here is the caller graph for this function:

bool eof ( ) const

Check for end-of-file condition.

bool get_boolean_internal ( bool &  result,
MsqError err 
)
private

Internal implementation of get_Booleans.

Definition at line 272 of file Misc/FileTokenizer.cpp.

References MSQ_CHKERR, and MSQ_SETERR.

273 {
274  // Get a token
275  const char *token = get_string( err );
276  if (MSQ_CHKERR(err))
277  return false;
278 
279  if (token[1] || (token[0] != '0' && token[0] != '1'))
280  {
282  "Syntax error at line %d: expected 0 or 1, got \"%s\"",
283  line_number(), token );
284  return false;
285  }
286 
287  result = token[0] == '1';
288  return true;
289 }
int line_number() const
Get the line number the last token was read from.
const char * get_string(MsqError &err)
get next token
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
Error parsing input (or input file)
bool get_boolean_internal ( bool &  result,
MsqError err 
)
private

Internal implementation of get_Booleans.

bool get_booleans ( size_t  count,
bool *  array,
MsqError err 
)

Parse a sequence of bit or boolean values.

Read the specified number of space-deliminated values.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.

Definition at line 363 of file Misc/FileTokenizer.cpp.

References i, and MSQ_CHKERR.

Referenced by MeshImpl::vtk_read_typed_data().

364 {
365  for (size_t i = 0; i < count; ++i)
366  {
367  get_boolean_internal( *array, err );
368  if (MSQ_CHKERR(err))
369  return false;
370  ++array;
371  }
372  return true;
373 }
bool get_boolean_internal(bool &result, MsqError &err)
Internal implementation of get_Booleans.
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
blockLoc i
Definition: read.cpp:79

Here is the caller graph for this function:

bool get_booleans ( size_t  count,
bool *  array,
MsqError err 
)

Parse a sequence of bit or boolean values.

Read the specified number of space-deliminated values.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.
bool get_byte_internal ( unsigned char &  result,
MsqError err 
)
private

Internal implementation of get_bytes.

Definition at line 221 of file Misc/FileTokenizer.cpp.

References i, MSQ_CHKERR, and MSQ_SETERR.

222 {
223  long i;
224  get_long_int_internal( i, err );
225  if (MSQ_CHKERR(err))
226  return false;
227 
228  result = (unsigned char)i;
229  if (i != (long)result)
230  {
231  MSQ_SETERR(err)( MsqError::PARSE_ERROR, "Numberic overflow at line %d.", line_number() );
232  return false;
233  }
234 
235  return true;
236 }
int line_number() const
Get the line number the last token was read from.
bool get_long_int_internal(long &result, MsqError &err)
Internal implementation of get_long_ints.
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
blockLoc i
Definition: read.cpp:79
Error parsing input (or input file)
bool get_byte_internal ( unsigned char &  result,
MsqError err 
)
private

Internal implementation of get_bytes.

bool get_bytes ( size_t  count,
unsigned char *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.

Definition at line 314 of file Misc/FileTokenizer.cpp.

References i, and MSQ_CHKERR.

315 {
316  for (size_t i = 0; i < count; ++i)
317  {
318  get_byte_internal( *array, err );
319  if (MSQ_CHKERR(err))
320  return false;
321  ++array;
322  }
323  return true;
324 }
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
blockLoc i
Definition: read.cpp:79
bool get_byte_internal(unsigned char &result, MsqError &err)
Internal implementation of get_bytes.
bool get_bytes ( size_t  count,
unsigned char *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.
bool get_double_internal ( double &  result,
MsqError err 
)
private

Internal implementation of get_doubles.

bool get_double_internal ( double &  result,
MsqError err 
)
private

Internal implementation of get_doubles.

Definition at line 144 of file Misc/FileTokenizer.cpp.

References MSQ_CHKERR, and MSQ_SETERR.

145 {
146  // Get a token
147  const char *token_end, *token = get_string( err );
148  if (MSQ_CHKERR(err))
149  return false;
150 
151  // Check for hex value -- on some platforms (e.g. Linux), strtod
152  // will accept hex values, on others (e.g. Sun) it wil not. Force
153  // failure on hex numbers for consistancy.
154  if (token[0] && token[1] && token[0] == '0' && toupper(token[1]) == 'X')
155  {
157  "Syntax error at line %d: expected number, got \"%s\"",
158  line_number(), token );
159  return false;
160  }
161 
162 
163  // Parse token as double
164  result = strtod( token, (char**)&token_end );
165 
166  // If the one past the last char read by strtod is
167  // not the NULL character terminating the string,
168  // then parse failed.
169  if (*token_end)
170  {
172  "Syntax error at line %d: expected number, got \"%s\"",
173  line_number(), token );
174  return false;
175  }
176 
177  return true;
178 }
int line_number() const
Get the line number the last token was read from.
const char * get_string(MsqError &err)
get next token
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
Error parsing input (or input file)
bool get_doubles ( size_t  count,
double *  array,
MsqError err 
)

Parse a sequence of double values.

Read the specified number of space-deliminated doubles.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.

Definition at line 302 of file Misc/FileTokenizer.cpp.

References i, and MSQ_CHKERR.

Referenced by MeshImpl::vtk_read_field(), MeshImpl::vtk_read_polydata(), MeshImpl::vtk_read_rectilinear_grid(), MeshImpl::vtk_read_scalar_attrib(), MeshImpl::vtk_read_structured_grid(), MeshImpl::vtk_read_structured_points(), MeshImpl::vtk_read_typed_data(), and MeshImpl::vtk_read_unstructured_grid().

303 {
304  for (size_t i = 0; i < count; ++i)
305  {
306  get_double_internal( *array, err );
307  if (MSQ_CHKERR(err))
308  return false;
309  ++array;
310  }
311  return true;
312 }
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
blockLoc i
Definition: read.cpp:79
bool get_double_internal(double &result, MsqError &err)
Internal implementation of get_doubles.

Here is the caller graph for this function:

bool get_doubles ( size_t  count,
double *  array,
MsqError err 
)

Parse a sequence of double values.

Read the specified number of space-deliminated doubles.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.
bool get_float_internal ( float &  result,
MsqError err 
)
private

Internal implementation of get_floats.

Definition at line 180 of file Misc/FileTokenizer.cpp.

References d, MSQ_CHKERR, and MSQ_SETERR.

181 {
182  double d;
183  get_double_internal( d, err );
184  if (MSQ_CHKERR(err))
185  return false;
186 
187  result = (float)d;
188  if (d != (double)result)
189  {
190  MSQ_SETERR(err)( MsqError::PARSE_ERROR, "Numberic overflow at line %d.", line_number() );
191  return false;
192  }
193 
194  return true;
195 }
int line_number() const
Get the line number the last token was read from.
const NT & d
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
bool get_double_internal(double &result, MsqError &err)
Internal implementation of get_doubles.
Error parsing input (or input file)
bool get_float_internal ( float &  result,
MsqError err 
)
private

Internal implementation of get_floats.

bool get_floats ( size_t  count,
float *  array,
MsqError err 
)

Parse a sequence of float values.

Read the specified number of space-deliminated doubles.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.

Definition at line 291 of file Misc/FileTokenizer.cpp.

References i.

Referenced by MeshImpl::vtk_read_color_attrib(), and MeshImpl::vtk_read_scalar_attrib().

292 {
293  for (size_t i = 0; i < count; ++i)
294  {
295  if (!get_float_internal( *array, err ))
296  return false;
297  ++array;
298  }
299  return true;
300 }
bool get_float_internal(float &result, MsqError &err)
Internal implementation of get_floats.
blockLoc i
Definition: read.cpp:79

Here is the caller graph for this function:

bool get_floats ( size_t  count,
float *  array,
MsqError err 
)

Parse a sequence of float values.

Read the specified number of space-deliminated doubles.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.
bool get_integer_internal ( int &  result,
MsqError err 
)
private

Internal implementation of get_integers.

bool get_integer_internal ( int &  result,
MsqError err 
)
private

Internal implementation of get_integers.

Definition at line 255 of file Misc/FileTokenizer.cpp.

References i, MSQ_CHKERR, and MSQ_SETERR.

256 {
257  long i;
258  get_long_int_internal( i, err );
259  if (MSQ_CHKERR(err))
260  return false;
261 
262  result = (int)i;
263  if (i != (long)result)
264  {
265  MSQ_SETERR(err)( MsqError::PARSE_ERROR, "Numberic overflow at line %d.", line_number() );
266  return false;
267  }
268 
269  return true;
270 }
int line_number() const
Get the line number the last token was read from.
bool get_long_int_internal(long &result, MsqError &err)
Internal implementation of get_long_ints.
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
blockLoc i
Definition: read.cpp:79
Error parsing input (or input file)
bool get_integers ( size_t  count,
int *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.
bool get_integers ( size_t  count,
int *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.

Definition at line 339 of file Misc/FileTokenizer.cpp.

References i, and MSQ_CHKERR.

Referenced by MeshImpl::vtk_read_texture_attrib(), and MeshImpl::vtk_read_typed_data().

340 {
341  for (size_t i = 0; i < count; ++i)
342  {
343  get_integer_internal( *array, err );
344  if (MSQ_CHKERR(err))
345  return false;
346  ++array;
347  }
348  return true;
349 }
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
blockLoc i
Definition: read.cpp:79
bool get_integer_internal(int &result, MsqError &err)
Internal implementation of get_integers.

Here is the caller graph for this function:

bool get_long_int_internal ( long &  result,
MsqError err 
)
private

Internal implementation of get_long_ints.

Definition at line 197 of file Misc/FileTokenizer.cpp.

References MSQ_CHKERR, and MSQ_SETERR.

198 {
199  // Get a token
200  const char *token_end, *token = get_string( err );
201  if (MSQ_CHKERR(err))
202  return false;
203 
204  // Parse token as long
205  result = strtol( token, (char**)&token_end, 0 );
206 
207  // If the one past the last char read by strtol is
208  // not the NULL character terminating the string,
209  // then parse failed.
210  if (*token_end)
211  {
213  "Syntax error at line %d: expected integer, got \"%s\"",
214  line_number(), token );
215  return false;
216  }
217 
218  return true;
219 }
int line_number() const
Get the line number the last token was read from.
const char * get_string(MsqError &err)
get next token
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
Error parsing input (or input file)
bool get_long_int_internal ( long &  result,
MsqError err 
)
private

Internal implementation of get_long_ints.

bool get_long_ints ( size_t  count,
long *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.
bool get_long_ints ( size_t  count,
long *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.

Definition at line 351 of file Misc/FileTokenizer.cpp.

References i, and MSQ_CHKERR.

Referenced by MeshImpl::read_vtk(), MeshImpl::vtk_read_color_attrib(), MeshImpl::vtk_read_field(), MeshImpl::vtk_read_polydata(), MeshImpl::vtk_read_polygons(), MeshImpl::vtk_read_rectilinear_grid(), MeshImpl::vtk_read_scalar_attrib(), MeshImpl::vtk_read_structured_grid(), MeshImpl::vtk_read_structured_points(), and MeshImpl::vtk_read_unstructured_grid().

352 {
353  for (size_t i = 0; i < count; ++i)
354  {
355  get_long_int_internal( *array, err );
356  if (MSQ_CHKERR(err))
357  return false;
358  ++array;
359  }
360  return true;
361 }
bool get_long_int_internal(long &result, MsqError &err)
Internal implementation of get_long_ints.
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
blockLoc i
Definition: read.cpp:79

Here is the caller graph for this function:

bool get_newline ( MsqError err)

check for newline

Consume whitespace upto 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.

Returns
True if a newline was found before any non-space character. False otherwise.
bool get_newline ( MsqError err)

check for newline

Consume whitespace upto 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.

Returns
True if a newline was found before any non-space character. False otherwise.

Definition at line 442 of file Misc/FileTokenizer.cpp.

References cimg_library::cimg::fread(), and MSQ_SETERR.

Referenced by MeshImpl::vtk_read_polydata(), MeshImpl::vtk_read_polygons(), MeshImpl::vtk_read_rectilinear_grid(), MeshImpl::vtk_read_structured_grid(), MeshImpl::vtk_read_structured_points(), and MeshImpl::vtk_read_unstructured_grid().

443 {
444  if (lastChar == '\n')
445  {
446  lastChar = ' ';
447  ++lineNumber;
448  return true;
449  }
450 
451  // Loop until either we a) find a newline, b) find a non-whitespace
452  // character or c) reach the end of the file.
453  for (;;)
454  {
455  // If the buffer is empty, read more.
456  if (nextToken == bufferEnd)
457  {
458  size_t count = fread( buffer, 1, sizeof(buffer), filePtr );
459  if (!count)
460  {
461  if (eof())
462  MSQ_SETERR(err)( "File truncated.", MsqError::PARSE_ERROR );
463  else
465  return false;
466  }
467 
468  nextToken = buffer;
469  bufferEnd = buffer + count;
470  }
471 
472  // If the current character is not a space, the we've failed.
473  if (!isspace(*nextToken))
474  {
475  MSQ_SETERR(err)( MsqError::PARSE_ERROR, "Expected newline at line %d.", line_number() );
476  return false;
477  }
478 
479  // If the current space character is a newline,
480  // increment the line number count.
481  if (*nextToken == '\n')
482  {
483  ++lineNumber;
484  ++nextToken;
485  lastChar = ' ';
486  return true;
487  }
488  ++nextToken;
489  }
490 
491  // should never reach this
492  return false;
493 }
An I/O error occured (e.g.
int line_number() const
Get the line number the last token was read from.
int fread(T *const ptr, const unsigned int nmemb, std::FILE *stream)
Read file data, and check for possible errors.
Definition: CImg.h:5569
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
char * bufferEnd
One past the last used byte of the buffer.
char * nextToken
One past the end of the last token returned.
msq_stdc::FILE * filePtr
Pointer to standard C FILE struct.
Error parsing input (or input file)
int lineNumber
Line number of last returned token.
char lastChar
The whitespace character marking the end of the last returned token.
bool eof() const
Check for end-of-file condition.

Here is the call graph for this function:

Here is the caller graph for this function:

bool get_short_int_internal ( short &  result,
MsqError err 
)
private

Internal implementation of get_short_ints.

Definition at line 238 of file Misc/FileTokenizer.cpp.

References i, MSQ_CHKERR, and MSQ_SETERR.

239 {
240  long i;
241  get_long_int_internal( i, err );
242  if (MSQ_CHKERR(err))
243  return false;
244 
245  result = (short)i;
246  if (i != (long)result)
247  {
248  MSQ_SETERR(err)( MsqError::PARSE_ERROR, "Numberic overflow at line %d.", line_number() );
249  return false;
250  }
251 
252  return true;
253 }
int line_number() const
Get the line number the last token was read from.
bool get_long_int_internal(long &result, MsqError &err)
Internal implementation of get_long_ints.
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
blockLoc i
Definition: read.cpp:79
Error parsing input (or input file)
bool get_short_int_internal ( short &  result,
MsqError err 
)
private

Internal implementation of get_short_ints.

bool get_short_ints ( size_t  count,
short *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.
bool get_short_ints ( size_t  count,
short *  array,
MsqError err 
)

Parse a sequence of integer values.

Read the specified number of space-deliminated ints.

Parameters
countThe number of values to read.
arrayThe memory at which to store the values.
Returns
true if successful, false otherwise.

Definition at line 326 of file Misc/FileTokenizer.cpp.

References i, and MSQ_CHKERR.

327 {
328  for (size_t i = 0; i < count; ++i)
329  {
330  get_short_int_internal( *array, err );
331  if (MSQ_CHKERR(err))
332  return false;
333  ++array;
334  }
335  return true;
336 }
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
blockLoc i
Definition: read.cpp:79
bool get_short_int_internal(short &result, MsqError &err)
Internal implementation of get_short_ints.
const char * get_string ( MsqError err)

get next token

Get the next whitesapce-deliminated 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.

Returns
A pointer to the buffer space containing the string, or NULL if an error occured.

Definition at line 56 of file Misc/FileTokenizer.cpp.

References cimg_library::cimg::fread(), and MSQ_SETERR.

Referenced by MeshImpl::vtk_read_attrib_data(), MeshImpl::vtk_read_field(), and MeshImpl::vtk_read_scalar_attrib().

57 {
58  // If the whitepsace character marking the end of the
59  // last token was a newline, increment the line count.
60  if (lastChar == '\n')
61  ++lineNumber;
62 
63  // Loop until either found the start of a token to return or have
64  // reached the end of the file.
65  for (;;)
66  {
67  // If the buffer is empty, read more.
68  if (nextToken == bufferEnd)
69  {
70  size_t count = fread( buffer, 1, sizeof(buffer) - 1, filePtr );
71  if (!count)
72  {
73  if (feof(filePtr))
74  MSQ_SETERR(err)( "File truncated.\n", MsqError::PARSE_ERROR );
75  else
77  return NULL;
78  }
79 
80  nextToken = buffer;
81  bufferEnd = buffer + count;
82  }
83 
84  // If the current character is not a space, we've found a token.
85  if (!isspace(*nextToken))
86  break;
87 
88  // If the current space character is a newline,
89  // increment the line number count.
90  if (*nextToken == '\n')
91  ++lineNumber;
92  ++nextToken;
93  }
94 
95  // Store the start of the token in "result" and
96  // advance "nextToken" to one past the end of the
97  // token.
98  char* result = nextToken;
99  while (nextToken != bufferEnd && !isspace(*nextToken))
100  ++nextToken;
101 
102  // If we have reached the end of the buffer without finding
103  // a whitespace character terminating the token, we need to
104  // read more from the file. Only try once. If the token is
105  // too large to fit in the buffer, give up.
106  if (nextToken == bufferEnd)
107  {
108  // Shift the (possibly) partial token to the start of the buffer.
109  size_t remaining = bufferEnd - result;
110  memmove( buffer, result, remaining );
111  result = buffer;
112  nextToken = result + remaining;
113 
114  // Fill the remainder of the buffer after the token.
115  size_t count = fread( nextToken, 1, sizeof(buffer) - remaining - 1, filePtr );
116  if (!count && !feof(filePtr))
117  {
118  MSQ_SETERR(err)( "I/O error.\n", MsqError::IO_ERROR );
119  return NULL;
120  }
121  bufferEnd = nextToken + count;
122 
123  // Continue to advance nextToken until we find the space
124  // terminating the token.
125  while (nextToken != bufferEnd && !isspace(*nextToken))
126  ++nextToken;
127 
128  if (nextToken == bufferEnd) // EOF
129  {
130  *bufferEnd = '\0';
131  ++bufferEnd;
132  }
133  }
134 
135  // Save terminating whitespace character (or NULL char if EOF).
136  lastChar = *nextToken;
137  // Put null in buffer to mark end of current token.
138  *nextToken = '\0';
139  // Advance nextToken to the next character to search next time.
140  ++nextToken;
141  return result;
142 }
An I/O error occured (e.g.
int fread(T *const ptr, const unsigned int nmemb, std::FILE *stream)
Read file data, and check for possible errors.
Definition: CImg.h:5569
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
char * bufferEnd
One past the last used byte of the buffer.
char * nextToken
One past the end of the last token returned.
msq_stdc::FILE * filePtr
Pointer to standard C FILE struct.
Error parsing input (or input file)
int lineNumber
Line number of last returned token.
char lastChar
The whitespace character marking the end of the last returned token.

Here is the call graph for this function:

Here is the caller graph for this function:

const char* get_string ( MsqError err)

get next token

Get the next whitesapce-deliminated 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.

Returns
A pointer to the buffer space containing the string, or NULL if an error occured.
int line_number ( ) const
inline

Get the line number the last token was read from.

Definition at line 184 of file src/Misc/FileTokenizer.hpp.

References FileTokenizer::lineNumber.

184 { return lineNumber; }
int lineNumber
Line number of last returned token.
bool match_token ( const char *  string,
MsqError err 
)

Match current token to passed string.

If token doesn't match, set error message.

bool match_token ( const char *  string,
MsqError err 
)

Match current token to passed string.

If token doesn't match, set error message.

Definition at line 392 of file Misc/FileTokenizer.cpp.

References MSQ_CHKERR, and MSQ_SETERR.

Referenced by MeshImpl::read_vtk(), MeshImpl::vtk_read_attrib_data(), MeshImpl::vtk_read_dataset(), MeshImpl::vtk_read_field(), MeshImpl::vtk_read_polydata(), MeshImpl::vtk_read_rectilinear_grid(), MeshImpl::vtk_read_scalar_attrib(), MeshImpl::vtk_read_structured_grid(), MeshImpl::vtk_read_structured_points(), MeshImpl::vtk_read_tensor_attrib(), MeshImpl::vtk_read_texture_attrib(), MeshImpl::vtk_read_unstructured_grid(), and MeshImpl::vtk_read_vector_attrib().

393 {
394  // Get a token
395  const char *token = get_string( err );
396  if (MSQ_CHKERR(err))
397  return false;
398 
399  // Check if it matches
400  if (0 == strcmp( token, str ))
401  return true;
402 
403  // Construct error message
405  "Syntax error at line %d: expected \"%s\", got \"%s\"",
406  line_number(), str, token );
407  return false;
408 } // namespace Mesquite
int line_number() const
Get the line number the last token was read from.
const char * get_string(MsqError &err)
get next token
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
Error parsing input (or input file)

Here is the caller graph for this function:

int match_token ( const char *const *  string_list,
MsqError err 
)

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.

Parameters
string_listA NULL-terminated array of strings.
Returns
One greater than the index of the matched string, or zero if no match.
int match_token ( const char *const *  string_list,
MsqError err 
)

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.

Parameters
string_listA NULL-terminated array of strings.
Returns
One greater than the index of the matched string, or zero if no match.

Definition at line 411 of file Misc/FileTokenizer.cpp.

References MSQ_CHKERR, and MSQ_SETERR.

412 {
413  // Get a token
414  const char *token = get_string( err );
415  if (MSQ_CHKERR(err))
416  return false;
417 
418  // Check if it matches any input string
419  const char* const* ptr;
420  for (ptr = list; *ptr; ++ptr)
421  if (0 == strcmp( token, *ptr ))
422  return ptr - list + 1;
423 
424  // No match, constuct error message
425  msq_std::string message( "Parsing error at line " );
426  char lineno[16];
427  sprintf( lineno, "%d", line_number() );
428  message += lineno;
429  message += ": expected one of {";
430  for (ptr = list; *ptr; ++ptr)
431  {
432  message += " ";
433  message += *ptr;
434  }
435  message += " } got \"";
436  message += token;
437  message += "\"";
438  MSQ_SETERR(err)( message, MsqError::PARSE_ERROR );
439  return false;
440 }
int line_number() const
Get the line number the last token was read from.
const char * get_string(MsqError &err)
get next token
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
Error parsing input (or input file)
void unget_token ( )

Put current token back in buffer.

Can only unget one token.

void unget_token ( )

Put current token back in buffer.

Can only unget one token.

Definition at line 375 of file Misc/FileTokenizer.cpp.

Referenced by MeshImpl::read_vtk(), and MeshImpl::vtk_read_scalar_attrib().

376 {
377  if (nextToken - buffer < 2)
378  return;
379 
380  --nextToken;
381  *nextToken = lastChar;
382  --nextToken;
383  while (nextToken > buffer && *nextToken)
384  --nextToken;
385 
386  if (!*nextToken)
387  ++nextToken;
388 
389  lastChar = '\0';
390 }
char * nextToken
One past the end of the last token returned.
char lastChar
The whitespace character marking the end of the last returned token.

Here is the caller graph for this function:

Member Data Documentation

char buffer
private

Input buffer.

Definition at line 230 of file includeLinks/FileTokenizer.hpp.

char * bufferEnd
private

One past the last used byte of the buffer.

Definition at line 235 of file includeLinks/FileTokenizer.hpp.

msq_stdc::FILE * filePtr
private

Pointer to standard C FILE struct.

Definition at line 227 of file includeLinks/FileTokenizer.hpp.

char lastChar
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 245 of file includeLinks/FileTokenizer.hpp.

int lineNumber
private

Line number of last returned token.

Definition at line 238 of file includeLinks/FileTokenizer.hpp.

Referenced by FileTokenizer::line_number().

char * nextToken
private

One past the end of the last token returned.

Definition at line 233 of file includeLinks/FileTokenizer.hpp.


The documentation for this class was generated from the following files: