libFAUDES

Sections

Index

cfl_tokenwriter.h

Go to the documentation of this file.
00001 /** @file cfl_tokenwriter.h @brief Class TokenWriter */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005 Copyright (C) 2006  Bernd Opitz
00006 Exclusive copyright is granted to Klaus Schmidt
00007 
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Lesser General Public
00010 License as published by the Free Software Foundation; either
00011 version 2.1 of the License, or (at your option) any later version.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Lesser General Public License for more details.
00017 
00018 You should have received a copy of the GNU Lesser General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00021 
00022 
00023 #ifndef FAUDES_TOKENWRITER_H
00024 
00025 #include "cfl_definitions.h"
00026 #include "cfl_token.h"
00027 #include <algorithm>
00028 #include <string>
00029 #include <fstream>
00030 #include <iostream>
00031 
00032 namespace faudes {
00033 
00034 /**
00035  * A TokenWriter writes sequential tokens to a file, a string or stdout. It is the counterpart
00036  * of the TokenReader. Since wrtiting data comparatively straight foreward, there is no explicit 
00037  * support of sections etc. It is left to the calling function to organise the output.
00038  * 
00039  * @ingroup TokenIO
00040  */
00041 
00042 class TokenWriter {
00043 
00044  public:
00045 
00046   /**
00047    * Mode of operation: write to from file, string or stdout
00048    */
00049   enum Mode {File, XmlFile, Stdout, String};
00050 
00051   /**
00052    * Console or String TokenWriter constructor
00053    *
00054    * Technical detail: the console tokenwriter holds a ref. to std::cout; it will behave 
00055    * strange and perhaps cause segfaults if it is instantiated befor std::cout is
00056    * up; you can avoid this issue by instantiating you console tokenwriter using the 
00057    * new operator.
00058    * 
00059    *
00060    * @exception Exception
00061    *   - faudes::Exception ios error opening file (id 2)
00062    */
00063   TokenWriter(Mode mode);
00064 
00065   /** 
00066    * File TokenWriter constructor
00067    *
00068    * @param rFilename
00069    *   File to write
00070    * @param openmode
00071    *   std::ios::openmode
00072    * @exception Exception
00073    *   - faudes::Exception ios error opening file (id 2)
00074    */
00075   TokenWriter(const std::string& rFilename, 
00076         std::ios::openmode openmode = std::ios::out|std::ios::trunc);
00077 
00078   /** 
00079    * Xml File TokenWriter constructor
00080    *
00081    * @param rFilename
00082    *   File to write
00083    * @param doctype
00084    *   String to indicate XML doctype.
00085    * @exception Exception
00086    *   - faudes::Exception ios error opening file (id 2)
00087    */
00088   TokenWriter(const std::string& rFilename, const std::string& doctype);
00089 
00090   /** Destructor. Calls close */
00091   ~TokenWriter(void);
00092 
00093   /**
00094    * Get the filename.
00095    * Return dummy values for console or string mode.
00096    * 
00097    * @return 
00098    *   Filename
00099    */
00100   std::string FileName(void) const;
00101 
00102   /**
00103    * Flush any buffers.
00104    * 
00105    */
00106   void Flush(void);
00107 
00108   /**
00109    * Test for file mode (incl. XmlFile)
00110    * 
00111    * @return 
00112    *   Mode
00113    */
00114   bool FileMode(void) const { return mMode==File || mMode==XmlFile ;};
00115 
00116   /**
00117    * Test for xml file mode
00118    * 
00119    * @return 
00120    *   Mode
00121    */
00122   bool XmlMode(void) const { return mMode==XmlFile ;};
00123 
00124   /**
00125    * Test for file mode.
00126    * 
00127    * @return 
00128    *   Mode
00129    */
00130   bool StdoutMode(void) const { return mMode==Stdout;};
00131 
00132   /** 
00133    * Retrieve output as string (if in String mode)
00134    *
00135    * @exception Exception
00136    *   - faudes::Exception not in string mode (id 2)
00137    */
00138   std::string Str(void);
00139   
00140   /**
00141    * Get number of columns in a line
00142    *
00143    * @return
00144    *   # of columns in a line
00145    */
00146   int Columns(void) const;
00147 
00148   /**
00149    * Set number of columns in a line 
00150    *
00151    * @param columns
00152    *   # of columns in a line
00153    */
00154   void Columns(int columns);
00155 
00156   /**
00157    * Write endl separator
00158    *
00159    * @exception Exception
00160    *   - faudes::Exception ios error writing file (id 2)
00161    */
00162   void Endl(void);
00163 
00164   /**
00165    * Turn endl separator on/off
00166    *
00167    */
00168   void Endl(bool on);
00169 
00170   /**
00171    * Write next token
00172    *
00173    * @param rToken
00174    *   Token to write
00175    * @exception Exception
00176    *   - faudes::Exception ios error wrtiting file (id 2)
00177    */
00178   void Write(const Token& rToken);
00179 
00180   /**
00181    * Write string.
00182    *
00183    * Writes a std string token, i.e. enclosed in double quotes
00184    * any quotes in the string will be escaped, controls ignored.
00185    *
00186    * @param rString
00187    *   String to write
00188    * @exception Exception
00189    *   - faudes::Exception ios error wrtiting file (id 2)
00190    */
00191   void WriteString(const std::string& rString);
00192 
00193   /**
00194    * Write text section.
00195    *
00196    * Writes the specified string eclosed in begin/end tags.
00197    *
00198    * @param rLabel
00199    *   String to write
00200    * @param rText
00201    *   String to write
00202    * @exception Exception
00203    *   - faudes::Exception ios error wrtiting file (id 2)
00204    */
00205   void WriteText(const std::string& rLabel, const std::string& rText);
00206 
00207   /**
00208    * Write text section.
00209    *
00210    * Writes the specified string eclosed in begin/end tags.
00211    * Use this version to have attributes in the begin tag.
00212    *
00213    * @param rBeginTag
00214    *   Begin tag.
00215    * @param rText
00216    *   String to write
00217    * @exception Exception
00218    *   - faudes::Exception ios error wrtiting file (id 2)
00219    */
00220   void WriteText(const Token& rBeginTag, const std::string& rText);
00221 
00222   /**
00223    * Write string. 
00224    *
00225    * Writes string enclosed in verbatim markes __VERBATIM__, incl 
00226    * controls.
00227    *
00228    * @param rString
00229    *   String to write
00230    * @exception Exception
00231    *   - faudes::Exception ios error wrtiting file (id 2)
00232    *   - faudes::Exception tag is not a begin tag  (id 2)
00233    */
00234   void WriteVerbatim(const std::string& rString);
00235 
00236   /**
00237    * Write non negative integer 
00238    *
00239    * @param index
00240    *   Integer to write
00241    * @exception Exception
00242    *   - faudes::Exception ios error wrtiting file (id 2)
00243    */
00244   void WriteInteger(Idx index);
00245 
00246   /**
00247    * Write float
00248    *
00249    * @param val
00250    *   float to write
00251    * @exception Exception
00252    *   - faudes::Exception ios error wrtiting file (id 2)
00253    */
00254   void WriteFloat(const double& val);
00255 
00256   /**
00257    * Write integer as hex
00258    *
00259    * @param val
00260    *   Integer to write
00261    * @exception Exception
00262    *   - faudes::Exception ios error wrtiting file (id 2)
00263    */
00264   void WriteInteger16(long int val);
00265 
00266   /**
00267    * Write option (may not contain any "+")
00268    *
00269    * @param rOpt
00270    *   option to write
00271    * @exception Exception
00272    *   - faudes::Exception ios error wrtiting file (id 2)
00273    */
00274   void WriteOption(const std::string& rOpt);
00275 
00276   /**
00277    * Write begin label
00278    * 
00279    * @param rLabel
00280    *   End label, e.g. "Alphabet"
00281    * @exception Exception
00282    *   - faudes::Exception ios error wrtiting file (id 2)
00283    */
00284   void WriteBegin(const std::string& rLabel);
00285 
00286   /**
00287    * Write end label
00288    * 
00289    * @param rLabel
00290    *   End label, e.g. "Alphabet"
00291    * @exception Exception
00292    *   - faudes::Exception ios error wrtiting file (id 2)
00293    */
00294   void WriteEnd(const std::string& rLabel);
00295   
00296   /**
00297    * Write empty section label
00298    * 
00299    * @param rLabel
00300    *   End label, e.g. "Alphabet"
00301    * @exception Exception
00302    *   - faudes::Exception ios error wrtiting file (id 2)
00303    */
00304   void WriteEmpty(const std::string& rLabel);
00305 
00306   /**
00307    * Write comment in faudes format
00308    * 
00309    * @param rComment
00310    *   Comment to write
00311    * @exception Exception
00312    *   - faudes::Exception ios error wrtiting file (id 2)
00313    */
00314   void WriteComment(const std::string& rComment);
00315 
00316   /**
00317    * Write comment in Xml format
00318    * 
00319    * @param rComment
00320    *   Comment to write
00321    * @exception Exception
00322    *   - faudes::Exception ios error wrtiting file (id 2)
00323    */
00324   void WriteXmlComment(const std::string& rComment);
00325 
00326   /**
00327    * Write comment
00328    * 
00329    * @param len
00330    *   Number of bytes to write
00331    * @param pData
00332    *   Data to write
00333    * @exception Exception
00334    *   - faudes::Exception ios error wrtiting file (id 2)
00335    */
00336   void WriteBinary(const char* pData, long int len);
00337 
00338   /** 
00339    * Operator for writing tokens
00340    *
00341    * @param rToken
00342    *   Token to write
00343    * @return
00344    *   Reference to this TokenWriter
00345    * @exception Exception
00346    *   - faudes::Exception ios error wrtiting file (id 2)
00347    */
00348   TokenWriter& operator << (Token& rToken) {
00349     Write(rToken);
00350     return *this;
00351   }
00352 
00353   /** 
00354    * Operator for writing std::strings to a stream 
00355    *
00356    * @param rString
00357    *   String to write
00358    * @return
00359    *   Reference to this TokenWriter
00360    * @exception Exception
00361    *   - faudes::Exception ios error wrtiting file (id 2)
00362    */
00363   TokenWriter& operator << (const std::string& rString) {
00364     WriteString(rString);
00365     return *this;
00366   }
00367 
00368   /** 
00369    * Operator for writing Idxs to a stream
00370    *
00371    * @param index
00372    *   Index to write
00373    * @return
00374    *   Reference to this TokenWriter
00375    * @exception Exception
00376    *   - faudes::Exception ios error wrtiting file (id 2)
00377    */
00378   TokenWriter& operator << (const Idx index) {
00379     WriteInteger(index);
00380     return *this;
00381   }
00382 
00383 
00384  private:
00385   /** Output mode */
00386   Mode mMode;
00387 
00388   /** ostream object pointer*/
00389   std::ostream* mpStream;
00390 
00391   /** Actual stream object, file output */
00392   std::ofstream mFStream;
00393 
00394   /** Actual stream object, string output */
00395   std::ostringstream mSStream;
00396 
00397   /** Filename */
00398   std::string mFileName;
00399 
00400   /** Number of columns */
00401   int mColumns;
00402 
00403   /** Column counter */
00404   int mColCount;
00405 
00406   /** Endl seperator on/off */
00407   bool mEndl;
00408 
00409   /** Xml doctype if in xml mode */
00410   std::string  mDocType;
00411 
00412 
00413 };
00414 
00415 } // namespace faudes
00416 
00417 #define FAUDES_TOKENWRITER_H
00418 #endif 
00419 

libFAUDES 2.18b --- 2010-12-17 --- c++ source docu by doxygen 1.6.3