/[pearpc]/src/tools/stream.h
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /src/tools/stream.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Wed Sep 5 17:11:21 2007 UTC (11 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 10715 byte(s)
import upstream CVS
1 dpavlin 1 /*
2     * HT Editor
3     * stream.h
4     *
5     * Copyright (C) 1999-2002 Stefan Weyergraf
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License version 2 as
9     * published by the Free Software Foundation.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19     */
20    
21     #ifndef __STREAM_H__
22     #define __STREAM_H__
23    
24     #include <stdarg.h>
25     #include <stdio.h>
26    
27     #include "data.h"
28     #include "str.h"
29     #include "system/file.h"
30    
31     class String;
32    
33     /* Stream access mode */
34     enum IOAccessModeAtomic {
35     IOAM_NULL = 0,
36     IOAM_READ = 1,
37     IOAM_WRITE = 2
38     };
39    
40     typedef uint IOAccessMode;
41    
42     /**
43     * A stream.
44     */
45     class Stream {
46     private:
47     IOAccessMode mAccessMode;
48     // Container *mListeners;
49     protected:
50     void checkAccess(IOAccessMode mask);
51     // void notifyListeners(StreamEvent event,...);
52     public:
53     Stream();
54     virtual ~Stream();
55     /* new */
56     // void addEventListener(StreamEventListener *l, StreamEvent mask);
57     virtual uint copyAllTo(Stream *stream);
58     virtual uint copyTo(Stream *stream, uint count);
59     virtual IOAccessMode getAccessMode() const;
60     virtual String & getDesc(String &result) const;
61     virtual uint read(void *buf, uint size);
62     void readx(void *buf, uint size);
63     // void removeEventListener(StreamEventListener *l);
64     virtual int setAccessMode(IOAccessMode mode);
65     virtual uint write(const void *buf, uint size);
66     void writex(const void *buf, uint size);
67     };
68    
69     /**
70     * A stream, layering a stream.
71     */
72     class StreamLayer: public Stream {
73     protected:
74     Stream *mStream;
75     bool mOwnStream;
76    
77     public:
78    
79     StreamLayer(Stream *stream, bool own_stream);
80     virtual ~StreamLayer();
81     /* extends Stream */
82     virtual IOAccessMode getAccessMode() const;
83     virtual String & getDesc(String &result) const;
84     virtual uint read(void *buf, uint size);
85     virtual int setAccessMode(IOAccessMode mode);
86     virtual uint write(const void *buf, uint size);
87     /* new */
88     Stream * getLayered() const;
89     void setLayered(Stream *newLayered, bool ownNewLayered);
90     };
91    
92     #define OS_FMT_DEC 0
93     #define OS_FMT_HEX 1
94    
95     /* cntl cmd */
96     #define FCNTL_MODS_INVD 0x00000001
97     #define FCNTL_MODS_FLUSH 0x00000002
98     #define FCNTL_MODS_IS_DIRTY 0x00000003 // const FileOfs &offset, const FileOfs &range, bool &isdirty
99     //#define FCNTL_MODS_CLEAR_DIRTY 0x00000004
100     //#define FCNTL_MODS_CLEAR_DIRTY_RANGE 0x00000005 // const FileOfs &offset, const FileOfs &range
101     #define FCNTL_FLUSH_STAT 0x00000006
102     #define FCNTL_GET_RELOC 0x00000007 // bool &enabled
103     #define FCNTL_SET_RELOC 0x00000008 // bool enable
104     #define FCNTL_GET_FD 0x00000009 // int &fd
105    
106     // Return a "modification count" that changes everytime the file state ( content, size, pstat )
107     // changes.
108     // While identical mod-counts imply identical file states,
109     // different mod-counts do not necessarily imply different file states !
110     #define FCNTL_GET_MOD_COUNT 0x0000000a // int &mcount
111    
112     #define IS_DIRTY_SINGLEBIT 0x80000000
113    
114     /* File open mode */
115     enum FileOpenMode {
116     FOM_EXISTS,
117     FOM_CREATE,
118     FOM_APPEND
119     };
120    
121     /**
122     * A file.
123     */
124     class File: public Stream {
125     protected:
126     int mcount;
127     public:
128     File();
129     /* new */
130     int cntl(uint cmd, ...);
131     virtual void del(uint size);
132     virtual void extend(FileOfs newsize);
133     virtual String & getFilename(String &result) const;
134     virtual FileOfs getSize() const;
135     virtual void insert(const void *buf, uint size);
136     virtual void pstat(pstat_t &s) const;
137     virtual void seek(FileOfs offset);
138     virtual FileOfs tell() const;
139     virtual void truncate(FileOfs newsize);
140     virtual int vcntl(uint cmd, va_list vargs);
141     };
142    
143     /**
144     * A file, layering a file.
145     */
146     class FileLayer: public File {
147     protected:
148     File *mFile;
149     bool mOwnFile;
150     public:
151     FileLayer(File *file, bool own_file);
152     virtual ~FileLayer();
153     /* extends File */
154     virtual void del(uint size);
155     virtual void extend(FileOfs newsize);
156     virtual IOAccessMode getAccessMode() const;
157     virtual String & getDesc(String &result) const;
158     virtual String & getFilename(String &result) const;
159     virtual FileOfs getSize() const;
160     virtual void insert(const void *buf, uint size);
161     virtual void pstat(pstat_t &s) const;
162     virtual uint read(void *buf, uint size);
163     virtual void seek(FileOfs offset);
164     virtual int setAccessMode(IOAccessMode mode);
165     virtual FileOfs tell() const;
166     virtual void truncate(FileOfs newsize);
167     virtual int vcntl(uint cmd, va_list vargs);
168     virtual uint write(const void *buf, uint size);
169     /* new */
170     File * getLayered() const;
171     void setLayered(File *newLayered, bool ownNewLayered);
172     };
173    
174     /**
175     * A local file (file descriptor [fd]).
176     */
177     class LocalFileFD: public File {
178     protected:
179     String mFilename;
180     FileOpenMode mOpenMode;
181    
182     int fd;
183     bool own_fd;
184    
185     FileOfs offset;
186    
187     int setAccessModeInternal(IOAccessMode mode);
188     public:
189    
190     LocalFileFD(const String &aFilename, IOAccessMode mode, FileOpenMode aOpenMode);
191     LocalFileFD(int fd, bool own_fd, IOAccessMode mode);
192     virtual ~LocalFileFD();
193     /* extends File */
194     virtual String & getDesc(String &result) const;
195     virtual String & getFilename(String &result) const;
196     virtual FileOfs getSize() const;
197     virtual uint read(void *buf, uint size);
198     virtual void seek(FileOfs offset);
199     virtual int setAccessMode(IOAccessMode mode);
200     virtual FileOfs tell() const;
201     virtual void truncate(FileOfs newsize);
202     virtual int vcntl(uint cmd, va_list vargs);
203     virtual uint write(const void *buf, uint size);
204     };
205    
206     /**
207     * A local file (file stream [FILE*]).
208     */
209     class LocalFile: public File {
210     protected:
211     String mFilename;
212     FileOpenMode mOpenMode;
213    
214     FILE * file;
215     bool own_file;
216    
217     FileOfs offset;
218    
219     int setAccessModeInternal(IOAccessMode mode);
220     public:
221     LocalFile(const String &aFilename, IOAccessMode mode=IOAM_READ, FileOpenMode aOpenMode=FOM_EXISTS);
222     LocalFile(FILE *file, bool own_file, IOAccessMode mode);
223     virtual ~LocalFile();
224     /* extends File */
225     virtual String & getDesc(String &result) const;
226     virtual String & getFilename(String &result) const;
227     virtual FileOfs getSize() const;
228     virtual void pstat(pstat_t &s) const;
229     virtual uint read(void *buf, uint size);
230     virtual void seek(FileOfs offset);
231     virtual int setAccessMode(IOAccessMode mode);
232     virtual FileOfs tell() const;
233     virtual void truncate(FileOfs newsize);
234     virtual int vcntl(uint cmd, va_list vargs);
235     virtual uint write(const void *buf, uint size);
236     };
237    
238     /**
239     * A temporary file.
240     */
241     class TempFile: public LocalFile {
242     public:
243     TempFile(IOAccessMode mode);
244     /* extends File */
245     virtual String & getDesc(String &result) const;
246     virtual void pstat(pstat_t &s) const;
247     };
248    
249     /**
250     * A fixed-size, read-only file, mapping a area of memory.
251     */
252     class ConstMemMapFile: public File {
253     protected:
254     FileOfs pos;
255     uint size;
256     const void *buf;
257     public:
258     ConstMemMapFile(const void *buf, uint size);
259     /* extends File */
260     virtual String & getDesc(String &result) const;
261     virtual FileOfs getSize() const;
262     virtual uint read(void *buf, uint size);
263     virtual void seek(FileOfs offset);
264     virtual FileOfs tell() const;
265     };
266    
267     /**
268     * A fixed-size file, mapping a area of memory.
269     */
270     class MemMapFile: public ConstMemMapFile {
271     public:
272     MemMapFile(void *buf, uint size);
273     /* extends Stream */
274     virtual uint write(const void *buf, uint size);
275     };
276    
277     /**
278     * A file layer, representing a cropped version of a file
279     */
280     class CroppedFile: public FileLayer {
281     protected:
282     FileOfs mCropStart;
283     bool mHasCropSize;
284     FileOfs mCropSize;
285     public:
286     // crop [start; start+size-1]
287     CroppedFile(File *file, bool own_file, FileOfs aCropStart, FileOfs aCropSize);
288     // no size, just start
289     CroppedFile(File *file, bool own_file, FileOfs aCropStart);
290     /* extends FileLayer */
291     virtual void extend(FileOfs newsize);
292     virtual String & getDesc(String &result) const;
293     virtual FileOfs getSize() const;
294     virtual void pstat(pstat_t &s) const;
295     virtual uint read(void *buf, uint size);
296     virtual void seek(FileOfs offset);
297     virtual FileOfs tell() const;
298     virtual void truncate(FileOfs newsize);
299     virtual uint write(const void *buf, uint size);
300     };
301    
302     /**
303     * A (read-only) file with zero-content.
304     */
305     class NullFile: public File {
306     public:
307     NullFile();
308     /* extends File */
309     virtual void extend(FileOfs newsize);
310     virtual String & getDesc(String &result) const;
311     virtual FileOfs getSize() const;
312     virtual void pstat(pstat_t &s) const;
313     virtual uint read(void *buf, uint size);
314     virtual void seek(FileOfs offset);
315     virtual int setAccessMode(IOAccessMode mode);
316     virtual FileOfs tell() const;
317     virtual void truncate(FileOfs newsize);
318     virtual uint write(const void *buf, uint size);
319     };
320    
321     /**
322     * A file, existing only in memory.
323     */
324     class MemoryFile: public File {
325     protected:
326     FileOfs ofs;
327     FileOfs pos;
328     uint bufsize, dsize, ibufsize;
329     byte *buf;
330    
331     virtual uint extendBufSize(uint bufsize);
332     virtual uint shrinkBufSize(uint bufsize);
333     void extendBuf();
334     void shrinkBuf();
335     void resizeBuf(uint newsize);
336     public:
337     MemoryFile(FileOfs ofs = 0, uint size = 0, IOAccessMode mode = IOAM_READ | IOAM_WRITE);
338     virtual ~MemoryFile();
339     /* extends File */
340     virtual void extend(FileOfs newsize);
341     virtual IOAccessMode getAccessMode() const;
342     virtual String & getDesc(String &result) const;
343     virtual FileOfs getSize() const;
344     virtual void pstat(pstat_t &s) const;
345     virtual uint read(void *buf, uint size);
346     virtual void seek(FileOfs offset);
347     virtual int setAccessMode(IOAccessMode mode);
348     virtual FileOfs tell() const;
349     virtual void truncate(FileOfs newsize);
350     virtual uint write(const void *buf, uint size);
351     /* new */
352     byte * getBufPtr() const;
353     };
354    
355     void fileMove(File *file, FileOfs src, FileOfs dest, FileOfs size);
356    
357     /** read string from file (zero-terminated, 8-bit chars) */
358     char *fgetstrz(File *file);
359     /** read string from stream (zero-terminated, 8-bit chars) */
360     char *getstrz(Stream *stream);
361     /** write string into stream (zero-terminated, 8-bit chars) */
362     void putstrz(Stream *stream, const char *str);
363    
364     /** read string from stream (8-bit length followed by content aka. Pascal-style) */
365     char *getstrp(Stream *stream);
366     /** write string into stream (8-bit length followed by content aka. Pascal-style) */
367     void putstrp(Stream *stream, const char *str);
368    
369     /** read string from stream (zero-terminated, 16-bit chars) */
370     char *getstrw(Stream *stream);
371     /** write string into stream (zero-terminated, 16-bit chars) */
372     void putstrw(Stream *stream, const char *str);
373    
374     #endif /* __STREAM_H__ */

  ViewVC Help
Powered by ViewVC 1.1.26