libStatGen Software 1
|
Class for easily reading/writing files without having to worry about file type (uncompressed, gzip, bgzf) when reading. More...
#include <InputFile.h>
Public Types | |
enum | ifileCompression { DEFAULT , UNCOMPRESSED , GZIP , BGZF } |
Compression to use when writing a file & decompression used when reading a file from stdin. More... | |
Public Member Functions | |
InputFile () | |
Default constructor. More... | |
~InputFile () | |
Destructor. More... | |
InputFile (const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT) | |
Constructor for opening a file. More... | |
void | bufferReads (unsigned int bufferSize=DEFAULT_BUFFER_SIZE) |
Set the buffer size for reading from files so that bufferSize bytes are read at a time and stored until accessed by another read call. More... | |
void | disableBuffering () |
Disable read buffering. More... | |
int | ifclose () |
Close the file. More... | |
int | ifread (void *buffer, unsigned int size) |
Read size bytes from the file into the buffer. More... | |
int | readTilChar (const std::string &stopChars, std::string &stringRef) |
Read until the specified characters, returning which character was found causing the stop, -1 returned for EOF, storing the other read characters into the specified string. More... | |
int | readTilChar (const std::string &stopChars) |
Read until the specified characters, returning which character was found causing the stop, -1 returned for EOF, dropping all read chars. More... | |
int | discardLine () |
Read until the end of the line, discarding the characters, returning -1 returned for EOF and returning 0 if the end of the line was found. More... | |
int | readLine (std::string &line) |
Read, appending the characters into the specified string until new line or EOF is found, returning -1 if EOF is found first and 0 if new line is found first. More... | |
int | readTilTab (std::string &field) |
Read, appending the characters into the specified string until tab, new line, or EOF is found, returning -1 if EOF is found first, 0 if new line is found first, or 1 if a tab is found first. More... | |
int | ifgetc () |
Get a character from the file. More... | |
bool | ifgetline (void *voidBuffer, size_t max) |
Get a line from the file. More... | |
void | ifrewind () |
Reset to the beginning of the file. More... | |
int | ifeof () const |
Check to see if we have reached the EOF. More... | |
unsigned int | ifwrite (const void *buffer, unsigned int size) |
Write the specified buffer into the file. More... | |
bool | isOpen () const |
Returns whether or not the file was successfully opened. More... | |
int64_t | iftell () |
Get current position in the file. More... | |
bool | ifseek (int64_t offset, int origin) |
Seek to the specified offset from the origin. More... | |
const char * | getFileName () const |
Get the filename that is currently opened. More... | |
void | setAttemptRecovery (bool flag=false) |
Enable (default) or disable recovery. More... | |
bool | attemptRecoverySync (bool(*checkSignature)(void *data), int length) |
bool | openFile (const char *filename, const char *mode, InputFile::ifileCompression compressionMode) |
Protected Member Functions | |
int | readFromFile (void *buffer, unsigned int size) |
Protected Attributes | |
FileType * | myFileTypePtr |
unsigned int | myAllocatedBufferSize |
char * | myFileBuffer |
int | myBufferIndex |
int | myCurrentBufferSize |
std::string | myFileName |
Static Protected Attributes | |
static const unsigned int | DEFAULT_BUFFER_SIZE = 65536 |
Class for easily reading/writing files without having to worry about file type (uncompressed, gzip, bgzf) when reading.
It hides the low level file operations/structure from the user, allowing them to generically open and operate on a file using the same interface without knowing the file format (standard uncompressed, gzip, or bgzf). For writing, the user must specify the file type. There is a typedef IFILE which is InputFile* and setup to mimic FILE including global methods that take IFILE as a parameter.
Definition at line 36 of file InputFile.h.
Compression to use when writing a file & decompression used when reading a file from stdin.
Any other read checks the file to determine how to uncompress it.
Enumerator | |
---|---|
DEFAULT | Check the extension, if it is ".gz", treat as gzip, otherwise treat it as UNCOMPRESSED. |
UNCOMPRESSED | uncompressed file. |
GZIP | gzip file. |
BGZF | bgzf file. |
Definition at line 44 of file InputFile.h.
|
inline |
Default constructor.
Definition at line 52 of file InputFile.h.
InputFile::~InputFile | ( | ) |
Destructor.
Definition at line 385 of file InputFile.cpp.
InputFile::InputFile | ( | const char * | filename, |
const char * | mode, | ||
InputFile::ifileCompression | compressionMode = InputFile::DEFAULT |
||
) |
Constructor for opening a file.
filename | file to open |
mode | same format as fopen: "r" for read & "w" for write. |
compressionMode | set the type of file to open for writing or for reading from stdin (when reading files, the compression type is determined by reading the file). |
Definition at line 28 of file InputFile.cpp.
|
inline |
Definition at line 490 of file InputFile.h.
|
inline |
Set the buffer size for reading from files so that bufferSize bytes are read at a time and stored until accessed by another read call.
This improves performance over reading the file small bits at a time. Buffering reads disables the tell call for bgzf files. Any previous values in the buffer will be deleted.
bufferSize | number of bytes to read/buffer at a time, turn off read buffering by setting bufferSize = 1; |
Definition at line 83 of file InputFile.h.
Referenced by disableBuffering().
|
inline |
Disable read buffering.
Definition at line 121 of file InputFile.h.
References bufferReads().
Referenced by SamFile::DisableBuffering().
int InputFile::discardLine | ( | ) |
Read until the end of the line, discarding the characters, returning -1 returned for EOF and returning 0 if the end of the line was found.
Definition at line 95 of file InputFile.cpp.
References ifgetc().
|
inline |
Get the filename that is currently opened.
Definition at line 473 of file InputFile.h.
Referenced by SamFile::IsStream(), SamFile::ReadBamIndex(), and SamRecord::setBufferFromFile().
|
inline |
Close the file.
Definition at line 133 of file InputFile.h.
Referenced by ifclose().
|
inline |
Check to see if we have reached the EOF.
Definition at line 386 of file InputFile.h.
Referenced by ifeof(), readLine(), and readTilTab().
|
inline |
Get a character from the file.
Read a character from the internal buffer, or if the end of the buffer has been reached, read from the file into the buffer and return index 0.
Definition at line 324 of file InputFile.h.
Referenced by discardLine(), ifgetc(), ifgetline(), operator>>(), readLine(), readTilChar(), and readTilTab().
|
inline |
Get a line from the file.
buffer | the buffer into which data is to be placed |
max | the maximum size of the buffer, in bytes |
Definition at line 347 of file InputFile.h.
References ifgetc().
Referenced by ifgetline().
|
inline |
Read size bytes from the file into the buffer.
buffer | pointer to memory at least size bytes big to write the data into. |
size | number of bytes to be read |
Definition at line 153 of file InputFile.h.
Referenced by ifread().
|
inline |
Reset to the beginning of the file.
Definition at line 368 of file InputFile.h.
Referenced by ifrewind().
|
inline |
Seek to the specified offset from the origin.
offset | offset into the file to move to (must be from a tell call) |
origin | can be any of the following: Note: not all are valid for all filetypes. SEEK_SET - Beginning of file SEEK_CUR - Current position of the file pointer SEEK_END - End of file |
Definition at line 457 of file InputFile.h.
Referenced by ifseek().
|
inline |
Get current position in the file.
Definition at line 436 of file InputFile.h.
Referenced by iftell().
|
inline |
Write the specified buffer into the file.
buffer | buffer containing size bytes to write to the file. |
size | number of bytes to write |
Definition at line 411 of file InputFile.h.
Referenced by ifwrite(), and operator<<().
|
inline |
Returns whether or not the file was successfully opened.
Definition at line 423 of file InputFile.h.
Referenced by ifopen(), SamFile::IsOpen(), FastQFile::isOpen(), GlfHeader::read(), SamRecord::setBufferFromFile(), GlfHeader::write(), and SamRecord::writeRecordBuffer().
bool InputFile::openFile | ( | const char * | filename, |
const char * | mode, | ||
InputFile::ifileCompression | compressionMode | ||
) |
Definition at line 353 of file InputFile.cpp.
|
inlineprotected |
Definition at line 505 of file InputFile.h.
int InputFile::readLine | ( | std::string & | line | ) |
Read, appending the characters into the specified string until new line or EOF is found, returning -1 if EOF is found first and 0 if new line is found first.
The new line and EOF are not written into the specified string.
line | reference to a string that the read characters should be apppended to (does not include the new line or eof). |
Definition at line 112 of file InputFile.cpp.
int InputFile::readTilChar | ( | const std::string & | stopChars | ) |
Read until the specified characters, returning which character was found causing the stop, -1 returned for EOF, dropping all read chars.
Note: If stopChars is just '
', discardLine is faster.
stopChars | characters to stop reading when they are hit. |
Definition at line 73 of file InputFile.cpp.
References ifgetc().
int InputFile::readTilChar | ( | const std::string & | stopChars, |
std::string & | stringRef | ||
) |
Read until the specified characters, returning which character was found causing the stop, -1 returned for EOF, storing the other read characters into the specified string.
Note: If stopChars is just '
', readLine is faster and if stopChars is just '
' and '\t', readTilTab is faster.
stopChars | characters to stop reading when they are hit. |
stringRef | reference to a string that the read characters should be apppended to (does not include the stopchar). |
Definition at line 44 of file InputFile.cpp.
References ifgetc().
int InputFile::readTilTab | ( | std::string & | field | ) |
Read, appending the characters into the specified string until tab, new line, or EOF is found, returning -1 if EOF is found first, 0 if new line is found first, or 1 if a tab is found first.
The tab, new line, and EOF are not written into the specified string.
field | reference to a string that the read characters should be apppended to (does not include the tab, new line, or eof). |
Definition at line 133 of file InputFile.cpp.
|
inline |
Enable (default) or disable recovery.
When true, we can attach a myFileTypePtr that implements a recovery capable decompressor. This requires that the caller be able to catch the exception XXX "blah blah blah".
Definition at line 485 of file InputFile.h.
Referenced by SamFile::OpenForRead().
|
staticprotected |
Definition at line 526 of file InputFile.h.
|
protected |
Definition at line 531 of file InputFile.h.
|
protected |
Definition at line 539 of file InputFile.h.
|
protected |
Definition at line 544 of file InputFile.h.
|
protected |
Definition at line 535 of file InputFile.h.
|
protected |
Definition at line 546 of file InputFile.h.
|
protected |
Definition at line 529 of file InputFile.h.