BgzfFileType.h

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __BGZFFILETYPE_H__
00019 #define __BGZFFILETYPE_H__
00020 
00021 #include <stdexcept> // stdexcept header file
00022 #include "bgzf.h"
00023 #include "FileType.h"
00024 
00025 class BgzfFileType : public FileType
00026 {
00027 public:
00028     BgzfFileType()
00029     {
00030         bgzfHandle = NULL;
00031         myEOF = false;
00032     }
00033 
00034     virtual ~BgzfFileType()
00035     {
00036         bgzfHandle = NULL;
00037     }
00038 
00039     BgzfFileType(const char * filename, const char * mode);
00040 
00041     virtual bool operator == (void * rhs)
00042     {
00043         // No two file pointers are the same, so if rhs is not NULL, then
00044         // the two pointers are different (false).
00045         if (rhs != NULL)
00046             return false;
00047         return (bgzfHandle == rhs);
00048     }
00049 
00050     virtual bool operator != (void * rhs)
00051     {
00052         // No two file pointers are the same, so if rhs is not NULL, then
00053         // the two pointers are different (true).
00054         if (rhs != NULL)
00055             return true;
00056         return (bgzfHandle != rhs);
00057     }
00058 
00059     // Close the file.
00060     virtual inline int close()
00061     {
00062         int result = bgzf_close(bgzfHandle);
00063         bgzfHandle = NULL;
00064         return result;
00065     }
00066 
00067 
00068     // Reset to the beginning of the file.
00069     virtual inline void rewind()
00070     {
00071         // Just call rewind to move to the beginning of the file.
00072         seek(myStartPos, SEEK_SET);
00073     }
00074 
00075     // Check to see if we have reached the EOF.
00076     virtual inline int eof()
00077     {
00078         //  check the file for eof.
00079         return myEOF;
00080     }
00081 
00082     // Check to see if the file is open.
00083     virtual inline bool isOpen()
00084     {
00085         if (bgzfHandle != NULL)
00086         {
00087             // bgzfHandle is not null, so the file is open.
00088             return(true);
00089         }
00090         return(false);
00091     }
00092 
00093     // Write to the file
00094     virtual inline unsigned int write(const void * buffer, unsigned int size)
00095     {
00096         return bgzf_write(bgzfHandle, buffer, size);
00097     }
00098 
00099     // Read into a buffer from the file.  Since the buffer is passed in and
00100     // this would bypass the fileBuffer used by this class, this method must
00101     // be protected.
00102     virtual inline int read(void * buffer, unsigned int size)
00103     {
00104         int bytesRead = bgzf_read(bgzfHandle, buffer, size);
00105         if ((bytesRead == 0) && (size != 0))
00106         {
00107             myEOF = true;
00108         }
00109         else if((bytesRead != (int)size) & (bytesRead >= 0))
00110         {
00111             // Less then the requested size was read 
00112             // and an error was not returned (bgzf_read returns -1 on error).
00113             myEOF = true;
00114         }
00115         else
00116         {
00117             myEOF = false;
00118         }
00119         return bytesRead;
00120     }
00121 
00122 
00123     // Get current position in the file.
00124     // -1 return value indicates an error.
00125     virtual inline int64_t tell()
00126     {
00127         if(myUsingBuffer)
00128         {
00129             throw std::runtime_error("IFILE: CANNOT use buffered reads and tell for BGZF files");
00130         }
00131         return bgzf_tell(bgzfHandle);
00132     }
00133 
00134 
00135     // Seek to the specified offset from the origin.
00136     // origin can be any of the following:
00137     // Note: not all are valid for all filetypes.
00138     //   SEEK_SET - Beginning of file
00139     //   SEEK_CUR - Current position of the file pointer
00140     //   SEEK_END - End of file
00141     // Returns true on successful seek and false on a failed seek.
00142     virtual inline bool seek(int64_t offset, int origin)
00143     {
00144         int64_t returnVal = bgzf_seek(bgzfHandle, offset, origin);
00145         // Check for failure.
00146         if (returnVal == -1)
00147         {
00148             return false;
00149         }
00150         // Successful.
00151         // Reset EOF, assuming no longer at eof - first read will indicate
00152         // eof if it is at the end.
00153         myEOF = false;
00154         return true;
00155     }
00156 
00157     // Set whether or not to require the EOF block at the end of the
00158     // file.  True - require the block.  False - do not require the block.
00159     static void setRequireEofBlock(bool requireEofBlock);
00160 
00161 protected:
00162     // A bgzfFile is used.
00163     BGZF* bgzfHandle;
00164 
00165     // Flag indicating EOF since there isn't one on the handle.
00166     bool myEOF;
00167 
00168     int64_t myStartPos;
00169 
00170     // Static variable to track whether or not to require the EOF Block
00171     // at the end of the file.  If the block is required, but not on the file,
00172     // the constructor fails to open the file.
00173     static bool ourRequireEofBlock;
00174 };
00175 
00176 #endif
Generated on Tue Aug 23 18:19:04 2011 for libStatGen Software by  doxygen 1.6.3