MOAB: Mesh Oriented datABase  (version 5.4.1)
FileTokenizer.hpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Lawrence Livermore National Laboratory.  Under
00005     the terms of Contract B545069 with the University of Wisconsin --
00006     Madison, Lawrence Livermore National Laboratory retains certain
00007     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     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     [email protected]
00024 
00025   ***************************************************************** */
00026 
00027 #ifndef FILE_TOKENIZER_HPP
00028 #define FILE_TOKENIZER_HPP
00029 
00030 #include "Mesquite.hpp"
00031 #include <cstdio>
00032 #include <sys/types.h>
00033 
00034 namespace MBMesquite
00035 {
00036 
00037 class MsqError;
00038 
00039 /**
00040  * \class  FileTokenizer
00041  * \brief  Parse a file as space-separated tokens
00042  * \author Jason Kraftcheck
00043  * \date   30 Sept 2004
00044  *
00045  * Read a file, separating it into space-separated tokens.
00046  * This is provided in place of using the standard C or C++
00047  * file parsing routines because it counts lines, which is
00048  * useful for error reporting.  Also provides some useful
00049  * utility methods for parsing VTK files (which is the
00050  * intended use of this implementation.)
00051  *
00052  * Uses raw reads/writes, implementing internal buffering.
00053  * Token size may not exceed buffer size.
00054  */
00055 
00056 class FileTokenizer
00057 {
00058   public:
00059     /** \brief constructor
00060      *
00061      * \param file_ptr The file to read from.
00062      */
00063     FileTokenizer( std::FILE* file_ptr );
00064 
00065     /** \brief destructor : closes file.
00066      *
00067      * The destructor closes the passed file handle.   This
00068      * is done as a convenience feature.  If the caller
00069      * creates an instance of this object on the stack, the
00070      * file will automatically be closed when the caller
00071      * returns.
00072      */
00073     ~FileTokenizer();
00074 
00075     /** \brief get next token
00076      *
00077      * Get the next whitesapce-deliminated token from the file.
00078      * NOTE: The returned string is only valid until the next
00079      *       call to any of the functions in this class that
00080      *       read from the file.
00081      *
00082      * \return A pointer to the buffer space containing the string,
00083      *         or NULL if an error occured.
00084      */
00085     const char* get_string( MsqError& err );
00086 
00087     /** \brief check for newline
00088      *
00089      * Consume whitespace upto and including the next newline.
00090      * If a non-space character is found before a newline,
00091      * the function will stop, set the error message, and
00092      * return false.
00093      *
00094      * \return True if a newline was found before any non-space
00095      *         character.  False otherwise.
00096      */
00097     bool get_newline( MsqError& err );
00098 
00099     /** \brief Parse a sequence of double values.
00100      *
00101      * Read the specified number of space-deliminated 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_doubles( size_t count, double* array, MsqError& err );
00108 
00109     /** \brief Parse a sequence of float values.
00110      *
00111      * Read the specified number of space-deliminated doubles.
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_floats( size_t count, float* array, MsqError& err );
00118 
00119     /** \brief Parse a sequence of integer values.
00120      *
00121      * Read the specified number of space-deliminated 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_integers( size_t count, int* array, MsqError& err );
00128 
00129     /** \brief Parse a sequence of integer values.
00130      *
00131      * Read the specified number of space-deliminated 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_long_ints( size_t count, long* array, MsqError& err );
00138 
00139     /** \brief Parse a sequence of integer values.
00140      *
00141      * Read the specified number of space-deliminated 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_short_ints( size_t count, short* array, MsqError& err );
00148 
00149     /** \brief Parse a sequence of integer values.
00150      *
00151      * Read the specified number of space-deliminated ints.
00152      *
00153      * \param count   The number of values to read.
00154      * \param array   The memory at which to store the values.
00155      * \return true if successful, false otherwise.
00156      */
00157     bool get_bytes( size_t count, unsigned char* array, MsqError& err );
00158 
00159     /** \brief Parse a sequence of bit or boolean values.
00160      *
00161      * Read the specified number of space-deliminated values.
00162      *
00163      * \param count   The number of values to read.
00164      * \param array   The memory at which to store the values.
00165      * \return true if successful, false otherwise.
00166      */
00167     bool get_booleans( size_t count, bool* array, MsqError& err );
00168 
00169     /**
00170      * Check for end-of-file condition.
00171      */
00172     bool eof() const;
00173 
00174     /**
00175      * Get the line number the last token was read from.
00176      */
00177     int line_number() const
00178     {
00179         return lineNumber;
00180     }
00181 
00182     /**
00183      * Put current token back in buffer.  Can only unget one token.
00184      */
00185     void unget_token();
00186 
00187     /**
00188      * Match current token to passed string.  If token
00189      * doesn't match, set error message.
00190      */
00191     bool match_token( const char* string, MsqError& err );
00192 
00193     /**
00194      * Match the current token to one of an array of strings.
00195      * Sets the error message if the current token doesn't
00196      * match any of the input strings.
00197      *
00198      * \param  string_list A NULL-terminated array of strings.
00199      * \return One greater than the index of the matched
00200      *         string, or zero if no match.
00201      */
00202     int match_token( const char* const* string_list, MsqError& err );
00203 
00204   private:
00205     /** Internal implementation of get_doubles() */
00206     bool get_double_internal( double& result, MsqError& err );
00207     /** Internal implementation of get_long_ints() */
00208     bool get_long_int_internal( long& result, MsqError& err );
00209     /** Internal implementation of get_Booleans() */
00210     bool get_boolean_internal( bool& result, MsqError& err );
00211 
00212     /** Internal implementation of get_floats() */
00213     bool get_float_internal( float& result, MsqError& err );
00214     /** Internal implementation of get_integers() */
00215     bool get_integer_internal( int& result, MsqError& err );
00216     /** Internal implementation of get_short_ints() */
00217     bool get_short_int_internal( short& result, MsqError& err );
00218     /** Internal implementation of get_bytes() */
00219     bool get_byte_internal( unsigned char& result, MsqError& err );
00220 
00221     /** Pointer to standard C FILE struct */
00222     std::FILE* filePtr;
00223 
00224     /** Input buffer */
00225     char buffer[512];
00226 
00227     /** One past the end of the last token returned */
00228     char* nextToken;
00229     /** One past the last used byte of the buffer */
00230     char* bufferEnd;
00231 
00232     /** Line number of last returned token */
00233     int lineNumber;
00234 
00235     /** The whitespace character marking the end of the
00236      *  last returned token.  Saved here because if it
00237      *  is a newline, the line count will need to be
00238      *  incremented when the next token is returned.
00239      */
00240     char lastChar;
00241 };
00242 
00243 }  // namespace MBMesquite
00244 
00245 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines