Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXStream.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                         Persistent Storage Stream                             *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2002 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXStream.h,v 1.12 2002/01/18 22:42:55 jeroen Exp $                       *
00023 ********************************************************************************/
00024 #ifndef FXSTREAM_H
00025 #define FXSTREAM_H
00026 
00027 
00028 
00029 /// Stream data flow direction
00030 enum FXStreamDirection {
00031   FXStreamDead=0,               // Unopened stream
00032   FXStreamSave=1,               // Saving stuff to stream
00033   FXStreamLoad=2                // Loading stuff from stream
00034   };
00035 
00036 
00037 /// Stream status codes
00038 enum FXStreamStatus {
00039   FXStreamOK=0,                 // OK
00040   FXStreamEnd=1,                // Try read past end of stream
00041   FXStreamFull=2,               // Filled up stream buffer or disk full
00042   FXStreamNoWrite=3,            // Unable to open for write
00043   FXStreamNoRead=4,             // Unable to open for read
00044   FXStreamFormat=5,             // Stream format error
00045   FXStreamUnknown=6,            // Trying to read unknown class
00046   FXStreamAlloc=7,              // Alloc failed
00047   FXStreamFailure=8             // General failure
00048   };
00049 
00050 
00051 // Hash table entry
00052 struct FXStreamHashEntry {
00053   FXuint    ref;                // Object reference number
00054   FXObject* obj;                // Pointer to object
00055   };
00056 
00057 
00058 class FXString;
00059 
00060 
00061 /************************  Persistent Store Definition  *************************/
00062 
00063 /// Persistent store definition
00064 class FXAPI FXStream {
00065 
00066   FXStreamHashEntry* table;     // Hash table
00067   FXuint             ntable;    // Amount of table that is filled
00068   FXuint             ninit;     // Table size
00069   FXuint             no;        // Count objects
00070   FXbool             swap;      // Swap bytes on readin
00071   const FXObject*    parent;    // Parent object
00072   void               grow();    // Enlarge the table
00073 
00074 protected:
00075   FXStreamDirection  dir;       // Direction of current transfer
00076   FXStreamStatus     code;      // Status code
00077   unsigned long      pos;       // Position
00078 
00079 protected:
00080 
00081   /// Save bunch of items
00082   virtual void saveItems(const void *buf,FXuint n);
00083 
00084   /// Load bunch of items
00085   virtual void loadItems(void *buf,FXuint n);
00086 
00087 public:
00088 
00089   /// Constructor
00090   FXStream(const FXObject* cont=NULL);
00091 
00092   /// Open archive return TRUE if OK
00093   FXbool open(FXStreamDirection save_or_load);
00094 
00095   /// Close; return TRUE if OK
00096   FXbool close();
00097 
00098   /// Get status code
00099   FXStreamStatus status() const { return code; }
00100 
00101   /// Set status code
00102   void setError(FXStreamStatus err);
00103 
00104   /// Obtain direction
00105   FXStreamDirection direction() const { return dir; }
00106 
00107   /// Get parent object
00108   const FXObject* container() const { return parent; }
00109 
00110   /// Get position
00111   unsigned long position() const { return pos; }
00112 
00113   /// Move to position
00114   virtual FXbool position(unsigned long p);
00115 
00116   /// Change swap bytes flag
00117   void swapBytes(FXbool s){ swap=s; }
00118 
00119   /// Get swap bytes flag
00120   FXbool swapBytes() const { return swap; }
00121 
00122   /// Return implementation's endianness
00123   static FXbool isLittleEndian(){ return !FOX_BIGENDIAN; }
00124 
00125   /// Save to stream
00126   virtual FXStream& operator<<(const FXuchar& v);
00127   virtual FXStream& operator<<(const FXchar& v);
00128   FXStream& operator<<(const FXushort& v);
00129   FXStream& operator<<(const FXshort& v);
00130   FXStream& operator<<(const FXuint& v);
00131   FXStream& operator<<(const FXint& v);
00132   FXStream& operator<<(const FXfloat& v);
00133   FXStream& operator<<(const FXdouble& v);
00134 
00135 #ifdef FX_LONG
00136   FXStream& operator<<(const FXlong& v);
00137   FXStream& operator<<(const FXulong& v);
00138 #endif
00139 
00140   FXStream& save(const FXuchar* p,FXuint n);
00141   FXStream& save(const FXchar* p,FXuint n);
00142   FXStream& save(const FXushort* p,FXuint n);
00143   FXStream& save(const FXshort* p,FXuint n);
00144   FXStream& save(const FXuint* p,FXuint n);
00145   FXStream& save(const FXint* p,FXuint n);
00146   FXStream& save(const FXfloat* p,FXuint n);
00147   FXStream& save(const FXdouble* p,FXuint n);
00148 
00149 #ifdef FX_LONG
00150   FXStream& save(const FXlong* p,FXuint n);
00151   FXStream& save(const FXulong* p,FXuint n);
00152 #endif
00153 
00154   /// Save object
00155   FXStream& saveObject(const FXObject* v);
00156 
00157   /// Load from stream
00158   virtual FXStream& operator>>(FXuchar& v);
00159   virtual FXStream& operator>>(FXchar& v);
00160   FXStream& operator>>(FXushort& v);
00161   FXStream& operator>>(FXshort& v);
00162   FXStream& operator>>(FXuint& v);
00163   FXStream& operator>>(FXint& v);
00164   FXStream& operator>>(FXfloat& v);
00165   FXStream& operator>>(FXdouble& v);
00166 
00167 #ifdef FX_LONG
00168   FXStream& operator>>(FXlong& v);
00169   FXStream& operator>>(FXulong& v);
00170 #endif
00171 
00172   FXStream& load(FXuchar* p,FXuint n);
00173   FXStream& load(FXchar* p,FXuint n);
00174   FXStream& load(FXushort* p,FXuint n);
00175   FXStream& load(FXshort* p,FXuint n);
00176   FXStream& load(FXuint* p,FXuint n);
00177   FXStream& load(FXint* p,FXuint n);
00178   FXStream& load(FXfloat* p,FXuint n);
00179   FXStream& load(FXdouble* p,FXuint n);
00180 
00181 #ifdef FX_LONG
00182   FXStream& load(FXlong* p,FXuint n);
00183   FXStream& load(FXulong* p,FXuint n);
00184 #endif
00185 
00186   /// Load object
00187   FXStream& loadObject(FXObject*& v);
00188 
00189   /// Destructor
00190   virtual ~FXStream();
00191   };
00192 
00193 
00194 
00195 /***************************  File Store Definition  ***************************/
00196 
00197 /// File Store Definition
00198 class FXAPI FXFileStream : public FXStream {
00199   void* file;               // File being dealt with
00200 protected:
00201   virtual void saveItems(const void *buf,FXuint n);
00202   virtual void loadItems(void *buf,FXuint n);
00203 
00204 public:
00205 
00206   /// Create file store
00207   FXFileStream(const FXObject* cont=NULL);
00208 
00209   /// Open file store
00210   FXbool open(const FXString& filename,FXStreamDirection save_or_load);
00211 
00212   /// Close file store
00213   FXbool close();
00214 
00215   /// Move to position
00216   virtual FXbool position(unsigned long p);
00217 
00218   /// Save to stream
00219   virtual FXStream& operator<<(const FXuchar& v);
00220   virtual FXStream& operator<<(const FXchar& v);
00221 
00222   /// Load from stream
00223   virtual FXStream& operator>>(FXuchar& v);
00224   virtual FXStream& operator>>(FXchar& v);
00225 
00226   /// Destructor
00227   virtual ~FXFileStream();
00228   };
00229 
00230 
00231 /**************************  Memory Store Definition  **************************/
00232 
00233 /// Memory Store Definition
00234 class FXAPI FXMemoryStream : public FXStream {
00235   FXuchar *ptr;             // Memory pointer
00236   FXuint   space;           // Space in buffer
00237   FXbool   owns;            // Owns the data array
00238 protected:
00239   virtual void saveItems(const void *buf,FXuint n);
00240   virtual void loadItems(void *buf,FXuint n);
00241 
00242 public:
00243 
00244   /// Create memory store
00245   FXMemoryStream(const FXObject* cont=NULL);
00246 
00247   /// Open file store
00248   FXbool open(FXuchar* data,FXStreamDirection save_or_load);
00249 
00250   /// Open memory store
00251   FXbool open(FXuchar* data,FXuint sp,FXStreamDirection save_or_load);
00252 
00253   /// Get available space
00254   FXuint getSpace() const { return space; }
00255 
00256   /// Set available space
00257   void setSpace(FXuint sp);
00258 
00259   /// Take buffer away from stream
00260   void takeBuffer(FXuchar*& buffer,FXuint& sp);
00261 
00262   /// Give buffer to stream
00263   void giveBuffer(FXuchar *buffer,FXuint sp);
00264 
00265   /// Close memory store
00266   FXbool close();
00267 
00268   /// Move to position
00269   virtual FXbool position(unsigned long p);
00270 
00271   /// Save to stream
00272   virtual FXStream& operator<<(const FXuchar& v);
00273   virtual FXStream& operator<<(const FXchar& v);
00274 
00275   /// Load from stream
00276   virtual FXStream& operator>>(FXuchar& v);
00277   virtual FXStream& operator>>(FXchar& v);
00278 
00279   /// Destructor
00280   virtual ~FXMemoryStream();
00281   };
00282 
00283 
00284 
00285 #endif