Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
FileTokenizer.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 #ifndef FILE_TOKENIZER_HPP
00017 #define FILE_TOKENIZER_HPP
00018 
00019 #include "moab/Types.hpp"
00020 #include <cstdio>
00021 #include <sys/types.h>
00022 
00023 namespace moab
00024 {
00025 
00026 class ReadUtilIface;
00027 
00028 /**
00029  * \brief  Parse a file as space-separated tokens
00030  * \author Jason Kraftcheck
00031  * \date   30 Sept 2004
00032  *
00033  * Read a file, separating it into space-separated tokens.
00034  * This is provided in place of using the standard C or C++
00035  * file parsing routines because it counts lines, which is
00036  * useful for error reporting.  Also provides some useful
00037  * utility methods for parsing VTK files (which is the
00038  * intended use of this implementation.)
00039  *
00040  * Uses raw reads/writes, implementing internal buffering.
00041  * Token size may not exceed buffer size.
00042  */
00043 
00044 class FileTokenizer
00045 {
00046   public:
00047     /** \brief constructor
00048      *
00049      * \param file_ptr The file to read from.
00050      * \param read_util_ptr Pointer to ReadUtilIface to use for
00051      *                      reporting errors.
00052      */
00053     FileTokenizer( std::FILE* file_ptr, ReadUtilIface* read_util_ptr );
00054 
00055     /** \brief destructor : closes file.
00056      *
00057      * The destructor closes the passed file handle.   This
00058      * is done as a convenience feature.  If the caller
00059      * creates an instance of this object on the stack, the
00060      * file will automatically be closed when the caller
00061      * returns.
00062      */
00063     ~FileTokenizer();
00064 
00065     /** \brief get next token
00066      *
00067      * Get the next whitespace-delimited token from the file.
00068      * NOTE: The returned string is only valid until the next
00069      *       call to any of the functions in this class that
00070      *       read from the file.
00071      *
00072      * \return A pointer to the buffer space containing the string,
00073      *         or NULL if an error occurred.
00074      */
00075     const char* get_string();
00076 
00077     /** \brief check for newline
00078      *
00079      * Consume whitespace up to and including the next newline.
00080      * If a non-space character is found before a newline,
00081      * the function will stop, set the error message, and
00082      * return false.
00083      *
00084      * \return True if a newline was found before any non-space
00085      *         character.  False otherwise.
00086      */
00087     bool get_newline( bool report_error = true );
00088 
00089     /** \brief Parse a sequence of double values.
00090      *
00091      * Read the specified number of space-delimited doubles.
00092      *
00093      * \param count   The number of values to read.
00094      * \param array   The memory at which to store the values.
00095      * \return true if successful, false otherwise.
00096      */
00097     bool get_doubles( size_t count, double* array );
00098 
00099     /** \brief Parse a sequence of float values.
00100      *
00101      * Read the specified number of space-delimited doubles.
00102      *
00103      * \param count   The number of values to read.
00104      * \param array   The memory at which to store the values.
00105      * \return true if successful, false otherwise.
00106      */
00107     bool get_floats( size_t count, float* array );
00108 
00109     /** \brief Parse a sequence of integer values.
00110      *
00111      * Read the specified number of space-delimited ints.
00112      *
00113      * \param count   The number of values to read.
00114      * \param array   The memory at which to store the values.
00115      * \return true if successful, false otherwise.
00116      */
00117     bool get_integers( size_t count, int* array );
00118 
00119     /** \brief Parse a sequence of integer values.
00120      *
00121      * Read the specified number of space-delimited ints.
00122      *
00123      * \param count   The number of values to read.
00124      * \param array   The memory at which to store the values.
00125      * \return true if successful, false otherwise.
00126      */
00127     bool get_long_ints( size_t count, long* array );
00128 
00129     /** \brief Parse a sequence of integer values.
00130      *
00131      * Read the specified number of space-delimited ints.
00132      *
00133      * \param count   The number of values to read.
00134      * \param array   The memory at which to store the values.
00135      * \return true if successful, false otherwise.
00136      */
00137     bool get_short_ints( size_t count, short* array );
00138 
00139     /** \brief Parse a sequence of integer values.
00140      *
00141      * Read the specified number of space-delimited ints.
00142      *
00143      * \param count   The number of values to read.
00144      * \param array   The memory at which to store the values.
00145      * \return true if successful, false otherwise.
00146      */
00147     bool get_bytes( size_t count, unsigned char* array );
00148 
00149     /** \brief Read binary data (interleaved with ASCII)
00150      *
00151      * Read a block of binary data.
00152      *\param bytes  Number of bytes to read
00153      *\param mem    Memory address at which to store data.
00154      */
00155     bool get_binary( size_t bytes, void* mem );
00156 
00157     /** \brief Parse a sequence of bit or boolean values.
00158      *
00159      * Read the specified number of space-delimited values.
00160      *
00161      * \param count   The number of values to read.
00162      * \param array   The memory at which to store the values.
00163      * \return true if successful, false otherwise.
00164      */
00165     bool get_booleans( size_t count, bool* array );
00166 
00167     /**
00168      * Check for end-of-file condition.
00169      */
00170     bool eof() const;
00171 
00172     /**
00173      * Get the line number the last token was read from.
00174      */
00175     int line_number() const
00176     {
00177         return lineNumber;
00178     }
00179 
00180     /**
00181      * Put current token back in buffer.  Can only unget one token.
00182      */
00183     void unget_token();
00184 
00185     /**
00186      * Match current token to passed string.  If token
00187      * doesn't match, set error message.
00188      */
00189     bool match_token( const char* string, bool print_error = true );
00190 
00191     /**
00192      * Match the current token to one of an array of strings.
00193      * Sets the error message if the current token doesn't
00194      * match any of the input strings.
00195      *
00196      * \param  string_list A NULL-terminated array of strings.
00197      * \return One greater than the index of the matched
00198      *         string, or zero if no match.
00199      */
00200     int match_token( const char* const* string_list, bool print_error = true );
00201 
00202   private:
00203     /** Internal implementation of \ref get_doubles */
00204     bool get_double_internal( double& result );
00205     /** Internal implementation of \ref get_long_ints */
00206     bool get_long_int_internal( long& result );
00207     /** Internal implementation of \ref get_Booleans */
00208     bool get_boolean_internal( bool& result );
00209 
00210     /** Internal implementation of \ref get_floats */
00211     bool get_float_internal( float& result );
00212     /** Internal implementation of \ref get_integers */
00213     bool get_integer_internal( int& result );
00214     /** Internal implementation of \ref get_short_ints */
00215     bool get_short_int_internal( short& result );
00216     /** Internal implementation of \ref get_bytes */
00217     bool get_byte_internal( unsigned char& result );
00218 
00219     /** Pointer to standard C FILE struct */
00220     std::FILE* filePtr;
00221 
00222     /** Input buffer */
00223     char buffer[512];
00224 
00225     /** One past the end of the last token returned */
00226     char* nextToken;
00227     /** One past the last used byte of the buffer */
00228     char* bufferEnd;
00229 
00230     /** Line number of last returned token */
00231     int lineNumber;
00232 
00233     /** The whitespace character marking the end of the
00234      *  last returned token.  Saved here because if it
00235      *  is a newline, the line count will need to be
00236      *  incremented when the next token is returned.
00237      */
00238     char lastChar;
00239 };
00240 
00241 }  // namespace moab
00242 
00243 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines