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 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. More...
int | ifclose (IFILE &file)
| Close the file. More...
unsigned int | ifread (IFILE file, void *buffer, unsigned int size)
| Read up to size bytes from the file into the buffer. More...
int | ifgetc (IFILE file)
| Get a character from the file. More...
bool | ifgetline (IFILE file, void *buffer, size_t max)
| Get a line from the file. More...
void | ifrewind (IFILE file)
| Reset to the beginning of the file (cannot be done for stdin/stdout). More...
int | ifeof (IFILE file)
| Check to see if we have reached the EOF (returns 0 if not EOF). More...
unsigned int | ifwrite (IFILE file, const void *buffer, unsigned int size)
| Write the specified number of bytes from the specified buffer into the file. More...
int64_t | iftell (IFILE file)
| Get current position in the file. More...
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. More...
int | ifprintf (IFILE output, const char *format,...)
| Write to a file using fprintf format. More...
IFILE | operator>> (IFILE stream, std::string &str)
| Read a line from a file using streaming. More...
InputFile & | operator<< (InputFile &stream, const std::string &str)
| Write to a file using streaming. More...
InputFile & | operator<< (InputFile &stream, const char *str)
| Write to a file using streaming. More...
InputFile & | operator<< (InputFile &stream, double num)
| Write to a file using streaming. More...
InputFile & | operator<< (InputFile &stream, int num)
| Write to a file using streaming. More...
InputFile & | operator<< (InputFile &stream, unsigned int num)
| Write to a file using streaming. More...
InputFile & | operator<< (InputFile &stream, char ch)
| Write to a file using streaming. More...

## Function Documentation

## ifclose()

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

{
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);
}

References InputFile::ifclose().

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

## ifeof()

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

{
if(file == NULL)
{
// No file, so that is considered to be EOF, so return 1.
return(1);
}
return(file->ifeof());
}

References InputFile::ifeof().

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

## ifgetc()

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

{
if(file == NULL)
{
// return eof since there is no file.
return(EOF);
}
return(file->ifgetc());
}

References InputFile::ifgetc().

## ifgetline()

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

{
if(file == NULL)
{
// return eof since there is no file.
return(true);
}
return(file->ifgetline(buffer, max));
}

References InputFile::ifgetline().

## ifopen()

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

{
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;
}

References InputFile::isOpen().

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

## ifprintf()

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

{
String buffer;
va_list ap;
va_start(ap, format);
buffer.vprintf(format, ap);
va_end(ap);
return ::ifwrite(output, (const char *) buffer, buffer.Length());
}

References ifwrite().

## ifread()

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

{
if(file == NULL)
{
// No file was passed in, so 0 bytes were read.
return(0);
}
return(file->ifread(buffer, size));
}

References InputFile::ifread().

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

## ifrewind()

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

{
if(file == NULL)
{
return;
}
file->ifrewind();
}

References InputFile::ifrewind().

Referenced by SamFile::OpenForRead().

## ifseek()

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

{
if(file == NULL)
{
// Could not see since no file was specified.
return(false);
}
return (file->ifseek(offset, origin));
}

References InputFile::ifseek().

## iftell()

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

{
if(file == NULL)
{
return(-1);
}
return (file->iftell());
}

References InputFile::iftell().

Referenced by SamFile::GetCurrentPosition().

## ifwrite()

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

{
if(file == NULL)
{
// No file specified, so retun 0 bytes written.
return(0);
}
return(file->ifwrite(buffer, size));
}

References InputFile::ifwrite().

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

## operator<<() [1/6]

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

{
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);
}

References InputFile::ifwrite().

## operator<<() [2/6]

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

{
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);
}

References InputFile::ifwrite().

## operator<<() [3/6]

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

{
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);
}

References InputFile::ifwrite().

## operator<<() [4/6]

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

{
String val;
val = num;
stream << val;
return(stream);
}

## operator<<() [5/6]

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

{
String val;
val = num;
stream << val;
return(stream);
}

## operator<<() [6/6]

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

{
String val;
val = num;
stream << val;
return(stream);
}

## operator>>()

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

  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.

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

References InputFile::ifgetc().

InputFile::ifwrite
unsigned int ifwrite(const void *buffer, unsigned int size)
Write the specified buffer into the file.
Definition: InputFile.h:411
InputFile::iftell
int64_t iftell()
Get current position in the file.
Definition: InputFile.h:436
String
Definition: StringBasics.h:38
InputFile::isOpen
bool isOpen() const
Returns whether or not the file was successfully opened.
Definition: InputFile.h:423
InputFile::ifeof
int ifeof() const
Check to see if we have reached the EOF.
Definition: InputFile.h:386
InputFile::ifgetc
int ifgetc()
Get a character from the file.
Definition: InputFile.h:324
InputFile::ifgetline
bool ifgetline(void *voidBuffer, size_t max)
Get a line from the file.
Definition: InputFile.h:347
InputFile::ifseek
bool ifseek(int64_t offset, int origin)
Seek to the specified offset from the origin.
Definition: InputFile.h:457
InputFile::ifclose
int ifclose()
Close the file.
Definition: InputFile.h:133
InputFile
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition: InputFile.h:36
InputFile::ifread
int ifread(void *buffer, unsigned int size)
Read size bytes from the file into the buffer.
Definition: InputFile.h:153
ifwrite
unsigned int ifwrite(IFILE file, const void *buffer, unsigned int size)
Write the specified number of bytes from the specified buffer into the file.
Definition: InputFile.h:669
InputFile::ifrewind
void ifrewind()
Reset to the beginning of the file.
Definition: InputFile.h:368