1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/********************************************************************************
* *
* Persistent Storage Stream *
* *
*********************************************************************************
* Copyright (C) 1997,2002 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*********************************************************************************
* $Id: FXStream.h,v 1.12 2002/01/18 22:42:55 jeroen Exp $ *
********************************************************************************/
#ifndef FXSTREAM_H
#define FXSTREAM_H
/// Stream data flow direction
enum FXStreamDirection {
FXStreamDead=0, // Unopened stream
FXStreamSave=1, // Saving stuff to stream
FXStreamLoad=2 // Loading stuff from stream
};
/// Stream status codes
enum FXStreamStatus {
FXStreamOK=0, // OK
FXStreamEnd=1, // Try read past end of stream
FXStreamFull=2, // Filled up stream buffer or disk full
FXStreamNoWrite=3, // Unable to open for write
FXStreamNoRead=4, // Unable to open for read
FXStreamFormat=5, // Stream format error
FXStreamUnknown=6, // Trying to read unknown class
FXStreamAlloc=7, // Alloc failed
FXStreamFailure=8 // General failure
};
// Hash table entry
struct FXStreamHashEntry {
FXuint ref; // Object reference number
FXObject* obj; // Pointer to object
};
class FXString;
/************************ Persistent Store Definition *************************/
/// Persistent store definition
class FXAPI FXStream {
FXStreamHashEntry* table; // Hash table
FXuint ntable; // Amount of table that is filled
FXuint ninit; // Table size
FXuint no; // Count objects
FXbool swap; // Swap bytes on readin
const FXObject* parent; // Parent object
void grow(); // Enlarge the table
protected:
FXStreamDirection dir; // Direction of current transfer
FXStreamStatus code; // Status code
unsigned long pos; // Position
protected:
/// Save bunch of items
virtual void saveItems(const void *buf,FXuint n);
/// Load bunch of items
virtual void loadItems(void *buf,FXuint n);
public:
/// Constructor
FXStream(const FXObject* cont=NULL);
/// Open archive return TRUE if OK
FXbool open(FXStreamDirection save_or_load);
/// Close; return TRUE if OK
FXbool close();
/// Get status code
FXStreamStatus status() const { return code; }
/// Set status code
void setError(FXStreamStatus err);
/// Obtain direction
FXStreamDirection direction() const { return dir; }
/// Get parent object
const FXObject* container() const { return parent; }
/// Get position
unsigned long position() const { return pos; }
/// Move to position
virtual FXbool position(unsigned long p);
/// Change swap bytes flag
void swapBytes(FXbool s){ swap=s; }
/// Get swap bytes flag
FXbool swapBytes() const { return swap; }
/// Return implementation's endianness
static FXbool isLittleEndian(){ return !FOX_BIGENDIAN; }
/// Save to stream
virtual FXStream& operator<<(const FXuchar& v);
virtual FXStream& operator<<(const FXchar& v);
FXStream& operator<<(const FXushort& v);
FXStream& operator<<(const FXshort& v);
FXStream& operator<<(const FXuint& v);
FXStream& operator<<(const FXint& v);
FXStream& operator<<(const FXfloat& v);
FXStream& operator<<(const FXdouble& v);
#ifdef FX_LONG
FXStream& operator<<(const FXlong& v);
FXStream& operator<<(const FXulong& v);
#endif
FXStream& save(const FXuchar* p,FXuint n);
FXStream& save(const FXchar* p,FXuint n);
FXStream& save(const FXushort* p,FXuint n);
FXStream& save(const FXshort* p,FXuint n);
FXStream& save(const FXuint* p,FXuint n);
FXStream& save(const FXint* p,FXuint n);
FXStream& save(const FXfloat* p,FXuint n);
FXStream& save(const FXdouble* p,FXuint n);
#ifdef FX_LONG
FXStream& save(const FXlong* p,FXuint n);
FXStream& save(const FXulong* p,FXuint n);
#endif
/// Save object
FXStream& saveObject(const FXObject* v);
/// Load from stream
virtual FXStream& operator>>(FXuchar& v);
virtual FXStream& operator>>(FXchar& v);
FXStream& operator>>(FXushort& v);
FXStream& operator>>(FXshort& v);
FXStream& operator>>(FXuint& v);
FXStream& operator>>(FXint& v);
FXStream& operator>>(FXfloat& v);
FXStream& operator>>(FXdouble& v);
#ifdef FX_LONG
FXStream& operator>>(FXlong& v);
FXStream& operator>>(FXulong& v);
#endif
FXStream& load(FXuchar* p,FXuint n);
FXStream& load(FXchar* p,FXuint n);
FXStream& load(FXushort* p,FXuint n);
FXStream& load(FXshort* p,FXuint n);
FXStream& load(FXuint* p,FXuint n);
FXStream& load(FXint* p,FXuint n);
FXStream& load(FXfloat* p,FXuint n);
FXStream& load(FXdouble* p,FXuint n);
#ifdef FX_LONG
FXStream& load(FXlong* p,FXuint n);
FXStream& load(FXulong* p,FXuint n);
#endif
/// Load object
FXStream& loadObject(FXObject*& v);
/// Destructor
virtual ~FXStream();
};
/*************************** File Store Definition ***************************/
/// File Store Definition
class FXAPI FXFileStream : public FXStream {
void* file; // File being dealt with
protected:
virtual void saveItems(const void *buf,FXuint n);
virtual void loadItems(void *buf,FXuint n);
public:
/// Create file store
FXFileStream(const FXObject* cont=NULL);
/// Open file store
FXbool open(const FXString& filename,FXStreamDirection save_or_load);
/// Close file store
FXbool close();
/// Move to position
virtual FXbool position(unsigned long p);
/// Save to stream
virtual FXStream& operator<<(const FXuchar& v);
virtual FXStream& operator<<(const FXchar& v);
/// Load from stream
virtual FXStream& operator>>(FXuchar& v);
virtual FXStream& operator>>(FXchar& v);
/// Destructor
virtual ~FXFileStream();
};
/************************** Memory Store Definition **************************/
/// Memory Store Definition
class FXAPI FXMemoryStream : public FXStream {
FXuchar *ptr; // Memory pointer
FXuint space; // Space in buffer
FXbool owns; // Owns the data array
protected:
virtual void saveItems(const void *buf,FXuint n);
virtual void loadItems(void *buf,FXuint n);
public:
/// Create memory store
FXMemoryStream(const FXObject* cont=NULL);
/// Open file store
FXbool open(FXuchar* data,FXStreamDirection save_or_load);
/// Open memory store
FXbool open(FXuchar* data,FXuint sp,FXStreamDirection save_or_load);
/// Get available space
FXuint getSpace() const { return space; }
/// Set available space
void setSpace(FXuint sp);
/// Take buffer away from stream
void takeBuffer(FXuchar*& buffer,FXuint& sp);
/// Give buffer to stream
void giveBuffer(FXuchar *buffer,FXuint sp);
/// Close memory store
FXbool close();
/// Move to position
virtual FXbool position(unsigned long p);
/// Save to stream
virtual FXStream& operator<<(const FXuchar& v);
virtual FXStream& operator<<(const FXchar& v);
/// Load from stream
virtual FXStream& operator>>(FXuchar& v);
virtual FXStream& operator>>(FXchar& v);
/// Destructor
virtual ~FXMemoryStream();
};
#endif
|