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
00110         {
00111             myEOF = false;
00112         }
00113         return bytesRead;
00114     }
00115 
00116 
00117     // Get current position in the file.
00118     // -1 return value indicates an error.
00119     virtual inline long int tell()
00120     {
00121         if(myUsingBuffer)
00122         {
00123             throw std::runtime_error("IFILE: CANNOT use buffered reads and tell for BGZF files");
00124         }
00125         return bgzf_tell(bgzfHandle);
00126     }
00127 
00128 
00129     // Seek to the specified offset from the origin.
00130     // origin can be any of the following:
00131     // Note: not all are valid for all filetypes.
00132     //   SEEK_SET - Beginning of file
00133     //   SEEK_CUR - Current position of the file pointer
00134     //   SEEK_END - End of file
00135     // Returns true on successful seek and false on a failed seek.
00136     virtual inline bool seek(long int offset, int origin)
00137     {
00138         long int returnVal = bgzf_seek(bgzfHandle, offset, origin);
00139         // Check for failure.
00140         if (returnVal == -1)
00141         {
00142             return false;
00143         }
00144         // Successful.
00145         // Reset EOF, assuming no longer at eof - first read will indicate
00146         // eof if it is at the end.
00147         myEOF = false;
00148         return true;
00149     }
00150 
00151     // Set whether or not to require the EOF block at the end of the
00152     // file.  True - require the block.  False - do not require the block.
00153     static void setRequireEofBlock(bool requireEofBlock);
00154 
00155 protected:
00156     // A bgzfFile is used.
00157     BGZF* bgzfHandle;
00158 
00159     // Flag indicating EOF since there isn't one on the handle.
00160     bool myEOF;
00161 
00162     long int myStartPos;
00163 
00164     // Static variable to track whether or not to require the EOF Block
00165     // at the end of the file.  If the block is required, but not on the file,
00166     // the constructor fails to open the file.
00167     static bool ourRequireEofBlock;
00168 };
00169 
00170 #endif
Generated on Tue Mar 22 22:50:15 2011 for StatGen Software by  doxygen 1.6.3