InputFile.h File Reference

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdint.h>
#include "FileType.h"
Include dependency graph for InputFile.h:
This graph shows which files directly or indirectly include this file:

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 InputFileIFILE
 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.
InputFileoperator<< (InputFile &stream, const std::string &str)
 Write to a file using streaming.
InputFileoperator<< (InputFile &stream, const char *str)
 Write to a file using streaming.
InputFileoperator<< (InputFile &stream, double num)
 Write to a file using streaming.
InputFileoperator<< (InputFile &stream, int num)
 Write to a file using streaming.
InputFileoperator<< (InputFile &stream, unsigned int num)
 Write to a file using streaming.
InputFileoperator<< (InputFile &stream, char ch)
 Write to a file using streaming.

Detailed Description

Definition in file InputFile.h.


Function Documentation

int ifclose ( IFILE  file  )  [inline]

Close the file.

Parameters:
file file to be closed - IFILE is a pointer to an InputFile object
Returns:
status of the close (0 is success or if NULL is passed in).

Definition at line 581 of file InputFile.h.

References InputFile::ifclose().

Referenced by FastQFile::closeFile(), GenomeSequence::loadDBSNP(), BamIndex::readIndex(), and SamFile::resetFile().

00582 {
00583     if(file == NULL)
00584     {
00585         // NULL Pointer passed in, so return 0, since no file is open, so
00586         // does not need to be closed.
00587         return(0);
00588     }
00589     int result = file->ifclose();
00590     delete file;
00591     file = NULL;
00592     return(result);
00593 }

int ifeof ( IFILE  file  )  [inline]

Check to see if we have reached the EOF (returns 0 if not EOF).

Parameters:
file file to be checked - IFILE is a pointer to an InputFile object
Returns:
0 if not EOF, any other value means EOF.

Definition at line 655 of file InputFile.h.

References InputFile::ifeof().

Referenced by GlfFile::isEOF(), FastQFile::isEof(), SamFile::IsEOF(), GlfRefSection::read(), FastQFile::readFastQSequence(), and SamRecord::setBufferFromFile().

00656 {
00657     if(file == NULL)
00658     {
00659         // No file, so that is considered to be EOF, so return 1.
00660         return(1);
00661     }
00662     return(file->ifeof());
00663 }

int ifgetc ( IFILE  file  )  [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.

Parameters:
file file to be read - IFILE is a pointer to an InputFile object
Returns:
character that was read or EOF.

Definition at line 616 of file InputFile.h.

References InputFile::ifgetc().

00617 {
00618     if(file == NULL)
00619     {
00620         // return eof since there is no file.
00621         return(EOF);
00622     }
00623     return(file->ifgetc());
00624 }

bool ifgetline ( IFILE  file,
void *  buffer,
size_t  max 
) [inline]

Get a line from the file.

Parameters:
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
Returns:
true if the last character read was an EOF

Definition at line 631 of file InputFile.h.

References InputFile::ifgetline().

00632 {
00633     if(file == NULL)
00634     {
00635         // return eof since there is no file.
00636         return(EOF);
00637     }
00638     return(file->ifgetline(buffer, max));
00639 }

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.

Parameters:
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).
Returns:
IFILE - pointer to the InputFile object that has been opened.

Definition at line 563 of file InputFile.h.

References InputFile::isOpen().

Referenced by GenomeSequence::loadDBSNP(), FastQFile::openFile(), GlfFile::openForRead(), SamFile::OpenForRead(), GlfFile::openForWrite(), SamFile::OpenForWrite(), Tabix::readIndex(), and BamIndex::readIndex().

00565 {
00566     IFILE file = new InputFile(filename, mode, compressionMode);
00567     if (!file->isOpen())
00568     {
00569 
00570         // Not open, so delete the file, and return null.
00571         delete file;
00572         file = NULL;
00573     }
00574     return file;
00575 }

int ifprintf ( IFILE  output,
const char *  format,
  ... 
)

Write to a file using fprintf format.

Parameters:
file file to write to - IFILE is a pointer to an InputFile object
format printf format for writing, followed by parameters.
Returns:
number of bytes written

Definition at line 399 of file InputFile.cpp.

References ifwrite().

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 }

unsigned int ifread ( IFILE  file,
void *  buffer,
unsigned int  size 
) [inline]

Read up to size bytes from the file into the buffer.

Parameters:
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
Returns:
number of bytes read

Definition at line 601 of file InputFile.h.

References InputFile::ifread().

Referenced by SamFile::OpenForRead(), GlfRefSection::read(), GlfRecord::read(), GlfHeader::read(), Tabix::readIndex(), BamIndex::readIndex(), and SamRecord::setBufferFromFile().

00602 {
00603     if(file == NULL)
00604     {
00605         // No file was passed in, so 0 bytes were read.
00606         return(0);
00607     }
00608     return(file->ifread(buffer, size));
00609 }

void ifrewind ( IFILE  file  )  [inline]

Reset to the beginning of the file (cannot be done for stdin/stdout).

