libStatGen Software
1
|
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdint.h>
#include "FileType.h"
Go to the source code of this file.
Classes | |
class | InputFile |
Class for easily reading/writing files without having to worry about file type (uncompressed, gzip, bgzf) when reading. More... | |
Typedefs | |
typedef InputFile * | IFILE |
Define IFILE as a pointer to an InputFile object. | |
Functions | |
IFILE | ifopen (const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT) |
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout. | |
int | ifclose (IFILE &file) |
Close the file. | |
unsigned int | ifread (IFILE file, void *buffer, unsigned int size) |
Read up to size bytes from the file into the buffer. | |
int | ifgetc (IFILE file) |
Get a character from the file. | |
bool | ifgetline (IFILE file, void *buffer, size_t max) |
Get a line from the file. | |
void | ifrewind (IFILE file) |
Reset to the beginning of the file (cannot be done for stdin/stdout). | |
int | ifeof (IFILE file) |
Check to see if we have reached the EOF (returns 0 if not EOF). | |
unsigned int | ifwrite (IFILE file, const void *buffer, unsigned int size) |
Write the specified number of bytes from the specified buffer into the file. | |
int64_t | iftell (IFILE file) |
Get current position in the file. | |
bool | ifseek (IFILE file, int64_t offset, int origin) |
Seek to the specified position (result from an iftell), but cannot be done for stdin/stdout. | |
int | ifprintf (IFILE output, const char *format,...) |
Write to a file using fprintf format. | |
IFILE | operator>> (IFILE stream, std::string &str) |
Read a line from a file using streaming. | |
InputFile & | operator<< (InputFile &stream, const std::string &str) |
Write to a file using streaming. | |
InputFile & | operator<< (InputFile &stream, const char *str) |
Write to a file using streaming. | |
InputFile & | operator<< (InputFile &stream, double num) |
Write to a file using streaming. | |
InputFile & | operator<< (InputFile &stream, int num) |
Write to a file using streaming. | |
InputFile & | operator<< (InputFile &stream, unsigned int num) |
Write to a file using streaming. | |
InputFile & | operator<< (InputFile &stream, char ch) |
Write to a file using streaming. |
Definition in file InputFile.h.
Close the file.
file | file to be closed - IFILE is a pointer to an InputFile object |
Definition at line 580 of file InputFile.h.
References InputFile::ifclose().
Referenced by FastQFile::closeFile(), GenomeSequence::loadDBSNP(), BamIndex::readIndex(), and SamFile::resetFile().
{ if(file == NULL) { // NULL Pointer passed in, so return 0, since no file is open, so // does not need to be closed. return(0); } int result = file->ifclose(); delete file; file = NULL; return(result); }
Check to see if we have reached the EOF (returns 0 if not EOF).
file | file to be checked - IFILE is a pointer to an InputFile object |
Definition at line 654 of file InputFile.h.
References InputFile::ifeof().
Referenced by FastQFile::isEof(), GlfFile::isEOF(), SamFile::IsEOF(), GlfRefSection::read(), FastQFile::readFastQSequence(), and SamRecord::setBufferFromFile().
{ if(file == NULL) { // No file, so that is considered to be EOF, so return 1. return(1); } return(file->ifeof()); }
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.
file | file to be read - IFILE is a pointer to an InputFile object |
Definition at line 615 of file InputFile.h.
References InputFile::ifgetc().
{ if(file == NULL) { // return eof since there is no file. return(EOF); } return(file->ifgetc()); }
Get a line from the file.
file | file to be read - IFILE is a pointer to an InputFile object |
buffer | the buffer into which data is to be placed |
max | the maximum size of the buffer, in bytes |
Definition at line 630 of file InputFile.h.
References InputFile::ifgetline().
{ if(file == NULL) { // return eof since there is no file. return(true); } return(file->ifgetline(buffer, max)); }
IFILE ifopen | ( | const char * | filename, |
const char * | mode, | ||
InputFile::ifileCompression | compressionMode = InputFile::DEFAULT |
||
) | [inline] |
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout.
filename | file to open ("-" meands stdin/stdout) |
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 not from stdin, the compression type is determined by reading the file). |
Definition at line 562 of file InputFile.h.
References InputFile::isOpen().
Referenced by GenomeSequence::loadDBSNP(), FastQFile::openFile(), GlfFile::openForRead(), SamFile::OpenForRead(), GlfFile::openForWrite(), SamFile::OpenForWrite(), BamIndex::readIndex(), and Tabix::readIndex().
Write to a file using fprintf format.
file | file to write to - IFILE is a pointer to an InputFile object |
format | printf format for writing, followed by parameters. |
Definition at line 398 of file InputFile.cpp.
References ifwrite().
{ String buffer; va_list ap; va_start(ap, format); buffer.vprintf(format, ap); va_end(ap); return ::ifwrite(output, (const char *) buffer, buffer.Length()); }
Read up to size bytes from the file into the buffer.
file | file to be read - IFILE is a pointer to an InputFile object |
buffer | pointer to memory at least size bytes big to write the data into. |
size | number of bytes to be read |
Definition at line 600 of file InputFile.h.
References InputFile::ifread().
Referenced by SamFile::OpenForRead(), GlfRecord::read(), GlfHeader::read(), GlfRefSection::read(), BamIndex::readIndex(), Tabix::readIndex(), and SamRecord::setBufferFromFile().
{ if(file == NULL) { // No file was passed in, so 0 bytes were read. return(0); } return(file->ifread(buffer, size)); }
Reset to the beginning of the file (cannot be done for stdin/stdout).
file | file to be rewound - IFILE is a pointer to an InputFile object |
Definition at line 642 of file InputFile.h.
References InputFile::ifrewind().
Referenced by SamFile::OpenForRead().
{ if(file == NULL) { return; } file->ifrewind(); }
Seek to the specified position (result from an iftell), but cannot be done for stdin/stdout.
file | file to perform seek on - IFILE is a pointer to an InputFile object |
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 701 of file InputFile.h.
References InputFile::ifseek().
{ if(file == NULL) { // Could not see since no file was specified. return(false); } return (file->ifseek(offset, origin)); }
Get current position in the file.
Can be fed back into ifseek.
file | file to perform tell on - IFILE is a pointer to an InputFile object |
Definition at line 682 of file InputFile.h.
References InputFile::iftell().
Referenced by SamFile::GetCurrentPosition().
{ if(file == NULL) { return(-1); } return (file->iftell()); }
Write the specified number of bytes from the specified buffer into the file.
file | file to write to - IFILE is a pointer to an InputFile object |
buffer | buffer containing size bytes to write to the file. |
size | number of bytes to write |
Definition at line 669 of file InputFile.h.
References InputFile::ifwrite().
Referenced by ifprintf(), GlfHeader::write(), GlfRefSection::write(), and SamRecord::writeRecordBuffer().
{ if(file == NULL) { // No file specified, so retun 0 bytes written. return(0); } return(file->ifwrite(buffer, size)); }
Write to a file using streaming.
stream | file to write to - IFILE is a pointer to an InputFile object |
str | string containing what should be written to the file. |
Definition at line 736 of file InputFile.h.
References InputFile::ifwrite().
{ unsigned int numExpected = str.length(); unsigned int numWritten = stream.ifwrite(str.c_str(), numExpected); if(numExpected != numWritten) { std::cerr << "Failed to stream to IFILE, expected " << numExpected << " but only wrote " << numWritten << std::endl; } return(stream); }
Write to a file using streaming.
stream | file to write to - IFILE is a pointer to an InputFile object |
str | string containing what should be written to the file. |
Definition at line 753 of file InputFile.h.
References InputFile::ifwrite().
{ unsigned int numExpected = strlen(str); unsigned int numWritten = stream.ifwrite(str, numExpected); if(numExpected != numWritten) { std::cerr << "Failed to stream to IFILE, expected " << numExpected << " but only wrote " << numWritten << std::endl; } return(stream); }
Write to a file using streaming.
stream | file to write to - IFILE is a pointer to an InputFile object |
num | number that should be written to the file. |
Definition at line 413 of file InputFile.cpp.
{ String val; val = num; stream << val; return(stream); }
Write to a file using streaming.
stream | file to write to - IFILE is a pointer to an InputFile object |
num | number that should be written to the file. |
Definition at line 422 of file InputFile.cpp.
{ String val; val = num; stream << val; return(stream); }
Write to a file using streaming.
stream | file to write to - IFILE is a pointer to an InputFile object |
num | number that should be written to the file. |
Definition at line 431 of file InputFile.cpp.
{ String val; val = num; stream << val; return(stream); }
Write to a file using streaming.
stream | file to write to - IFILE is a pointer to an InputFile object |
ch | character that should be written to the file. |
Definition at line 786 of file InputFile.h.
References InputFile::ifwrite().
{ unsigned int numWritten = stream.ifwrite(&ch, 1); if(1 != numWritten) { std::cerr << "Failed to stream to IFILE, expected 1, but only wrote " << numWritten << std::endl; } return(stream); }
Read a line from a file using streaming.
Will not fail when the file hits EOF, so do not do: while(iFile >> iStr) unless within your loop you check for ifeof and break. Instead, do something like: while(!iFile->ifeof() && iFile >> iStr)
stream | file to read from - IFILE is a pointer to an InputFile object |
str | output string containing the line read from the file. |
Definition at line 724 of file InputFile.h.
References InputFile::ifgetc().
{ str.clear(); int ch; // not safe... newline handling? while ((ch = stream->ifgetc())!=EOF && (ch != '\n')) str.push_back(ch); return stream; }