Branch data Line data Source code
1 : : /* *****************************************************************
2 : : MESQUITE -- The Mesh Quality Improvement Toolkit
3 : :
4 : : Copyright 2004 Lawrence Livermore National Laboratory. Under
5 : : the terms of Contract B545069 with the University of Wisconsin --
6 : : Madison, Lawrence Livermore National Laboratory retains certain
7 : : rights in this software.
8 : :
9 : : This library is free software; you can redistribute it and/or
10 : : modify it under the terms of the GNU Lesser General Public
11 : : License as published by the Free Software Foundation; either
12 : : version 2.1 of the License, or (at your option) any later version.
13 : :
14 : : This library is distributed in the hope that it will be useful,
15 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : Lesser General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU Lesser General Public License
20 : : (lgpl.txt) along with this library; if not, write to the Free Software
21 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 : :
23 : : [email protected]
24 : :
25 : : ***************************************************************** */
26 : :
27 : : #ifndef FILE_TOKENIZER_HPP
28 : : #define FILE_TOKENIZER_HPP
29 : :
30 : : #include "Mesquite.hpp"
31 : : #include <cstdio>
32 : : #include <sys/types.h>
33 : :
34 : : namespace MBMesquite
35 : : {
36 : :
37 : : class MsqError;
38 : :
39 : : /**
40 : : * \class FileTokenizer
41 : : * \brief Parse a file as space-separated tokens
42 : : * \author Jason Kraftcheck
43 : : * \date 30 Sept 2004
44 : : *
45 : : * Read a file, separating it into space-separated tokens.
46 : : * This is provided in place of using the standard C or C++
47 : : * file parsing routines because it counts lines, which is
48 : : * useful for error reporting. Also provides some useful
49 : : * utility methods for parsing VTK files (which is the
50 : : * intended use of this implementation.)
51 : : *
52 : : * Uses raw reads/writes, implementing internal buffering.
53 : : * Token size may not exceed buffer size.
54 : : */
55 : :
56 : : class FileTokenizer
57 : : {
58 : : public:
59 : : /** \brief constructor
60 : : *
61 : : * \param file_ptr The file to read from.
62 : : */
63 : : FileTokenizer( std::FILE* file_ptr );
64 : :
65 : : /** \brief destructor : closes file.
66 : : *
67 : : * The destructor closes the passed file handle. This
68 : : * is done as a convenience feature. If the caller
69 : : * creates an instance of this object on the stack, the
70 : : * file will automatically be closed when the caller
71 : : * returns.
72 : : */
73 : : ~FileTokenizer();
74 : :
75 : : /** \brief get next token
76 : : *
77 : : * Get the next whitesapce-deliminated token from the file.
78 : : * NOTE: The returned string is only valid until the next
79 : : * call to any of the functions in this class that
80 : : * read from the file.
81 : : *
82 : : * \return A pointer to the buffer space containing the string,
83 : : * or NULL if an error occured.
84 : : */
85 : : const char* get_string( MsqError& err );
86 : :
87 : : /** \brief check for newline
88 : : *
89 : : * Consume whitespace upto and including the next newline.
90 : : * If a non-space character is found before a newline,
91 : : * the function will stop, set the error message, and
92 : : * return false.
93 : : *
94 : : * \return True if a newline was found before any non-space
95 : : * character. False otherwise.
96 : : */
97 : : bool get_newline( MsqError& err );
98 : :
99 : : /** \brief Parse a sequence of double values.
100 : : *
101 : : * Read the specified number of space-deliminated doubles.
102 : : *
103 : : * \param count The number of values to read.
104 : : * \param array The memory at which to store the values.
105 : : * \return true if successful, false otherwise.
106 : : */
107 : : bool get_doubles( size_t count, double* array, MsqError& err );
108 : :
109 : : /** \brief Parse a sequence of float values.
110 : : *
111 : : * Read the specified number of space-deliminated doubles.
112 : : *
113 : : * \param count The number of values to read.
114 : : * \param array The memory at which to store the values.
115 : : * \return true if successful, false otherwise.
116 : : */
117 : : bool get_floats( size_t count, float* array, MsqError& err );
118 : :
119 : : /** \brief Parse a sequence of integer values.
120 : : *
121 : : * Read the specified number of space-deliminated ints.
122 : : *
123 : : * \param count The number of values to read.
124 : : * \param array The memory at which to store the values.
125 : : * \return true if successful, false otherwise.
126 : : */
127 : : bool get_integers( size_t count, int* array, MsqError& err );
128 : :
129 : : /** \brief Parse a sequence of integer values.
130 : : *
131 : : * Read the specified number of space-deliminated ints.
132 : : *
133 : : * \param count The number of values to read.
134 : : * \param array The memory at which to store the values.
135 : : * \return true if successful, false otherwise.
136 : : */
137 : : bool get_long_ints( size_t count, long* array, MsqError& err );
138 : :
139 : : /** \brief Parse a sequence of integer values.
140 : : *
141 : : * Read the specified number of space-deliminated ints.
142 : : *
143 : : * \param count The number of values to read.
144 : : * \param array The memory at which to store the values.
145 : : * \return true if successful, false otherwise.
146 : : */
147 : : bool get_short_ints( size_t count, short* array, MsqError& err );
148 : :
149 : : /** \brief Parse a sequence of integer values.
150 : : *
151 : : * Read the specified number of space-deliminated ints.
152 : : *
153 : : * \param count The number of values to read.
154 : : * \param array The memory at which to store the values.
155 : : * \return true if successful, false otherwise.
156 : : */
157 : : bool get_bytes( size_t count, unsigned char* array, MsqError& err );
158 : :
159 : : /** \brief Parse a sequence of bit or boolean values.
160 : : *
161 : : * Read the specified number of space-deliminated values.
162 : : *
163 : : * \param count The number of values to read.
164 : : * \param array The memory at which to store the values.
165 : : * \return true if successful, false otherwise.
166 : : */
167 : : bool get_booleans( size_t count, bool* array, MsqError& err );
168 : :
169 : : /**
170 : : * Check for end-of-file condition.
171 : : */
172 : : bool eof() const;
173 : :
174 : : /**
175 : : * Get the line number the last token was read from.
176 : : */
177 : 162 : int line_number() const
178 : : {
179 : 162 : return lineNumber;
180 : : }
181 : :
182 : : /**
183 : : * Put current token back in buffer. Can only unget one token.
184 : : */
185 : : void unget_token();
186 : :
187 : : /**
188 : : * Match current token to passed string. If token
189 : : * doesn't match, set error message.
190 : : */
191 : : bool match_token( const char* string, MsqError& err );
192 : :
193 : : /**
194 : : * Match the current token to one of an array of strings.
195 : : * Sets the error message if the current token doesn't
196 : : * match any of the input strings.
197 : : *
198 : : * \param string_list A NULL-terminated array of strings.
199 : : * \return One greater than the index of the matched
200 : : * string, or zero if no match.
201 : : */
202 : : int match_token( const char* const* string_list, MsqError& err );
203 : :
204 : : private:
205 : : /** Internal implementation of get_doubles() */
206 : : bool get_double_internal( double& result, MsqError& err );
207 : : /** Internal implementation of get_long_ints() */
208 : : bool get_long_int_internal( long& result, MsqError& err );
209 : : /** Internal implementation of get_Booleans() */
210 : : bool get_boolean_internal( bool& result, MsqError& err );
211 : :
212 : : /** Internal implementation of get_floats() */
213 : : bool get_float_internal( float& result, MsqError& err );
214 : : /** Internal implementation of get_integers() */
215 : : bool get_integer_internal( int& result, MsqError& err );
216 : : /** Internal implementation of get_short_ints() */
217 : : bool get_short_int_internal( short& result, MsqError& err );
218 : : /** Internal implementation of get_bytes() */
219 : : bool get_byte_internal( unsigned char& result, MsqError& err );
220 : :
221 : : /** Pointer to standard C FILE struct */
222 : : std::FILE* filePtr;
223 : :
224 : : /** Input buffer */
225 : : char buffer[512];
226 : :
227 : : /** One past the end of the last token returned */
228 : : char* nextToken;
229 : : /** One past the last used byte of the buffer */
230 : : char* bufferEnd;
231 : :
232 : : /** Line number of last returned token */
233 : : int lineNumber;
234 : :
235 : : /** The whitespace character marking the end of the
236 : : * last returned token. Saved here because if it
237 : : * is a newline, the line count will need to be
238 : : * incremented when the next token is returned.
239 : : */
240 : : char lastChar;
241 : : };
242 : :
243 : : } // namespace MBMesquite
244 : :
245 : : #endif
|