libStatGen Software  1
InputFile.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2012 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 /*! \file */
18 #ifndef __INPUTFILE_H__
19 #define __INPUTFILE_H__
20 
21 #include <stdio.h>
22 #include <iostream>
23 #include <cstring>
24 #include <stdint.h>
25 
26 #include "FileType.h"
27 
28 /// Class for easily reading/writing files without having to worry about
29 /// file type (uncompressed, gzip, bgzf) when reading.
30 /// It hides the low level file operations/structure from the user, allowing
31 /// them to generically open and operate on a file using the same
32 /// interface without knowing the file format (standard uncompressed,
33 /// gzip, or bgzf). For writing, the user must specify the file type.
34 /// There is a typedef IFILE which is InputFile* and setup to mimic FILE
35 /// including global methods that take IFILE as a parameter.
36 class InputFile
37 {
38  bool myAttemptRecovery; // use recovery techniques if possible
39 public:
40 
41  /// Compression to use when writing a file & decompression used when
42  /// reading a file from stdin. Any other read checks the file to determine
43  /// how to uncompress it.
45  DEFAULT, ///< Check the extension, if it is ".gz", treat as gzip, otherwise treat it as UNCOMPRESSED.
46  UNCOMPRESSED, ///< uncompressed file.
47  GZIP, ///< gzip file.
48  BGZF ///< bgzf file.
49  };
50 
51  /// Default constructor
53  {
54  myAttemptRecovery = false;
55  myFileTypePtr = NULL;
56  myBufferIndex = 0;
57  myCurrentBufferSize = 0;
58  // Default to buffer.
59  myAllocatedBufferSize = DEFAULT_BUFFER_SIZE;
60  myFileBuffer = new char[myAllocatedBufferSize];
61  myFileName.clear();
62  }
63 
64  /// Destructor
65  ~InputFile();
66 
67  /// Constructor for opening a file.
68  /// \param filename file to open
69  /// \param mode same format as fopen: "r" for read & "w" for write.
70  /// \param compressionMode set the type of file to open for writing or
71  /// for reading from stdin (when reading files, the compression type is
72  /// determined by reading the file).
73  InputFile(const char * filename, const char * mode,
75 
76  /// Set the buffer size for reading from files so that bufferSize bytes
77  /// are read at a time and stored until accessed by another read call.
78  /// This improves performance over reading the file small bits at a time.
79  /// Buffering reads disables the tell call for bgzf files.
80  /// Any previous values in the buffer will be deleted.
81  /// \param bufferSize number of bytes to read/buffer at a time,
82  /// turn off read buffering by setting bufferSize = 1;
83  inline void bufferReads(unsigned int bufferSize = DEFAULT_BUFFER_SIZE)
84  {
85  // If the buffer size is the same, do nothing.
86  if(bufferSize == myAllocatedBufferSize)
87  {
88  return;
89  }
90  // Delete the previous buffer.
91  if(myFileBuffer != NULL)
92  {
93  delete[] myFileBuffer;
94  }
95  myBufferIndex = 0;
96  myCurrentBufferSize = 0;
97  // The buffer size must be at least 1 so one character can be
98  // read and ifgetc can just assume reading into the buffer.
99  if(bufferSize < 1)
100  {
101  bufferSize = 1;
102  }
103  myFileBuffer = new char[bufferSize];
104  myAllocatedBufferSize = bufferSize;
105 
106  if(myFileTypePtr != NULL)
107  {
108  if(bufferSize == 1)
109  {
110  myFileTypePtr->setBuffered(false);
111  }
112  else
113  {
114  myFileTypePtr->setBuffered(true);
115  }
116  }
117  }
118 
119 
120  /// Disable read buffering.
121  inline void disableBuffering()
122  {
123  bufferReads(1);
124  if(myFileTypePtr != NULL)
125  {
126  myFileTypePtr->setBuffered(false);
127  }
128  }
129 
130 
131  /// Close the file.
132  /// \return status of the close (0 is success).
133  inline int ifclose()
134  {
135  if (myFileTypePtr == NULL)
136  {
137  return EOF;
138  }
139  int result = myFileTypePtr->close();
140  delete myFileTypePtr;
141  myFileTypePtr = NULL;
142  myFileName.clear();
143  return result;
144  }
145 
146  /// Read size bytes from the file into the buffer.
147  /// \param buffer pointer to memory at least size bytes big to write the
148  /// data into.
149  /// \param size number of bytes to be read
150  /// \return number of bytes read, if it is not equal to size,
151  /// there was either an error or the end of the file was reached, use
152  /// ifeof to determine which case it was.
153  inline int ifread(void * buffer, unsigned int size)
154  {
155  // There are 2 cases:
156  // 1) There are already size available bytes in buffer.
157  // 2) There are not size bytes in buffer.
158 
159  // Determine the number of available bytes in the buffer.
160  unsigned int availableBytes = myCurrentBufferSize - myBufferIndex;
161  int returnSize = 0;
162 
163  // Case 1: There are already size available bytes in buffer.
164  if (size <= availableBytes)
165  {
166  // Just copy from the buffer, increment the index and return.
167  memcpy(buffer, myFileBuffer+myBufferIndex, size);
168  // Increment the buffer index.
169  myBufferIndex += size;
170  returnSize = size;
171  }
172  // Case 2: There are not size bytes in buffer.
173  else
174  {
175  // Check to see if there are some bytes in the buffer.
176  if (availableBytes > 0)
177  {
178  // Size > availableBytes > 0
179  // Copy the available bytes into the buffer.
180  memcpy(buffer, myFileBuffer+myBufferIndex, availableBytes);
181  }
182  // So far availableBytes have been copied into the read buffer.
183  returnSize = availableBytes;
184  // Increment myBufferIndex by what was read.
185  myBufferIndex += availableBytes;
186 
187  unsigned int remainingSize = size - availableBytes;
188 
189  // Check if the remaining size is more or less than the
190  // max buffer size.
191  if(remainingSize < myAllocatedBufferSize)
192  {
193  // the remaining size is not the full buffer, but read
194  // a full buffer worth of data anyway.
195  myCurrentBufferSize =
196  readFromFile(myFileBuffer, myAllocatedBufferSize);
197 
198  // Check for an error.
199  if(myCurrentBufferSize <= 0)
200  {
201  // No more data was successfully read, so check to see
202  // if any data was copied to the return buffer at all.
203  if( returnSize == 0)
204  {
205  // No data has been copied at all into the
206  // return read buffer, so just return the value
207  // returned from readFromFile.
208  returnSize = myCurrentBufferSize;
209  // Otherwise, returnSize is already set to the
210  // available bytes that was already copied (so no
211  // else statement is needed).
212  }
213  // Set myBufferIndex & myCurrentBufferSize to 0.
214  myCurrentBufferSize = 0;
215  myBufferIndex = 0;
216  }
217  else
218  {
219  // Successfully read more data.
220  // Check to see how much was copied.
221  int copySize = remainingSize;
222  if(copySize > myCurrentBufferSize)
223  {
224  // Not the entire requested amount was read
225  // (either from EOF or there was a partial read due to
226  // an error), so set the copySize to what was read.
227  copySize = myCurrentBufferSize;
228  }
229 
230  // Now copy the rest of the bytes into the buffer.
231  memcpy((char*)buffer+availableBytes,
232  myFileBuffer, copySize);
233 
234  // set the buffer index to the location after what we are
235  // returning as read.
236  myBufferIndex = copySize;
237 
238  returnSize += copySize;
239  }
240  }
241  else
242  {
243  // More remaining to be read than the max buffer size, so just
244  // read directly into the output buffer.
245  int readSize = readFromFile((char*)buffer + availableBytes,
246  remainingSize);
247 
248  // Already used the buffer, so "clear" it.
249  myCurrentBufferSize = 0;
250  myBufferIndex = 0;
251  if(readSize <= 0)
252  {
253  // No more data was successfully read, so check to see
254  // if any data was copied to the return buffer at all.
255  if(returnSize == 0)
256  {
257  // No data has been copied at all into the
258  // return read buffer, so just return the value
259  // returned from readFromFile.
260  returnSize = readSize;
261  // Otherwise, returnSize is already set to the
262  // available bytes that was already copied (so no
263  // else statement is needed).
264  }
265  }
266  else
267  {
268  // More data was read, so increment the return count.
269  returnSize += readSize;
270  }
271  }
272  }
273  return(returnSize);
274  }
275 
276  /// Read until the specified characters, returning which character was
277  /// found causing the stop, -1 returned for EOF, storing the other read
278  /// characters into the specified string.
279  /// Note: If stopChars is just '\n', readLine is faster and if
280  /// stopChars is just '\n' and '\t', readTilTab is faster.
281  /// \param stopChars characters to stop reading when they are hit.
282  /// \param stringRef reference to a string that the read characters should
283  /// be apppended to (does not include the stopchar).
284  /// \return index of the character in stopChars that caused it to stop
285  /// reading or -1 for EOF.
286  int readTilChar(const std::string& stopChars, std::string& stringRef);
287 
288  /// Read until the specified characters, returning which character was
289  /// found causing the stop, -1 returned for EOF, dropping all read chars.
290  /// Note: If stopChars is just '\n', discardLine is faster.
291  /// \param stopChars characters to stop reading when they are hit.
292  /// \return index of the character in stopChars that caused it to stop
293  /// reading or -1 for EOF.
294  int readTilChar(const std::string& stopChars);
295 
296  /// Read until the end of the line, discarding the characters,
297  /// returning -1 returned for EOF and returning 0 if the end of the line
298  /// was found.
299  /// \return 0 if the end of the line was found before EOF or -1 for EOF.
300  int discardLine();
301 
302  /// Read, appending the characters into the specified string until new
303  /// line or EOF is found, returning -1 if EOF is found first and 0 if new
304  /// line is found first. The new line and EOF are not written into the
305  /// specified string.
306  /// \param line reference to a string that the read characters should
307  /// be apppended to (does not include the new line or eof).
308  /// \return 0 if new line and -1 for EOF.
309  int readLine(std::string& line);
310 
311  /// Read, appending the characters into the specified string until tab, new
312  /// line, or EOF is found, returning -1 if EOF is found first, 0 if new
313  /// line is found first, or 1 if a tab is found first. The tab, new line,
314  /// and EOF are not written into the specified string.
315  /// \param field reference to a string that the read characters should
316  /// be apppended to (does not include the tab, new line, or eof).
317  /// \return 1 if tab is found, 0 if new line, and -1 for EOF.
318  int readTilTab(std::string& field);
319 
320  /// Get a character from the file. Read a character from the internal
321  /// buffer, or if the end of the buffer has been reached, read from the
322  /// file into the buffer and return index 0.
323  /// \return character that was read or EOF.
324  inline int ifgetc()
325  {
326  if (myBufferIndex >= myCurrentBufferSize)
327  {
328  // at the last index, read a new buffer.
329  myCurrentBufferSize = readFromFile(myFileBuffer, myAllocatedBufferSize);
330  myBufferIndex = 0;
331  // If the buffer index is still greater than or equal to the
332  // myCurrentBufferSize, then we failed to read the file - return EOF.
333  // NB: This only needs to be checked when myCurrentBufferSize
334  // is changed. Simplify check - readFromFile returns zero on EOF
335  if (myCurrentBufferSize == 0)
336  {
337  return(EOF);
338  }
339  }
340  return(myFileBuffer[myBufferIndex++]);
341  }
342 
343  /// Get a line from the file.
344  /// \param buffer the buffer into which data is to be placed
345  /// \param max the maximum size of the buffer, in bytes
346  /// \return true if the last character read was an EOF
347  inline bool ifgetline(void *voidBuffer, size_t max)
348  {
349  int ch;
350  char *buffer = (char *) voidBuffer;
351 
352  while( (ch=ifgetc()) != '\n' && ch != EOF) {
353  *buffer++ = ch;
354  if((--max)<2)
355  {
356  // truncate the line, so drop remainder
357  while( (ch=ifgetc()) && ch != '\n' && ch != EOF)
358  {
359  }
360  break;
361  }
362  }
363  *buffer++ = '\0';
364  return ch==EOF;
365  }
366 
367  /// Reset to the beginning of the file.
368  inline void ifrewind()
369  {
370  // Just set the myBufferIndex and the myCurrentBufferSize to 0 to simulate
371  // clearing the buffer and call rewind to move to the beginning of the
372  // file.
373  if (myFileTypePtr == NULL)
374  {
375  // No pointer, so nothing to rewind.
376  return;
377  }
378  myCurrentBufferSize = 0;
379  myBufferIndex = 0;
380  myFileTypePtr->rewind();
381  }
382 
383 
384  /// Check to see if we have reached the EOF.
385  /// \return 0 if not EOF, any other value means EOF.
386  inline int ifeof() const
387  {
388  // Not EOF if we are not at the end of the buffer.
389  if (myBufferIndex < myCurrentBufferSize)
390  {
391  // There are still available bytes in the buffer, so NOT EOF.
392  return false;
393  }
394  else
395  {
396  if (myFileTypePtr == NULL)
397  {
398  // No myFileTypePtr, so not eof (return 0).
399  return 0;
400  }
401  // exhausted our buffer, so check the file for eof.
402  return myFileTypePtr->eof();
403  }
404  }
405 
406  /// Write the specified buffer into the file.
407  /// \param buffer buffer containing size bytes to write to the file.
408  /// \param size number of bytes to write
409  /// \return number of bytes written
410  /// We do not buffer the write call, so just leave this as normal.
411  inline unsigned int ifwrite(const void * buffer, unsigned int size)
412  {
413  if (myFileTypePtr == NULL)
414  {
415  // No myFileTypePtr, so return 0 - nothing written.
416  return 0;
417  }
418  return myFileTypePtr->write(buffer, size);
419  }
420 
421  /// Returns whether or not the file was successfully opened.
422  /// \return true if the file is open, false if not.
423  inline bool isOpen() const
424  {
425  // It is open if the myFileTypePtr is set and says it is open.
426  if ((myFileTypePtr != NULL) && myFileTypePtr->isOpen())
427  {
428  return true;
429  }
430  // File was not successfully opened.
431  return false;
432  }
433 
434  /// Get current position in the file.
435  /// \return current position in the file, -1 indicates an error.
436  inline int64_t iftell()
437  {
438  if (myFileTypePtr == NULL)
439  {
440  // No myFileTypePtr, so return false - could not seek.
441  return -1;
442  }
443  int64_t pos = myFileTypePtr->tell();
444  pos -= (myCurrentBufferSize - myBufferIndex);
445  return(pos);
446  }
447 
448 
449  /// Seek to the specified offset from the origin.
450  /// \param offset offset into the file to move to (must be from a tell call)
451  /// \param origin can be any of the following:
452  /// Note: not all are valid for all filetypes.
453  /// SEEK_SET - Beginning of file
454  /// SEEK_CUR - Current position of the file pointer
455  /// SEEK_END - End of file
456  /// \return true on successful seek and false on a failed seek.
457  inline bool ifseek(int64_t offset, int origin)
458  {
459  if (myFileTypePtr == NULL)
460  {
461  // No myFileTypePtr, so return false - could not seek.
462  return false;
463  }
464  // TODO - may be able to seek within the buffer if applicable.
465  // Reset buffering since a seek is being done.
466  myBufferIndex = 0;
467  myCurrentBufferSize = 0;
468  return myFileTypePtr->seek(offset, origin);
469  }
470 
471  /// Get the filename that is currently opened.
472  /// \return filename associated with this class
473  const char* getFileName() const
474  {
475  return(myFileName.c_str());
476  }
477 
478  /// Enable (default) or disable recovery.
479  ///
480  /// When true, we can attach a myFileTypePtr
481  /// that implements a recovery capable decompressor.
482  /// This requires that the caller be able to catch
483  /// the exception XXX "blah blah blah".
484  ///
485  void setAttemptRecovery(bool flag = false)
486  {
487  myAttemptRecovery = flag;
488  }
489 
490  bool attemptRecoverySync(bool (*checkSignature)(void *data) , int length)
491  {
492  if(myFileTypePtr==NULL) return false;
493  return myFileTypePtr->attemptRecoverySync(checkSignature, length);
494  }
495 
496  // Open a file. Called by the constructor.
497  // Returns true if the file was successfully opened, false otherwise.
498  bool openFile(const char * filename, const char * mode,
499  InputFile::ifileCompression compressionMode);
500 
501 protected:
502  // Read into a buffer from the file. Since the buffer is passed in and
503  // this would bypass the myFileBuffer used by this class, this method must
504  // be protected.
505  inline int readFromFile(void * buffer, unsigned int size)
506  {
507  // If no myFileTypePtr, return 0 - nothing read.
508  if (myFileTypePtr == NULL)
509  {
510  return 0;
511  }
512  return myFileTypePtr->read(buffer, size);
513  }
514 
515 #ifdef __ZLIB_AVAILABLE__
516  // Only necessary with zlib to determine what file type on a new
517  // file. Without zlib, there are only uncompressed files, so a special
518  // method is not needed to determine the type of file to open.
519  // Open a file. This method will open a file with the specified name and
520  // mode with the fileTypePtr associated with the specified compressionMode.
521  void openFileUsingMode(const char* filename, const char* mode,
522  InputFile::ifileCompression compressionMode);
523 #endif
524 
525  // The size of the buffer used by this class.
526  static const unsigned int DEFAULT_BUFFER_SIZE = 65536;
527 
528  // Pointer to a class that interfaces with different file types.
529  FileType* myFileTypePtr;
530 
531  unsigned int myAllocatedBufferSize;
532 
533  // Buffer used to do large reads rather than 1 by 1 character reads
534  // from the file. The class is then managed to iterate through the buffer.
535  char* myFileBuffer;
536 
537  // Current index into the buffer. Used to track where we are in reading the
538  // file from the buffer.
539  int myBufferIndex;
540 
541  // Current number of entries in the buffer. Used to ensure that
542  // if a read did not fill the buffer, we stop before hitting the
543  // end of what was read.
544  int myCurrentBufferSize;
545 
546  std::string myFileName;
547 };
548 
549 
550 /// Define IFILE as a pointer to an InputFile object.
551 typedef InputFile* IFILE;
552 
553 
554 /// Open a file with the specified name and mode, using a filename of "-" to
555 /// indicate stdin/stdout.
556 /// \param filename file to open ("-" meands stdin/stdout)
557 /// \param mode same format as fopen: "r" for read & "w" for write.
558 /// \param compressionMode set the type of file to open for writing or
559 /// for reading from stdin (when reading files not from stdin, the compression
560 /// type is determined by reading the file).
561 /// \return IFILE - pointer to the InputFile object that has been opened.
562 inline IFILE ifopen(const char * filename, const char * mode,
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 }
575 
576 
577 /// Close the file.
578 /// \param file file to be closed - IFILE is a pointer to an InputFile object
579 /// \return status of the close (0 is success or if NULL is passed in).
580 inline int ifclose(IFILE &file)
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 }
593 
594 /// Read up to size bytes from the file into the buffer.
595 /// \param file file to be read - IFILE is a pointer to an InputFile object
596 /// \param buffer pointer to memory at least size bytes big to write the
597 /// data into.
598 /// \param size number of bytes to be read
599 /// \return number of bytes read
600 inline unsigned int ifread(IFILE file, void * buffer, unsigned int size)
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 }
609 
610 /// Get a character from the file. Read a character from the internal
611 /// buffer, or if the end of the buffer has been reached, read from the
612 /// file into the buffer and return index 0.
613 /// \param file file to be read - IFILE is a pointer to an InputFile object
614 /// \return character that was read or EOF.
615 inline int ifgetc(IFILE file)
616 {
617  if(file == NULL)
618  {
619  // return eof since there is no file.
620  return(EOF);
621  }
622  return(file->ifgetc());
623 }
624 
625 /// Get a line from the file.
626 /// \param file file to be read - IFILE is a pointer to an InputFile object
627 /// \param buffer the buffer into which data is to be placed
628 /// \param max the maximum size of the buffer, in bytes
629 /// \return true if the last character read was an EOF
630 inline bool ifgetline(IFILE file, void *buffer, size_t max)
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 }
639 
640 /// Reset to the beginning of the file (cannot be done for stdin/stdout).
641 /// \param file file to be rewound - IFILE is a pointer to an InputFile object
642 inline void ifrewind(IFILE file)
643 {
644  if(file == NULL)
645  {
646  return;
647  }
648  file->ifrewind();
649 }
650 
651 /// Check to see if we have reached the EOF (returns 0 if not EOF).
652 /// \param file file to be checked - IFILE is a pointer to an InputFile object
653 /// \return 0 if not EOF, any other value means EOF.
654 inline int ifeof(IFILE file)
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 }
663 
664 /// Write the specified number of bytes from the specified buffer into the file.
665 /// \param file file to write to - IFILE is a pointer to an InputFile object
666 /// \param buffer buffer containing size bytes to write to the file.
667 /// \param size number of bytes to write
668 /// \return number of bytes written
669 inline unsigned int ifwrite(IFILE file, const void * buffer, unsigned int size)
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 }
678 
679 /// Get current position in the file. Can be fed back into ifseek.
680 /// \param file file to perform tell on - IFILE is a pointer to an InputFile object
681 /// \return current position in the file, -1 indicates an error.
682 inline int64_t iftell(IFILE file)
683 {
684  if(file == NULL)
685  {
686  return(-1);
687  }
688  return (file->iftell());
689 }
690 
691 /// Seek to the specified position (result from an iftell), but cannot
692 /// be done for stdin/stdout.
693 /// \param file file to perform seek on - IFILE is a pointer to an InputFile object
694 /// \param offset offset into the file to move to (must be from a tell call)
695 /// \param origin can be any of the following:
696 /// Note: not all are valid for all filetypes.
697 /// SEEK_SET - Beginning of file
698 /// SEEK_CUR - Current position of the file pointer
699 /// SEEK_END - End of file
700 /// \return true on successful seek and false on a failed seek.
701 inline bool ifseek(IFILE file, int64_t offset, int origin)
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 }
710 
711 /// Write to a file using fprintf format.
712 /// \param file file to write to - IFILE is a pointer to an InputFile object
713 /// \param format printf format for writing, followed by parameters.
714 /// \return number of bytes written
715 int ifprintf(IFILE output, const char * format, ...);
716 
717 /// Read a line from a file using streaming.
718 /// Will not fail when the file hits EOF, so do not do: while(iFile >> iStr)
719 /// unless within your loop you check for ifeof and break.
720 /// Instead, do something like:
721 /// while(!iFile->ifeof() && iFile >> iStr)
722 /// \param stream file to read from - IFILE is a pointer to an InputFile object
723 /// \param str output string containing the line read from the file.
724 inline IFILE operator >> (IFILE stream, std::string &str)
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 }
732 
733 /// Write to a file using streaming.
734 /// \param stream file to write to - IFILE is a pointer to an InputFile object
735 /// \param str string containing what should be written to the file.
736 inline InputFile& operator << (InputFile& stream, const std::string& str)
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 }
749 
750 /// Write to a file using streaming.
751 /// \param stream file to write to - IFILE is a pointer to an InputFile object
752 /// \param str string containing what should be written to the file.
753 inline InputFile& operator << (InputFile& stream, const char* str)
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 }
766 
767 
768 /// Write to a file using streaming.
769 /// \param stream file to write to - IFILE is a pointer to an InputFile object
770 /// \param num number that should be written to the file.
771 InputFile& operator << (InputFile& stream, double num);
772 
773 /// Write to a file using streaming.
774 /// \param stream file to write to - IFILE is a pointer to an InputFile object
775 /// \param num number that should be written to the file.
776 InputFile& operator << (InputFile& stream, int num);
777 
778 /// Write to a file using streaming.
779 /// \param stream file to write to - IFILE is a pointer to an InputFile object
780 /// \param num number that should be written to the file.
781 InputFile& operator << (InputFile& stream, unsigned int num);
782 
783 /// Write to a file using streaming.
784 /// \param stream file to write to - IFILE is a pointer to an InputFile object
785 /// \param ch character that should be written to the file.
786 inline InputFile& operator << (InputFile& stream, char ch)
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 }
797 
798 #endif
799 
InputFile::~InputFile
~InputFile()
Destructor.
Definition: InputFile.cpp:385
InputFile::bufferReads
void bufferReads(unsigned int bufferSize=DEFAULT_BUFFER_SIZE)
Set the buffer size for reading from files so that bufferSize bytes are read at a time and stored unt...
Definition: InputFile.h:83
InputFile::setAttemptRecovery
void setAttemptRecovery(bool flag=false)
Enable (default) or disable recovery.
Definition: InputFile.h:485
InputFile::ifwrite
unsigned int ifwrite(const void *buffer, unsigned int size)
Write the specified buffer into the file.
Definition: InputFile.h:411
ifseek
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.
Definition: InputFile.h:701
InputFile::iftell
int64_t iftell()
Get current position in the file.
Definition: InputFile.h:436
InputFile::DEFAULT
@ DEFAULT
Check the extension, if it is ".gz", treat as gzip, otherwise treat it as UNCOMPRESSED.
Definition: InputFile.h:45
ifopen
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.
Definition: InputFile.h:562
InputFile::GZIP
@ GZIP
gzip file.
Definition: InputFile.h:47
InputFile::UNCOMPRESSED
@ UNCOMPRESSED
uncompressed file.
Definition: InputFile.h:46
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::ifileCompression
ifileCompression
Compression to use when writing a file & decompression used when reading a file from stdin.
Definition: InputFile.h:44
operator>>
IFILE operator>>(IFILE stream, std::string &str)
Read a line from a file using streaming.
Definition: InputFile.h:724
InputFile::getFileName
const char * getFileName() const
Get the filename that is currently opened.
Definition: InputFile.h:473
InputFile::readTilTab
int readTilTab(std::string &field)
Read, appending the characters into the specified string until tab, new line, or EOF is found,...
Definition: InputFile.cpp:133
InputFile::ifgetline
bool ifgetline(void *voidBuffer, size_t max)
Get a line from the file.
Definition: InputFile.h:347
InputFile::discardLine
int discardLine()
Read until the end of the line, discarding the characters, returning -1 returned for EOF and returnin...
Definition: InputFile.cpp:95
ifeof
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition: InputFile.h:654
ifrewind
void ifrewind(IFILE file)
Reset to the beginning of the file (cannot be done for stdin/stdout).
Definition: InputFile.h:642
FileType
Definition: FileType.h:23
ifread
unsigned int ifread(IFILE file, void *buffer, unsigned int size)
Read up to size bytes from the file into the buffer.
Definition: InputFile.h:600
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
IFILE
InputFile * IFILE
Define IFILE as a pointer to an InputFile object.
Definition: InputFile.h:551
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
ifprintf
int ifprintf(IFILE output, const char *format,...)
Write to a file using fprintf format.
Definition: InputFile.cpp:398
InputFile::disableBuffering
void disableBuffering()
Disable read buffering.
Definition: InputFile.h:121
ifgetline
bool ifgetline(IFILE file, void *buffer, size_t max)
Get a line from the file.
Definition: InputFile.h:630
InputFile::readLine
int readLine(std::string &line)
Read, appending the characters into the specified string until new line or EOF is found,...
Definition: InputFile.cpp:112
InputFile::readTilChar
int readTilChar(const std::string &stopChars, std::string &stringRef)
Read until the specified characters, returning which character was found causing the stop,...
Definition: InputFile.cpp:44
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
BGZF
Definition: bgzf.h:44
ifclose
int ifclose(IFILE &file)
Close the file.
Definition: InputFile.h:580
InputFile::ifrewind
void ifrewind()
Reset to the beginning of the file.
Definition: InputFile.h:368
operator<<
InputFile & operator<<(InputFile &stream, const std::string &str)
Write to a file using streaming.
Definition: InputFile.h:736
ifgetc
int ifgetc(IFILE file)
Get a character from the file.
Definition: InputFile.h:615
iftell
int64_t iftell(IFILE file)
Get current position in the file.
Definition: InputFile.h:682
InputFile::InputFile
InputFile()
Default constructor.
Definition: InputFile.h:52