00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "token.h"
00024
00025 namespace faudes {
00026
00027
00028 Token::Token(void) :
00029 mType(None),
00030 mStringValue(""),
00031 mIntegerValue(0),
00032 mFloatValue(0) {
00033 }
00034
00035
00036 Token::~Token(void){
00037 }
00038
00039
00040 void Token::SetNone(void ) {
00041 mType=None; mStringValue=""; mIntegerValue=0; mFloatValue=0;
00042 }
00043
00044
00045 void Token::SetString(const std::string& rName) {
00046 mType=String; mStringValue=rName; mIntegerValue=0; mFloatValue=0;
00047 }
00048
00049
00050 void Token::SetBegin(const std::string& rName) {
00051 mType=Begin; mStringValue=rName; mIntegerValue=0; mFloatValue=0;
00052 }
00053
00054
00055 void Token::SetEnd(const std::string& rName) {
00056 mType=End; mStringValue=rName; mIntegerValue=0; mFloatValue=0;
00057 }
00058
00059
00060 void Token::SetOption(const std::string& rName) {
00061 mType=Option; mStringValue=rName; mIntegerValue=0; mFloatValue=0;
00062 }
00063
00064
00065 void Token::SetInteger(const long int number) {
00066 mType=Integer; mStringValue=""; mIntegerValue=number; mFloatValue=number;
00067 }
00068
00069
00070 void Token::SetInteger16(const long int number) {
00071 mType=Integer16; mStringValue=""; mIntegerValue=number; mFloatValue=number;
00072 }
00073
00074
00075 void Token::SetFloat(const double number) {
00076 mType=Float; mStringValue=""; mIntegerValue=(long int) number; mFloatValue=number;
00077 }
00078
00079
00080 bool Token::ReadNumber(std::istream* pStream){
00081 char c, vc;
00082 double fv=0;
00083 long int iv=0;
00084 bool comma = false;
00085 bool minus = false;
00086 int base=10;
00087 bool ok=false;
00088 int cnt=0;
00089
00090 while(*pStream){
00091
00092 if (pStream->eof())
00093 {mType=None; return(false);};
00094
00095 c = pStream->peek();
00096 cnt++;
00097
00098 if (isspace(c)) break;
00099
00100 if(c=='x' && iv==0 && cnt==2 && !comma && !minus)
00101 {pStream->get(); base = 16; continue;}
00102
00103 if(c=='-' && cnt==1)
00104 {pStream->get(); minus = true; continue;}
00105
00106 if(c=='.' && comma==false && base==10)
00107 {pStream->get(); comma = true; continue;}
00108
00109 if (!isdigit(c) && base==10) break;
00110 if (!isxdigit(c) && base==16) break;
00111
00112 pStream->get();
00113
00114 vc=0;
00115 if(c>='0' && c<= '9') vc = c-'0';
00116 else if(c>='a' && c<= 'f') vc = c-'a' + 10;
00117 else if(c>='A' && c<= 'F') vc = c-'A' + 10;
00118
00119 iv = base * iv + vc;
00120 fv = base * fv + vc;
00121 if(comma) {
00122 iv=iv/base;
00123 fv=fv/base;
00124 }
00125 ok = true;
00126 }
00127
00128 if(minus) {
00129 iv=-iv;
00130 fv=-fv;
00131 }
00132 mIntegerValue = iv;
00133 mFloatValue = fv;
00134 mType= (comma || minus) ? Float : Integer;
00135 if(base == 16) mType = Integer16;
00136 return(ok);
00137 }
00138
00139
00140 void Token::Write(std::ostream* pStream){
00141 FD_DV("Token::Write: mType=" << (int) mType
00142 << "\" mStringValue=\"" << mStringValue
00143 << "\" mIntegerValue=" <<mIntegerValue
00144 << "\" mFloatValue=" <<mFloatValue <<"\n");
00145
00146 switch (mType) {
00147
00148 case Begin:
00149 *pStream << '<' << mStringValue << '>';
00150 break;
00151
00152 case End:
00153 *pStream << "</" << mStringValue << '>';
00154 break;
00155
00156 case String:
00157 *pStream << ExpandString("\""+mStringValue+"\"", FD_NAMELEN);
00158 break;
00159
00160 case Option:
00161 *pStream << ExpandString("+"+mStringValue+"+", FD_NAMELEN);
00162 break;
00163
00164 case Integer:
00165 *pStream << ExpandString(ToStringInteger(mIntegerValue), FD_NAMELEN);
00166 break;
00167
00168 case Integer16:
00169 *pStream << ExpandString(ToStringInteger16(mIntegerValue), FD_NAMELEN);
00170 break;
00171
00172 case Float:
00173 *pStream << ExpandString(ToStringFloat(mFloatValue), FD_NAMELEN);
00174 break;
00175
00176 default:
00177 assert(0);
00178 }
00179 }
00180
00181
00182 bool Token::ReadString(std::istream* pStream, char stop){
00183 char c;
00184 std::string v = "";
00185
00186 while (*pStream) {
00187
00188 if (pStream->eof()) {mType=None; return false;};
00189
00190 c = pStream->peek();
00191
00192 if (iscntrl(c)) return false;
00193
00194 pStream->get();
00195
00196 if(c == stop) break;
00197
00198 v.append(1,c);
00199 }
00200
00201 mStringValue = v;
00202 return(true);
00203 }
00204
00205
00206
00207 int Token::ReadSpace(std::istream* pStream){
00208 char c = '\0';
00209 int lc = 0;
00210
00211 while (*pStream) {
00212 while (*pStream) {
00213
00214 if (pStream->eof()) {return(lc);};
00215
00216 c = pStream->peek();
00217
00218 if((! isspace(c)) && (! iscntrl(c))) break;
00219
00220
00221 pStream->get();
00222
00223 if(c == '\n') ++lc;
00224 }
00225
00226 if (c != '%') break;
00227 while (*pStream) {
00228
00229 if (pStream->eof()) {return(lc);};
00230
00231 c = pStream->get();
00232
00233 if (c == '\n') ++lc;
00234
00235 if (iscntrl(c)) break;
00236 }
00237 }
00238
00239
00240 return (lc);
00241 }
00242
00243
00244 int Token::Read(std::istream* pStream){
00245 char c1, c2;
00246 int lc = 0;
00247
00248 SetNone();
00249
00250 if (pStream->eof()) return(lc);
00251
00252 lc += ReadSpace(pStream);
00253
00254 if (pStream->eof()) return(lc);
00255
00256 c1 = pStream->peek();
00257 if (isdigit(c1) || (c1=='-') ) {
00258
00259 ReadNumber(pStream);
00260 }
00261
00262 else if (c1 == '"') {
00263 pStream->get();
00264
00265 if(ReadString(pStream,'"')) mType = String;
00266 }
00267
00268 else if (c1 == '+') {
00269 pStream->get();
00270
00271 if(ReadString(pStream,'+')) mType = Option;
00272 }
00273
00274 else if (c1 == '<') {
00275 pStream->get();
00276
00277 if (pStream->eof()) return(lc);
00278 c2 = pStream->peek();
00279
00280 if (c2 != '/') {
00281 if (ReadString(pStream,'>')) mType = Begin;
00282 }
00283 else {
00284 pStream->get();
00285
00286 c2=pStream->peek();
00287
00288 if (ReadString(pStream,'>')) mType = End;
00289 }
00290 }
00291 FD_DV("Token::Read: char1='" << c1 << "' mType=" << (int) mType
00292 << "\" mStringValue=\"" << mStringValue
00293 << "\" mIntegerValue=" << mIntegerValue
00294 << "\" mFloatValue=" << mFloatValue <<"\n");
00295 return(lc);
00296 }
00297
00298 }