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