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