cfl_tokenreader.h
Go to the documentation of this file.
1 /** @file cfl_tokenreader.h @brief Class TokenReader */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5 Copyright (C) 2006, 2010 Thomas Moor
6 Copyright (C) 2006 Bernd Opitz
7 Exclusive copyright is granted to Klaus Schmidt
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
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 
24 
25 #ifndef FAUDES_TOKENREADER_H
26 #define FAUDES_TOKENREADER_H
27 
28 
29 #include <vector>
30 
31 #include "cfl_definitions.h"
32 #include "cfl_exception.h"
33 #include "cfl_token.h"
34 
35 namespace faudes {
36 
37 /**
38  * A TokenReader reads sequential tokens from a file or string. It can get or peek
39  * the next token and it will track line numbers for informative diagnosis output.
40  *
41  * The token stream is meant to be XML compliant, i.e., there are two dedicated
42  * token types that mark the begin and the end of XML elements while all other token
43  * types represent atomic data such as integers or strings that form the XML character data;
44  * see the documentation of the class Token for details.
45  *
46  * The TokenReader maintains a current position in the stream and implements searching
47  * within nested elements and for sequential access of the tokens within each element;
48  * e.g. ReadBegin(const std::string&) will search for the element with the specified name.
49  *
50  * Reading from the Tokenreader by a particular method encodes the type of the
51  * requested data, e.g. ReadInteger() to read an integer token. If the token at the
52  * current position does not match the requiested type, an exception is thrown.
53  * There are alose Get() and Peek() methods to retrieve and inspect the token at the current
54  * position.
55  *
56  * For convenience, the TokenReader also implements reading all contents of an alement
57  * and larger chunks of data formtated as CDATA markup within an element; e.g.
58  * ReadVerbatim(const std::string&, std::string&) reads the contents of the specified element
59  * en block as one string.
60  *
61  * @ingroup TokenIO
62  */
63 
65 public:
66 
67  /**
68  * Mode of operation: read from file, stdin or string
69  */
70  enum Mode {File, Stdin, String};
71 
72  /**
73  * TokenReader constructor
74  *
75  * @param mode
76  * select source: File, Stdin or String
77  * @param rInString
78  * string to read from or filename, respectively.
79  *
80  * @exception Exception
81  * - faudes::Exception ios error opening file (id 1)
82  * - faudes::Exception invalid mode (Stdin not implemented) (id 1)
83  */
84  TokenReader(Mode mode, const std::string& rInString="");
85 
86  /**
87  * Creates a TokenReader for reading a file.
88  *
89  * This is a convenience wrapper for TokenReader(Mode, const std::string&).
90  *
91  * @param rFilename
92  * file to read
93  *
94  * @exception Exception
95  * - faudes::Exception ios error opening file (id 1)
96  */
97  TokenReader(const std::string& rFilename);
98 
99 
100  /**
101  * Destruct
102  */
103  ~TokenReader(void);
104 
105  /**
106  * Rewind stream
107  *
108  * Reset the internal state to its initial value, i.e., the
109  * current position is the beginning of the stream. This is
110  * not functional in console mode.
111  *
112  * @exception Exception
113  * - faudes::Exception ios error reading file (id 1)
114  */
115  void Rewind(void);
116 
117  /**
118  * Access C++ stream
119  *
120  * This method provides direct access to the underlying C++ stream.
121  * After any such access, the TokenReader must be Rewind() to reset
122  * its internal state.
123  *
124  * @return
125  * pointer to C++ input stream
126  */
127  std::istream* Streamp(void);
128 
129  /**
130  * Access stream mode.
131  *
132  * Returns mode from construction, i.e. file, string or console.
133  *
134  * @return
135  * Mode
136  */
137  Mode SourceMode(void) const { return mMode;};
138 
139  /**
140  * Access the filename.
141  *
142  * Returns the name of the attached file, if any. For string mode and
143  * console mode dummy values are returned.
144  *
145  * @return
146  * filename
147  */
148  std::string FileName(void) const;
149 
150  /**
151  * Peek next token.
152  *
153  * Copies the next token to the provided reference and returns
154  * true on success. The token remains in an internal buffer.
155  *
156  * Technical note: we should have used a const-ref as return
157  * in orde to avoid the copy. However, this will require a tedious
158  * rewrite.
159  *
160  * @param token
161  * reference to token
162  * @return
163  * true for a valid token, false for eof or other stream errors
164  *
165  * @exception Exception
166  * - faudes::Exception ios error reading file (id 1)
167  *
168  */
169  bool Peek(Token& token);
170 
171  /**
172  * Get next token.
173  *
174  * Same as Peek() except that the token is removed from the buffer.
175  *
176  * @param token
177  * Reference to token
178  *
179  * @exception Exception
180  * faudes exception ios (id 1)
181  *
182  * @return
183  * true for a valid token, false for eof or other stream errors
184  */
185  bool Get(Token& token);
186 
187 
188  /**
189  * Search for specified element
190  *
191  * This function searches for the specified section on the current level.
192  * It skips any sections on the levels below, and it will wrap to the begin
193  * of the current section once. In the case of success, it returns true, else
194  * false. In contrast to other token i/o methodes, this method will not throw
195  * any execptions.
196  *
197  * In the case of success, the next token is the begin-tag of the specified element,
198  * which can be read with ReadBegin().
199  *
200  * @param rLabel
201  * Token label to specify section
202  *
203  * @return
204  * True if sectiob exists
205  *
206  */
207  bool ExistsBegin(const std::string& rLabel);
208 
209  /**
210  * Open a section by specified label. This function searches
211  * for the section on this level, it skips any sections on levels
212  * below this level, and it will wrap once to the begin of the current
213  * section. In the case of success, the matching begin token is the
214  * last token read. After processing the section, a matching ReadEnd(label)
215  * must be called. If the specified element does not exist, an exception is thrown.
216  *
217  * @param rLabel
218  * Token label to specify section
219  *
220  * @exception Exception
221  * - faudes::Exception ios error reading file (id 1)
222  * - Section begin label not found (id 51)
223  *
224  */
225  void ReadBegin(const std::string& rLabel);
226 
227  /**
228  * Open a section by specified label.
229  *
230  * This wrapper ReadBegin(const std::string&) will return the actual
231  * begin tag in its second argument, e.g., to inspect XML attributes.
232  *
233  * @param rLabel
234  * Token label to specify section
235  * @param rToken
236  * Begin tag as found in token stream.
237  *
238  * @exception Exception
239  * - faudes::Exception ios error reading file (id 1)
240  * - Section begin label not found (id 51)
241  *
242  */
243  void ReadBegin(const std::string& rLabel, Token& rToken);
244 
245  /**
246  * Close the current section by matching the previous ReadBegin().
247  * Reads all tokens up to and including end of current section.
248  *
249  * @param rLabel
250  * Token label to specify section
251  *
252  * @exception Exception
253  * - faudes::Exception ios error reading file (id 1)
254  * - Section end label not found (id 51)
255  *
256  */
257  void ReadEnd(const std::string& rLabel);
258 
259  /**
260  * Find specified begin label.
261  *
262  * This function searches for the section on this level and any descending level.
263  * It does not read the specified section tag, but stops just
264  * one token before (and in this regard matches the behaviour of ExistsBegin()).
265  *
266  * Technical note:
267  * Former versions of libFAUDES also read the actual begin token and
268  * required a matching call of SeekEnd(). As of version 2.18a, this is not
269  * supported anymore. The previous behaviour was rarely needed and can be
270  * mimiqued by an ordinary ReadEnd() with a subsequent Recover(level).
271  *
272  * @param rLabel
273  * Label to specify section
274  *
275  * @exception Exception
276  * - faudes::Exception ios error reading file (id 1)
277  * - Section begin not found (id 51)
278  *
279  */
280  void SeekBegin(const std::string& rLabel);
281 
282  /**
283  * Find specified begin label.
284  *
285  * This version SeekBegin(const std::string&) will return the actual
286  * begin tag in its second argument.
287  *
288  * @param rLabel
289  * Token label to specify section
290  * @param rToken
291  * Begin tag as found in token stream.
292  *
293  * @exception Exception
294  * - faudes::Exception ios error reading file (id 1)
295  * - Section begin label not found (id 51)
296  *
297  */
298  void SeekBegin(const std::string& rLabel, Token& rToken);
299 
300  /**
301  * Peek a token and check whether it ends the specified section.
302  * This function is meant for scanning a section with a while- construct.
303  * \code
304  * ReadBegin("MySec");
305  * while(!Eos("MySec")) {
306  * ...
307  * };
308  * ReadEnd("MySec");
309  * \endcode
310  *
311  * @param rLabel
312  * Token label to specify section
313  *
314  * @exception Exception
315  * - faudes::Exception ios error reading file (id 1)
316  * - unexpected eof or section mismatch (id 51)
317  *
318  * @return
319  * True at end of section
320  */
321  bool Eos(const std::string& rLabel);
322 
323  /**
324  * Read integer token
325  *
326  * Reads the next token and interprets it as an non-negative integer.
327  *
328  * @exception Exception
329  * - faudes::Exception ios error reading file (id 1)
330  * - Token mismatch (id 50)
331  *
332  * @return
333  * value of integer token
334  */
335  long int ReadInteger(void);
336 
337  /**
338  * Read float token
339  *
340  * Reads the next token and interprets it as a float.
341  *
342  * @exception Exception
343  * - faudes::Exception ios error reading file (id 1)
344  * - Token mismatch (id 50)
345  *
346  * @return
347  * float value of token
348  */
349  double ReadFloat(void);
350 
351  /**
352  * Read string token
353  *
354  * Reads the next token and interprets it as a string.
355  *
356  * @exception Exception
357  * - faudes::Exception ios error reading file (id 1)
358  * - Token mismatch (id 50)
359  *
360  * @return
361  * value of name token
362  */
363  std::string ReadString(void);
364 
365  /**
366  * Read option token
367  *
368  * Reads the next token and interprets it as an option.
369  *
370  * @exception Exception
371  * - faudes::Exception ios error reading file (id 1)
372  * - Token mismatch (id 50)
373  *
374  * @return
375  * value of name token
376  */
377  std::string ReadOption(void);
378 
379  /**
380  * Read binary token.
381  *
382  * Reads the next token and interprets it as an base64
383  * encoded binary array.
384  *
385  * @param rData
386  * Buffer to read data
387  *
388  * @exception Exception
389  * - faudes::Exception ios error reading file (id 1)
390  * - Token mismatch (id 50)
391  *
392  */
393  void ReadBinary(std::string& rData);
394 
395 
396  /**
397  * Read plain text
398  *
399  * Interpret the specified section as plain charater data section, read
400  * the character data and interpret relevant entities. Leading and trailing
401  * whitespaces are ignored, other formating is maintained.
402  *
403  * This method facilitates the input of paragraphs of plain ASCII text with
404  * no other markup as the relevant entities (i.e. no HTML or RTF).
405  *
406  * @exception Exception
407  * - faudes::Exception ios error reading file (id 1)
408  * - Stream mismatch (id 50)
409  *
410  * @param rLabel
411  * Buffer to read Text
412  * @param rText
413  * Name of section to read text from
414  */
415  void ReadText(const std::string& rLabel, std::string& rText);
416 
417 
418  /**
419  * Read verbatim text
420  *
421  * Interpret the section as plain charater data section, read
422  * the character data from either one faudes string token or from consecutive CDATA markups.
423  * Leading and trailing whitespaces are ignored, other formating is maintained.
424  *
425  * This method facilitates the input of paragraphs of plain ASCII text with
426  * excessive use of special characters, e.g., program fragments.
427  *
428  * @exception Exception
429  * - faudes::Exception ios error reading file (id 1)
430  * - Stream mismatch (id 50)
431  *
432  * @param rLabel
433  * Buffer to read Text
434  * @param rText
435  * Name of section to read text from
436  */
437  void ReadVerbatim(const std::string& rLabel, std::string& rText);
438 
439 
440  /**
441  * Read plain text
442  *
443  * Read all text until and excluding the next markup tag. This method
444  * does no interpretation/substitution at all. It is meant to implemet carbon
445  * copy of text sections.
446  *
447  * @exception Exception
448  * - faudes::Exception ios error reading file (id 1)
449  * - Stream mismatch (id 50)
450  *
451  * @param rData
452  * Buffer to read characterdata
453  */
454  void ReadCharacterData(std::string& rData);
455 
456 
457  /**
458  * Read XML section
459  *
460  * Reads the current element, including all character data and markup,
461  * until and excluding the matching end tag. This method does no interpretation whatsoever.
462  * The result is a string that represents the respective section in plain XML format
463  * and can be used for expernal post-processing.
464  *
465  * @exception Exception
466  * - faudes::Exception ios error reading file (id 1)
467  * - Stream mismatch (id 50)
468  *
469  * @param rSectionString
470  * Buffer to read section
471  */
472  void ReadSection(std::string& rSectionString);
473 
474 
475  /** Operator for get */
476  bool operator >> (Token& token) {
477  return Get(token);
478  }
479 
480  /**
481  * Return number of lines read
482  *
483  * @return
484  * Number of lines read
485  */
486  int Line(void) const;
487 
488  /**
489  * Return current level of section nesting.
490  *
491  * @return
492  * Number of lines read
493  */
494  int Level(void) const { return mLevel;};
495 
496  /**
497  * Recover by skipping tokens until returning to the specified level of section nesting
498  *
499  * @return
500  * True on success
501  */
502  bool Recover(int level);
503 
504  /**
505  * Reset to the begining of the specified level of section nesting.
506  *
507  * @param level
508  * target level, defaults to current level
509  * @return
510  * True on success
511  */
512  bool Reset(int level=-1);
513 
514  /**
515  * Return "filename:line"
516  */
517  std::string FileLine(void) const;
518 
519  private:
520 
521  /** input mode */
523 
524  /** istream object pointer */
525  std::istream* mpStream;
526 
527  /** actual stream object, file input */
528  std::ifstream mFStream;
529 
530  /** actual stream object on heap, string input */
531  std::istringstream* mpSStream;
532 
533  /** Filename */
534  std::string mFileName;
535 
536  /** Line counter */
538 
539  /** file position */
540  long int mFilePos;
541 
542  /** flag to ignore faudes comments marked by '%' */
544 
545  /** Level (of nested sections) */
546  int mLevel;
547 
548  /** State on entry of respective level */
549  struct LState {
550  std::string mLabel;
551  long int mStartPosition;
552  long int mStartLine;
555  };
556  std::vector<LState> mLevelState;
557 
558  /** peek buffer */
560 };
561 
562 } // namespace faudes
563 
564 #endif
Compiletime options.
Class Exception.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:80
Class Token.
Elementary type.
A TokenReader reads sequential tokens from a file or string.
Mode
Mode of operation: read from file, stdin or string.
Token mPeekToken
peek buffer
bool mFaudesComments
flag to ignore faudes comments marked by ''
Mode mMode
input mode
int Level(void) const
Return current level of section nesting.
std::istringstream * mpSStream
actual stream object on heap, string input
std::istream * mpStream
istream object pointer
int mLineCount
Line counter.
std::ifstream mFStream
actual stream object, file input
int mLevel
Level (of nested sections)
std::string mFileName
Filename.
std::vector< LState > mLevelState
Mode SourceMode(void) const
Access stream mode.
long int mFilePos
file position
Tokens model atomic data for stream IO.
Definition: cfl_token.h:54
libFAUDES resides within the namespace faudes.
State on entry of respective level.

libFAUDES 2.32f --- 2024.12.22 --- c++ api documentaion by doxygen