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 
25 #include "cfl_definitions.h"
26 #include "cfl_token.h"
27 #include <algorithm>
28 #include <string>
29 #include <fstream>
30 #include <iostream>
31 
32 namespace faudes {
33 
34 /**
35  * A TokenWriter writes sequential tokens to a file, a string or stdout. It is the counterpart
36  * of the TokenReader. Since wrtiting data comparatively straight foreward, there is no explicit
37  * support of sections etc. It is left to the calling function to organise the output.
38  *
39  * @ingroup TokenIO
40  */
41 
42 class TokenWriter {
43 
44  public:
45 
46  /**
47  * Mode of operation: write to file, string or stdout
48  */
50 
51  /**
52  * Console or String TokenWriter constructor
53  *
54  * Technical detail: Stdout mode uses the global console object
55  * ConsoleOut::G() declared in cfl_helper.h.
56  *
57  * @exception Exception
58  * - faudes::Exception ios error opening file (id 2)
59  */
60  TokenWriter(Mode mode);
61 
62  /**
63  * File TokenWriter constructor
64  *
65  * @param rFilename
66  * File to write
67  * @param openmode
68  * std::ios::openmode
69  * @exception Exception
70  * - faudes::Exception ios error opening file (id 2)
71  */
72  TokenWriter(const std::string& rFilename,
73  std::ios::openmode openmode = std::ios::out|std::ios::trunc);
74 
75  /**
76  * Xml File TokenWriter constructor
77  *
78  * @param rFilename
79  * File to write
80  * @param doctype
81  * String to indicate XML doctype.
82  * @exception Exception
83  * - faudes::Exception ios error opening file (id 2)
84  */
85  TokenWriter(const std::string& rFilename, const std::string& doctype);
86 
87  /**
88  * Stream TokenWriter constructor
89  *
90  * @param rStream
91  * stream C++ stream to write to
92  * @param doctype
93  * String to indicate XML doctype.
94  * @exception Exception
95  * - faudes::Exception ios error opening file (id 2)
96  */
97  TokenWriter(std::ostream& rStream, const std::string& doctype="");
98 
99  /** Destructor. Calls close */
100  ~TokenWriter(void);
101 
102  /**
103  * Get the filename.
104  * Return dummy values for console or string mode.
105  *
106  * @return
107  * Filename
108  */
109  std::string FileName(void) const;
110 
111  /**
112  * Flush any buffers.
113  *
114  */
115  void Flush(void);
116 
117  /**
118  * Test for file mode (incl. XmlFile)
119  *
120  * @return
121  * Mode
122  */
123  bool FileMode(void) const { return mMode==File || mMode==XmlFile ;};
124 
125  /**
126  * Test for xml file mode
127  *
128  * @return
129  * Mode
130  */
131  bool XmlMode(void) const { return mMode==XmlFile ;};
132 
133  /**
134  * Test for file mode.
135  *
136  * @return
137  * Mode
138  */
139  bool StdoutMode(void) const { return mMode==Stdout;};
140 
141  /**
142  * Retrieve output as string (if in String mode)
143  *
144  * @exception Exception
145  * - faudes::Exception not in string mode (id 2)
146  */
147  std::string Str(void);
148 
149  /**
150  * Access C++ stream
151  *
152  */
153  std::ostream* Streamp(void);
154 
155  /**
156  * Get number of columns in a line
157  *
158  * @return
159  * # of columns in a line
160  */
161  int Columns(void) const;
162 
163  /**
164  * Set number of columns in a line
165  *
166  * @param columns
167  * # of columns in a line
168  */
169  void Columns(int columns);
170 
171  /**
172  * Write endl separator
173  *
174  * @exception Exception
175  * - faudes::Exception ios error writing file (id 2)
176  */
177  void Endl(void);
178 
179  /**
180  * Turn endl separator on/off
181  *
182  */
183  void Endl(bool on);
184 
185  /**
186  * Write next token
187  *
188  * @param rToken
189  * Token to write
190  * @exception Exception
191  * - faudes::Exception ios error wrtiting file (id 2)
192  */
193  void Write(const Token& rToken);
194 
195  /**
196  * Write string.
197  *
198  * Writes a std string token, i.e. enclosed in double quotes
199  * any quotes in the string will be escaped, controls ignored.
200  *
201  * @param rString
202  * String to write
203  * @exception Exception
204  * - faudes::Exception ios error wrtiting file (id 2)
205  */
206  void WriteString(const std::string& rString);
207 
208  /**
209  * Write text.
210  *
211  * Writes the specified string. Relevant enteties are escaped.
212  *
213  * @param rText
214  * String to write
215  * @exception Exception
216  * - faudes::Exception ios error wrtiting file (id 2)
217  */
218  void WriteText(const std::string& rText);
219 
220  /**
221  * Write text section.
222  *
223  * Writes the specified string eclosed in begin/end tags.
224  *
225  * @param rLabel
226  * String to write
227  * @param rText
228  * String to write
229  * @exception Exception
230  * - faudes::Exception ios error wrtiting file (id 2)
231  */
232  void WriteText(const std::string& rLabel, const std::string& rText);
233 
234  /**
235  * Write text section.
236  *
237  * Writes the specified string eclosed in begin/end tags.
238  * Use this version to have attributes in the begin tag.
239  *
240  * @param rBeginTag
241  * Begin tag.
242  * @param rText
243  * String to write
244  * @exception Exception
245  * - faudes::Exception ios error wrtiting file (id 2)
246  */
247  void WriteText(const Token& rBeginTag, const std::string& rText);
248 
249  /**
250  * Write character data
251  *
252  * Writes the specified string as it is. Thus, relevant enteties
253  * must be escaped befrand.
254  *
255  * @param rCharData
256  * String to write
257  * @exception Exception
258  * - faudes::Exception ios error wrtiting file (id 2)
259  */
260  void WriteCharacterData(const std::string& rCharData);
261 
262  /**
263  * Write string.
264  *
265  * Writes string enclosed in verbatim markes __VERBATIM__, incl
266  * controls.
267  *
268  * @param rString
269  * String to write
270  * @exception Exception
271  * - faudes::Exception ios error wrtiting file (id 2)
272  * - faudes::Exception tag is not a begin tag (id 2)
273  */
274  void WriteVerbatim(const std::string& rString);
275 
276  /**
277  * Write non negative integer
278  *
279  * @param index
280  * Integer to write
281  * @exception Exception
282  * - faudes::Exception ios error wrtiting file (id 2)
283  */
284  void WriteInteger(Idx index);
285 
286  /**
287  * Write float
288  *
289  * @param val
290  * float to write
291  * @exception Exception
292  * - faudes::Exception ios error wrtiting file (id 2)
293  */
294  void WriteFloat(const double& val);
295 
296  /**
297  * Write integer as hex
298  *
299  * @param val
300  * Integer to write
301  * @exception Exception
302  * - faudes::Exception ios error wrtiting file (id 2)
303  */
304  void WriteInteger16(long int val);
305 
306  /**
307  * Write option (may not contain any "+")
308  *
309  * @param rOpt
310  * option to write
311  * @exception Exception
312  * - faudes::Exception ios error wrtiting file (id 2)
313  */
314  void WriteOption(const std::string& rOpt);
315 
316  /**
317  * Write begin label
318  *
319  * @param rLabel
320  * End label, e.g. "Alphabet"
321  * @exception Exception
322  * - faudes::Exception ios error wrtiting file (id 2)
323  */
324  void WriteBegin(const std::string& rLabel);
325 
326  /**
327  * Write end label
328  *
329  * @param rLabel
330  * End label, e.g. "Alphabet"
331  * @exception Exception
332  * - faudes::Exception ios error wrtiting file (id 2)
333  */
334  void WriteEnd(const std::string& rLabel);
335 
336  /**
337  * Write empty section label
338  *
339  * @param rLabel
340  * End label, e.g. "Alphabet"
341  * @exception Exception
342  * - faudes::Exception ios error wrtiting file (id 2)
343  */
344  void WriteEmpty(const std::string& rLabel);
345 
346  /**
347  * Write comment in faudes format
348  *
349  * @param rComment
350  * Comment to write
351  * @exception Exception
352  * - faudes::Exception ios error wrtiting file (id 2)
353  */
354  void WriteComment(const std::string& rComment);
355 
356  /**
357  * Write comment in Xml format
358  *
359  * @param rComment
360  * Comment to write
361  * @exception Exception
362  * - faudes::Exception ios error wrtiting file (id 2)
363  */
364  void WriteXmlComment(const std::string& rComment);
365 
366  /**
367  * Write comment
368  *
369  * @param len
370  * Number of bytes to write
371  * @param pData
372  * Data to write
373  * @exception Exception
374  * - faudes::Exception ios error wrtiting file (id 2)
375  */
376  void WriteBinary(const char* pData, long int len);
377 
378  /**
379  * Operator for writing tokens
380  *
381  * @param rToken
382  * Token to write
383  * @return
384  * Reference to this TokenWriter
385  * @exception Exception
386  * - faudes::Exception ios error wrtiting file (id 2)
387  */
389  Write(rToken);
390  return *this;
391  }
392 
393  /**
394  * Operator for writing std::strings to a stream
395  *
396  * @param rString
397  * String to write
398  * @return
399  * Reference to this TokenWriter
400  * @exception Exception
401  * - faudes::Exception ios error wrtiting file (id 2)
402  */
403  TokenWriter& operator << (const std::string& rString) {
404  WriteString(rString);
405  return *this;
406  }
407 
408  /**
409  * Operator for writing Idxs to a stream
410  *
411  * @param index
412  * Index to write
413  * @return
414  * Reference to this TokenWriter
415  * @exception Exception
416  * - faudes::Exception ios error wrtiting file (id 2)
417  */
418  TokenWriter& operator << (const Idx index) {
419  WriteInteger(index);
420  return *this;
421  }
422 
423 
424  private:
425  /** Output mode */
427 
428  /** ostream object pointer*/
429  std::ostream* mpStream;
430 
431  /** Actual stream object, file output */
432  std::ofstream mFStream;
433 
434  /** Actual stream object, string output */
435  std::ostringstream mSStream;
436 
437  /** Actual stream object, stream output */
438  std::ostream* pSStream;
439 
440  /** Outputbuffer */
443 
444  /** Filename */
445  std::string mFileName;
446 
447  /** Number of columns */
448  int mColumns;
449 
450  /** Column counter */
452 
453  /** Endl seperator on/off */
454  bool mEndl;
455 
456  /** Xml doctype if in xml mode */
457  std::string mDocType;
458 
459  /** Flush internal buffer */
460  void DoFlush(void);
461 
462 };
463 
464 } // namespace faudes
465 
466 #define FAUDES_TOKENWRITER_H
467 #endif
468 

libFAUDES 2.24g --- 2014.09.15 --- c++ api documentaion by doxygen