1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
/*
* Copyright (C) 2010-2012 Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "InputFile.h"
#include "StringBasics.h"
#include "GzipHeader.h"
#include "BgzfFileType.h"
#include "BgzfFileTypeRecovery.h"
#include "GzipFileType.h"
#include "UncompressedFileType.h"
#include <stdarg.h>
InputFile::InputFile(const char * filename, const char * mode,
InputFile::ifileCompression compressionMode)
{
// XXX duplicate code
myAttemptRecovery = false;
myFileTypePtr = NULL;
myBufferIndex = 0;
myCurrentBufferSize = 0;
myAllocatedBufferSize = DEFAULT_BUFFER_SIZE;
myFileBuffer = new char[myAllocatedBufferSize];
myFileName.clear();
openFile(filename, mode, compressionMode);
}
int InputFile::readTilChar(const std::string& stopChars, std::string& stringRef)
{
int charRead = 0;
size_t pos = std::string::npos;
// Loop until the character was not found in the stop characters.
while(pos == std::string::npos)
{
charRead = ifgetc();
// First Check for EOF. If EOF is found, just return -1
if(charRead == EOF)
{
return(-1);
}
// Try to find the character in the stopChars.
pos = stopChars.find(charRead);
if(pos == std::string::npos)
{
// Didn't find a stop character and it is not an EOF,
// so add it to the string.
stringRef += charRead;
}
}
return(pos);
}
int InputFile::readTilChar(const std::string& stopChars)
{
int charRead = 0;
size_t pos = std::string::npos;
// Loop until the character was not found in the stop characters.
while(pos == std::string::npos)
{
charRead = ifgetc();
// First Check for EOF. If EOF is found, just return -1
if(charRead == EOF)
{
return(-1);
}
// Try to find the character in the stopChars.
pos = stopChars.find(charRead);
}
return(pos);
}
int InputFile::discardLine()
{
int charRead = 0;
// Loop until the character was not found in the stop characters.
while((charRead != EOF) && (charRead != '\n'))
{
charRead = ifgetc();
}
// First Check for EOF. If EOF is found, just return -1
if(charRead == EOF)
{
return(-1);
}
return(0);
}
int InputFile::readLine(std::string& line)
{
int charRead = 0;
while(!ifeof())
{
charRead = ifgetc();
if(charRead == EOF)
{
return(-1);
}
if(charRead == '\n')
{
return(0);
}
line += charRead;
}
// Should never get here.
return(-1);
}
int InputFile::readTilTab(std::string& field)
{
int charRead = 0;
while(!ifeof())
{
charRead = ifgetc();
if(charRead == EOF)
{
return(-1);
}
if(charRead == '\n')
{
return(0);
}
if(charRead == '\t')
{
return(1);
}
field += charRead;
}
return(-1);
}
#ifdef __ZLIB_AVAILABLE__
// Open a file. Called by the constructor.
// Returns true if the file was successfully opened, false otherwise.
bool InputFile::openFile(const char * filename, const char * mode,
InputFile::ifileCompression compressionMode)
{
//
// if recovering, we don't want to issue big readaheads, since
// that interferes with the decompression - we only want to
// decompress one at a time, and handle the exceptions immediately
// rather than at some indeterminate point in time.
//
if(myAttemptRecovery) {
bufferReads(1);
}
// If a file is for write, just open a new file.
if (mode[0] == 'w' || mode[0] == 'W')
{
openFileUsingMode(filename, mode, compressionMode);
}
else
{
// Check if reading from stdin.
if((strcmp(filename, "-") == 0) || (strcmp(filename, "-.gz") == 0))
{
// Reading from stdin, open it based on the
// compression mode.
openFileUsingMode(filename, mode, compressionMode);
}
else
{
// Not from stdin, so determine the file type.
// Open the file read only to determine file type.
UncompressedFileType file(filename, "r");
// If the file could not be opened, either create a new one or
// return failure.
if (!file.isOpen())
{
// If the mode is for read, then the file must exist, otherwise,
// create a new file.
if (mode[0] == 'r' || mode[0] == 'R')
{
// File must exist.
if (myFileTypePtr != NULL)
{
delete myFileTypePtr;
myFileTypePtr = NULL;
}
// Return false, was not opened.
return false;
}
else
{
openFileUsingMode(filename, mode, compressionMode);
}
}
else
{
// File was successfully opened, so try to determine the
// filetype from the file.
// Read the file to see if it a gzip file.
GzipHeader gzipHeader;
bool isGzip = gzipHeader.readHeader(file);
// The file header has been read, so close the file, so it can
// be re-opened as the correct type.
file.close();
if (isGzip)
{
// This file is a gzip file.
// Check to see if it is BGZF Compression.
if (gzipHeader.isBgzfFile())
{
// This file has BGZF Compression, so set the file
// pointer.
if(myAttemptRecovery) {
// NB: this reader will throw std::runtime_error when it recovers
myFileTypePtr = new BgzfFileTypeRecovery(filename, mode);
} else {
// use the standard bgzf reader (samtools)
myFileTypePtr = new BgzfFileType(filename, mode);
}
}
else
{
// Not BGZF, just a normal gzip.
myFileTypePtr = new GzipFileType(filename, mode);
}
}
else
{
// The file is a uncompressed, uncompressed file,
// so set the myFileTypePtr accordingly.
myFileTypePtr = new UncompressedFileType(filename, mode);
}
}
}
}
if(myFileTypePtr == NULL)
{
return(false);
}
if (!myFileTypePtr->isOpen())
{
// The file was not opened, so delete the pointer and set to null.
delete myFileTypePtr;
myFileTypePtr = NULL;
return false;
}
if(myAllocatedBufferSize == 1)
{
myFileTypePtr->setBuffered(false);
}
else
{
myFileTypePtr->setBuffered(true);
}
myFileName = filename;
return true;
}
// Open a file. This method will open a file with the specified name and
// mode with the fileTypePtr associated with the specified compressionMode.
void InputFile::openFileUsingMode(const char * filename, const char * mode,
ifileCompression compressionMode)
{
switch (compressionMode)
{
case GZIP:
// Gzipped.
myFileTypePtr = new GzipFileType(filename, mode);
break;
case BGZF:
//
// BGZF compression - recovery is possible, so use
// Bgzf recovery reader if asked.
//
if(myAttemptRecovery && ((mode[0] == 'r') || (mode[0] == 'R')))
{
// NB: this reader will throw std::runtime_error when it recovers
myFileTypePtr = new BgzfFileTypeRecovery(filename, mode);
}
else
{
myFileTypePtr = new BgzfFileType(filename, mode);
}
break;
case UNCOMPRESSED:
myFileTypePtr = new UncompressedFileType(filename, mode);
break;
case InputFile::DEFAULT:
default:
// Check the extension. If it is ".gz", treat as gzip.
// otherwise treat it as UNCOMPRESSED.
int lastchar = 0;
while (filename[lastchar] != 0) lastchar++;
if ((lastchar >= 3 &&
filename[lastchar - 3] == '.' &&
filename[lastchar - 2] == 'g' &&
filename[lastchar - 1] == 'z'))
{
// .gz files files should be gzipped.
myFileTypePtr = new GzipFileType(filename, mode);
}
else
{
// Create an uncompressed file.
myFileTypePtr = new UncompressedFileType(filename, mode);
}
break;
}
if(myFileTypePtr == NULL)
{
return;
}
if(myAllocatedBufferSize == 1)
{
myFileTypePtr->setBuffered(false);
}
else
{
myFileTypePtr->setBuffered(true);
}
}
#else
// No zlib, so just treat all files as std files.
// Open a file. Called by the constructor.
// Returns true if the file was successfully opened, false otherwise.
bool InputFile::openFile(const char * filename, const char * mode,
InputFile::ifileCompression compressionMode)
{
// No zlib, so it is a uncompressed, uncompressed file.
myFileTypePtr = new UncompressedFileType(filename, mode);
if(myFileTypePtr == NULL)
{
return(false);
}
if (!myFileTypePtr->isOpen())
{
// The file was not opened, so delete the pointer and set to null.
delete myFileTypePtr;
myFileTypePtr = NULL;
return false;
}
if(myAllocatedBufferSize == 1)
{
myFileTypePtr->setBuffered(false);
}
else
{
myFileTypePtr->setBuffered(true);
}
myFileName = filename;
return true;
}
#endif
InputFile::~InputFile()
{
delete myFileTypePtr;
myFileTypePtr = NULL;
if(myFileBuffer != NULL)
{
delete[] myFileBuffer;
myFileBuffer = NULL;
}
}
int ifprintf(IFILE output, const char * format, ...)
{
String buffer;
va_list ap;
va_start(ap, format);
buffer.vprintf(format, ap);
va_end(ap);
return ::ifwrite(output, (const char *) buffer, buffer.Length());
}
InputFile& operator << (InputFile& stream, double num)
{
String val;
val = num;
stream << val;
return(stream);
}
InputFile& operator << (InputFile& stream, int num)
{
String val;
val = num;
stream << val;
return(stream);
}
InputFile& operator << (InputFile& stream, unsigned int num)
{
String val;
val = num;
stream << val;
return(stream);
}
|