libFAUDES
Sections
Index
|
cfl_tokenwriter.hGo 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, Stream, XmlStream}; 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 /** 00091 * Stream TokenWriter constructor 00092 * 00093 * @param rStream 00094 * stream C++ stream to write to 00095 * @param doctype 00096 * String to indicate XML doctype. 00097 * @exception Exception 00098 * - faudes::Exception ios error opening file (id 2) 00099 */ 00100 TokenWriter(std::ostream& rStream, const std::string& doctype=""); 00101 00102 /** Destructor. Calls close */ 00103 ~TokenWriter(void); 00104 00105 /** 00106 * Get the filename. 00107 * Return dummy values for console or string mode. 00108 * 00109 * @return 00110 * Filename 00111 */ 00112 std::string FileName(void) const; 00113 00114 /** 00115 * Flush any buffers. 00116 * 00117 */ 00118 void Flush(void); 00119 00120 /** 00121 * Test for file mode (incl. XmlFile) 00122 * 00123 * @return 00124 * Mode 00125 */ 00126 bool FileMode(void) const { return mMode==File || mMode==XmlFile ;}; 00127 00128 /** 00129 * Test for xml file mode 00130 * 00131 * @return 00132 * Mode 00133 */ 00134 bool XmlMode(void) const { return mMode==XmlFile ;}; 00135 00136 /** 00137 * Test for file mode. 00138 * 00139 * @return 00140 * Mode 00141 */ 00142 bool StdoutMode(void) const { return mMode==Stdout;}; 00143 00144 /** 00145 * Retrieve output as string (if in String mode) 00146 * 00147 * @exception Exception 00148 * - faudes::Exception not in string mode (id 2) 00149 */ 00150 std::string Str(void); 00151 00152 /** 00153 * Access C++ stream 00154 * 00155 */ 00156 std::ostream* Streamp(void); 00157 00158 /** 00159 * Get number of columns in a line 00160 * 00161 * @return 00162 * # of columns in a line 00163 */ 00164 int Columns(void) const; 00165 00166 /** 00167 * Set number of columns in a line 00168 * 00169 * @param columns 00170 * # of columns in a line 00171 */ 00172 void Columns(int columns); 00173 00174 /** 00175 * Write endl separator 00176 * 00177 * @exception Exception 00178 * - faudes::Exception ios error writing file (id 2) 00179 */ 00180 void Endl(void); 00181 00182 /** 00183 * Turn endl separator on/off 00184 * 00185 */ 00186 void Endl(bool on); 00187 00188 /** 00189 * Write next token 00190 * 00191 * @param rToken 00192 * Token to write 00193 * @exception Exception 00194 * - faudes::Exception ios error wrtiting file (id 2) 00195 */ 00196 void Write(const Token& rToken); 00197 00198 /** 00199 * Write string. 00200 * 00201 * Writes a std string token, i.e. enclosed in double quotes 00202 * any quotes in the string will be escaped, controls ignored. 00203 * 00204 * @param rString 00205 * String to write 00206 * @exception Exception 00207 * - faudes::Exception ios error wrtiting file (id 2) 00208 */ 00209 void WriteString(const std::string& rString); 00210 00211 /** 00212 * Write text. 00213 * 00214 * Writes the specified string. Relevant enteties are escaped. 00215 * 00216 * @param rText 00217 * String to write 00218 * @exception Exception 00219 * - faudes::Exception ios error wrtiting file (id 2) 00220 */ 00221 void WriteText(const std::string& rText); 00222 00223 /** 00224 * Write text section. 00225 * 00226 * Writes the specified string eclosed in begin/end tags. 00227 * 00228 * @param rLabel 00229 * String to write 00230 * @param rText 00231 * String to write 00232 * @exception Exception 00233 * - faudes::Exception ios error wrtiting file (id 2) 00234 */ 00235 void WriteText(const std::string& rLabel, const std::string& rText); 00236 00237 /** 00238 * Write text section. 00239 * 00240 * Writes the specified string eclosed in begin/end tags. 00241 * Use this version to have attributes in the begin tag. 00242 * 00243 * @param rBeginTag 00244 * Begin tag. 00245 * @param rText 00246 * String to write 00247 * @exception Exception 00248 * - faudes::Exception ios error wrtiting file (id 2) 00249 */ 00250 void WriteText(const Token& rBeginTag, const std::string& rText); 00251 00252 /** 00253 * Write character data 00254 * 00255 * Writes the specified string as it is. Thus, relevant enteties 00256 * must be escaped befrand. 00257 * 00258 * @param rCharData 00259 * String to write 00260 * @exception Exception 00261 * - faudes::Exception ios error wrtiting file (id 2) 00262 */ 00263 void WriteCharacterData(const std::string& rCharData); 00264 00265 /** 00266 * Write string. 00267 * 00268 * Writes string enclosed in verbatim markes __VERBATIM__, incl 00269 * controls. 00270 * 00271 * @param rString 00272 * String to write 00273 * @exception Exception 00274 * - faudes::Exception ios error wrtiting file (id 2) 00275 * - faudes::Exception tag is not a begin tag (id 2) 00276 */ 00277 void WriteVerbatim(const std::string& rString); 00278 00279 /** 00280 * Write non negative integer 00281 * 00282 * @param index 00283 * Integer to write 00284 * @exception Exception 00285 * - faudes::Exception ios error wrtiting file (id 2) 00286 */ 00287 void WriteInteger(Idx index); 00288 00289 /** 00290 * Write float 00291 * 00292 * @param val 00293 * float to write 00294 * @exception Exception 00295 * - faudes::Exception ios error wrtiting file (id 2) 00296 */ 00297 void WriteFloat(const double& val); 00298 00299 /** 00300 * Write integer as hex 00301 * 00302 * @param val 00303 * Integer to write 00304 * @exception Exception 00305 * - faudes::Exception ios error wrtiting file (id 2) 00306 */ 00307 void WriteInteger16(long int val); 00308 00309 /** 00310 * Write option (may not contain any "+") 00311 * 00312 * @param rOpt 00313 * option to write 00314 * @exception Exception 00315 * - faudes::Exception ios error wrtiting file (id 2) 00316 */ 00317 void WriteOption(const std::string& rOpt); 00318 00319 /** 00320 * Write begin label 00321 * 00322 * @param rLabel 00323 * End label, e.g. "Alphabet" 00324 * @exception Exception 00325 * - faudes::Exception ios error wrtiting file (id 2) 00326 */ 00327 void WriteBegin(const std::string& rLabel); 00328 00329 /** 00330 * Write end label 00331 * 00332 * @param rLabel 00333 * End label, e.g. "Alphabet" 00334 * @exception Exception 00335 * - faudes::Exception ios error wrtiting file (id 2) 00336 */ 00337 void WriteEnd(const std::string& rLabel); 00338 00339 /** 00340 * Write empty section label 00341 * 00342 * @param rLabel 00343 * End label, e.g. "Alphabet" 00344 * @exception Exception 00345 * - faudes::Exception ios error wrtiting file (id 2) 00346 */ 00347 void WriteEmpty(const std::string& rLabel); 00348 00349 /** 00350 * Write comment in faudes format 00351 * 00352 * @param rComment 00353 * Comment to write 00354 * @exception Exception 00355 * - faudes::Exception ios error wrtiting file (id 2) 00356 */ 00357 void WriteComment(const std::string& rComment); 00358 00359 /** 00360 * Write comment in Xml format 00361 * 00362 * @param rComment 00363 * Comment to write 00364 * @exception Exception 00365 * - faudes::Exception ios error wrtiting file (id 2) 00366 */ 00367 void WriteXmlComment(const std::string& rComment); 00368 00369 /** 00370 * Write comment 00371 * 00372 * @param len 00373 * Number of bytes to write 00374 * @param pData 00375 * Data to write 00376 * @exception Exception 00377 * - faudes::Exception ios error wrtiting file (id 2) 00378 */ 00379 void WriteBinary(const char* pData, long int len); 00380 00381 /** 00382 * Operator for writing tokens 00383 * 00384 * @param rToken 00385 * Token to write 00386 * @return 00387 * Reference to this TokenWriter 00388 * @exception Exception 00389 * - faudes::Exception ios error wrtiting file (id 2) 00390 */ 00391 TokenWriter& operator << (Token& rToken) { 00392 Write(rToken); 00393 return *this; 00394 } 00395 00396 /** 00397 * Operator for writing std::strings to a stream 00398 * 00399 * @param rString 00400 * String to write 00401 * @return 00402 * Reference to this TokenWriter 00403 * @exception Exception 00404 * - faudes::Exception ios error wrtiting file (id 2) 00405 */ 00406 TokenWriter& operator << (const std::string& rString) { 00407 WriteString(rString); 00408 return *this; 00409 } 00410 00411 /** 00412 * Operator for writing Idxs to a stream 00413 * 00414 * @param index 00415 * Index to write 00416 * @return 00417 * Reference to this TokenWriter 00418 * @exception Exception 00419 * - faudes::Exception ios error wrtiting file (id 2) 00420 */ 00421 TokenWriter& operator << (const Idx index) { 00422 WriteInteger(index); 00423 return *this; 00424 } 00425 00426 00427 private: 00428 /** Output mode */ 00429 Mode mMode; 00430 00431 /** ostream object pointer*/ 00432 std::ostream* mpStream; 00433 00434 /** Actual stream object, file output */ 00435 std::ofstream mFStream; 00436 00437 /** Actual stream object, string output */ 00438 std::ostringstream mSStream; 00439 00440 /** Actual stream object, stream output */ 00441 std::ostream* pSStream; 00442 00443 /** Outputbuffer */ 00444 Token mOutBuffer; 00445 bool mHasOutBuffer; 00446 00447 /** Filename */ 00448 std::string mFileName; 00449 00450 /** Number of columns */ 00451 int mColumns; 00452 00453 /** Column counter */ 00454 int mColCount; 00455 00456 /** Endl seperator on/off */ 00457 bool mEndl; 00458 00459 /** Xml doctype if in xml mode */ 00460 std::string mDocType; 00461 00462 /** Flush internal buffer */ 00463 void DoFlush(void); 00464 00465 }; 00466 00467 } // namespace faudes 00468 00469 #define FAUDES_TOKENWRITER_H 00470 #endif 00471 |
libFAUDES 2.20d --- 2011.04.26 --- c++ source docu by doxygen