|
libFAUDES
Sections
Index
|
cfl_token.hGo to the documentation of this file.00001 /** @file cfl_token.h @brief Class Token */ 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_TOKEN_H 00024 00025 #include "cfl_definitions.h" 00026 #include "cfl_helper.h" 00027 #include <string> 00028 #include <iostream> 00029 #include <fstream> 00030 #include <assert.h> 00031 00032 namespace faudes { 00033 00034 /** 00035 * Tokens model atomic data for stream IO. 00036 * 00037 * A Token models a string or numeric datum that can be read from a 00038 * or written to a C++ stream. The class itself implements the representation of 00039 * the data including its type. For section handling and actual file processing 00040 * see TokenReader and TokenWriter. 00041 * 00042 * @param mType 00043 * faudes::TokenType of the Token 00044 * @param mStringValue 00045 * Token value as C++-type std::string 00046 * @param mIntegerValue 00047 * Token value as C++-type long integer 00048 * @param mFloatValue 00049 * Token value of C++-type double 00050 */ 00051 00052 class Token { 00053 public: 00054 00055 /** 00056 * Empty constructor, constructs None token 00057 */ 00058 Token(void); 00059 00060 /** 00061 * Copy constructor 00062 */ 00063 Token(const Token& rToken); 00064 00065 00066 /** 00067 * Token destructor 00068 */ 00069 ~Token(void); 00070 00071 /** Assignment operator */ 00072 Token& operator= (const Token& rOther); 00073 00074 /** 00075 * Token types: 00076 */ 00077 enum TokenType { 00078 None, ///< Invalid/empty token 00079 Begin, ///< <label> (begin of section) 00080 End, ///< <\\label> (end of section) 00081 String, ///< "name" (the name may not contain a quote (\") ) 00082 Option, ///< +xyZ+ (the option string may not contain a "+") 00083 Integer, ///< 1234 (non-negative) 00084 Integer16, ///< 0x12fff ("0x" makes a number Integer16) 00085 Float, ///< -12.34 ("-" or "." make a number a float) 00086 Binary ///< =ABhlkjj= (base64 encoded binary data) 00087 }; 00088 00089 00090 /** 00091 * Initialize None token 00092 * 00093 */ 00094 void SetNone(void); 00095 00096 /** 00097 * Initialize as String token 00098 * 00099 * @param rName 00100 * String to fill the Token 00101 */ 00102 void SetString(const std::string& rName); 00103 00104 /** 00105 * Initialize as Begin token 00106 * 00107 * @param rName 00108 * Title of section to fill the Token 00109 */ 00110 void SetBegin(const std::string& rName); 00111 00112 /** 00113 * Initialize as End token 00114 * 00115 * @param rName 00116 * Title of section to fill the Token 00117 */ 00118 void SetEnd(const std::string& rName); 00119 00120 /** 00121 * Initialize as Option token 00122 * 00123 * @param rName 00124 * Option to fill the Token 00125 */ 00126 void SetOption(const std::string& rName); 00127 00128 /** 00129 * Initialize as Integer token 00130 * 00131 * @param number 00132 * Number to fill the Token 00133 */ 00134 void SetInteger(const long int number); 00135 00136 /** 00137 * Initialize as Integer16 token 00138 * 00139 * @param number 00140 * Number to fill the Token 00141 */ 00142 void SetInteger16(const long int number); 00143 00144 /** 00145 * Initialize as Float token 00146 * 00147 * @param number 00148 * Number to fill the Token 00149 */ 00150 void SetFloat(const double number); 00151 00152 /** 00153 * Initialize Binary token. 00154 * This method allocates a copy of the data. 00155 * For writing only, you may use the TokenWriter interface 00156 * to avoid the local copy. 00157 * 00158 * @param data 00159 * Reference to raw data record 00160 * @param len 00161 * Number of bytes in record 00162 */ 00163 void SetBinary(const char* data, long int len); 00164 00165 /** 00166 * Get integer value of a numeric token 00167 * 00168 * @return 00169 * Token's integer value 00170 */ 00171 long int IntegerValue(void) const { 00172 return(mIntegerValue); 00173 } 00174 00175 /** 00176 * Get float value of a numeric token 00177 * 00178 * @return 00179 * Token float value 00180 */ 00181 double FloatValue(void) const { 00182 return(mFloatValue); 00183 } 00184 00185 /** 00186 * Get string value of a name token 00187 * 00188 * @return 00189 * Token's name value 00190 */ 00191 const std::string& StringValue(void) const { 00192 return(mStringValue); 00193 } 00194 00195 /** 00196 * Get token Type 00197 * 00198 * @return 00199 * Token's TokenType 00200 */ 00201 TokenType Type(void) const { 00202 return(mType); 00203 } 00204 00205 /** 00206 * Read Token from input stream 00207 * 00208 * @param pStream 00209 * Pointer to std::ifstream 00210 * @exception Exception 00211 * - ios exceptions (eg file io error) 00212 */ 00213 int Read(std::istream* pStream); 00214 00215 /** 00216 * Write Token to output stream 00217 * 00218 * @param pStream 00219 * Pointer to ostream 00220 * @exception Exception 00221 * - ios exceptions (eg file io error,) 00222 */ 00223 void Write(std::ostream* pStream); 00224 00225 /** Write specified binary data as base64 string to output stream 00226 * 00227 * @param pStream 00228 * Reference to std::ostream 00229 * @param len 00230 * Number of bytes to write 00231 * @param pData 00232 * Data to write 00233 */ 00234 static void WriteBinary(std::ostream* pStream, const char* pData, long int len); 00235 00236 /** Write a std::string value to an output stream. 00237 * 00238 * This method writes a string verbatim, i.e. incl all control characters. 00239 * It is enclosed by a marker which defaults to "__VERBATIM__". If the string 00240 * contains the marker, a variation is used. 00241 * 00242 * @param pStream 00243 * Reference to std::ostream 00244 * @param rString 00245 * Character to escap eby prepending "\\" 00246 */ 00247 static void WriteVerbatim(std::ostream* pStream, const std::string& rString); 00248 00249 00250 private: 00251 00252 /** Token type */ 00253 TokenType mType; 00254 00255 /** Token std::string value (if Token is of type Name, Begein or End) */ 00256 std::string mStringValue; 00257 00258 /** Token integer value (if Token is of type Integer or Integer16) */ 00259 long int mIntegerValue; 00260 00261 /** Token float value (if Token is of type Float) */ 00262 double mFloatValue; 00263 00264 /** Write a std::string value to an output stream. 00265 * 00266 * This method writes the string enclosed by a signle stop 00267 * character, typically '"'. Any stop character within the string 00268 * is excaped by a preceeding "\". If the string comtains a "\", it will 00269 * also be escaped by another "\". Control codes are replaced by a single blanc. 00270 * 00271 * @param pStream 00272 * Reference to std::ostream 00273 * @param stop 00274 * Character to escape by prepending "\\" 00275 */ 00276 void WriteString(std::ostream* pStream, char stop); 00277 00278 00279 00280 /** Write my binary data as base64 string to output stream 00281 * 00282 * @param pStream 00283 * Reference to std::ostream 00284 */ 00285 void WriteBinary(std::ostream* pStream); 00286 00287 /** 00288 * Read a number from an input file stream 00289 * 00290 * @param pStream 00291 * Reference to std::istream 00292 * 00293 * @return 00294 * Success 00295 */ 00296 bool ReadNumber(std::istream* pStream); 00297 00298 /** Read a std::string value from an input file stream. 00299 * 00300 * This method assumes that the string was written in the format 00301 * of WriteString, i.e. enclosed by single stop characters. However, 00302 * for practical reasons, it is assumed that the first stop character has 00303 * been read allready. 00304 * 00305 * @param pStream 00306 * Reference to std::istream 00307 * @param stop 00308 * Stop character 00309 * 00310 * @return 00311 * Line count or -1 for error 00312 */ 00313 int ReadString(std::istream* pStream, char stop); 00314 00315 /** Read a std::string value from an input file stream. 00316 * 00317 * This method assumes that the string was written in the format 00318 * of WriteVerbatim, i.e. enclosed by a start and stop markers 00319 * "__VERBATIM__" or variations thereof. However, for practical reasons, 00320 * it is assumed that the first character "_" has been read allready. 00321 * 00322 * @param pStream 00323 * Reference to std::istream 00324 * 00325 * @return 00326 * Line count or -1 for error 00327 */ 00328 int ReadVerbatim(std::istream* pStream); 00329 00330 /** 00331 * Read a base64 binary string from an input file stream 00332 * 00333 * @param pStream 00334 * Reference to std::istream 00335 * 00336 * @return 00337 * Line count or -1 for error 00338 */ 00339 int ReadBinary(std::istream* pStream); 00340 00341 /** 00342 * Read (ignore) spaces and comments in an input file stream 00343 * 00344 * @param pStream 00345 * Reference to std::istream 00346 * 00347 * @return 00348 * Number of lines read 00349 */ 00350 int ReadSpace(std::istream* pStream); 00351 }; 00352 00353 } // namespace faudes 00354 00355 #define FAUDES_TOKEN_H 00356 #endif 00357 00358 |
libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3