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

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen