vtkbone
CommandStyleFileReader.h
Go to the documentation of this file.
1/*=========================================================================
2
3 vtkbone
4
5 VTK classes for building and analyzing Numerics88 finite element models.
6
7 Copyright (c) 2010, Numerics88 Solutions Ltd.
8 All rights reserved.
9
10=========================================================================*/
11
12#ifndef __CommandStyleFileReader_h
13#define __CommandStyleFileReader_h
14
15#include <iostream>
16#include <string>
17#include <sstream>
18#include <map>
19#include <stack>
20
21#define FSDF_OK 0
22#define FSDF_ERROR 1
23
138{
139public:
140
141 // Defines a pointer to a member function that takes no arguments
142 // and returns an int.
144
145 // A set of commmand handler methods mapped to command key words.
146 typedef std::map<std::string,CommandHandler_t> CommandContext_t;
147
148 // Constructor
149 // stream - an already-opened input stream (file or string)
151
152 // Method that the user calls to read and process input stream.
153 //
154 // Returns FSDF_OK on success and FSDF_ERROR on failure.
155 virtual int Read();
156
157 virtual void DebugOn() {debug = 1;}
158 virtual void DebugOff() {debug = 0;}
159 virtual void SetDebug(int d) {debug = d;}
160
162 const char * GetErrorMsg() {return errorMsg.c_str();}
163
164protected:
165
166 // Register a method of your derived class to handle a particular command.
167 //
168 // commandName - identifying name of the command that you want to handle.
169 // handler - member function pointer to your method.
170 // NOTE: You must use reinterpret_cast<CommandHandler_t> on this
171 // argument when calling this function. Refer to the above
172 // discussion.
174 CommandHandler_t handler);
175
176 // Refer to the above discussion for the purpose, implementation, and
177 // calling method of this function.
179
180 // Called after all file lines have been read and processed.
181 virtual int Finish() { return FSDF_OK; }
182
183 // Reads the next line from the file and stores it in "line".
184 // Checks for error conditions and increments lineCount.
185 //
186 // Returns 1 if a line is successfully obtained, and 0 otherwise.
187 virtual int GetLine();
188
189 // Returns 1 if the current line is a command.
190 //
191 // If a command is found, commandName should be set to the command name.
192 virtual int IsCommand () = 0;
193
194 // Returns 1 if the line is a comment line.
195 // No default implementation, since comment line syntax varies considerably.
196 // You don't need to implement this if you don't use it, since it is not
197 // currently used by CommandStyleFileReader.
198 virtual int IsCommentLine() = 0;
199
200 // Search the input stream for a command.
201 // If a command is found, commandName is set and ProcessCommand is called.
202 //
203 // Returns FSDF_OK on success and FSDF_ERROR on failure.
204 virtual int FindCommand();
205
206 // Check if the current command has been registered for any command
207 // handlers, and if so, call that handler.
208 // If no match is found, call FindCommandBoundaries (recursively) to
209 // search for sub-commands.
210 //
211 // Returns FSDF_OK on success and FSDF_ERROR on failure.
212 virtual int ProcessCommand();
213
214 // Sets errorStatus to FSDF_ERROR and stores msg in erroMsg.
215 // Will not overwrite a pre-existing error message.
216 virtual void SetError (const std::string& msg);
217
218 // Does nothing if debug is 0, otherwise prints msg to std::cerr.
219 // Implement in derived class to provide your own debug message functionality.
220 virtual void DebugMessage (const std::string& msg);
221
222 // Prints msg to std::cerr.
223 // Implement in derived class to provide your own debug message functionality.
224 virtual void WarningMessage (const std::string& msg);
225
226 std::istream& stream;
227 std::string commandName;
230
231 std::string line;
232
233 std::stack<CommandContext_t> commandContextStack;
234
235 int debug;
236
238 std::string errorMsg;
239
240private:
241 // Prevent compiler from making public versions of these.
243 void operator= (const CommandStyleFileReader&);
244
245};
246
247//----------------------------------------------------------------------------
248// This macro is used to record errors
249// frSetErrorMsgMacro("Error message" << variable);
250//
251#define frSetErrorMsgMacro(x) \
252 { \
253 std::ostringstream msg; \
254 msg << "ERROR: In " << __FILE__ << ", line " << __LINE__ \
255 << "\n" << this << ": " << x << "\n"; \
256 SetError(msg.str()); \
257 }
258
259//----------------------------------------------------------------------------
260// This macro is used to print out debugging info
261// frSetDebugMsgMacro("Debug message" << variable);
262//
263#define frSetDebugMsgMacro(x) \
264 { \
265 if (debug) \
266 { \
267 std::ostringstream msg; \
268 msg << "Debug: In " << __FILE__ << ", line " << __LINE__ \
269 << "\n" << this << ": " << x << "\n"; \
270 DebugMessage (msg.str()); \
271 } \
272 }
273
274//----------------------------------------------------------------------------
275// This macro is used to print out debugging info
276// frSetDebugMsgMacro("Debug message" << variable);
277//
278#define frSetWarningMsgMacro(x) \
279 { \
280 std::ostringstream msg; \
281 msg << "WARNING: In " << __FILE__ << ", line " << __LINE__ \
282 << "\n" << this << ": " << x << "\n"; \
283 WarningMessage (msg.str()); \
284 }
285
286#endif // __CommandStyleFileReader_h
#define FSDF_OK
An abstract base class for reading data files that are structured as text with a fixed maximum line l...
virtual int FindCommand()
virtual void DebugMessage(const std::string &msg)
virtual int ProcessCommand()
CommandStyleFileReader(std::istream &stream)
std::stack< CommandContext_t > commandContextStack
virtual int IsCommand()=0
int RegisterCommandHandler(const char *commandName, CommandHandler_t handler)
std::map< std::string, CommandHandler_t > CommandContext_t
virtual int GetLine()
virtual int CallCommandHandler(CommandHandler_t handler)
virtual int IsCommentLine()=0
virtual void SetError(const std::string &msg)
virtual void WarningMessage(const std::string &msg)
int(CommandStyleFileReader::* CommandHandler_t)()
int