libStatGen Software 1
Loading...
Searching...
No Matches
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.
 

Typedef Documentation

◆ IFILE

typedef InputFile* IFILE

Define IFILE as a pointer to an InputFile object.

Definition at line 551 of file InputFile.h.

Function Documentation

◆ ifclose()

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.

581{
582 if(file == NULL)
583 {
584 // NULL Pointer passed in, so return 0, since no file is open, so
585 // does not need to be closed.
586 return(0);
587 }
588 int result = file->ifclose();
589 delete file;
590 file = NULL;
591 return(result);
592}
int ifclose()
Close the file.
Definition InputFile.h:133

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

655{
656 if(file == NULL)
657 {
658 // No file, so that is considered to be EOF, so return 1.
659 return(1);
660 }
661 return(file->ifeof());
662}
int ifeof() const
Check to see if we have reached the EOF.
Definition InputFile.h:386

References InputFile::ifeof().

Referenced by FastQFile::isEof(), GlfFile::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
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.

616{
617 if(file == NULL)
618 {
619 // return eof since there is no file.
620 return(EOF);
621 }
622 return(file->ifgetc());
623}
int ifgetc()
Get a character from the file.
Definition InputFile.h:324

References InputFile::ifgetc().

◆ ifgetline()

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.

631{
632 if(file == NULL)
633 {
634 // return eof since there is no file.
635 return(true);
636 }
637 return(file->ifgetline(buffer, max));
638}
bool ifgetline(void *voidBuffer, size_t max)
Get a line from the file.
Definition InputFile.h:347

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

564{
565 IFILE file = new InputFile(filename, mode, compressionMode);
566 if (!file->isOpen())
567 {
568
569 // Not open, so delete the file, and return null.
570 delete file;
571 file = NULL;
572 }
573 return file;
574}
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition InputFile.h:37
bool isOpen() const
Returns whether or not the file was successfully opened.
Definition InputFile.h:423

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

399{
400 String buffer;
401
402 va_list ap;
403 va_start(ap, format);
404
405 buffer.vprintf(format, ap);
406
407 va_end(ap);
408
409 return ::ifwrite(output, (const char *) buffer, buffer.Length());
410}

◆ ifread()

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.

601{
602 if(file == NULL)
603 {
604 // No file was passed in, so 0 bytes were read.
605 return(0);
606 }
607 return(file->ifread(buffer, size));
608}
int ifread(void *buffer, unsigned int size)
Read size bytes from the file into the buffer.
Definition InputFile.h:153

References InputFile::ifread().

Referenced by SamFile::OpenForRead(), GlfHeader::read(), GlfRecord::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
filefile to be rewound - IFILE is a pointer to an InputFile object

Definition at line 642 of file InputFile.h.

643{
644 if(file == NULL)
645 {
646 return;
647 }
648 file->ifrewind();
649}
void ifrewind()
Reset to the beginning of the file.
Definition InputFile.h:368

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

702{
703 if(file == NULL)
704 {
705 // Could not see since no file was specified.
706 return(false);
707 }
708 return (file->ifseek(offset, origin));
709}
bool ifseek(int64_t offset, int origin)
Seek to the specified offset from the origin.
Definition InputFile.h:457

References InputFile::ifseek().

◆ iftell()

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.

683{
684 if(file == NULL)
685 {
686 return(-1);
687 }
688 return (file->iftell());
689}
int64_t iftell()
Get current position in the file.
Definition InputFile.h:436

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

670{
671 if(file == NULL)
672 {
673 // No file specified, so retun 0 bytes written.
674 return(0);
675 }
676 return(file->ifwrite(buffer, size));
677}
unsigned int ifwrite(const void *buffer, unsigned int size)
Write the specified buffer into the file.
Definition InputFile.h:411

References InputFile::ifwrite().

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

◆ operator<<() [1/6]

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.

787{
788 unsigned int numWritten =
789 stream.ifwrite(&ch, 1);
790 if(1 != numWritten)
791 {
792 std::cerr << "Failed to stream to IFILE, expected 1, but only wrote "
793 << numWritten << std::endl;
794 }
795 return(stream);
796}

References InputFile::ifwrite().

◆ operator<<() [2/6]

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.

754{
755 unsigned int numExpected = strlen(str);
756 unsigned int numWritten =
757 stream.ifwrite(str, numExpected);
758 if(numExpected != numWritten)
759 {
760 std::cerr << "Failed to stream to IFILE, expected "
761 << numExpected << " but only wrote "
762 << numWritten << std::endl;
763 }
764 return(stream);
765}

References InputFile::ifwrite().

◆ operator<<() [3/6]

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.

737{
738 unsigned int numExpected = str.length();
739 unsigned int numWritten =
740 stream.ifwrite(str.c_str(), numExpected);
741 if(numExpected != numWritten)
742 {
743 std::cerr << "Failed to stream to IFILE, expected "
744 << numExpected << " but only wrote "
745 << numWritten << std::endl;
746 }
747 return(stream);
748}

References InputFile::ifwrite().

◆ operator<<() [4/6]

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.

414{
415 String val;
416 val = num;
417 stream << val;
418 return(stream);
419}

◆ operator<<() [5/6]

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.

423{
424 String val;
425 val = num;
426 stream << val;
427 return(stream);
428}

◆ operator<<() [6/6]

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.

432{
433 String val;
434 val = num;
435 stream << val;
436 return(stream);
437}

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

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

References InputFile::ifgetc().