InputFile.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "InputFile.h"
00019 #include "StringBasics.h"
00020 #include "GzipHeader.h"
00021 #include "BgzfFileType.h"
00022 #include "BgzfFileTypeRecovery.h"
00023 #include "GzipFileType.h"
00024 #include "UncompressedFileType.h"
00025
00026 #include <stdarg.h>
00027
00028 InputFile::InputFile(const char * filename, const char * mode,
00029 InputFile::ifileCompression compressionMode)
00030 {
00031
00032 myAttemptRecovery = false;
00033 myFileTypePtr = NULL;
00034 myBufferIndex = 0;
00035 myCurrentBufferSize = 0;
00036 myAllocatedBufferSize = DEFAULT_BUFFER_SIZE;
00037 myFileBuffer = new char[myAllocatedBufferSize];
00038 myFileName.clear();
00039
00040 openFile(filename, mode, compressionMode);
00041 }
00042
00043
00044 int InputFile::readTilChar(const std::string& stopChars, std::string& stringRef)
00045 {
00046 int charRead = 0;
00047 size_t pos = std::string::npos;
00048
00049 while(pos == std::string::npos)
00050 {
00051 charRead = ifgetc();
00052
00053
00054 if(charRead == EOF)
00055 {
00056 return(-1);
00057 }
00058
00059
00060 pos = stopChars.find(charRead);
00061
00062 if(pos == std::string::npos)
00063 {
00064
00065
00066 stringRef += charRead;
00067 }
00068 }
00069 return(pos);
00070 }
00071
00072
00073 int InputFile::readTilChar(const std::string& stopChars)
00074 {
00075 int charRead = 0;
00076 size_t pos = std::string::npos;
00077
00078 while(pos == std::string::npos)
00079 {
00080 charRead = ifgetc();
00081
00082
00083 if(charRead == EOF)
00084 {
00085 return(-1);
00086 }
00087
00088
00089 pos = stopChars.find(charRead);
00090 }
00091 return(pos);
00092 }
00093
00094
00095 int InputFile::discardLine()
00096 {
00097 int charRead = 0;
00098
00099 while((charRead != EOF) && (charRead != '\n'))
00100 {
00101 charRead = ifgetc();
00102 }
00103
00104 if(charRead == EOF)
00105 {
00106 return(-1);
00107 }
00108 return(0);
00109 }
00110
00111
00112 int InputFile::readLine(std::string& line)
00113 {
00114 int charRead = 0;
00115 while(!ifeof())
00116 {
00117 charRead = ifgetc();
00118 if(charRead == EOF)
00119 {
00120 return(-1);
00121 }
00122 if(charRead == '\n')
00123 {
00124 return(0);
00125 }
00126 line += charRead;
00127 }
00128
00129 return(-1);
00130 }
00131
00132
00133 int InputFile::readTilTab(std::string& field)
00134 {
00135 int charRead = 0;
00136 while(!ifeof())
00137 {
00138 charRead = ifgetc();
00139 if(charRead == EOF)
00140 {
00141 return(-1);
00142 }
00143 if(charRead == '\n')
00144 {
00145 return(0);
00146 }
00147 if(charRead == '\t')
00148 {
00149 return(1);
00150 }
00151 field += charRead;
00152 }
00153 return(-1);
00154 }
00155
00156
00157 #ifdef __ZLIB_AVAILABLE__
00158
00159
00160
00161 bool InputFile::openFile(const char * filename, const char * mode,
00162 InputFile::ifileCompression compressionMode)
00163 {
00164
00165
00166
00167
00168
00169
00170 if(myAttemptRecovery) {
00171 bufferReads(1);
00172 }
00173
00174 if (mode[0] == 'w' || mode[0] == 'W')
00175 {
00176 openFileUsingMode(filename, mode, compressionMode);
00177 }
00178 else
00179 {
00180
00181 if((strcmp(filename, "-") == 0) || (strcmp(filename, "-.gz") == 0))
00182 {
00183
00184
00185 openFileUsingMode(filename, mode, compressionMode);
00186 }
00187 else
00188 {
00189
00190
00191
00192 FILE* filePtr = fopen(filename, "r");
00193
00194
00195
00196 if (filePtr == NULL)
00197 {
00198
00199
00200 if (mode[0] == 'r' || mode[0] == 'R')
00201 {
00202
00203 if (myFileTypePtr != NULL)
00204 {
00205 delete myFileTypePtr;
00206 myFileTypePtr = NULL;
00207 }
00208
00209 return false;
00210 }
00211 else
00212 {
00213 openFileUsingMode(filename, mode, compressionMode);
00214 }
00215 }
00216 else
00217 {
00218
00219
00220
00221 GzipHeader gzipHeader;
00222 bool isGzip = gzipHeader.readHeader(filePtr);
00223
00224
00225
00226 fclose(filePtr);
00227
00228 if (isGzip)
00229 {
00230
00231
00232 if (gzipHeader.isBgzfFile())
00233 {
00234
00235
00236 if(myAttemptRecovery) {
00237
00238 myFileTypePtr = new BgzfFileTypeRecovery(filename, mode);
00239 } else {
00240
00241 myFileTypePtr = new BgzfFileType(filename, mode);
00242 }
00243 }
00244 else
00245 {
00246
00247 myFileTypePtr = new GzipFileType(filename, mode);
00248 }
00249 }
00250 else
00251 {
00252
00253
00254 myFileTypePtr = new UncompressedFileType(filename, mode);
00255 }
00256 }
00257 }
00258 }
00259 if(myFileTypePtr == NULL)
00260 {
00261 return(false);
00262 }
00263 if (!myFileTypePtr->isOpen())
00264 {
00265
00266 delete myFileTypePtr;
00267 myFileTypePtr = NULL;
00268 return false;
00269 }
00270
00271 if(myAllocatedBufferSize == 1)
00272 {
00273 myFileTypePtr->setBuffered(false);
00274 }
00275 else
00276 {
00277 myFileTypePtr->setBuffered(true);
00278 }
00279 myFileName = filename;
00280 return true;
00281 }
00282
00283
00284
00285
00286 void InputFile::openFileUsingMode(const char * filename, const char * mode,
00287 ifileCompression compressionMode)
00288 {
00289 switch (compressionMode)
00290 {
00291 case GZIP:
00292
00293 myFileTypePtr = new GzipFileType(filename, mode);
00294 break;
00295 case BGZF:
00296
00297
00298
00299
00300 if(myAttemptRecovery && ((mode[0] == 'r') || (mode[0] == 'R')))
00301 {
00302
00303 myFileTypePtr = new BgzfFileTypeRecovery(filename, mode);
00304 }
00305 else
00306 {
00307 myFileTypePtr = new BgzfFileType(filename, mode);
00308 }
00309 break;
00310 case UNCOMPRESSED:
00311 myFileTypePtr = new UncompressedFileType(filename, mode);
00312 break;
00313 case InputFile::DEFAULT:
00314 default:
00315
00316
00317 int lastchar = 0;
00318 while (filename[lastchar] != 0) lastchar++;
00319 if ((lastchar >= 3 &&
00320 filename[lastchar - 3] == '.' &&
00321 filename[lastchar - 2] == 'g' &&
00322 filename[lastchar - 1] == 'z'))
00323 {
00324
00325 myFileTypePtr = new GzipFileType(filename, mode);
00326 }
00327 else
00328 {
00329
00330 myFileTypePtr = new UncompressedFileType(filename, mode);
00331 }
00332 break;
00333 }
00334
00335 if(myFileTypePtr == NULL)
00336 {
00337 return;
00338 }
00339 if(myAllocatedBufferSize == 1)
00340 {
00341 myFileTypePtr->setBuffered(false);
00342 }
00343 else
00344 {
00345 myFileTypePtr->setBuffered(true);
00346 }
00347 }
00348
00349 #else
00350
00351
00352
00353
00354 bool InputFile::openFile(const char * filename, const char * mode,
00355 InputFile::ifileCompression compressionMode)
00356 {
00357
00358 myFileTypePtr = new UncompressedFileType(filename, mode);
00359
00360 if(myFileTypePtr == NULL)
00361 {
00362 return(false);
00363 }
00364 if (!myFileTypePtr->isOpen())
00365 {
00366
00367 delete myFileTypePtr;
00368 myFileTypePtr = NULL;
00369 return false;
00370 }
00371 if(myAllocatedBufferSize == 1)
00372 {
00373 myFileTypePtr->setBuffered(false);
00374 }
00375 else
00376 {
00377 myFileTypePtr->setBuffered(true);
00378 }
00379 myFileName = filename;
00380 return true;
00381 }
00382
00383 #endif
00384
00385
00386 InputFile::~InputFile()
00387 {
00388 delete myFileTypePtr;
00389 myFileTypePtr = NULL;
00390
00391 if(myFileBuffer != NULL)
00392 {
00393 delete[] myFileBuffer;
00394 myFileBuffer = NULL;
00395 }
00396 }
00397
00398
00399 int ifprintf(IFILE output, const char * format, ...)
00400 {
00401 String buffer;
00402
00403 va_list ap;
00404 va_start(ap, format);
00405
00406 buffer.vprintf(format, ap);
00407
00408 va_end(ap);
00409
00410 return ::ifwrite(output, (const char *) buffer, buffer.Length());
00411 }
00412
00413
00414 InputFile& operator << (InputFile& stream, double num)
00415 {
00416 String val;
00417 val = num;
00418 stream << val;
00419 return(stream);
00420 }
00421
00422
00423 InputFile& operator << (InputFile& stream, int num)
00424 {
00425 String val;
00426 val = num;
00427 stream << val;
00428 return(stream);
00429 }
00430
00431
00432 InputFile& operator << (InputFile& stream, unsigned int num)
00433 {
00434 String val;
00435 val = num;
00436 stream << val;
00437 return(stream);
00438 }