Adonthell  0.4
fileops.cc
Go to the documentation of this file.
1 /*
2  $Id: fileops.cc,v 1.13 2003/01/24 22:15:43 ksterker Exp $
3 
4  Copyright (C) 1999/2000/2001/2002/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.cc
19  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
20  *
21  * @brief Defines the igzstream, ogzstream and fileops classes.
22  *
23  */
24 
25 #include <stdio.h>
26 #include <iostream>
27 #include <SDL/SDL_endian.h>
28 #include "fileops.h"
29 
30 
31 using namespace std;
32 
33 
35 {
36  opened = false;
37 }
38 
39 gz_file::gz_file (const string & fname, gz_type t)
40 {
41  opened = false;
42  open (fname, t);
43 }
44 
46 {
47  if (is_open ()) gzclose (file);
48 }
49 
50 bool gz_file::open (const string & fname, gz_type t)
51 {
52  if (t == READ) file = gzopen (fname.c_str (),"rb6");
53  else file = gzopen (fname.c_str (),"wb6");
54  if (!file) return false;
55  opened = true;
56  return true;
57 }
58 
60 {
61  if (is_open ()) gzclose (file);
62  opened = false;
63 }
64 
66 {
67 }
68 
69 igzstream::igzstream (const string & fname) : gz_file (fname, READ)
70 {
71 }
72 
74 {
75 }
76 
77 bool igzstream::open (const string & fname)
78 {
79  return gz_file::open (fname, READ);
80 }
81 
82 /// Reads a boolean.
83 bool& operator << (bool& n, igzstream& gfile)
84 {
85  u_int8 b;
86  gzread (gfile.file, &b, sizeof (b));
87  return (n = b);
88 }
89 
90 /// Reads a boolean.
92 {
93  u_int8 b;
94  gzread (file, &b, sizeof (b));
95  return b;
96 }
97 
98 /// Reads a char.
99 char& operator << (char& n, igzstream& gfile)
100 {
101  gzread (gfile.file, &n, sizeof (n));
102  return n;
103 }
104 
105 /// Read a block of size chars
106 void igzstream::get_block (void * to, u_int32 size)
107 {
108  gzread (file, to, size);
109 }
110 
111 /// Reads a u_int8.
113 {
114  gzread(gfile.file, &n, sizeof (n));
115  return n;
116 }
117 
118 /// Reads a u_int8.
120 {
121  u_int8 n;
122  gzread (file, &n, sizeof (n));
123  return n;
124 }
125 
126 /// Reads a s_int8.
128 {
129  gzread(gfile.file, &n, sizeof (n));
130  return n;
131 }
132 
133 /// Reads a s_int8.
135 {
136  s_int8 n;
137  gzread (file, &n, sizeof (n));
138  return n;
139 }
140 
141 /// Reads a u_int16.
143 {
144  gzread(gfile.file, &n, sizeof (n));
145  n = SDL_SwapLE16(n);
146  return n;
147 }
148 
149 /// Reads a u_int16.
151 {
152  u_int16 n;
153  gzread (file, &n, sizeof (n));
154  return SDL_SwapLE16(n);
155 }
156 
157 /// Reads a s_int16.
159 {
160  gzread(gfile.file, &n, sizeof (n));
161  n = SDL_SwapLE16(n);
162  return n;
163 }
164 
165 /// Reads a s_int16.
167 {
168  s_int16 n;
169  gzread (file, &n, sizeof (n));
170  return SDL_SwapLE16(n);
171 }
172 
173 /// Reads a u_int32.
175 {
176  gzread(gfile.file, &n, sizeof (n));
177  n = SDL_SwapLE32(n);
178  return n;
179 }
180 
181 /// Reads a u_int32.
183 {
184  u_int32 n;
185  gzread (file, &n, sizeof (n));
186  return SDL_SwapLE32(n);
187 }
188 
189 /// Reads a s_int32.
191 {
192  gzread(gfile.file, &n, sizeof (n));
193  n = SDL_SwapLE32(n);
194  return n;
195 }
196 
197 /// Reads a s_int32.
199 {
200  s_int32 n;
201  gzread (file, &n, sizeof (n));
202  return SDL_SwapLE32(n);
203 }
204 
205 /// Reads a string.
206 string& operator << (string& s, igzstream& gfile)
207 {
208  u_int16 strl;
209  char c;
210  s = "";
211  strl << gfile;
212  while (strl)
213  {
214  c << gfile;
215  s += c;
216  strl --;
217  }
218  return s;
219 }
220 
221 /// Reads a string.
223 {
224  string s;
225  s << *this;
226  return s;
227 }
228 
229 /// Reads a float.
230 float& operator << (float& f, igzstream& gfile)
231 {
232  string sf;
233  sf << gfile;
234 
235  // floats saved as strings to remain independent of architecture
236  sscanf (sf.c_str (), "%f", &f);
237 
238  return f;
239 }
240 
241 /// Reads a float.
243 {
244  float f;
245  f << *this;
246  return f;
247 }
248 
249 
251 {
252 }
253 
254 ogzstream::ogzstream (const string & fname) : gz_file (fname, WRITE)
255 {
256 }
257 
259 {
260 }
261 
262 bool ogzstream::open (const string & fname)
263 {
264  return gz_file::open (fname, WRITE);
265 }
266 
267 void ogzstream::put_block (void * to, u_int32 size)
268 {
269  gzwrite (file, to, size);
270 }
271 
272 /// Writes a boolean.
273 const bool& operator >> (const bool& n, ogzstream& gfile)
274 {
275  u_int8 b = n;
276  gzwrite (gfile.file, &b, sizeof (b));
277  return n;
278 }
279 
280 /// Writes a char.
281 const char& operator >> (const char& n, ogzstream& gfile)
282 {
283  gzwrite (gfile.file, (char *) &n, sizeof (n));
284  return n;
285 }
286 
287 /// Writes a u_int8.
288 const u_int8& operator >> (const u_int8& n, ogzstream& gfile)
289 {
290  gzwrite(gfile.file, (u_int8 *) &n, sizeof (n));
291  return n;
292 }
293 
294 /// Writes a s_int8.
295 const s_int8& operator >> (const s_int8& n, ogzstream& gfile)
296 {
297  gzwrite(gfile.file, (s_int8 *) &n, sizeof (n));
298  return n;
299 }
300 
301 /// Writes a u_int16.
302 const u_int16& operator >> (const u_int16& n, ogzstream& gfile)
303 {
304  u_int16 s = SDL_SwapLE16(n);
305  gzwrite(gfile.file, (u_int16 *) &s, sizeof (n));
306  return n;
307 }
308 
309 /// Writes a s_int16.
310 const s_int16& operator >> (const s_int16& n, ogzstream& gfile)
311 {
312  s_int16 s = SDL_SwapLE16(n);
313  gzwrite(gfile.file, (s_int16 *) &s, sizeof (n));
314  return n;
315 }
316 
317 /// Writes a u_int32.
318 const u_int32& operator >> (const u_int32& n, ogzstream& gfile)
319 {
320  u_int32 s = SDL_SwapLE32(n);
321  gzwrite(gfile.file, (u_int32 *) &s, sizeof (n));
322  return n;
323 }
324 
325 /// Writes a s_int32.
326 const s_int32& operator >> (const s_int32& n, ogzstream& gfile)
327 {
328  s_int32 s = SDL_SwapLE32(n);
329  gzwrite(gfile.file, (s_int32 *) &s, sizeof (n));
330  return n;
331 }
332 
333 /// Writes a string.
334 string& operator >> (const string& s, ogzstream& gfile)
335 {
336  u_int16 strl = s.length ();
337  string::iterator i;
338  strl >> gfile;
339 
340  for (i = ((string&) s).begin (); i != ((string&) s).end (); i++)
341  (*i) >> gfile;
342  return (string&) s;
343 }
344 
345 /// Writes a float.
346 const float& operator >> (const float& f, ogzstream& gfile)
347 {
348  char sf[16];
349 
350  // floats saved as strings to remain independent of architecture
351  snprintf (sf, 16, "%f", f);
352  sf >> gfile;
353 
354  return f;
355 }
356 
358 {
359  char c = 'v';
360  c >> file;
361  version >> file;
362 }
363 
364 // read version info from file and check whether we can handle it
365 bool fileops::get_version (igzstream& file, u_int16 min, u_int16 max, string name)
366 {
367  char vinfo;
368  u_int16 version;
369 
370  vinfo << file;
371 
372  if (name == "") name = "<unknown>";
373 
374  // file contains no version info
375  if (vinfo != 'v')
376  {
377  cerr << "Version information missing in file \"" << name << endl;
378  cerr << "You should get a more recent data package.\n";
379  return false;
380  }
381 
382  // check whether file has the version we expect
383  version << file;
384 
385  if (version < min || version > max)
386  {
387  cerr << "File \"" << name << "\" has\nversion number " << version << ", ";
388  cerr << "but I was expecting " << min << " <= version <= " << max << endl;
389 
390  // file is newer than code
391  if (version > max)
392  cerr << "You should get an up-to-date version of this program.\n\n";
393  // file is older than code
394  else
395  cerr << "You should probably get a more recent data package.\n";
396 
397  return false;
398  }
399  return true;
400 }
~igzstream()
Destructor.
Definition: fileops.cc:73
Class to write data from a Gzip compressed file.
Definition: fileops.h:223
void close()
Close the file that was opened.
Definition: fileops.cc:59
friend bool & operator<<(bool &n, igzstream &gfile)
Reads a boolean.
Definition: fileops.cc:83
gzFile file
The actual gzFile.
Definition: fileops.h:117
#define s_int32
32 bits long signed integer
Definition: types.h:44
Class to read data from a Gzip compressed file.
Definition: fileops.h:131
u_int32 get_uint32()
Reads a u_int32.
Definition: fileops.cc:182
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
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
s_int8 get_sint8()
Reads a s_int8.
Definition: fileops.cc:134
s_int16 get_sint16()
Reads a s_int16.
Definition: fileops.cc:166
bool open(const string &fname)
Opens a file for write access.
Definition: fileops.cc:262
bool open(const string &fname)
Opens a file for read access.
Definition: fileops.cc:77
float get_float()
Reads a float.
Definition: fileops.cc:242
static bool get_version(igzstream &file, u_int16 min, u_int16 max, string name)
Definition: fileops.cc:365
ogzstream()
Default constructor.
Definition: fileops.cc:250
friend const bool & operator>>(const bool &n, ogzstream &gfile)
Writes a boolean.
Definition: fileops.cc:273
igzstream()
Default constructor.
Definition: fileops.cc:65
gz_type
Enumeration to know whether a file is read or write opened.
Definition: fileops.h:45
bool open(const string &fname, gz_type t)
Opens a file.
Definition: fileops.cc:50
#define s_int16
16 bits long signed integer
Definition: types.h:41
~ogzstream()
Destructor.
Definition: fileops.cc:258
u_int16 get_uint16()
Reads a u_int16.
Definition: fileops.cc:150
virtual ~gz_file()
Destructor.
Definition: fileops.cc:45
gz_file()
Default constructor.
Definition: fileops.cc:34
static void put_version(ogzstream &file, u_int16 version)
Sets the version number of a file.
Definition: fileops.cc:357
s_int32 get_sint32()
Reads a s_int32.
Definition: fileops.cc:198
Declares the igzstream, ogzstream and fileops classes.
bool get_bool()
Reads a boolean.
Definition: fileops.cc:91
string get_string()
Reads a string.
Definition: fileops.cc:222
#define s_int8
8 bits long signed integer
Definition: types.h:38
void get_block(void *to, u_int32 size)
Reads a block of bytes from the file.
Definition: fileops.cc:106
Base class for igzstream and ogzstream.
Definition: fileops.h:52
void put_block(void *to, u_int32 size)
Writes a block of bytes to the file.
Definition: fileops.cc:267
u_int8 get_uint8()
Reads a u_int8.
Definition: fileops.cc:119