libStatGen Software  1
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:
filefile 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 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);
}
int ifeof ( IFILE  file) [inline]

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

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

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());
}
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:
filefile to be read - IFILE is a pointer to an InputFile object
Returns:
character that was read or EOF.

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());
}
bool ifgetline ( IFILE  file,
void *  buffer,
size_t  max 
) [inline]

Get a line from the file.

Parameters:
filefile to be read - IFILE is a pointer to an InputFile object
bufferthe buffer into which data is to be placed
maxthe maximum size of the buffer, in bytes
Returns:
true if the last character read was an EOF

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.

Parameters:
filenamefile to open ("-" meands stdin/stdout)
modesame format as fopen: "r" for read & "w" for write.
compressionModeset 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 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().

{
    IFILE file = new InputFile(filename, mode, compressionMode);
    if (!file->isOpen())
    {

        // Not open, so delete the file, and return null.
        delete file;
        file = NULL;
    }
    return file;
}
int ifprintf ( IFILE  output,
const char *  format,
  ... 
)

Write to a file using fprintf format.

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

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());
}
unsigned int ifread ( IFILE  file,
void *  buffer,
unsigned int  size 
) [inline]

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

Parameters:
filefile to be read - IFILE is a pointer to an InputFile object
bufferpointer to memory at least size bytes big to write the data into.
sizenumber of bytes to be read
Returns:
number of bytes 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));
}
void ifrewind ( IFILE  file) [inline]

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

Parameters:
filefile 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();
}
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:
filefile to perform seek on - IFILE is a pointer to an InputFile object
offsetoffset into the file to move to (must be from a tell call)
origincan 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 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));
}
int64_t iftell ( IFILE  file) [inline]

Get current position in the file.

Can be fed back into ifseek.

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

References InputFile::iftell().

Referenced by SamFile::GetCurrentPosition().

{
    if(file == NULL)
    {
        return(-1);
    }
    return (file->iftell());
}
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:
filefile to write to - IFILE is a pointer to an InputFile object
bufferbuffer containing size bytes to write to the file.
sizenumber of bytes to write
Returns:
number of bytes written

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));
}
InputFile& operator<< ( InputFile stream,
const std::string &  str 
) [inline]

Write to a file using streaming.

Parameters:
streamfile to write to - IFILE is a pointer to an InputFile object
strstring 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);
}
InputFile& operator<< ( InputFile stream,
const char *  str 
) [inline]

Write to a file using streaming.

Parameters:
streamfile to write to - IFILE is a pointer to an InputFile object
strstring 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);
}
InputFile& operator<< ( InputFile stream,
double  num 
)

Write to a file using streaming.

Parameters:
streamfile to write to - IFILE is a pointer to an InputFile object
numnumber that should be written to the file.

Definition at line 413 of file InputFile.cpp.

{
    String val;
    val = num;
    stream << val;
    return(stream);
}
InputFile& operator<< ( InputFile stream,
int  num 
)

Write to a file using streaming.

Parameters:
streamfile to write to - IFILE is a pointer to an InputFile object
numnumber that should be written to the file.

Definition at line 422 of file InputFile.cpp.

{
    String val;
    val = num;
    stream << val;
    return(stream);
}
InputFile& operator<< ( InputFile stream,
unsigned int  num 
)

Write to a file using streaming.

Parameters:
streamfile to write to - IFILE is a pointer to an InputFile object
numnumber that should be written to the file.

Definition at line 431 of file InputFile.cpp.

{
    String val;
    val = num;
    stream << val;
    return(stream);
}
InputFile& operator<< ( InputFile stream,
char  ch 
) [inline]

Write to a file using streaming.

Parameters:
streamfile to write to - IFILE is a pointer to an InputFile object
chcharacter 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);
}
IFILE operator>> ( IFILE  stream,
std::string &  str 
) [inline]

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)

Parameters:
streamfile to read from - IFILE is a pointer to an InputFile object
stroutput 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;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends