InputFile.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __INPUTFILE_H__
00019 #define __INPUTFILE_H__
00020
00021 #ifdef __gnu_linux__
00022 #ifndef __ZLIB_AVAILABLE__
00023 #define __ZLIB_AVAILABLE__
00024 #endif
00025 #endif
00026
00027 #include <stdio.h>
00028 #include <iostream>
00029 #include <cstring>
00030
00031 #include "FileType.h"
00032
00033 class InputFile
00034 {
00035 public:
00036
00037
00038
00039 enum ifileCompression {DEFAULT, UNCOMPRESSED, GZIP, BGZF};
00040
00041 InputFile()
00042 {
00043 myFileTypePtr = NULL;
00044 myBufferIndex = 0;
00045 myCurrentBufferSize = 0;
00046 }
00047
00048
00049 ~InputFile();
00050
00051
00052 InputFile(const char * filename, const char * mode,
00053 InputFile::ifileCompression compressionMode = InputFile::DEFAULT);
00054
00055
00056 inline int ifclose()
00057 {
00058 if (myFileTypePtr == NULL)
00059 {
00060 return EOF;
00061 }
00062 int result = myFileTypePtr->close();
00063 delete myFileTypePtr;
00064 myFileTypePtr = NULL;
00065 return result;
00066 }
00067
00068 inline int ifread(void * buffer, unsigned int size)
00069 {
00070
00071
00072
00073
00074
00075
00076 unsigned int availableBytes = myCurrentBufferSize - myBufferIndex;
00077
00078
00079 if (availableBytes == 0)
00080 {
00081
00082
00083 return(readFromFile(buffer, size));
00084 }
00085
00086 else if (size <= availableBytes)
00087 {
00088
00089 memcpy(buffer, myFileBuffer+myBufferIndex, size);
00090
00091 myBufferIndex += size;
00092 return size;
00093 }
00094
00095 else
00096 {
00097
00098
00099 memcpy(buffer, myFileBuffer+myBufferIndex, availableBytes);
00100
00101 myBufferIndex += availableBytes;
00102
00103 int totalBytes = availableBytes;
00104 totalBytes +=
00105 readFromFile((char*)buffer+availableBytes, size - availableBytes);
00106 return(totalBytes);
00107 }
00108 }
00109
00110
00111
00112
00113
00114 inline int ifgetc()
00115 {
00116 if (myBufferIndex >= myCurrentBufferSize)
00117 {
00118
00119 myCurrentBufferSize = readFromFile(myFileBuffer, MAX_BUFFER_SIZE);
00120 myBufferIndex = 0;
00121 }
00122
00123
00124 if (myBufferIndex >= myCurrentBufferSize)
00125 {
00126 return(EOF);
00127 }
00128 return(myFileBuffer[myBufferIndex++]);
00129 }
00130
00131
00132 inline void ifrewind()
00133 {
00134
00135
00136
00137 if (myFileTypePtr == NULL)
00138 {
00139
00140 return;
00141 }
00142 myCurrentBufferSize = 0;
00143 myBufferIndex = 0;
00144 myFileTypePtr->rewind();
00145 }
00146
00147
00148
00149 inline int ifeof()
00150 {
00151
00152 if (myBufferIndex < myCurrentBufferSize)
00153 {
00154
00155 return false;
00156 }
00157 else
00158 {
00159 if (myFileTypePtr == NULL)
00160 {
00161
00162 return 0;
00163 }
00164
00165 return myFileTypePtr->eof();
00166 }
00167 }
00168
00169
00170 inline unsigned int ifwrite(const void * buffer, unsigned int size)
00171 {
00172 if (myFileTypePtr == NULL)
00173 {
00174
00175 return 0;
00176 }
00177 return myFileTypePtr->write(buffer, size);
00178 }
00179
00180
00181 inline bool isOpen()
00182 {
00183
00184 if ((myFileTypePtr != NULL) && myFileTypePtr->isOpen())
00185 {
00186 return true;
00187 }
00188
00189 return false;
00190 }
00191
00192
00193
00194 inline long int iftell()
00195 {
00196 if (myFileTypePtr == NULL)
00197 {
00198
00199 return -1;
00200 }
00201 return myFileTypePtr->tell();
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 inline bool ifseek(long int offset, int origin)
00213 {
00214 if (myFileTypePtr == NULL)
00215 {
00216
00217 return false;
00218 }
00219 return myFileTypePtr->seek(offset, origin);
00220 }
00221
00222 protected:
00223
00224
00225 bool openFile(const char * filename, const char * mode,
00226 InputFile::ifileCompression compressionMode);
00227
00228
00229
00230
00231 inline int readFromFile(void * buffer, unsigned int size)
00232 {
00233
00234 if (myFileTypePtr == NULL)
00235 {
00236 return 0;
00237 }
00238 return myFileTypePtr->read(buffer, size);
00239 }
00240
00241 #ifdef __ZLIB_AVAILABLE__
00242
00243
00244
00245
00246
00247 void openFileUsingMode(const char* filename, const char* mode,
00248 InputFile::ifileCompression compressionMode);
00249 #endif
00250
00251
00252 static const int MAX_BUFFER_SIZE = 1048576;
00253
00254
00255 FileType* myFileTypePtr;
00256
00257
00258
00259 char myFileBuffer[MAX_BUFFER_SIZE];
00260
00261
00262
00263 int myBufferIndex;
00264
00265
00266
00267
00268 int myCurrentBufferSize;
00269
00270 };
00271
00272 typedef InputFile* IFILE;
00273
00274
00275
00276
00277 inline IFILE ifopen(const char * filename, const char * mode,
00278 InputFile::ifileCompression compressionMode = InputFile::DEFAULT)
00279 {
00280 IFILE file = new InputFile(filename, mode, compressionMode);
00281 if (!file->isOpen())
00282 {
00283
00284
00285 delete file;
00286 file = NULL;
00287 }
00288 return file;
00289 }
00290
00291
00292 inline int ifclose(IFILE file)
00293 {
00294 int result = file->ifclose();
00295 delete file;
00296 file = NULL;
00297 return(result);
00298 }
00299
00300 inline unsigned int ifread(IFILE file, void * buffer, unsigned int size)
00301 {
00302 return(file->ifread(buffer, size));
00303 }
00304
00305 inline int ifgetc(IFILE file)
00306 {
00307 return(file->ifgetc());
00308 }
00309
00310 inline void ifrewind(IFILE file)
00311 {
00312 file->ifrewind();
00313 }
00314
00315 inline int ifeof(IFILE file)
00316 {
00317 return(file->ifeof());
00318 }
00319
00320 inline unsigned int ifwrite(IFILE file, const void * buffer, unsigned int size)
00321 {
00322 return(file->ifwrite(buffer, size));
00323 }
00324
00325
00326
00327 inline long int iftell(IFILE file)
00328 {
00329 return (file->iftell());
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339 inline bool ifseek(IFILE file, long int offset, int origin)
00340 {
00341 return (file->ifseek(offset, origin));
00342 }
00343
00344 int ifprintf(IFILE output, char * format, ...);
00345
00346 inline IFILE operator >> (IFILE stream, std::string &str)
00347 {
00348 str.clear();
00349 int ch;
00350
00351 while ((ch = stream->ifgetc())!=EOF && (ch != '\n')) str.push_back(ch);
00352 return stream;
00353 }
00354
00355 #endif
00356