cfl_tokenwriter.h
Go to the documentation of this file.
1 /** @file cfl_tokenwriter.h @brief Class TokenWriter */
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 #ifndef FAUDES_TOKENWRITER_H
24 #define FAUDES_TOKENWRITER_H
25 
26 #include "cfl_definitions.h"
27 #include "cfl_token.h"
28 #include <algorithm>
29 #include <string>
30 #include <fstream>
31 #include <iostream>
32 
33 namespace faudes {
34 
35 /**
36  * A TokenWriter writes sequential tokens to a file, a string or stdout. It is the counterpart
37  * of the TokenReader. Since wrtiting data is comparatively straight foreward, there is no explicit
38  * support of sections etc. It is left to the calling function to organise the output appropriately.
39  *
40  * There are two twists required to get alogn with faudes-tokens within an XML context
41  * - The faudes comment mark % is convenient, but not XML compliant. The TokenWriter therefore must
42  * escape special XML characters when writing a faudes-comment.
43  * - Faudes style token streams are meant to formated to some extend, e.g., insertion of linefeeds
44  * after reaching a certain number of columns or befor the beginning of an element. While this is
45  * convenient for faudes-type serialisation, it is annoying for the processing of HTML style
46  * documentation.
47  *
48  * @ingroup TokenIO
49  */
50 
52 
53  public:
54 
55  /**
56  * Mode of operation: write to file, string or stdout
57  */
58  enum Mode {File, XmlFile, Stdout, String, Stream, XmlStream};
59 
60  /**
61  * Console or String TokenWriter constructor
62  *
63  * Technical detail: Stdout mode uses the global console object
64  * ConsoleOut::G() declared in cfl_helper.h.
65  *
66  * @exception Exception
67  * - faudes::Exception ios error opening file (id 2)
68  */
69  TokenWriter(Mode mode);
70 
71  /**
72  * File TokenWriter constructor
73  *
74  * @param rFilename
75  * File to write
76  * @param openmode
77  * std::ios::openmode
78  * @exception Exception
79  * - faudes::Exception ios error opening file (id 2)
80  */
81  TokenWriter(const std::string& rFilename,
82  std::ios::openmode openmode = std::ios::out|std::ios::trunc);
83 
84  /**
85  * Xml File TokenWriter constructor
86  *
87  * @param rFilename
88  * File to write
89  * @param doctype
90  * String to indicate XML doctype.
91  * @exception Exception
92  * - faudes::Exception ios error opening file (id 2)
93  */
94  TokenWriter(const std::string& rFilename, const std::string& doctype);
95 
96  /**
97  * Stream TokenWriter constructor
98  *
99  * @param rStream
100  * stream C++ stream to write to
101  * @param doctype
102  * String to indicate XML doctype.
103  * @exception Exception
104  * - faudes::Exception ios error opening file (id 2)
105  */
106  TokenWriter(std::ostream& rStream, const std::string& doctype="");
107 
108  /** Destructor. Calls close */
109  ~TokenWriter(void);
110 
111  /**
112  * Get the filename.
113  * Return dummy values for console or string mode.
114  *
115  * @return
116  * Filename
117  */
118  std::string FileName(void) const;
119 
120  /**
121  * Flush any buffers.
122  *
123  */
124  void Flush(void);
125 
126  /**
127  * Test for file mode (incl. XmlFile)
128  *
129  * @return
130  * Mode
131  */
132  bool FileMode(void) const { return mMode==File || mMode==XmlFile ;};
133 
134  /**
135  * Test for xml file mode
136  *
137  * @return
138  * Mode
139  */
140  bool XmlMode(void) const { return mMode==XmlFile ;};
141 
142  /**
143  * Test for console mode.
144  *
145  * @return
146  * Mode
147  */
148  bool StdoutMode(void) const { return mMode==Stdout;};
149 
150  /**
151  * Retrieve output as string (if in String mode)
152  *
153  * @exception Exception
154  * - faudes::Exception not in string mode (id 2)
155  */
156  std::string Str(void);
157 
158  /**
159  * Access C++ stream
160  *
161  */
162  std::ostream* Streamp(void);
163 
164  /**
165  * Get number of columns in a line
166  *
167  * @return
168  * # of columns in a line
169  */
170  int Columns(void) const;
171 
172  /**
173  * Set number of columns in a line
174  *
175  * @param columns
176  * # of columns in a line
177  */
178  void Columns(int columns);
179 
180  /**
181  * Write endl separator
182  *
183  * @exception Exception
184  * - faudes::Exception ios error writing file (id 2)
185  */
186  void Endl(void);
187 
188  /**
189  * Turn endl separator on/off
190  *
191  */
192  void Endl(bool on);
193 
194  /**
195  * Write next token
196  *
197  * @param rToken
198  * Token to write
199  * @exception Exception
200  * - faudes::Exception ios error wrtiting file (id 2)
201  */
202  void Write(Token& rToken);
203 
204  /**
205  * Write string.
206  *
207  * Writes a faudes string token, i.e. enclosed in double quotes
208  * if required and any special markup characters substitited
209  * by entity references.
210  *
211  * The string must not include any formating controls like linefeed.
212  *
213  * @param rString
214  * String to write
215  * @exception Exception
216  * - faudes::Exception ios error wrtiting file (id 2)
217  */
218  void WriteString(const std::string& rString);
219 
220  /**
221  * Write text.
222  *
223  * Converts the specified string to character data by substituting
224  * markup signal characters with the resp. entities and writes the
225  * result to the token stream.
226  *
227  * In contrast to WriteString(const std::string&), no quotes are applied and the
228  * output will in general be interpretable by multiple faudes string
229  * tokens.
230  *
231  * This function is depreciated as of libFAUDES 2.16. For a consistent
232  * readback, it is required to wrap the output between a begin-end-elemant pair.
233  * This is organized conveniently organised by WriteText(Token&, const std::string&).
234  *
235  *
236  * @param rText
237  * String to write
238  * @exception Exception
239  * - faudes::Exception ios error wrtiting file (id 2)
240  */
241  void WriteText(const std::string& rText);
242 
243  /**
244  * Write text section.
245  *
246  * Converts the specified string to character data by substituting
247  * markup signal characters with the resp. entities and writes the
248  * result to a section in the token stream. Formating by linefeeds
249  * etc is maintained.
250  *
251  * There is a matching ReadText() function for the Tokenreader
252  * to retrieve the original text.
253  *
254  * See also WriteText(const std::string&, const std::string&).
255  *
256  * @param rBeginTag
257  * Begin tag.
258  * @param rText
259  * String to write
260  * @exception Exception
261  * - faudes::Exception ios error wrtiting file (id 2)
262  */
263  void WriteText(Token& rBeginTag, const std::string& rText);
264 
265 
266  /**
267  * Write text section.
268  *
269  * Convenience wrapper for WriteText(Token&, const std::string&).
270  *
271  * @param rLabel
272  * Section label
273  * @param rText
274  * String to write
275  * @exception Exception
276  * - faudes::Exception ios error wrtiting file (id 2)
277  */
278  void WriteText(const std::string& rLabel, const std::string& rText);
279 
280 
281  /**
282  * Write verbatim text section.
283  *
284  * Writes a string verbatim as a sequence of CDATA markup in a deticated section. The string
285  * may contain any characters.
286  *
287  * In the libFAUDES context, the usecase are long fragments of text such as Lua code.
288  * For cosmetic reasons, the specified string in wrapped in linefeeds.
289  * Use the corresponding ReadVerbatim() for a consistent readback.
290  *
291  * See also WriteVerbatim(const std::string&, const std::string&).
292  *
293  * @param rBeginTag
294  * prepared begin token
295  * @param rText
296  * String to write
297  * @exception Exception
298  * - faudes::Exception ios error wrtiting file (id 2)
299  * - faudes::Exception tag is not a begin tag (id 2)
300  */
301  void WriteVerbatim(Token& rBeginTag, const std::string& rText);
302 
303 
304  /**
305  * Write verbatim text section.
306  *
307  * Convenience wrapper for WriteVerbatim(Token&, const std::string&).
308  *
309  * @param rLabel
310  * Section label
311  * @param rText
312  * String to write
313  * @exception Exception
314  * - faudes::Exception ios error wrtiting file (id 2)
315  */
316  void WriteVerbatim(const std::string& rLabel, const std::string& rText);
317 
318 
319  /**
320  * Write character data
321  *
322  * Writes a preproccessed string to the token stream. The string may or may
323  * not contain any markup. Formating like linefeed is maintained. This method
324  * is meant to support 1:1 copying from one stream to another. See also
325  * ReadCharacterData() from TokenReader.
326  *
327  * @param rCharData
328  * String to write
329  * @exception Exception
330  * - faudes::Exception ios error wrtiting file (id 2)
331  */
332  void WriteCharacterData(const std::string& rCharData);
333 
334  /**
335  * Write non negative integer
336  *
337  * @param index
338  * Integer to write
339  * @exception Exception
340  * - faudes::Exception ios error wrtiting file (id 2)
341  */
342  void WriteInteger(Idx index);
343 
344  /**
345  * Write float
346  *
347  * @param val
348  * float to write
349  * @exception Exception
350  * - faudes::Exception ios error wrtiting file (id 2)
351  */
352  void WriteFloat(const double& val);
353 
354  /**
355  * Write integer as hex
356  *
357  * @param val
358  * Integer to write
359  * @exception Exception
360  * - faudes::Exception ios error wrtiting file (id 2)
361  */
362  void WriteInteger16(long int val);
363 
364  /**
365  * Write option (may not contain any "+")
366  *
367  * @param rOpt
368  * option to write
369  * @exception Exception
370  * - faudes::Exception ios error wrtiting file (id 2)
371  */
372  void WriteOption(const std::string& rOpt);
373 
374  /**
375  * Write begin label
376  *
377  * @param rLabel
378  * End label, e.g. "Alphabet"
379  * @exception Exception
380  * - faudes::Exception ios error wrtiting file (id 2)
381  */
382  void WriteBegin(const std::string& rLabel);
383 
384  /**
385  * Write end label
386  *
387  * @param rLabel
388  * End label, e.g. "Alphabet"
389  * @exception Exception
390  * - faudes::Exception ios error wrtiting file (id 2)
391  */
392  void WriteEnd(const std::string& rLabel);
393 
394  /**
395  * Write empty section label
396  *
397  * @param rLabel
398  * End label, e.g. "Alphabet"
399  * @exception Exception
400  * - faudes::Exception ios error wrtiting file (id 2)
401  */
402  void WriteEmpty(const std::string& rLabel);
403 
404  /**
405  * Write comment in faudes format
406  *
407  * @param rComment
408  * Comment to write
409  * @exception Exception
410  * - faudes::Exception ios error wrtiting file (id 2)
411  */
412  void WriteComment(const std::string& rComment);
413 
414  /**
415  * Write comment in Xml format
416  *
417  * @param rComment
418  * Comment to write
419  * @exception Exception
420  * - faudes::Exception ios error wrtiting file (id 2)
421  */
422  void WriteXmlComment(const std::string& rComment);
423 
424  /**
425  * Write comment
426  *
427  * @param len
428  * Number of bytes to write
429  * @param pData
430  * Data to write
431  * @exception Exception
432  * - faudes::Exception ios error wrtiting file (id 2)
433  */
434  void WriteBinary(const char* pData, long int len);
435 
436  /**
437  * Operator for writing tokens
438  *
439  * @param rToken
440  * Token to write
441  * @return
442  * Reference to this TokenWriter
443  * @exception Exception
444  * - faudes::Exception ios error wrtiting file (id 2)
445  */
446  TokenWriter& operator << (Token& rToken) {
447  Write(rToken);
448  return *this;
449  }
450 
451  /**
452  * Operator for writing std::strings to a stream
453  *
454  * @param rString
455  * String to write
456  * @return
457  * Reference to this TokenWriter
458  * @exception Exception
459  * - faudes::Exception ios error wrtiting file (id 2)
460  */
461  TokenWriter& operator << (const std::string& rString) {
462  WriteString(rString);
463  return *this;
464  }
465 
466  /**
467  * Operator for writing Idxs to a stream
468  *
469  * @param index
470  * Index to write
471  * @return
472  * Reference to this TokenWriter
473  * @exception Exception
474  * - faudes::Exception ios error wrtiting file (id 2)
475  */
476  TokenWriter& operator << (const Idx index) {
477  WriteInteger(index);
478  return *this;
479  }
480 
481 
482  private:
483  /** Output mode */
485 
486  /** ostream object pointer*/
487  std::ostream* mpStream;
488 
489  /** Actual stream object, file output */
490  std::ofstream mFStream;
491 
492  /** Actual stream object, string output */
493  std::ostringstream mSStream;
494 
495  /** Actual stream object, stream output */
496  std::ostream* pSStream;
497 
498  /** Outputbuffer */
501 
502  /** Filename */
503  std::string mFileName;
504 
505  /** Number of columns */
506  int mColumns;
507 
508  /** Column counter */
510 
511  /** Endl seperator on/off */
512  bool mEndl;
513 
514  /** Xml doctype if in xml mode */
515  std::string mDocType;
516 
517  /** Flush internal buffer */
518  void DoFlush(bool clf=1);
519 
520 };
521 
522 } // namespace faudes
523 
524 #endif
525 
Compiletime options.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
Class Token.
Elementary type.
A TokenWriter writes sequential tokens to a file, a string or stdout.
bool StdoutMode(void) const
Test for console mode.
bool XmlMode(void) const
Test for xml file mode.
std::ostringstream mSStream
Actual stream object, string output.
bool mEndl
Endl seperator on/off.
std::ostream * mpStream
ostream object pointer
Mode mMode
Output mode.
std::string mFileName
Filename.
Token mOutBuffer
Outputbuffer.
Mode
Mode of operation: write to file, string or stdout.
int mColCount
Column counter.
int mColumns
Number of columns.
std::ostream * pSStream
Actual stream object, stream output.
bool FileMode(void) const
Test for file mode (incl.
std::string mDocType
Xml doctype if in xml mode.
std::ofstream mFStream
Actual stream object, file output.
Tokens model atomic data for stream IO.
Definition: cfl_token.h:53
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)

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