libStatGen Software  1
GzipFileType.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 __GZFILETYPE_H__
00019 #define __GZFILETYPE_H__
00020 
00021 #ifdef  __ZLIB_AVAILABLE__
00022 
00023 #if defined(_WIN32)
00024 #include <stdio.h>  // for NULL!
00025 #endif
00026 
00027 #include <stdlib.h>
00028 #include <zlib.h>
00029 #include "FileType.h"
00030 
00031 //#include <iostream>
00032 
00033 class GzipFileType : public FileType
00034 {
00035 public:
00036     GzipFileType()
00037     {
00038         gzHandle = NULL;
00039     }
00040 
00041     virtual ~GzipFileType()
00042     {
00043     }
00044 
00045     GzipFileType(const char * filename, const char * mode);
00046 
00047     bool operator == (void * rhs)
00048     {
00049         // No two file pointers are the same, so if rhs is not NULL, then
00050         // the two pointers are different (false).
00051         if (rhs != NULL)
00052             return false;
00053         return (gzHandle == rhs);
00054     }
00055 
00056     bool operator != (void * rhs)
00057     {
00058         // No two file pointers are the same, so if rhs is not NULL, then
00059         // the two pointers are different (true).
00060         if (rhs != NULL)
00061             return true;
00062         return (gzHandle != rhs);
00063     }
00064 
00065     // Close the file.
00066     inline int close()
00067     {
00068         int result = gzclose(gzHandle);
00069         gzHandle = NULL;
00070         return result;
00071     }
00072 
00073 
00074     // Reset to the beginning of the file.
00075     inline void rewind()
00076     {
00077         // Just call rewind to move to the beginning of the file.
00078         gzrewind(gzHandle);
00079     }
00080 
00081     // Check to see if we have reached the EOF.
00082     inline int eof()
00083     {
00084         //  check the file for eof.
00085         return gzeof(gzHandle);
00086     }
00087 
00088     // Check to see if the file is open.
00089     virtual inline bool isOpen()
00090     {
00091         if (gzHandle != NULL)
00092         {
00093             // gzHandle is not null, so the file is open.
00094             return(true);
00095         }
00096         return(false);
00097     }
00098 
00099     // Write to the file
00100     inline unsigned int write(const void * buffer, unsigned int size)
00101     {
00102         return gzwrite(gzHandle, buffer, size);
00103     }
00104 
00105     // Read into a buffer from the file.  Since the buffer is passed in and
00106     // this would bypass the fileBuffer used by this class, this method must
00107     // be protected.
00108     inline int read(void * buffer, unsigned int size)
00109     {
00110         unsigned int numBytes = gzread(gzHandle, buffer, size);
00111 //       if(numBytes != size)
00112 //       {
00113 //          std::cerr << "Error reading.  Read "<< numBytes << " instead of "<< size<<std::endl;
00114 //          int error_code = 0;
00115 //          const char* errorMsg = gzerror(gzHandle, &error_code);
00116 //          std::cerr << "ERROR Code: " << error_code << ";  Error Msg: " << errorMsg << std::endl;
00117 //       }
00118         return numBytes;
00119     }
00120 
00121     // Get current position in the file.
00122     // -1 return value indicates an error.
00123     virtual inline int64_t tell()
00124     {
00125         return gztell(gzHandle);
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(int64_t offset, int origin)
00137     {
00138         int64_t returnVal = gzseek(gzHandle, offset, origin);
00139         // Check for failure.
00140         if (returnVal == -1)
00141         {
00142             return false;
00143         }
00144         // Successful.
00145         return true;
00146     }
00147 
00148 
00149 protected:
00150     // A gzFile is used.
00151     gzFile gzHandle;
00152 };
00153 
00154 #endif
00155 
00156 #endif
00157 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends