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 <fstream>
00028 #include <iostream>
00029 
00030 namespace faudes {
00031 
00032 /**
00033  * A TokenWriter writes sequential tokens to a file, a string or stdout. It is the counterpart
00034  * of the TokenReader. Since wrtiting data comparatively straight foreward, there is no explicit 
00035  * support of sections etc. It is left to the calling function to organise the output. 
00036  */
00037 
00038 class TokenWriter {
00039 
00040  public:
00041 
00042   /**
00043    * Mode of operation: write to from file, string or stdout
00044    */
00045   enum Mode {File, Stdout, String};
00046 
00047   /**
00048    * Console or String TokenWriter constructor
00049    *
00050    * Technical detail: the console tokenwriter holds a ref. to std::cout; it will behave 
00051    * strange and perhaps cause segfaults if it is instantiated befor std::cout is
00052    * up; you can avoid this issue by instantiating you console tokenwriter using the 
00053    * new operator.
00054    * 
00055    *
00056    * @exception Exception
00057    *   - faudes::Exception ios error opening file (id 2)
00058    */
00059   TokenWriter(Mode mode);
00060 
00061   /** 
00062    * File TokenWriter constructor
00063    *
00064    * @param rFilename
00065    *   File to write
00066    * @param openmode
00067    *   std::ios::openmode
00068    * @exception Exception
00069    *   - faudes::Exception ios error opening file (id 2)
00070    */
00071   TokenWriter(const std::string& rFilename, 
00072         std::ios::openmode openmode = std::ios::out|std::ios::trunc);
00073 
00074   /** Destructor. Calls close */
00075   ~TokenWriter(void);
00076 
00077   /**
00078    * Get the filename.
00079    * Return dummy values for console or string mode.
00080    * 
00081    * @return 
00082    *   Filename
00083    */
00084   std::string FileName(void) const;
00085 
00086   /**
00087    * Flush any buffers.
00088    * 
00089    */
00090   void Flush(void);
00091 
00092   /**
00093    * Get file mode.
00094    * 
00095    * @return 
00096    *   Mode
00097    */
00098   Mode DestMode(void) const { return mMode;};
00099 
00100   /** 
00101    * Retrieve output as string (if in String mode)
00102    *
00103    * @exception Exception
00104    *   - faudes::Exception not in string mode (id 2)
00105    */
00106   std::string Str(void);
00107   
00108   /**
00109    * Get number of columns in a line
00110    *
00111    * @return
00112    *   # of columns in a line
00113    */
00114   int Columns(void) const;
00115 
00116   /**
00117    * Set number of columns in a line 
00118    *
00119    * @param columns
00120    *   # of columns in a line
00121    */
00122   void Columns(int columns);
00123 
00124   /**
00125    * Write endl separator
00126    *
00127    * @exception Exception
00128    *   - faudes::Exception ios error writing file (id 2)
00129    */
00130   void Endl(void);
00131 
00132   /**
00133    * Turn endl separator on/off
00134    *
00135    */
00136   void Endl(bool on);
00137 
00138   /**
00139    * Write next token
00140    *
00141    * @param rToken
00142    *   Token to write
00143    * @exception Exception
00144    *   - faudes::Exception ios error wrtiting file (id 2)
00145    */
00146   void Write(Token& rToken);
00147 
00148   /**
00149    * Write string.
00150    *
00151    * Writes a std string token, i.e. enclosed in double quotes
00152    * any quotes in the string will be escaped, controls ignored.
00153    *
00154    * @param rString
00155    *   String to write
00156    * @exception Exception
00157    *   - faudes::Exception ios error wrtiting file (id 2)
00158    */
00159   void WriteString(const std::string& rString);
00160 
00161   /**
00162    * Write string. 
00163    *
00164    * Writes string enclosed in verbatim markes __VERBATIM__, incl 
00165    * controls.
00166    *
00167    * @param rString
00168    *   String to write
00169    * @exception Exception
00170    *   - faudes::Exception ios error wrtiting file (id 2)
00171    */
00172   void WriteVerbatim(const std::string& rString);
00173 
00174   /**
00175    * Write non negative integer 
00176    *
00177    * @param index
00178    *   Integer to write
00179    * @exception Exception
00180    *   - faudes::Exception ios error wrtiting file (id 2)
00181    */
00182   void WriteInteger(Idx index);
00183 
00184   /**
00185    * Write float
00186    *
00187    * @param val
00188    *   float to write
00189    * @exception Exception
00190    *   - faudes::Exception ios error wrtiting file (id 2)
00191    */
00192   void WriteFloat(const double& val);
00193 
00194   /**
00195    * Write integer as hex
00196    *
00197    * @param val
00198    *   Integer to write
00199    * @exception Exception
00200    *   - faudes::Exception ios error wrtiting file (id 2)
00201    */
00202   void WriteInteger16(long int val);
00203 
00204   /**
00205    * Write option (may not contain any "+")
00206    *
00207    * @param rOpt
00208    *   option to write
00209    * @exception Exception
00210    *   - faudes::Exception ios error wrtiting file (id 2)
00211    */
00212   void WriteOption(const std::string& rOpt);
00213 
00214   /**
00215    * Write begin label
00216    * 
00217    * @param rLabel
00218    *   End label, e.g. "Alphabet"
00219    * @exception Exception
00220    *   - faudes::Exception ios error wrtiting file (id 2)
00221    */
00222   void WriteBegin(const std::string& rLabel);
00223 
00224   /**
00225    * Write end label
00226    * 
00227    * @param rLabel
00228    *   End label, e.g. "Alphabet"
00229    * @exception Exception
00230    *   - faudes::Exception ios error wrtiting file (id 2)
00231    */
00232   void WriteEnd(const std::string& rLabel);
00233   
00234   /**
00235    * Write comment
00236    * 
00237    * @param rComment
00238    *   Comment to write
00239    * @exception Exception
00240    *   - faudes::Exception ios error wrtiting file (id 2)
00241    */
00242   void WriteComment(const std::string& rComment);
00243 
00244   /**
00245    * Write comment
00246    * 
00247    * @param len
00248    *   Number of bytes to write
00249    * @param pData
00250    *   Data to write
00251    * @exception Exception
00252    *   - faudes::Exception ios error wrtiting file (id 2)
00253    */
00254   void WriteBinary(const char* pData, long int len);
00255 
00256   /** 
00257    * Operator for writing tokens
00258    *
00259    * @param rToken
00260    *   Token to write
00261    * @return
00262    *   Reference to this TokenWriter
00263    * @exception Exception
00264    *   - faudes::Exception ios error wrtiting file (id 2)
00265    */
00266   TokenWriter& operator << (Token& rToken) {
00267     Write(rToken);
00268     return *this;
00269   }
00270 
00271   /** 
00272    * Operator for writing std::strings to a stream 
00273    *
00274    * @param rString
00275    *   String to write
00276    * @return
00277    *   Reference to this TokenWriter
00278    * @exception Exception
00279    *   - faudes::Exception ios error wrtiting file (id 2)
00280    */
00281   TokenWriter& operator << (const std::string& rString) {
00282     WriteString(rString);
00283     return *this;
00284   }
00285 
00286   /** 
00287    * Operator for writing Idxs to a stream
00288    *
00289    * @param index
00290    *   Index to write
00291    * @return
00292    *   Reference to this TokenWriter
00293    * @exception Exception
00294    *   - faudes::Exception ios error wrtiting file (id 2)
00295    */
00296   TokenWriter& operator << (const Idx index) {
00297     WriteInteger(index);
00298     return *this;
00299   }
00300 
00301 
00302  private:
00303   /** Output mode */
00304   Mode mMode;
00305 
00306   /** ostream object pointer*/
00307   std::ostream* mpStream;
00308 
00309   /** Actual stream object, file output */
00310   std::ofstream mFStream;
00311 
00312   /** Actual stream object, string output */
00313   std::ostringstream mSStream;
00314 
00315   /** Filename */
00316   std::string mFileName;
00317 
00318   /** Number of columns */
00319   int mColumns;
00320 
00321   /** Column counter */
00322   int mColCount;
00323 
00324   /** Endl seperator on/off */
00325   bool mEndl;
00326 
00327 };
00328 
00329 } // namespace faudes
00330 
00331 #define FAUDES_TOKENWRITER_H
00332 #endif 
00333 

libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3