1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2004 Lawrence Livermore National Laboratory.  Under
    the terms of Contract B545069 with the University of Wisconsin --
    Madison, Lawrence Livermore National Laboratory retains certain
    rights in this software.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    (lgpl.txt) along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    [email protected]

  ***************************************************************** */

#ifndef FILE_TOKENIZER_HPP
#define FILE_TOKENIZER_HPP

#include "Mesquite.hpp"
#include <cstdio>
#include <sys/types.h>

namespace MBMesquite
{

class MsqError;

/**
 * \class  FileTokenizer
 * \brief  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.
 */

class FileTokenizer
{
  public:
    /** \brief constructor
     *
     * \param file_ptr The file to read from.
     */
    FileTokenizer( std::FILE* file_ptr );

    /** \brief 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.
     */
    ~FileTokenizer();

    /** \brief 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.
     *
     * \return A pointer to the buffer space containing the string,
     *         or NULL if an error occured.
     */
    const char* get_string( MsqError& err );

    /** \brief 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.
     *
     * \return True if a newline was found before any non-space
     *         character.  False otherwise.
     */
    bool get_newline( MsqError& err );

    /** \brief Parse a sequence of double values.
     *
     * Read the specified number of space-deliminated doubles.
     *
     * \param count   The number of values to read.
     * \param array   The memory at which to store the values.
     * \return true if successful, false otherwise.
     */
    bool get_doubles( size_t count, double* array, MsqError& err );

    /** \brief Parse a sequence of float values.
     *
     * Read the specified number of space-deliminated doubles.
     *
     * \param count   The number of values to read.
     * \param array   The memory at which to store the values.
     * \return true if successful, false otherwise.
     */
    bool get_floats( size_t count, float* array, MsqError& err );

    /** \brief Parse a sequence of integer values.
     *
     * Read the specified number of space-deliminated ints.
     *
     * \param count   The number of values to read.
     * \param array   The memory at which to store the values.
     * \return true if successful, false otherwise.
     */
    bool get_integers( size_t count, int* array, MsqError& err );

    /** \brief Parse a sequence of integer values.
     *
     * Read the specified number of space-deliminated ints.
     *
     * \param count   The number of values to read.
     * \param array   The memory at which to store the values.
     * \return true if successful, false otherwise.
     */
    bool get_long_ints( size_t count, long* array, MsqError& err );

    /** \brief Parse a sequence of integer values.
     *
     * Read the specified number of space-deliminated ints.
     *
     * \param count   The number of values to read.
     * \param array   The memory at which to store the values.
     * \return true if successful, false otherwise.
     */
    bool get_short_ints( size_t count, short* array, MsqError& err );

    /** \brief Parse a sequence of integer values.
     *
     * Read the specified number of space-deliminated ints.
     *
     * \param count   The number of values to read.
     * \param array   The memory at which to store the values.
     * \return true if successful, false otherwise.
     */
    bool get_bytes( size_t count, unsigned char* array, MsqError& err );

    /** \brief Parse a sequence of bit or boolean values.
     *
     * Read the specified number of space-deliminated values.
     *
     * \param count   The number of values to read.
     * \param array   The memory at which to store the values.
     * \return true if successful, false otherwise.
     */
    bool get_booleans( size_t count, bool* array, MsqError& err );

    /**
     * Check for end-of-file condition.
     */
    bool eof() const;

    /**
     * Get the line number the last token was read from.
     */
    int line_number() const
    {
        return lineNumber;
    }

    /**
     * Put current token back in buffer.  Can only unget one token.
     */
    void unget_token();

    /**
     * Match current token to passed string.  If token
     * doesn't match, set error message.
     */
    bool match_token( const char* string, 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.
     *
     * \param  string_list A NULL-terminated array of strings.
     * \return One greater than the index of the matched
     *         string, or zero if no match.
     */
    int match_token( const char* const* string_list, MsqError& err );

  private:
    /** Internal implementation of get_doubles() */
    bool get_double_internal( double& result, MsqError& err );
    /** Internal implementation of get_long_ints() */
    bool get_long_int_internal( long& result, MsqError& err );
    /** Internal implementation of get_Booleans() */
    bool get_boolean_internal( bool& result, MsqError& err );

    /** Internal implementation of get_floats() */
    bool get_float_internal( float& result, MsqError& err );
    /** Internal implementation of get_integers() */
    bool get_integer_internal( int& result, MsqError& err );
    /** Internal implementation of get_short_ints() */
    bool get_short_int_internal( short& result, MsqError& err );
    /** Internal implementation of get_bytes() */
    bool get_byte_internal( unsigned char& result, MsqError& err );

    /** Pointer to standard C FILE struct */
    std::FILE* filePtr;

    /** Input buffer */
    char buffer[512];

    /** One past the end of the last token returned */
    char* nextToken;
    /** One past the last used byte of the buffer */
    char* bufferEnd;

    /** Line number of last returned token */
    int lineNumber;

    /** 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.
     */
    char lastChar;
};

}  // namespace MBMesquite

#endif