![]() |
Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members
![]() |
00001 /******************************************************************************** 00002 * * 00003 * P e r s i s t e n t S t o r a g e S t r e a m C l a s s e s * 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.17 2002/04/15 02:35:29 fox Exp $ * 00023 ********************************************************************************/ 00024 #ifndef FXSTREAM_H 00025 #define FXSTREAM_H 00026 00027 00028 namespace FX { 00029 00030 00031 /// Stream data flow direction 00032 enum FXStreamDirection { 00033 FXStreamDead=0, /// Unopened stream 00034 FXStreamSave=1, /// Saving stuff to stream 00035 FXStreamLoad=2 /// Loading stuff from stream 00036 }; 00037 00038 00039 /// Stream status codes 00040 enum FXStreamStatus { 00041 FXStreamOK=0, /// OK 00042 FXStreamEnd=1, /// Try read past end of stream 00043 FXStreamFull=2, /// Filled up stream buffer or disk full 00044 FXStreamNoWrite=3, /// Unable to open for write 00045 FXStreamNoRead=4, /// Unable to open for read 00046 FXStreamFormat=5, /// Stream format error 00047 FXStreamUnknown=6, /// Trying to read unknown class 00048 FXStreamAlloc=7, /// Alloc failed 00049 FXStreamFailure=8 /// General failure 00050 }; 00051 00052 00053 // Hash table entry 00054 struct FXStreamHashEntry { 00055 FXuint ref; // Object reference number 00056 FXObject* obj; // Pointer to object 00057 }; 00058 00059 00060 class FXString; 00061 00062 00063 /************************ Persistent Store Definition *************************/ 00064 00065 /// Persistent store definition 00066 class FXAPI FXStream { 00067 private: 00068 FXStreamHashEntry* table; // Hash table 00069 FXuint ntable; // Amount of table that is filled 00070 FXuint ninit; // Table size 00071 FXuint no; // Count objects 00072 FXbool swap; // Swap bytes on readin 00073 const FXObject* parent; // Parent object 00074 void grow(); // Enlarge the table 00075 protected: 00076 FXStreamDirection dir; // Direction of current transfer 00077 FXStreamStatus code; // Status code 00078 unsigned long pos; // Position 00079 protected: 00080 00081 /// Save bunch of items 00082 virtual void saveItems(const void *buf,unsigned long n); 00083 00084 /// Load bunch of items 00085 virtual void loadItems(void *buf,unsigned long 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,unsigned long n); 00141 FXStream& save(const FXchar* p,unsigned long n); 00142 FXStream& save(const FXushort* p,unsigned long n); 00143 FXStream& save(const FXshort* p,unsigned long n); 00144 FXStream& save(const FXuint* p,unsigned long n); 00145 FXStream& save(const FXint* p,unsigned long n); 00146 FXStream& save(const FXfloat* p,unsigned long n); 00147 FXStream& save(const FXdouble* p,unsigned long n); 00148 00149 #ifdef FX_LONG 00150 FXStream& save(const FXlong* p,unsigned long n); 00151 FXStream& save(const FXulong* p,unsigned long 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,unsigned long n); 00173 FXStream& load(FXchar* p,unsigned long n); 00174 FXStream& load(FXushort* p,unsigned long n); 00175 FXStream& load(FXshort* p,unsigned long n); 00176 FXStream& load(FXuint* p,unsigned long n); 00177 FXStream& load(FXint* p,unsigned long n); 00178 FXStream& load(FXfloat* p,unsigned long n); 00179 FXStream& load(FXdouble* p,unsigned long n); 00180 00181 #ifdef FX_LONG 00182 FXStream& load(FXlong* p,unsigned long n); 00183 FXStream& load(FXulong* p,unsigned long 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 private: 00200 void *file; // File being dealt with 00201 protected: 00202 virtual void saveItems(const void *buf,unsigned long n); 00203 virtual void loadItems(void *buf,unsigned long n); 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 private: 00236 FXuchar *ptr; // Memory pointer 00237 unsigned long space; // Space in buffer 00238 FXbool owns; // Owns the data array 00239 protected: 00240 virtual void saveItems(const void *buf,unsigned long n); 00241 virtual void loadItems(void *buf,unsigned long n); 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,unsigned long sp,FXStreamDirection save_or_load); 00252 00253 /// Get available space 00254 unsigned long getSpace() const { return space; } 00255 00256 /// Set available space 00257 void setSpace(unsigned long sp); 00258 00259 /// Take buffer away from stream 00260 void takeBuffer(FXuchar*& buffer,unsigned long& sp); 00261 00262 /// Give buffer to stream 00263 void giveBuffer(FXuchar *buffer,unsigned long 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