• Main Page
  • Related Pages
  • Classes
  • Files
  • File List
  • File Members

fileops.h

Go to the documentation of this file.
00001 /*
00002    $Id: fileops.h,v 1.17 2003/01/20 20:18:43 ksterker Exp $
00003 
00004    Copyright (C) 2001/2003 Alexandre Courbot
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 
00016 
00017 /**
00018  * @file   fileops.h
00019  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
00020  * 
00021  * @brief  Declares the igzstream, ogzstream and fileops classes.
00022  * 
00023  */
00024 
00025 
00026 
00027 
00028 #ifndef FILEOPS_H_
00029 #define FILEOPS_H_
00030 
00031 #include <zlib.h>
00032 #include <string>
00033 #include "types.h"
00034 
00035 
00036 #ifndef SWIG
00037 using namespace std; 
00038 #endif
00039 
00040 
00041 /**
00042  * Enumeration to know whether a file is read or write opened.
00043  * 
00044  */ 
00045 typedef enum { READ, WRITE } gz_type;
00046 
00047 
00048 /**
00049  * Base class for igzstream and ogzstream.
00050  * 
00051  */ 
00052 class gz_file
00053 {
00054 public:
00055     /** 
00056      * Default constructor.
00057      * 
00058      */
00059     gz_file ();
00060 
00061 #ifndef SWIG
00062     /** 
00063      * 
00064      * 
00065      * @param fname name of the file to open.
00066      * @param t access (READ or WRITE).
00067      */
00068     gz_file (const string & fname, gz_type t);
00069 #endif
00070 
00071     /** 
00072      * Destructor.
00073      * 
00074      */
00075     virtual ~gz_file ();
00076     
00077     /** 
00078      * Opens a file.
00079      * 
00080      * @param fname name of the file to open.
00081      * @param t access (READ or WRITE).
00082      * 
00083      * @return true if succeed, false otherwise.
00084      */
00085     bool open (const string & fname, gz_type t);
00086 
00087     /** 
00088      * Close the file that was opened.
00089      * 
00090      */
00091     void close ();
00092 
00093     /** 
00094      * Returns whether the file is opened or not.
00095      * 
00096      * 
00097      * @return true if the file is opened, false otherwise.
00098      */
00099     bool is_open () { return opened; }
00100 
00101     /** 
00102      * Returns whether the file is at it's end or not.
00103      * 
00104      * 
00105      * @return true if the end of file is reached, else otherwise.
00106      */
00107     bool eof () 
00108     {
00109         return gzeof (file); 
00110     }
00111     
00112 protected:
00113     /** 
00114      * The actual gzFile.
00115      * 
00116      */ 
00117     gzFile file;
00118 
00119 private:
00120     /// NEVER pass this by value.
00121     gz_file (gz_file& src); 
00122     
00123     /// Opened or not?
00124     bool opened; 
00125 };
00126 
00127 
00128 /** 
00129  * Class to read data from a Gzip compressed file.
00130  */ 
00131 class igzstream : public gz_file
00132 {
00133 public:
00134     /**
00135      * Default constructor.
00136      * 
00137      */ 
00138     igzstream ();
00139 
00140 #ifndef SWIG
00141     /** 
00142      * Opens a file for read access.
00143      * 
00144      * @param fname name of the file to open.
00145      * 
00146      */ 
00147     igzstream (const string & fname);
00148 #endif
00149     
00150     /**
00151      * Destructor.
00152      * 
00153      */ 
00154     ~igzstream ();
00155 
00156     /** 
00157      * Opens a file for read access.
00158      * 
00159      * @param fname name of the file to open.
00160      * 
00161      * @return true if succeed, false otherwise.
00162      */
00163     bool open (const string & fname);
00164 
00165     /** 
00166      * Reads a block of bytes from the file.
00167      * 
00168      * @param to pointer to the buffer where to read.
00169      * @param size number of bytes to read.
00170      */
00171     void get_block (void * to, u_int32 size); 
00172 
00173 #ifndef SWIG
00174     /// Reads a boolean.
00175     friend bool& operator << (bool& n, igzstream& gfile);
00176 
00177     /// Reads a char.
00178     friend char& operator << (char& n, igzstream& gfile);
00179 
00180     /// Reads a u_int8.
00181     friend u_int8& operator << (u_int8& n, igzstream& gfile);
00182 
00183     /// Reads a s_int8.
00184     friend s_int8& operator << (s_int8& n, igzstream& gfile);
00185 
00186     /// Reads a u_int16.
00187     friend u_int16& operator << (u_int16& n, igzstream& gfile);
00188 
00189     /// Reads a s_int16.
00190     friend s_int16& operator << (s_int16& n, igzstream& gfile);
00191 
00192     /// Reads a u_int32.
00193     friend u_int32& operator << (u_int32& n, igzstream& gfile);
00194 
00195     /// Reads a s_int32.
00196     friend s_int32& operator << (s_int32& n, igzstream& gfile);
00197 
00198     /// Reads a string.
00199     friend string& operator << (string& s, igzstream& gfile);
00200     
00201     /// Reads a float.
00202     friend float& operator << (float& s, igzstream& gfile);
00203 #endif
00204 
00205     bool get_bool ();
00206     u_int8 get_uint8 ();
00207     s_int8 get_sint8 ();
00208     u_int16 get_uint16 ();
00209     s_int16 get_sint16 ();
00210     u_int32 get_uint32 ();
00211     s_int32 get_sint32 ();
00212     string get_string ();
00213     float get_float ();
00214 
00215 private:
00216     /// NEVER pass this by value.
00217     igzstream (igzstream& src);  
00218 };
00219 
00220 /** 
00221  * Class to write data from a Gzip compressed file.
00222  */ 
00223 class ogzstream : public gz_file
00224 {
00225 public:
00226     /**
00227      * Default constructor.
00228      * 
00229      */ 
00230     ogzstream ();
00231 
00232 #ifndef SWIG
00233     /** 
00234      * Opens a file for write access.
00235      * 
00236      * @param fname name of the file to open.
00237      * 
00238      */ 
00239     ogzstream (const string & fname);
00240 #endif
00241     
00242     /**
00243      * Destructor.
00244      * 
00245      */
00246     ~ogzstream ();
00247 
00248     /** 
00249      * Opens a file for write access.
00250      * 
00251      * @param fname name of the file to open.
00252      * 
00253      * @return true if succeed, false otherwise.
00254      */
00255     bool open (const string & fname);
00256 
00257     /** 
00258      * Writes a block of bytes to the file.
00259      * 
00260      * @param to pointer to the buffer to write.
00261      * @param size number of bytes to write.
00262      */
00263     void put_block (void * to, u_int32 size); 
00264 
00265 #ifndef SWIG
00266     /// Writes a boolean.
00267     friend const bool& operator >> (const bool& n, ogzstream& gfile);
00268 
00269     /// Writes a char.
00270     friend const char& operator >> (const char& n, ogzstream& gfile);
00271 
00272     /// Writes a u_int8.
00273     friend const u_int8& operator >> (const u_int8& n, ogzstream& gfile);
00274 
00275     /// Writes a s_int8.
00276     friend const s_int8& operator >> (const s_int8& n, ogzstream& gfile);
00277 
00278     /// Writes a u_int16.
00279     friend const u_int16& operator >> (const u_int16& n, ogzstream& gfile);
00280 
00281     /// Writes a s_int16.
00282     friend const s_int16& operator >> (const s_int16& n, ogzstream& gfile);
00283 
00284     /// Writes a u_int32.
00285     friend const u_int32& operator >> (const u_int32& n, ogzstream& gfile);
00286 
00287     /// Writes a s_int32.
00288     friend const s_int32& operator >> (const s_int32& n, ogzstream& gfile);
00289 
00290     /// Writes a string.
00291     friend string& operator >> (const string& s, ogzstream& gfile);
00292     
00293     /// Writes a float.
00294     friend const float& operator >> (const float& s, ogzstream& gfile);
00295 #endif
00296 
00297     void put_bool (const bool &n)         { n >> *this; }
00298     void put_uint8 (const u_int8 &n)      { n >> *this; }
00299     void put_sint8 (const s_int8 &n)      { n >> *this; }
00300     void put_uint16 (const u_int16 &n)    { n >> *this; }
00301     void put_sint16 (const s_int16 &n)    { n >> *this; }
00302     void put_uint32 (const u_int32 &n)    { n >> *this; }
00303     void put_sint32 (const s_int32 &n)    { n >> *this; }
00304     void put_string (const string& s)     { s >> *this; }
00305     void put_float (const float &n)       { n >> *this; }
00306     
00307 private:
00308     /// NEVER pass this by value.
00309     ogzstream (ogzstream& src);   
00310 };
00311 
00312 /// File version control class.
00313 class fileops
00314 {
00315 public:
00316     /** 
00317      * Sets the version number of a file.
00318      * 
00319      * @param file file where to write the version number.
00320      * @param version version number to write.
00321      */
00322     static void put_version (ogzstream& file, u_int16 version);  // Set version of a file
00323 
00324     /** 
00325      * 
00326      * 
00327      * @param file file to check version.
00328      * @param min minimum expected version number.
00329      * @param max maximum expected version number.
00330      * @param name filename of the passed file.
00331      * 
00332      * @return true if 
00333      */
00334     static bool get_version (igzstream& file, u_int16 min, u_int16 max, string name); // Check version
00335 };
00336 
00337 
00338 #endif // __FILEOPS_H__

Generated on Fri Mar 18 2011 for Adonthell by  doxygen 1.7.1