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