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.
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.

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 518 of file InputFile.h.

References InputFile::ifclose().

Referenced by SamFile::resetFile().

00519 {
00520     if(file == NULL)
00521     {
00522         // NULL Pointer passed in, so return 0, since no file is open, so
00523         // does not need to be closed.
00524         return(0);
00525     }
00526     int result = file->ifclose();
00527     delete file;
00528     file = NULL;
00529     return(result);
00530 }

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 577 of file InputFile.h.

References InputFile::ifeof().

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

00578 {
00579     if(file == NULL)
00580     {
00581         // No file, so that is considered to be EOF, so return 1.
00582         return(1);
00583     }
00584     return(file->ifeof());
00585 }

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 553 of file InputFile.h.

References InputFile::ifgetc().

00554 {
00555     if(file == NULL)
00556     {
00557         // return eof since there is no file.
00558         return(EOF);
00559     }
00560     return(file->ifgetc());
00561 }

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 500 of file InputFile.h.

References InputFile::isOpen().

Referenced by GlfFile::openForRead(), SamFile::OpenForRead(), GlfFile::openForWrite(), SamFile::OpenForWrite(), and BamIndex::readIndex().

00502 {
00503     IFILE file = new InputFile(filename, mode, compressionMode);
00504     if (!file->isOpen())
00505     {
00506 
00507         // Not open, so delete the file, and return null.
00508         delete file;
00509         file = NULL;
00510     }
00511     return file;
00512 }

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 282 of file InputFile.cpp.

References ifwrite().

00283 {
00284     String buffer;
00285 
00286     va_list  ap;
00287     va_start(ap, format);
00288 
00289     buffer.vprintf(format, ap);
00290 
00291     va_end(ap);
00292 
00293     return ::ifwrite(output, (const char *) buffer, buffer.Length());
00294 }

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 538 of file InputFile.h.

References InputFile::ifread().

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

00539 {
00540     if(file == NULL)
00541     {
00542         // No file was passed in, so 0 bytes were read.
00543         return(0);
00544     }
00545     return(file->ifread(buffer, size));
00546 }

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 565 of file InputFile.h.

References InputFile::ifrewind().

Referenced by SamFile::OpenForRead().

00566 {
00567     if(file == NULL)
00568     {
00569         return;
00570     }
00571     file->ifrewind();
00572 }

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 624 of file InputFile.h.

References InputFile::ifseek().

Referenced by SamFile::readIndexedRecord().

00625 {
00626     if(file == NULL)
00627     {
00628         // Could not see since no file was specified.
00629         return(false);
00630     }
00631     return (file->ifseek(offset, origin));
00632 }

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 605 of file InputFile.h.

References InputFile::iftell().

Referenced by SamFile::GetCurrentPosition(), and SamFile::readIndexedRecord().

00606 {
00607     if(file == NULL)
00608     {
00609         return(-1);
00610     }
00611     return (file->iftell());
00612 }

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 592 of file InputFile.h.

References InputFile::ifwrite().

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

00593 {
00594     if(file == NULL)
00595     {
00596         // No file specified, so retun 0 bytes written.
00597         return(0);
00598     }
00599     return(file->ifwrite(buffer, size));
00600 }

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 643 of file InputFile.h.

References InputFile::ifgetc().

00644 {
00645     str.clear();
00646     int ch;
00647     // not safe... newline handling?
00648     while ((ch = stream->ifgetc())!=EOF && (ch != '\n')) str.push_back(ch);
00649     return stream;
00650 }

Generated on Tue Sep 6 17:52:01 2011 for libStatGen Software by  doxygen 1.6.3