00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef INPUT_HPP
00037 #define INPUT_HPP
00038
00039 #include <stdio.h>
00040 #include <string.h>
00041
00042 #include "File.hpp"
00043
00044 #ifndef min
00045 #define min(x,y) ( (x)>(y) ? (y) : (x))
00046 #endif
00047
00048
00049 #define FILEBUF_SIZE 65536L
00050
00051
00052
00053 #define FillBufLen(mylen) if(endptr-curptr<(mylen)) { FillBuf(); if(endptr-curptr<(mylen)) {Error("Unexpected end of file!");Exit();}}
00054
00055 class Input : public CFile
00056 {
00057 char databuf[FILEBUF_SIZE];
00058 char *curptr;
00059 char *endptr;
00060
00061
00062 unsigned long curlineno;
00063 public:
00064 Input()
00065 {
00066 curptr=endptr=NULL;
00067 curlineno=1;
00068 }
00069
00070 void FillBuf()
00071 {
00072 int bytesread;
00073
00074 if(endptr-curptr>0)
00075
00076 {
00077 memmove(databuf,curptr,endptr-curptr);
00078 endptr=databuf+(endptr-curptr);
00079 curptr=databuf;
00080 }
00081 else
00082 curptr=endptr=databuf;
00083
00084
00085 bytesread=ReadBlock(endptr,databuf+FILEBUF_SIZE-endptr);
00086
00087 endptr+=bytesread;
00088 }
00089
00090 char OpenFile(char *filename)
00091
00092 {
00093 if(CFile::OpenFile(filename)==0)
00094 return 0;
00095
00096 curptr=endptr=databuf;
00097
00098 FillBuf();
00099
00100 curlineno=1;
00101
00102 return 1;
00103 }
00104
00105 char ReadData(char *dest,int len)
00106
00107
00108
00109
00110
00111 {
00112 int savelen=len;
00113
00114 while(endptr-curptr<len)
00115 {
00116 if(IsEndOfFile())
00117 return 1;
00118
00119 len-=endptr-curptr;
00120
00121 while(curptr<endptr)
00122 {
00123 *dest=*curptr;
00124 if(*curptr=='\n')
00125 curlineno++;
00126 dest++;
00127 curptr++;
00128 }
00129 FillBuf();
00130 }
00131
00132 while(len>0)
00133 {
00134 *dest=*curptr;
00135 if(*curptr=='\n')
00136 curlineno++;
00137 dest++;
00138 curptr++;
00139 len--;
00140 }
00141 return 0;
00142 }
00143
00144 void GetChar(char *ptr)
00145
00146 {
00147 FillBufLen(1);
00148 *ptr=*curptr;
00149 if(*curptr=='\n')
00150 curlineno++;
00151 curptr++;
00152 }
00153
00154
00155
00156 void ReadFullUInt32(unsigned long *data)
00157
00158 {
00159 FillBufLen(sizeof(unsigned long));
00160
00161 *data=*(unsigned long *)curptr;
00162 curptr+=4;
00163 }
00164
00165 void ReadUInt32(unsigned long *data)
00166
00167
00168
00169
00170
00171 {
00172 FillBufLen(1);
00173
00174 if(*(unsigned char *)curptr<128)
00175 *data=(unsigned)(unsigned char)*(curptr++);
00176 else
00177 {
00178 if(*(unsigned char *)curptr<192)
00179 {
00180 FillBufLen(1);
00181 *data=(((unsigned)(unsigned char)*(curptr++)-128)<<8)+(unsigned)(unsigned char)*(curptr++);
00182 }
00183 else
00184 {
00185 FillBufLen(3);
00186
00187 *data= (((unsigned)(unsigned char)*(curptr++)-192)<<24)+
00188 (((unsigned)(unsigned char)*(curptr++))<<16)+
00189 (((unsigned)(unsigned char)*(curptr++))<<8)+
00190 (unsigned)(unsigned char)*(curptr++);
00191 }
00192 }
00193 }
00194
00195
00196
00197 void PeekData(char *ptr,int len)
00198
00199 {
00200 FillBufLen(len);
00201 mymemcpy(ptr,curptr,len);
00202 }
00203
00204 void PeekChar(char *ptr)
00205
00206
00207
00208
00209 {
00210 FillBufLen(1);
00211 *ptr=*curptr;
00212 }
00213
00214 void SkipData(int len)
00215
00216
00217
00218
00219
00220
00221 {
00222 while(endptr-curptr<len)
00223 {
00224 len-=endptr-curptr;
00225 curptr=endptr=databuf;
00226
00227 FillBuf();
00228 }
00229 curptr+=len;
00230 }
00231
00232 void FastSkipData(int len)
00233
00234 {
00235 curptr+=len;
00236 }
00237
00238 void SkipChar()
00239
00240 {
00241 FillBufLen(1);
00242 if(*curptr=='\n')
00243 curlineno++;
00244 curptr++;
00245 }
00246
00247 char IsEndOfFile(unsigned len=0)
00248
00249 {
00250 return (curptr+len==endptr)&&(iseof);
00251 }
00252
00253 int GetCurBlockPtr(char **ptr)
00254
00255
00256 {
00257 *ptr=curptr;
00258 return endptr-curptr;
00259 }
00260
00261 void RefillAndGetCurBlockPtr(char **ptr,int *len)
00262
00263
00264
00265 {
00266 FillBuf();
00267 *ptr=curptr;
00268 *len=endptr-curptr;
00269 }
00270
00271 void UndoReadChar()
00272
00273 {
00274 curptr--;
00275 }
00276 };
00277
00278 #endif