libStatGen Software 1
GzipFileType.h
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __GZFILETYPE_H__
19#define __GZFILETYPE_H__
20
21#ifdef __ZLIB_AVAILABLE__
22
23#if defined(_WIN32)
24#include <stdio.h> // for NULL!
25#endif
26
27#include <stdlib.h>
28#include <zlib.h>
29#include "FileType.h"
30
31//#include <iostream>
32
33class GzipFileType : public FileType
34{
35public:
36 GzipFileType()
37 {
38 gzHandle = NULL;
39 }
40
41 virtual ~GzipFileType()
42 {
43 }
44
45 GzipFileType(const char * filename, const char * mode);
46
47 bool operator == (void * rhs)
48 {
49 // No two file pointers are the same, so if rhs is not NULL, then
50 // the two pointers are different (false).
51 if (rhs != NULL)
52 return false;
53 return (gzHandle == rhs);
54 }
55
56 bool operator != (void * rhs)
57 {
58 // No two file pointers are the same, so if rhs is not NULL, then
59 // the two pointers are different (true).
60 if (rhs != NULL)
61 return true;
62 return (gzHandle != rhs);
63 }
64
65 // Close the file.
66 inline int close()
67 {
68 int result = gzclose(gzHandle);
69 gzHandle = NULL;
70 return result;
71 }
72
73
74 // Reset to the beginning of the file.
75 inline void rewind()
76 {
77 // Just call rewind to move to the beginning of the file.
78 gzrewind(gzHandle);
79 }
80
81 // Check to see if we have reached the EOF.
82 inline int eof()
83 {
84 // check the file for eof.
85 return gzeof(gzHandle);
86 }
87
88 // Check to see if the file is open.
89 virtual inline bool isOpen()
90 {
91 if (gzHandle != NULL)
92 {
93 // gzHandle is not null, so the file is open.
94 return(true);
95 }
96 return(false);
97 }
98
99 // Write to the file
100 inline unsigned int write(const void * buffer, unsigned int size)
101 {
102 return gzwrite(gzHandle, buffer, size);
103 }
104
105 // Read into a buffer from the file. Since the buffer is passed in and
106 // this would bypass the fileBuffer used by this class, this method must
107 // be protected.
108 inline int read(void * buffer, unsigned int size)
109 {
110 unsigned int numBytes = gzread(gzHandle, buffer, size);
111// if(numBytes != size)
112// {
113// std::cerr << "Error reading. Read "<< numBytes << " instead of "<< size<<std::endl;
114// int error_code = 0;
115// const char* errorMsg = gzerror(gzHandle, &error_code);
116// std::cerr << "ERROR Code: " << error_code << "; Error Msg: " << errorMsg << std::endl;
117// }
118 return numBytes;
119 }
120
121 // Get current position in the file.
122 // -1 return value indicates an error.
123 virtual inline int64_t tell()
124 {
125 return gztell(gzHandle);
126 }
127
128
129 // Seek to the specified offset from the origin.
130 // origin can be any of the following:
131 // Note: not all are valid for all filetypes.
132 // SEEK_SET - Beginning of file
133 // SEEK_CUR - Current position of the file pointer
134 // SEEK_END - End of file
135 // Returns true on successful seek and false on a failed seek.
136 virtual inline bool seek(int64_t offset, int origin)
137 {
138 int64_t returnVal = gzseek(gzHandle, offset, origin);
139 // Check for failure.
140 if (returnVal == -1)
141 {
142 return false;
143 }
144 // Successful.
145 return true;
146 }
147
148
149protected:
150 // A gzFile is used.
151 gzFile gzHandle;
152};
153
154#endif
155
156#endif
157