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

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen