00001
00002
00004
00005 #include "Token.h"
00006 #include <stdlib.h>
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <ctype.h>
00010
00012
00013 Tokenizer::Tokenizer() {
00014 File=0;
00015 LineNum=0;
00016 strcpy(FileName,"");
00017 }
00018
00020
00021 Tokenizer::~Tokenizer() {
00022 if(File) {
00023 printf("ERROR: Tokenizer::~Tokenizer()- Closing file '%s'\n",FileName);
00024 fclose((FILE*)File);
00025 }
00026 }
00027
00029
00030 bool Tokenizer::Open(const char *fname) {
00031 File=(void*)fopen(fname,"r");
00032 LineNum=1;
00033 if(File==0) {
00034 printf("ERROR: crTokenzier::Open()- Can't open file '%s'\n",fname);
00035 return false;
00036 }
00037 strcpy(FileName,fname);
00038 return true;
00039 }
00040
00042
00043 bool Tokenizer::Close() {
00044 if(File) fclose((FILE*)File);
00045 else return false;
00046
00047 File=0;
00048 return true;
00049 }
00050
00052
00053 bool Tokenizer::Abort(char *error) {
00054 printf("ERROR '%s' line %d: %s\n",FileName,LineNum,error);
00055 Close();
00056 return false;
00057 }
00058
00060
00061 char Tokenizer::GetChar() {
00062 char c=char(getc((FILE*)File));
00063 if(c=='\n') LineNum++;
00064 return c;
00065 }
00066
00068
00069 char Tokenizer::CheckChar() {
00070 int c=getc((FILE*)File);
00071 ungetc(c,(FILE*)File);
00072 return char(c);
00073 }
00074
00076
00077 int Tokenizer::GetInt() {
00078 SkipWhitespace();
00079 int pos=0;
00080 char temp[256];
00081
00082
00083 char c=CheckChar();
00084 if(c=='-') {
00085 temp[pos++]=GetChar();
00086 c=CheckChar();
00087 }
00088 if(!isdigit(c)) {
00089 printf("ERROR: Tokenizer::GetInt()- Expecting int on line %d of '%s'\n",LineNum,FileName);
00090 return 0xFFFFFFFF;
00091 }
00092 temp[pos++]=GetChar();
00093
00094
00095 while(isdigit(c=CheckChar())) temp[pos++]=GetChar();
00096
00097
00098 temp[pos++]='\0';
00099 return atoi(temp);
00100 }
00101
00103
00104
00105
00106
00107
00108 float Tokenizer::GetFloat() {
00109 SkipWhitespace();
00110 int pos=0;
00111 char temp[256];
00112
00113
00114 char c=CheckChar();
00115 if(c=='-') {
00116 temp[pos++]=GetChar();
00117 c=CheckChar();
00118 }
00119 if(!isdigit(c)) {
00120 printf("ERROR: Tokenizer::GetFloat()- Expecting float on line %d of '%s' '%c'\n",LineNum,FileName,c);
00121 return 0xFFFFFFFF;
00122 }
00123 temp[pos++]=GetChar();
00124
00125
00126 while(isdigit(c=CheckChar())) temp[pos++]=GetChar();
00127
00128
00129 if(c=='.') {
00130 temp[pos++]=GetChar();
00131 while(isdigit(c=CheckChar())) temp[pos++]=GetChar();
00132 }
00133
00134
00135 if(c=='e' || c=='E') {
00136 temp[pos++]=GetChar();
00137 c=CheckChar();
00138 if(c=='+' || c=='-') {
00139 temp[pos++]=GetChar();
00140 c=CheckChar();
00141 }
00142 if(!isdigit(c)) {
00143 printf("ERROR: Tokenizer::GetFloat()- Poorly formatted float exponent on line %d of '%s'\n",LineNum,FileName);
00144 return 0.0f;
00145 }
00146 while(isdigit(c=CheckChar())) temp[pos++]=GetChar();
00147 }
00148
00149
00150 temp[pos++]='\0';
00151 return float(atof(temp));
00152 }
00153
00155
00156 bool Tokenizer::GetToken(char *str) {
00157 SkipWhitespace();
00158
00159 int pos=0;
00160 char c=CheckChar();
00161 while(c!=' ' && c!='\n' && c!='\t' && c!='\r' && c!='(' && c!=')'
00162 && !feof((FILE*)File)) {
00163 str[pos++]=GetChar();
00164 c=CheckChar();
00165 }
00166 str[pos]='\0';
00167 return true;
00168 }
00169
00171
00172 bool Tokenizer::FindToken(const char *tok) {
00173 int pos=0;
00174 while(tok[pos]!='\0') {
00175 if(feof((FILE*)File)) return false;
00176 char c=GetChar();
00177 if(c==tok[pos]) pos++;
00178 else pos=0;
00179 }
00180 return true;
00181 }
00182
00184
00185 bool Tokenizer::SkipWhitespace() {
00186 char c=CheckChar();
00187 bool white=false;
00188 while(isspace(c) || c=='(' || c==')') {
00189 GetChar();
00190 c=CheckChar();
00191 white=true;
00192 }
00193 return white;
00194 }
00195
00197
00198 bool Tokenizer::SkipLine() {
00199 char c=GetChar();
00200 while(c!='\n') {
00201 if(feof((FILE*)File)) return false;
00202 c=GetChar();
00203 }
00204 return true;
00205 }
00206
00208
00209 bool Tokenizer::Reset() {
00210 if(fseek((FILE*)File,0,SEEK_SET)) return false;
00211 return true;
00212 }
00213