Adonthell  0.4
fileops.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001/2003 Alexandre Courbot
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 
21 /**
22  * @file fileops.h
23  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
24  *
25  * @brief Declares the igzstream, ogzstream and fileops classes.
26  *
27  */
28 
29 
30 
31 
32 #ifndef FILEOPS_H_
33 #define FILEOPS_H_
34 
35 #include <zlib.h>
36 #include <string>
37 #include "types.h"
38 
39 
40 #ifndef SWIG
41 using namespace std;
42 #endif
43 
44 
45 /**
46  * Enumeration to know whether a file is read or write opened.
47  *
48  */
49 typedef enum { READ, WRITE } gz_type;
50 
51 
52 /**
53  * Base class for igzstream and ogzstream.
54  *
55  */
56 class gz_file
57 {
58 public:
59  /**
60  * Default constructor.
61  *
62  */
63  gz_file ();
64 
65 #ifndef SWIG
66  /**
67  *
68  *
69  * @param fname name of the file to open.
70  * @param t access (READ or WRITE).
71  */
72  gz_file (const string & fname, gz_type t);
73 #endif
74 
75  /**
76  * Destructor.
77  *
78  */
79  virtual ~gz_file ();
80 
81  /**
82  * Opens a file.
83  *
84  * @param fname name of the file to open.
85  * @param t access (READ or WRITE).
86  *
87  * @return true if succeed, false otherwise.
88  */
89  bool open (const string & fname, gz_type t);
90 
91  /**
92  * Close the file that was opened.
93  *
94  */
95  void close ();
96 
97  /**
98  * Returns whether the file is opened or not.
99  *
100  *
101  * @return true if the file is opened, false otherwise.
102  */
103  bool is_open () { return opened; }
104 
105  /**
106  * Returns whether the file is at it's end or not.
107  *
108  *
109  * @return true if the end of file is reached, else otherwise.
110  */
111  bool eof ()
112  {
113  return gzeof (file);
114  }
115 
116 protected:
117  /**
118  * The actual gzFile.
119  *
120  */
121  gzFile file;
122 
123 private:
124  /// NEVER pass this by value.
125  gz_file (gz_file& src);
126 
127  /// Opened or not?
128  bool opened;
129 };
130 
131 
132 /**
133  * Class to read data from a Gzip compressed file.
134  */
135 class igzstream : public gz_file
136 {
137 public:
138  /**
139  * Default constructor.
140  *
141  */
142  igzstream ();
143 
144 #ifndef SWIG
145  /**
146  * Opens a file for read access.
147  *
148  * @param fname name of the file to open.
149  *
150  */
151  igzstream (const string & fname);
152 #endif
153 
154  /**
155  * Destructor.
156  *
157  */
158  ~igzstream ();
159 
160  /**
161  * Opens a file for read access.
162  *
163  * @param fname name of the file to open.
164  *
165  * @return true if succeed, false otherwise.
166  */
167  bool open (const string & fname);
168 
169  /**
170  * Reads a block of bytes from the file.
171  *
172  * @param to pointer to the buffer where to read.
173  * @param size number of bytes to read.
174  */
175  void get_block (void * to, u_int32 size);
176 
177 #ifndef SWIG
178  /// Reads a boolean.
179  friend bool& operator << (bool& n, igzstream& gfile);
180 
181  /// Reads a char.
182  friend char& operator << (char& n, igzstream& gfile);
183 
184  /// Reads a u_int8.
185  friend u_int8& operator << (u_int8& n, igzstream& gfile);
186 
187  /// Reads a s_int8.
188  friend s_int8& operator << (s_int8& n, igzstream& gfile);
189 
190  /// Reads a u_int16.
191  friend u_int16& operator << (u_int16& n, igzstream& gfile);
192 
193  /// Reads a s_int16.
194  friend s_int16& operator << (s_int16& n, igzstream& gfile);
195 
196  /// Reads a u_int32.
197  friend u_int32& operator << (u_int32& n, igzstream& gfile);
198 
199  /// Reads a s_int32.
200  friend s_int32& operator << (s_int32& n, igzstream& gfile);
201 
202  /// Reads a string.
203  friend string& operator << (string& s, igzstream& gfile);
204 
205  /// Reads a float.
206  friend float& operator << (float& s, igzstream& gfile);
207 #endif
208 
209  bool get_bool ();
210  u_int8 get_uint8 ();
211  s_int8 get_sint8 ();
212  u_int16 get_uint16 ();
213  s_int16 get_sint16 ();
214  u_int32 get_uint32 ();
215  s_int32 get_sint32 ();
216  string get_string ();
217  float get_float ();
218 
219 private:
220  /// NEVER pass this by value.
221  igzstream (igzstream& src);
222 };
223 
224 /**
225  * Class to write data from a Gzip compressed file.
226  */
227 class ogzstream : public gz_file
228 {
229 public:
230  /**
231  * Default constructor.
232  *
233  */
234  ogzstream ();
235 
236 #ifndef SWIG
237  /**
238  * Opens a file for write access.
239  *
240  * @param fname name of the file to open.
241  *
242  */
243  ogzstream (const string & fname);
244 #endif
245 
246  /**
247  * Destructor.
248  *
249  */
250  ~ogzstream ();
251 
252  /**
253  * Opens a file for write access.
254  *
255  * @param fname name of the file to open.
256  *
257  * @return true if succeed, false otherwise.
258  */
259  bool open (const string & fname);
260 
261  /**
262  * Writes a block of bytes to the file.
263  *
264  * @param to pointer to the buffer to write.
265  * @param size number of bytes to write.
266  */
267  void put_block (void * to, u_int32 size);
268 
269 #ifndef SWIG
270  /// Writes a boolean.
271  friend const bool& operator >> (const bool& n, ogzstream& gfile);
272 
273  /// Writes a char.
274  friend const char& operator >> (const char& n, ogzstream& gfile);
275 
276  /// Writes a u_int8.
277  friend const u_int8& operator >> (const u_int8& n, ogzstream& gfile);
278 
279  /// Writes a s_int8.
280  friend const s_int8& operator >> (const s_int8& n, ogzstream& gfile);
281 
282  /// Writes a u_int16.
283  friend const u_int16& operator >> (const u_int16& n, ogzstream& gfile);
284 
285  /// Writes a s_int16.
286  friend const s_int16& operator >> (const s_int16& n, ogzstream& gfile);
287 
288  /// Writes a u_int32.
289  friend const u_int32& operator >> (const u_int32& n, ogzstream& gfile);
290 
291  /// Writes a s_int32.
292  friend const s_int32& operator >> (const s_int32& n, ogzstream& gfile);
293 
294  /// Writes a string.
295  friend string& operator >> (const string& s, ogzstream& gfile);
296 
297  /// Writes a float.
298  friend const float& operator >> (const float& s, ogzstream& gfile);
299 #endif
300 
301  void put_bool (const bool &n) { n >> *this; }
302  void put_uint8 (const u_int8 &n) { n >> *this; }
303  void put_sint8 (const s_int8 &n) { n >> *this; }
304  void put_uint16 (const u_int16 &n) { n >> *this; }
305  void put_sint16 (const s_int16 &n) { n >> *this; }
306  void put_uint32 (const u_int32 &n) { n >> *this; }
307  void put_sint32 (const s_int32 &n) { n >> *this; }
308  void put_string (const string& s) { s >> *this; }
309  void put_float (const float &n) { n >> *this; }
310 
311 private:
312  /// NEVER pass this by value.
313  ogzstream (ogzstream& src);
314 };
315 
316 /// File version control class.
317 class fileops
318 {
319 public:
320  /**
321  * Sets the version number of a file.
322  *
323  * @param file file where to write the version number.
324  * @param version version number to write.
325  */
326  static void put_version (ogzstream& file, u_int16 version); // Set version of a file
327 
328  /**
329  *
330  *
331  * @param file file to check version.
332  * @param min minimum expected version number.
333  * @param max maximum expected version number.
334  * @param name filename of the passed file.
335  *
336  * @return true if
337  */
338  static bool get_version (igzstream& file, u_int16 min, u_int16 max, string name); // Check version
339 };
340 
341 
342 #endif // __FILEOPS_H__
types.h
Declares some basic types.
igzstream
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
u_int32
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
s_int32
#define s_int32
32 bits long signed integer
Definition: types.h:50
ogzstream
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
u_int8
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
s_int16
#define s_int16
16 bits long signed integer
Definition: types.h:47
gz_file::is_open
bool is_open()
Returns whether the file is opened or not.
Definition: fileops.h:103
gz_type
gz_type
Enumeration to know whether a file is read or write opened.
Definition: fileops.h:49
s_int8
#define s_int8
8 bits long signed integer
Definition: types.h:44
gz_file
Base class for igzstream and ogzstream.
Definition: fileops.h:56
operator>>
const bool & operator>>(const bool &n, ogzstream &gfile)
Writes a boolean.
Definition: fileops.cc:277
u_int16
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
fileops
File version control class.
Definition: fileops.h:317
gz_file::file
gzFile file
The actual gzFile.
Definition: fileops.h:121
operator<<
bool & operator<<(bool &n, igzstream &gfile)
Reads a boolean.
Definition: fileops.cc:87
gz_file::eof
bool eof()
Returns whether the file is at it's end or not.
Definition: fileops.h:111