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 "bgzf.h"
00022 #include "FileType.h"
00023 
00024 class BgzfFileType : public FileType
00025 {
00026 public:
00027     BgzfFileType()
00028     {
00029         bgzfHandle = NULL;
00030         myEOF = false;
00031     }
00032 
00033     virtual ~BgzfFileType()
00034     {
00035         bgzfHandle = NULL;
00036     }
00037 
00038     BgzfFileType(const char * filename, const char * mode);
00039 
00040     virtual bool operator == (void * rhs)
00041     {
00042         // No two file pointers are the same, so if rhs is not NULL, then
00043         // the two pointers are different (false).
00044         if (rhs != NULL)
00045             return false;
00046         return (bgzfHandle == rhs);
00047     }
00048 
00049     virtual bool operator != (void * rhs)
00050     {
00051         // No two file pointers are the same, so if rhs is not NULL, then
00052         // the two pointers are different (true).
00053         if (rhs != NULL)
00054             return true;
00055         return (bgzfHandle != rhs);
00056     }
00057 
00058     // Close the file.
00059     virtual inline int close()
00060     {
00061         int result = bgzf_close(bgzfHandle);
00062         bgzfHandle = NULL;
00063         return result;
00064     }
00065 
00066 
00067     // Reset to the beginning of the file.
00068     virtual inline void rewind()
00069     {
00070         // Just call rewind to move to the beginning of the file.
00071         seek(myStartPos, SEEK_SET);
00072     }
00073 
00074     // Check to see if we have reached the EOF.
00075     virtual inline int eof()
00076     {
00077         //  check the file for eof.
00078         return myEOF;
00079     }
00080 
00081     // Check to see if the file is open.
00082     virtual inline bool isOpen()
00083     {
00084         if (bgzfHandle != NULL)
00085         {
00086             // bgzfHandle is not null, so the file is open.
00087             return(true);
00088         }
00089         return(false);
00090     }
00091 
00092     // Write to the file
00093     virtual inline unsigned int write(const void * buffer, unsigned int size)
00094     {
00095         return bgzf_write(bgzfHandle, buffer, size);
00096     }
00097 
00098     // Read into a buffer from the file.  Since the buffer is passed in and
00099     // this would bypass the fileBuffer used by this class, this method must
00100     // be protected.
00101     virtual inline int read(void * buffer, unsigned int size)
00102     {
00103         int bytesRead = bgzf_read(bgzfHandle, buffer, size);
00104         if ((bytesRead == 0) && (size != 0))
00105         {
00106             myEOF = true;
00107         }
00108         else
00109         {
00110             myEOF = false;
00111         }
00112         return bytesRead;
00113     }
00114 
00115 
00116     // Get current position in the file.
00117     // -1 return value indicates an error.
00118     virtual inline long int tell()
00119     {
00120         return bgzf_tell(bgzfHandle);
00121     }
00122 
00123 
00124     // Seek to the specified offset from the origin.
00125     // origin can be any of the following:
00126     // Note: not all are valid for all filetypes.
00127     //   SEEK_SET - Beginning of file
00128     //   SEEK_CUR - Current position of the file pointer
00129     //   SEEK_END - End of file
00130     // Returns true on successful seek and false on a failed seek.
00131     virtual inline bool seek(long int offset, int origin)
00132     {
00133         long int returnVal = bgzf_seek(bgzfHandle, offset, origin);
00134         // Check for failure.
00135         if (returnVal == -1)
00136         {
00137             return false;
00138         }
00139         // Successful.
00140         // Reset EOF, assuming no longer at eof - first read will indicate
00141         // eof if it is at the end.
00142         myEOF = false;
00143         return true;
00144     }
00145 
00146     // Set whether or not to require the EOF block at the end of the
00147     // file.  True - require the block.  False - do not require the block.
00148     static void setRequireEofBlock(bool requireEofBlock);
00149 
00150 protected:
00151     // A bgzfFile is used.
00152     BGZF* bgzfHandle;
00153 
00154     // Flag indicating EOF since there isn't one on the handle.
00155     bool myEOF;
00156 
00157     long int myStartPos;
00158 
00159     // Static variable to track whether or not to require the EOF Block
00160     // at the end of the file.  If the block is required, but not on the file,
00161     // the constructor fails to open the file.
00162     static bool ourRequireEofBlock;
00163 };
00164 
00165 #endif
Generated on Wed Nov 17 15:38:28 2010 for StatGen Software by  doxygen 1.6.3