| |
libFAUDES
Sections
Index
|
token.hGo to the documentation of this file.00001 /** @file 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 "definitions.h" 00026 #include "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 00237 private: 00238 00239 /** Token type */ 00240 TokenType mType; 00241 00242 /** Token std::string value (if Token is of type Name, Begein or End) */ 00243 std::string mStringValue; 00244 00245 /** Token integer value (if Token is of type Integer or Integer16) */ 00246 long int mIntegerValue; 00247 00248 /** Token float value (if Token is of type Float) */ 00249 double mFloatValue; 00250 00251 /** Write a std::string value to an output stream 00252 * 00253 * @param pStream 00254 * Reference to std::ostream 00255 * @param stop 00256 * Character to escap eby prepending "\\" 00257 */ 00258 void WriteString(std::ostream* pStream, char stop); 00259 00260 00261 /** Write my binary data as base64 string to output stream 00262 * 00263 * @param pStream 00264 * Reference to std::ostream 00265 */ 00266 void WriteBinary(std::ostream* pStream); 00267 00268 /** 00269 * Read a number from an input file stream 00270 * 00271 * @param pStream 00272 * Reference to std::istream 00273 * 00274 * @return 00275 * Success 00276 */ 00277 bool ReadNumber(std::istream* pStream); 00278 00279 /** 00280 * Read a base64 binary string from an input file stream 00281 * 00282 * @param pStream 00283 * Reference to std::istream 00284 * 00285 * @return 00286 * Line count or -1 for error 00287 */ 00288 int ReadBinary(std::istream* pStream); 00289 00290 /** Read a std::string value from an input file stream 00291 * 00292 * @param pStream 00293 * Reference to std::istream 00294 * @param stop 00295 * Stop character 00296 * 00297 * @return 00298 * Line count or -1 for error 00299 */ 00300 int ReadString(std::istream* pStream, char stop); 00301 00302 /** 00303 * Read (ignore) spaces and comments in an input file stream 00304 * 00305 * @param pStream 00306 * Reference to std::istream 00307 * 00308 * @return 00309 * Number of lines read 00310 */ 00311 int ReadSpace(std::istream* pStream); 00312 }; 00313 00314 } // namespace faudes 00315 00316 #define FAUDES_TOKEN_H 00317 #endif 00318 00319 |
libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6