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