Parameters:
file file to be rewound - IFILE is a pointer to an InputFile object

Definition at line 643 of file InputFile.h.

References InputFile::ifrewind().

Referenced by SamFile::OpenForRead().

00644 {
00645     if(file == NULL)
00646     {
00647         return;
00648     }
00649     file->ifrewind();
00650 }

bool ifseek ( IFILE  file,
int64_t  offset,
int  origin 
) [inline]

Seek to the specified position (result from an iftell), but cannot be done for stdin/stdout.

Parameters:
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
Returns:
true on successful seek and false on a failed seek.

Definition at line 702 of file InputFile.h.

References InputFile::ifseek().

00703 {
00704     if(file == NULL)
00705     {
00706         // Could not see since no file was specified.
00707         return(false);
00708     }
00709     return (file->ifseek(offset, origin));
00710 }

int64_t iftell ( IFILE  file  )  [inline]

Get current position in the file.

Can be fed back into ifseek.

Parameters:
file file to perform tell on - IFILE is a pointer to an InputFile object
Returns:
current position in the file, -1 indicates an error.

Definition at line 683 of file InputFile.h.

References InputFile::iftell().

Referenced by SamFile::GetCurrentPosition().

00684 {
00685     if(file == NULL)
00686     {
00687         return(-1);
00688     }
00689     return (file->iftell());
00690 }

unsigned int ifwrite ( IFILE  file,
const void *  buffer,
unsigned int  size 
) [inline]

Write the specified number of bytes from the specified buffer into the file.

Parameters:
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
Returns:
number of bytes written

Definition at line 670 of file InputFile.h.

References InputFile::ifwrite().

Referenced by ifprintf(), GlfRefSection::write(), GlfHeader::write(), and SamRecord::writeRecordBuffer().

00671 {
00672     if(file == NULL)
00673     {
00674         // No file specified, so retun 0 bytes written.
00675         return(0);
00676     }
00677     return(file->ifwrite(buffer, size));
00678 }

InputFile& operator<< ( InputFile stream,
char  ch 
) [inline]

Write to a file using streaming.

Parameters:
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 783 of file InputFile.h.

References InputFile::ifwrite().

00784 {
00785     unsigned int numWritten = 
00786         stream.ifwrite(&ch, 1);
00787     if(1 != numWritten)
00788     {
00789         std::cerr << "Failed to stream to IFILE, expected 1, but only wrote " 
00790                   << numWritten << std::endl;
00791     }
00792     return(stream);
00793 }

InputFile& operator<< ( InputFile stream,
unsigned int  num 
)

Write to a file using streaming.

Parameters:
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 432 of file InputFile.cpp.

00433 {
00434     String val;
00435     val = num;
00436     stream << val;
00437     return(stream);
00438 }

InputFile& operator<< ( InputFile stream,
int  num 
)

Write to a file using streaming.

Parameters:
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 423 of file InputFile.cpp.

00424 {
00425     String val;
00426     val = num;
00427     stream << val;
00428     return(stream);
00429 }

InputFile& operator<< ( InputFile stream,
double  num 
)

Write to a file using streaming.

Parameters:
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 414 of file InputFile.cpp.

00415 {
00416     String val;
00417     val = num;
00418     stream << val;
00419     return(stream);
00420 }

InputFile& operator<< ( InputFile stream,
const char *  str 
) [inline]

Write to a file using streaming.

Parameters:
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 750 of file InputFile.h.

References InputFile::ifwrite().

00751 {
00752     unsigned int numExpected = strlen(str);
00753     unsigned int numWritten = 
00754         stream.ifwrite(str, numExpected);
00755     if(numExpected != numWritten)
00756     {
00757         std::cerr << "Failed to stream to IFILE, expected " 
00758                   << numExpected << " but only wrote "
00759                   << numWritten << std::endl;
00760     }
00761     return(stream);
00762 }

InputFile& operator<< ( InputFile stream,
const std::string &  str 
) [inline]

Write to a file using streaming.

Parameters:
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 733 of file InputFile.h.

References InputFile::ifwrite().

00734 {
00735     unsigned int numExpected = str.length();
00736     unsigned int numWritten = 
00737         stream.ifwrite(str.c_str(), numExpected);
00738     if(numExpected != numWritten)
00739     {
00740         std::cerr << "Failed to stream to IFILE, expected " 
00741                   << numExpected << " but only wrote "
00742                   << numWritten << std::endl;
00743     }
00744     return(stream);
00745 }

IFILE operator>> ( IFILE  stream,
std::string &  str 
) [inline]

Read a line from a file using streaming.

Parameters:
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 721 of file InputFile.h.

References InputFile::ifgetc().

00722 {
00723     str.clear();
00724     int ch;
00725     // not safe... newline handling?
00726     while ((ch = stream->ifgetc())!=EOF && (ch != '\n')) str.push_back(ch);
00727     return stream;
00728 }

Generated on Mon Feb 11 13:45:20 2013 for libStatGen Software by  doxygen 1.6.3