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