Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

stream.h

Go to the documentation of this file.
00001 /* 
00002  *      HT Editor
00003  *      stream.h
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifndef __STREAM_H__
00022 #define __STREAM_H__
00023 
00024 #include "common.h"
00025 #include "global.h"
00026 #include "htio.h"
00027 
00028 #include <stdarg.h>
00029 #include <stdio.h>
00030 
00031 #ifndef HAVE_FILEOFS
00032 #define HAVE_FILEOFS
00033 typedef UINT FILEOFS;
00034 #endif
00035 
00036 /*
00037  *      CLASS ht_stream
00038  */
00039 
00040 /* stream_error_func return values */
00041 #define SERR_FAIL               0
00042 #define SERR_RETRY              1
00043 
00044 /* ht_stream.error */
00045 #define STERR_SYSTEM            0x80000000
00046 
00047 class ht_stream;
00048 
00049 typedef int (*stream_error_func_ptr)(ht_stream *stream);
00050 
00051 class ht_stream {
00052 protected:
00053         stream_error_func_ptr stream_error_func;
00054         int     error;
00055         UINT    access_mode;
00056 
00057                    int          call_error_func();
00058 public:
00059         virtual                         ~ht_stream();
00060 
00061                    void         init();
00062         virtual void            done();
00063 /* new */
00064         virtual void            copy_to(ht_stream *stream);
00065         virtual UINT            get_access_mode();
00066         virtual int             get_error();
00067         virtual const char *get_desc();
00068         virtual UINT            read(void *buf, UINT size);
00069         virtual bool            set_access_mode(UINT access_mode);
00070         virtual void            set_error(int error);
00071         virtual void            set_error_func(stream_error_func_ptr stream_error_func);
00072         virtual UINT            write(const void *buf, UINT size);
00073 };
00074 
00075 /*
00076  *      CLASS ht_layer_stream
00077  */
00078 
00079 class ht_layer_stream: public ht_stream {
00080 protected:
00081         ht_stream *stream;
00082         bool own_stream;
00083         
00084 public:
00085         
00086                    void         init(ht_stream *stream, bool own_stream);
00087         virtual void            done();
00088 /* overwritten */
00089         virtual void            copy_to(ht_stream *stream);
00090         virtual UINT            get_access_mode();
00091         virtual int             get_error();
00092         virtual const char *get_desc();
00093         virtual UINT            read(void *buf, UINT size);
00094         virtual bool            set_access_mode(UINT access_mode);
00095         virtual void            set_error(int error);
00096         virtual void            set_error_func(stream_error_func_ptr stream_error_func);
00097                    void         set_stream_ownership(bool own);
00098         virtual UINT            write(const void *buf, UINT size);
00099 };
00100 
00101 /*
00102  *      CLASS ht_object_stream
00103  */
00104 
00105 class ht_object_stream: public ht_layer_stream {
00106 public:
00107                         void            init(ht_stream *s);
00108         virtual void            done();
00109         
00110 /* new */
00111         virtual void *  getBinary(int size, char *desc) = 0;
00112         virtual void            getBinary(void *p, int size, char *desc) = 0;
00113         virtual bool            getBool(char *desc) = 0;
00114                         int             getInt(int size, char *desc);
00115         virtual int             getIntDec(int size, char *desc) = 0;
00116         virtual int             getIntHex(int size, char *desc) = 0;
00117         virtual qword   getQWordDec(int size, char *desc) = 0;
00118         virtual qword   getQWordHex(int size, char *desc) = 0;
00119         virtual Object *        getObject(char *name) = 0;
00120         virtual void            getObject(Object *&o, char *name) = 0;
00121         virtual void            getSeparator() = 0;
00122         virtual char *  getString(char *desc) = 0;
00123         virtual UINT            recordStart(UINT size);
00124         virtual void            recordEnd(UINT);
00125         virtual void            putBinary(void *mem, int size, char *desc) = 0;
00126         virtual void            putBool(bool b, char *desc) = 0;
00127         virtual void            putInfo(char *info) = 0;
00128                         void            putInt(int a, int size, char *desc);
00129         virtual void            putIntDec(int a, int size, char *desc) = 0;
00130         virtual void            putIntHex(int a, int size, char *desc) = 0;
00131         virtual void            putQWordDec(qword a, int size, char *desc) = 0;
00132         virtual void            putQWordHex(qword a, int size, char *desc) = 0;
00133         virtual void            putObject(Object *obj, char *name) = 0;
00134         virtual void            putSeparator() = 0;
00135         virtual void            putString(char *string, char *desc) = 0;
00136 };
00137 
00138 #define PUT_BINARY(st, d, size) st->putBinary(d, size, #d)
00139 #define PUT_BOOL(st, d) st->putBool(d, #d)
00140 #define PUT_INT(st, d) st->putInt(d, 4, #d)
00141 #define PUT_INT_DEC(st, d) st->putIntDec(d, 4, #d)
00142 #define PUT_INT_HEX(st, d) st->putIntHex(d, 4, #d)
00143 #define PUT_QWORD_DEC(st, d) st->putQWordDec(d, 8, #d)
00144 #define PUT_QWORD_HEX(st, d) st->putQWordHex(d, 8, #d)
00145 #define PUT_OBJECT(st, o) st->putObject(o, #o)
00146 #define PUT_STRING(st, d) st->putString(d, #d)
00147 
00148 #define GET_BOOL(st, d) d=st->getBool(#d)
00149 #define GET_INT(st, d) d=st->getInt(4, #d)
00150 #define GET_INT_DEC(st, d) d=st->getIntDec(4, #d)
00151 #define GET_INT_HEX(st, d) d=st->getIntHex(4, #d)
00152 #define GET_QWORD_DEC(st, d) d=st->getQWordDec(8, #d)
00153 #define GET_QWORD_HEX(st, d) d=st->getQwordHex(8, #d)
00154 #define GET_OBJECT(st, o) st->getObject(*(Object**)&o, #o)
00155 #define GET_STRING(st, d) d=st->getString(#d)
00156 
00157 /*
00158  *      CLASS ht_streamfile
00159  */
00160 
00161 /* cntl cmd */
00162 #define FCNTL_MODS_INVD                         0x00000001
00163 #define FCNTL_MODS_FLUSH                                0x00000002
00164 #define FCNTL_MODS_IS_DIRTY                     0x00000003      // FILEOFS offset, UINT range, bool *isdirty
00165 #define FCNTL_MODS_CLEAR_DIRTY          0x00000004
00166 #define FCNTL_MODS_CLEAR_DIRTY_RANGE    0x00000005      // FILEOFS offset, UINT range
00167 #define FCNTL_FLUSH_STAT                                0x00000006
00168 #define FCNTL_GET_RELOC                         0x00000007      // bool *enabled
00169 #define FCNTL_SET_RELOC                         0x00000008      // bool enable
00170 
00171 #define IS_DIRTY_SINGLEBIT                      0x80000000
00172 
00173 /* ht_file.access_mode */
00174 #define FAM_NULL                0
00175 #define FAM_READ                1
00176 #define FAM_WRITE               2
00177 
00178 /* ht_file open mode */
00179 #define FOM_EXISTS              0
00180 #define FOM_CREATE              1
00181 #define FOM_APPEND              2
00182 
00183 class ht_streamfile: public ht_stream {
00184 public:
00185 /* new */
00186                    int                  cntl(UINT cmd, ...);
00187         virtual int                     extend(UINT newsize);
00188         virtual const char *    get_filename();
00189         virtual ht_streamfile * get_layered();
00190         virtual UINT                    get_size();
00191         virtual void                    pstat(pstat_t *s);
00192         virtual int                     seek(FILEOFS offset);
00193         virtual void                    set_layered(ht_streamfile *streamfile);
00194         virtual FILEOFS         tell();
00195         virtual int                     truncate(UINT newsize);
00196         virtual int                     vcntl(UINT cmd, va_list vargs);
00197 };
00198 
00199 /*
00200  *      CLASS ht_layer_streamfile
00201  */
00202 
00203 class ht_layer_streamfile: public ht_streamfile {
00204 protected:
00205         ht_streamfile *streamfile;
00206         bool own_streamfile;
00207 public:
00208                    void init(ht_streamfile *streamfile, bool own_streamfile);
00209         virtual void    done();
00210 /* overwritten */
00211         virtual void                    copy_to(ht_stream *stream);
00212         virtual int                     extend(UINT newsize);
00213         virtual UINT                    get_access_mode();
00214         virtual int                     get_error();
00215         virtual const char *    get_desc();
00216         virtual const char *    get_filename();
00217         virtual ht_streamfile * get_layered();
00218         virtual UINT                    get_size();
00219         virtual void                    pstat(pstat_t *s);
00220         virtual UINT                    read(void *buf, UINT size);
00221         virtual int                     seek(FILEOFS offset);
00222         virtual bool                    set_access_mode(UINT access_mode);
00223         virtual void                    set_error(int error);
00224         virtual void                    set_error_func(stream_error_func_ptr stream_error_func);
00225         virtual void                    set_layered(ht_streamfile *streamfile);
00226                    void                 set_streamfile_ownership(bool own);
00227         virtual FILEOFS         tell();
00228         virtual int                     truncate(UINT newsize);
00229         virtual int                     vcntl(UINT cmd, va_list vargs);
00230         virtual UINT                    write(const void *buf, UINT size);
00231 };
00232 
00233 /*
00234  *      CLASS ht_sys_file
00235  */
00236 
00237 class ht_sys_file: public ht_streamfile {
00238 protected:
00239         int fd;
00240         bool own_fd;
00241 
00242         FILEOFS offset;
00243 public:
00244 
00245                    void         init(int fd, bool own_fd, UINT access_mode);
00246         virtual void            done();
00247 /* overwritten */
00248         virtual int             extend(UINT newsize);
00249         virtual const char *get_desc();
00250         virtual UINT            get_size();
00251         virtual UINT            read(void *buf, UINT size);
00252         virtual int             seek(FILEOFS offset);
00253         virtual FILEOFS         tell();
00254         virtual UINT            write(const void *buf, UINT size);
00255 };
00256 
00257 /*
00258  *      CLASS ht_stdio_file
00259  */
00260 
00261 class ht_stdio_file: public ht_streamfile {
00262 protected:
00263         FILE *file;
00264         bool own_file;
00265 
00266         FILEOFS offset;
00267 public:
00268 
00269                    void         init(FILE *file, bool own_file, UINT access_mode);
00270         virtual void            done();
00271 /* overwritten */
00272         virtual int             extend(UINT newsize);
00273         virtual const char *get_desc();
00274         virtual UINT            get_size();
00275         virtual UINT            read(void *buf, UINT size);
00276         virtual int             seek(FILEOFS offset);
00277         virtual FILEOFS         tell();
00278         virtual UINT            write(const void *buf, UINT size);
00279 };
00280 
00281 /*
00282  *      CLASS ht_file
00283  */
00284 
00285 class ht_file: public ht_stdio_file {
00286 protected:
00287         char *filename;
00288         UINT open_mode;
00289 
00290         FILEOFS offset;
00291 
00292         bool    set_access_mode_internal(UINT access_mode);
00293 public:
00294 
00295                    void         init(const char *filename, UINT access_mode, UINT open_mode);
00296         virtual void            done();
00297 /* overwritten */
00298         virtual const char *get_desc();
00299         virtual const char *get_filename();
00300         virtual void            pstat(pstat_t *s);
00301         virtual bool            set_access_mode(UINT access_mode);
00302         virtual int             truncate(UINT newsize);
00303         virtual int             vcntl(UINT cmd, va_list vargs);
00304 };
00305 
00306 /*
00307  *      CLASS ht_temp_file
00308  */
00309 
00310 class ht_temp_file: public ht_stdio_file {
00311 public:
00312                    void         init(UINT access_mode);
00313         virtual void            done();
00314 /* overwritten */
00315         virtual const char *get_desc();
00316         virtual void            pstat(pstat_t *s);
00317 };
00318 
00319 /*
00320  *      CLASS ht_memmap_file
00321  */
00322 
00323 class ht_memmap_file: public ht_streamfile {
00324 protected:
00325         FILEOFS pos;
00326         UINT size;
00327         byte *buf;
00328 
00329 public:
00330                    void         init(byte *buf, UINT size);
00331         virtual void            done();
00332 /* overwritten */
00333         virtual const char *get_desc();
00334         virtual UINT            get_size();
00335         virtual UINT            read(void *buf, UINT size);
00336         virtual int             seek(FILEOFS offset);
00337         virtual FILEOFS         tell();
00338         virtual UINT            write(const void *buf, UINT size);
00339 };
00340 
00341 /*
00342  *      CLASS ht_null_file
00343  */
00344 
00345 class ht_null_file: public ht_streamfile {
00346 protected:
00347 public:
00348                    void         init();
00349         virtual void            done();
00350 /* overwritten */
00351         virtual int             extend(UINT newsize);
00352         virtual UINT            get_access_mode();
00353         virtual const char *get_desc();
00354         virtual UINT            get_size();
00355         virtual void            pstat(pstat_t *s);
00356         virtual UINT            read(void *buf, UINT size);
00357         virtual int             seek(FILEOFS offset);
00358         virtual bool            set_access_mode(UINT access_mode);
00359         virtual FILEOFS         tell();
00360         virtual int             truncate(UINT newsize);
00361         virtual UINT            write(const void *buf, UINT size);
00362 };
00363 
00364 /*
00365  *      CLASS ht_mem_file
00366  */
00367 
00368 class ht_mem_file: public ht_streamfile {
00369 protected:
00370         FILEOFS ofs;
00371         FILEOFS pos;
00372         UINT bufsize, dsize, ibufsize;
00373         byte *buf;
00374 
00375         virtual UINT            extendbufsize(UINT bufsize);
00376         virtual UINT            shrinkbufsize(UINT bufsize);
00377                    void         extendbuf();
00378                    void         shrinkbuf();
00379                    void         resizebuf(UINT newsize);
00380 public:
00381                    void         init();
00382                    void         init(FILEOFS ofs, UINT size, UINT access_mode);
00383         virtual void            done();
00384 /* overwritten */
00385         virtual int             extend(UINT newsize);
00386         virtual UINT            get_access_mode();
00387         virtual const char *get_desc();
00388         virtual UINT            get_size();
00389         virtual void            pstat(pstat_t *s);
00390         virtual UINT            read(void *buf, UINT size);
00391         virtual int             seek(FILEOFS offset);
00392         virtual bool            set_access_mode(UINT access_mode);
00393         virtual FILEOFS         tell();
00394         virtual int             truncate(UINT newsize);
00395         virtual UINT            write(const void *buf, UINT size);
00396 /* new */
00397                    void*                bufptr();
00398 };
00399 
00400 /*
00401  *      string stream functions
00402  */
00403 
00404 /* 0-terminated ASCII */
00405 char *fgetstrz(ht_streamfile *file);
00406 char *getstrz(ht_stream *stream);
00407 void putstrz(ht_stream *stream, const char *str);
00408 
00409 /* pascal-style ASCII */
00410 char *getstrp(ht_stream *stream);
00411 void putstrp(ht_stream *stream, const char *str);
00412 
00413 /* 0-terminated wide/unicode */
00414 char *getstrw(ht_stream *stream);
00415 void putstrw(ht_stream *stream, const char *str);
00416 
00417 char *ht_strerror(int error);
00418 
00419 #endif /* __STREAM_H__ */
00420 

Generated on Fri May 7 21:15:44 2004 by doxygen 1.3.5