1 /*****************************************************************************/
2 /* */
3 /* STREAM.H */
4 /* */
5 /* (C) 1993-96 Ullrich von Bassewitz */
6 /* Wacholderweg 14 */
7 /* D-70597 Stuttgart */
8 /* EMail: uz@musoftware.com */
9 /* */
10 /*****************************************************************************/
11
12
13
14 // $Id$
15 //
16 // $Log$
17 //
18 //
19
20
21
22 // Some words on the bit read and write functions for class Stream:
23 // The functions
24 //
25 // BitWrite
26 // BitRead
27 // BitFlush
28 //
29 // depend on the normal Read and Write functions and may *not* be used
30 // in a mixed fashion. BitWrite and BitRead manipulate byte sized buffers
31 // that are written to / read from the stream using Read and Write when
32 // needed. You *must* call BitFlush before calling any other non-"Bit"
33 // function, or your data will get corrupted. BitWrite and BitRead use
34 // the same buffer byte, so they may not be used together without calling
35 // BitFlush first.
36
37
38
39 #ifndef _STREAM_H
40 #define _STREAM_H
41
42
43
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "machine.h"
48
49 class Stream;
50
51 Stream& operator << (Stream&, char);
52 Stream& operator << (Stream&, unsigned char);
53 Stream& operator << (Stream&, signed char);
54 Stream& operator << (Stream&, i16);
55 Stream& operator << (Stream&, u16);
56 Stream& operator << (Stream&, i32);
57 Stream& operator << (Stream&, u32);
58 Stream& operator << (Stream&, float);
59 Stream& operator << (Stream&, double);
60 Stream& operator << (Stream&, char*);
61
62 Stream& operator >> (Stream&, char&);
63 Stream& operator >> (Stream&, unsigned char&);
64 Stream& operator >> (Stream&, signed char&);
65 Stream& operator >> (Stream&, i16&);
66 Stream& operator >> (Stream&, u16&);
67 Stream& operator >> (Stream&, i32&);
68 Stream& operator >> (Stream&, u32&);
69 Stream& operator >> (Stream&, float&);
70 Stream& operator >> (Stream&, double&);
71 Stream& operator >> (Stream&, char*);
72
73
74 #include "strmable.h"
75 #include "coll.h"
76 #include "str.h"
77
78
79
80
81 static const int stOk = 0; // things are allright
82 static const int stInitError = 1; // error initializing the stream
83 static const int stReadError = 2; // error reading from the stream
84 static const int stWriteError = 3; // error writing to the stream
85 static const int stGetError = 4; // get found not registered class
86 static const int stPutError = 5; // put found not registered class
87 static const int stMemoryError = 6; // not enough memory
88 static const int stStoreError = 7; // Keine Store-Methode angegeben
89 static const int stLoadError = 8; // Keine Load-Methode oder Fehler bei Load
90 static const int stCopyError = 9; // CopyFrom: error of source stream
91 static const int stSeekError = 10; // error using Seek, GetPos etc.
92 static const int stReadTimeout = 11; // Timeout on read (CharacterStream only)
93 static const int stWriteTimeout = 12; // Timeout on write (CharcterStream only)
94
95
96
97 // Register a class
98 #define LINK(Class, ID) static StreamableClass __r##Class (Class::Build, ID)
99
100
101
102 /*****************************************************************************/
103 /* class StreamableClass */
104 /*****************************************************************************/
105
106
107
108 // Object to keep track of registered streamable objects
109 class StreamableClass : public Object {
110
111 friend class StreamableClasses;
112 friend class Stream;
113
114 private:
115 u16 ID; // ID of streamable class
116 Streamable* (*Builder) (); // Pointer to Build member function
117
118 public:
119 StreamableClass (Streamable* (*BuildFunc) (), u16 ClassID);
120
121 };
122
123
124
125 /*****************************************************************************/
126 /* class Stream */
127 /*****************************************************************************/
128
129
130
131 class Stream : public Streamable {
132
133 public:
134 typedef void (*ErrorFunc) (Stream&);
135 // Type of function that is called from Error
136
137 private:
138 // Bit writing stuff
139 unsigned BitCount;
140 unsigned char BitBuf;
141
142 protected:
143 int Status;
144 int ErrorInfo;
145
146 ErrorFunc StreamError;
147 // Function that is called in case of errors
148
149 virtual void Error (int Code, int Info);
150 // Sets the error and status codes and calls StreamError
151
152 public:
153 Stream ();
154
155 void BitWrite (u32 Bits, unsigned Count);
156 // Write the given bits to the stream, LSB first. Count may not be
157 // greater than 32.
158
159 u32 BitRead (unsigned Count);
160 // Read some bits from the stream. Count may not be greater than 32.
161
162 void BitFlush ();
163 // Flush the bit write buffer if needed.
164
165 void BitReset ();
166 // Reset the bit buffer to empty without writing anything. This is needed
167 // to reset to a byte boundary between two BitReads.
168
169 void CopyFrom (Stream& S, size_t Count);
170 // Copy data from another stream
171
172 virtual void Flush ();
173 // Flush associated buffers
174
175 Streamable* Get ();
176 // Read an object instance from the stream
177
178 virtual u32 GetPos ();
179 // Get the current value of the stream pointer
180
181 virtual u32 GetSize ();
182 // Return the size of the stream in bytes
183
184 int GetStatus () const;
185 // Return the stream status
186
187 int GetErrorInfo () const;
188 // Return the error information
189
190 void Put (const Streamable&);
191 void Put (const Streamable*);
192 // Write an object instance into a stream
193
194 virtual void Read (void* Buf, size_t Count);
195 // Read from the stream
196
197 virtual int Read ();
198 // Read a character from the stream, return EOF in case of errors or
199 // end of stream (need Reset to continue)
200
201 char* ReadStr ();
202 // Read a char* from the stream
203
204 virtual void Reset ();
205 // Reset the error codes
206
207 virtual void Seek (unsigned long Pos);
208 // Set the stream pointer to the specified position
209
210 virtual void SeekToEnd ();
211 // Set the stream pointer to the end of the stream
212
213 ErrorFunc SetErrorFunc (ErrorFunc);
214 // Set a new StreamError function
215
216 virtual void Truncate ();
217 // Truncate the stream at the current position
218
219 virtual void Write (const void* Buf, size_t Count);
220 // Write to the stream
221
222 void WriteStr (const char* S);
223 // Write a char* to the stream
224
225 friend inline Stream& operator << (Stream&, Stream& (*F) (Stream&));
226 // Manipulator support function
227
228 friend Stream& operator << (Stream&, char);
229 friend Stream& operator << (Stream&, unsigned char);
230 friend Stream& operator << (Stream&, signed char);
231 friend Stream& operator << (Stream&, i16);
232 friend Stream& operator << (Stream&, u16);
233 friend Stream& operator << (Stream&, i32);
234 friend Stream& operator << (Stream&, u32);
235 friend Stream& operator << (Stream&, float);
236 friend Stream& operator << (Stream&, double);
237 friend Stream& operator << (Stream&, char*);
238
239 friend Stream& operator >> (Stream&, char&);
240 friend Stream& operator >> (Stream&, unsigned char&);
241 friend Stream& operator >> (Stream&, signed char&);
242 friend Stream& operator >> (Stream&, i16&);
243 friend Stream& operator >> (Stream&, u16&);
244 friend Stream& operator >> (Stream&, i32&);
245 friend Stream& operator >> (Stream&, u32&);
246 friend Stream& operator >> (Stream&, float&);
247 friend Stream& operator >> (Stream&, double&);
248 friend Stream& operator >> (Stream&, char*);
249 };
250
251
252
BitReset()253 inline void Stream::BitReset ()
254 // Reset the bit buffer to empty without writing anything. This is needed
255 // to reset to a byte boundary between two BitReads.
256 {
257 BitCount = 0;
258 BitBuf = 0;
259 }
260
261
262
GetStatus()263 inline int Stream::GetStatus () const
264 {
265 return Status;
266 }
267
268
269
GetErrorInfo()270 inline int Stream::GetErrorInfo () const
271 {
272 return ErrorInfo;
273 }
274
275
276
Put(const Streamable * X)277 inline void Stream::Put (const Streamable* X)
278 {
279 Put (*X);
280 }
281
282
283
284 inline Stream& operator << (Stream& S, Stream& (*Func) (Stream&))
285 // Manipulator support function
286 {
287 return Func (S);
288 }
289
290
291
292 /*****************************************************************************/
293 /* Manipulators for class Stream */
294 /*****************************************************************************/
295
296
297
298 Stream& Flush (Stream& S);
299 // Flush the stream
300
301
302
303 /*****************************************************************************/
304 /* class FileStream */
305 /*****************************************************************************/
306
307
308
309 class FileStream : public Stream {
310
311 private:
312 void SetBuf (size_t BufSize);
313 // Set the file buffer. For use in the constructors only!
314
315
316 protected:
317 FILE *F;
318 String Name;
319
320
321 public:
322 FileStream (const String& FileName, size_t BufSize = BUFSIZ);
323 // Open a file stream. If the file with the given name does not exist,
324 // it is created.
325
326 FileStream (const String& FileName, const String& Mode, size_t BufSize = BUFSIZ);
327 // Open a file stream using the given mode.
328
329 FileStream (size_t BufSize = BUFSIZ);
330 // Create a temporary file stream. The corresponding file will be deleted
331 // when the stream is closed. You cannot get the name of a temporary file.
332
333 virtual ~FileStream ();
334
335 virtual void Flush ();
336 // Flush associated buffers
337
338 virtual u32 GetPos ();
339 // Get the current value of the stream pointer
340
341 virtual u32 GetSize ();
342 // Return the size of the stream in bytes
343
344 virtual void Read (void *Buf, size_t Count);
345 // Read from the stream
346
347 virtual int Read ();
348 // Read a byte from the stream. Returns EOF if end of file is reached.
349 // EOF is no error condition (as in all other reads)
350
351 virtual void Reset ();
352
353 virtual void Seek (unsigned long Pos);
354 // Set the stream pointer to the given value
355
356 void SeekToEnd ();
357 // Set the stream pointer to the end of the stream
358
359 virtual void Truncate ();
360 // Truncate the stream at the current stream pointer position
361
362 virtual void Write (const void *Buf, size_t Count);
363 // Write data to the stream
364
365 const String& GetName () const;
366 // Return the filename
367
368 const FILE* GetFile () const;
369 // Return the file variable
370
371 };
372
373
374
GetName()375 inline const String& FileStream::GetName () const
376 {
377 return Name;
378 }
379
380
381
GetFile()382 inline const FILE* FileStream::GetFile () const
383 {
384 return F;
385 }
386
387
388
389 // End of STREAM.H
390
391 #endif
392
393