| |
libFAUDES
Sections
Index
|
tokenwriter.hGo to the documentation of this file.00001 /** @file 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 "definitions.h" 00026 #include "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 * Get file mode. 00088 * 00089 * @return 00090 * Mode 00091 */ 00092 Mode DestMode(void) const { return mMode;}; 00093 00094 /** 00095 * Retrieve output as string (if in String mode) 00096 * 00097 * @exception Exception 00098 * - faudes::Exception not in string mode (id 2) 00099 */ 00100 std::string Str(void); 00101 00102 /** 00103 * Get number of columns in a line 00104 * 00105 * @return 00106 * # of columns in a line 00107 */ 00108 int Columns(void) const; 00109 00110 /** 00111 * Set number of columns in a line 00112 * 00113 * @param columns 00114 * # of columns in a line 00115 */ 00116 void Columns(int columns); 00117 00118 /** 00119 * Write endl separator 00120 * 00121 * @exception Exception 00122 * - faudes::Exception ios error writing file (id 2) 00123 */ 00124 void Endl(void); 00125 00126 /** 00127 * Turn endl separator on/off 00128 * 00129 */ 00130 void Endl(bool on); 00131 00132 /** 00133 * Write next token 00134 * 00135 * @param rToken 00136 * Token to write 00137 * @exception Exception 00138 * - faudes::Exception ios error wrtiting file (id 2) 00139 */ 00140 void Write(Token& rToken); 00141 00142 /** 00143 * Write string (may not contain any quotes 00144 * 00145 * @param rString 00146 * String to write 00147 * @exception Exception 00148 * - faudes::Exception ios error wrtiting file (id 2) 00149 */ 00150 void WriteString(const std::string& rString); 00151 00152 /** 00153 * Write non negative integer 00154 * 00155 * @param index 00156 * Integer to write 00157 * @exception Exception 00158 * - faudes::Exception ios error wrtiting file (id 2) 00159 */ 00160 void WriteInteger(Idx index); 00161 00162 /** 00163 * Write float 00164 * 00165 * @param val 00166 * float to write 00167 * @exception Exception 00168 * - faudes::Exception ios error wrtiting file (id 2) 00169 */ 00170 void WriteFloat(const double& val); 00171 00172 /** 00173 * Write integer as hex 00174 * 00175 * @param val 00176 * Integer to write 00177 * @exception Exception 00178 * - faudes::Exception ios error wrtiting file (id 2) 00179 */ 00180 void WriteInteger16(long int val); 00181 00182 /** 00183 * Write option (may not contain any "+") 00184 * 00185 * @param rOpt 00186 * option to write 00187 * @exception Exception 00188 * - faudes::Exception ios error wrtiting file (id 2) 00189 */ 00190 void WriteOption(const std::string& rOpt); 00191 00192 /** 00193 * Write begin label 00194 * 00195 * @param rLabel 00196 * End label, e.g. "Alphabet" 00197 * @exception Exception 00198 * - faudes::Exception ios error wrtiting file (id 2) 00199 */ 00200 void WriteBegin(const std::string& rLabel); 00201 00202 /** 00203 * Write end label 00204 * 00205 * @param rLabel 00206 * End label, e.g. "Alphabet" 00207 * @exception Exception 00208 * - faudes::Exception ios error wrtiting file (id 2) 00209 */ 00210 void WriteEnd(const std::string& rLabel); 00211 00212 /** 00213 * Write comment 00214 * 00215 * @param rComment 00216 * Comment to write 00217 * @exception Exception 00218 * - faudes::Exception ios error wrtiting file (id 2) 00219 */ 00220 void WriteComment(const std::string& rComment); 00221 00222 /** 00223 * Write comment 00224 * 00225 * @param len 00226 * Number of bytes to write 00227 * @param pData 00228 * Data to write 00229 * @exception Exception 00230 * - faudes::Exception ios error wrtiting file (id 2) 00231 */ 00232 void WriteBinary(const char* pData, long int len); 00233 00234 /** 00235 * Operator for writing tokens 00236 * 00237 * @param rToken 00238 * Token to write 00239 * @return 00240 * Reference to this TokenWriter 00241 * @exception Exception 00242 * - faudes::Exception ios error wrtiting file (id 2) 00243 */ 00244 TokenWriter& operator << (Token& rToken) { 00245 Write(rToken); 00246 return *this; 00247 } 00248 00249 /** 00250 * Operator for writing std::strings to a stream 00251 * 00252 * @param rString 00253 * String to write 00254 * @return 00255 * Reference to this TokenWriter 00256 * @exception Exception 00257 * - faudes::Exception ios error wrtiting file (id 2) 00258 */ 00259 TokenWriter& operator << (const std::string& rString) { 00260 WriteString(rString); 00261 return *this; 00262 } 00263 00264 /** 00265 * Operator for writing Idxs to a stream 00266 * 00267 * @param index 00268 * Index to write 00269 * @return 00270 * Reference to this TokenWriter 00271 * @exception Exception 00272 * - faudes::Exception ios error wrtiting file (id 2) 00273 */ 00274 TokenWriter& operator << (const Idx index) { 00275 WriteInteger(index); 00276 return *this; 00277 } 00278 00279 00280 private: 00281 /** Output mode */ 00282 Mode mMode; 00283 00284 /** ostream object pointer*/ 00285 std::ostream* mpStream; 00286 00287 /** Actual stream object, file output */ 00288 std::ofstream mFStream; 00289 00290 /** Actual stream object, string output */ 00291 std::ostringstream mSStream; 00292 00293 /** Filename */ 00294 std::string mFileName; 00295 00296 /** Number of columns */ 00297 int mColumns; 00298 00299 /** Column counter */ 00300 int mColCount; 00301 00302 /** Endl seperator on/off */ 00303 bool mEndl; 00304 00305 }; 00306 00307 } // namespace faudes 00308 00309 #define FAUDES_TOKENWRITER_H 00310 #endif 00311 |
libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6