/* librock/stream.h - - - - - - - - - - - - Copyright Notice - - - - - - - - - - - - Copyright 1998 Forrest J. Cavalier III See http://www.mibsoftware.com/librock/ for documentation and original copies of this software. This software shall be distributed with the implementation source code according to the license terms therein. */ /* stream.h Summary/Purpose: classes and derivatives for a read/write source/sink. */ #ifndef librock_INC_stream_h #define librock_INC_stream_h #include #ifdef librock_EXPERIMENTAL_IStream_x001 class librock_IStream { public: librock_IStream(void) {}; virtual unsigned read(char librock_PTR *dest,unsigned max) = 0; virtual char *aReadLine(char librock_PTR * librock_PTR *dest); virtual const char *pLine(char librock_PTR * librock_PTR *ppasz,long *pcb); virtual unsigned write(const char librock_PTR *src, unsigned max) = 0; unsigned writesz(const char librock_PTR *src); virtual void flush(void); virtual long copy_out(librock_IStream librock_PTR*dest,long cbToCopy); /* Default version exists. */ virtual long seek(long pos,int mode) = 0; virtual long length(); virtual long tell(); }; class librock_IMemoryStream : public librock_IStream { public: virtual long GetSeg(long offset,char librock_PTR * librock_PTR *p, int cbToStore) = 0; virtual char *GetCont(long offset, int cb) = 0; virtual void WrittenTo(long offset) = 0; }; /***************************************************************/ class librock_IWStream : public librock_IStream { /* write-only stream. Not positionable. */ public: /* Primitives. Lock out Read and Seek */ private: virtual unsigned read(char librock_PTR *dest,unsigned max) { return 0;}; virtual long seek(long pos,int mode) { return -1; } }; /***************************************************************/ class librock_IRStream : public librock_IStream { /* read-only stream. Not positionable. */ public: private: /* Primitives. Lock out Write and Seek */ virtual unsigned Write(char librock_PTR *src,unsigned max) { return 0;}; virtual long Seek(long pos,int mode) { return -1; } }; #ifdef librock_FILE /***************************************************************/ class librock_CStream_FILE : public librock_IStream { /* readable, positionable stream: a file */ private: librock_CStream_FILE() { /* Don't call this one */ m_f = 0; }; public: librock_CStream_FILE(librock_FILE *f) { m_f = f; }; librock_FILE *attach(librock_FILE *f) { /* 3/6/2000 */ librock_FILE *ret_f = f; m_f = f; return ret_f; }; ~librock_CStream_FILE() { }; protected: librock_FILE *m_f; public: /* Primitive Implementation */ virtual unsigned read(char librock_PTR *dest, unsigned max); virtual long seek(long pos,int mode); virtual unsigned write(const char librock_PTR *src, unsigned max); /* advanced overrides. Normally the base class implements using the primitives */ public: virtual void flush(void); virtual long tell(); }; class librock_CStream_FILE_section : public librock_IStream { /* 3/28/2000 */ /* readable, positionable stream: a file */ private: librock_CStream_FILE_section() { /* Don't call this one */ m_f = 0; }; public: librock_CStream_FILE_section(librock_FILE *f,long pos,long cbSection); librock_FILE *attach(librock_FILE *f,long pos,long cbSection); ~librock_CStream_FILE_section() { }; protected: librock_FILE *m_f; long m_base; long m_cbSection; public: /* Primitive Implementation */ virtual unsigned read(char librock_PTR *dest, unsigned max); virtual long seek(long pos,int mode); virtual unsigned write(const char librock_PTR *src, unsigned max); /* advanced overrides. Normally the base class implements using the primitives */ public: virtual void flush(void); virtual long tell(); }; #endif class librock_CStreamPRCL : public librock_IMemoryStream { /* Implemented in sprcl.cpp */ public: librock_CStreamPRCL() { m_pfirst = 0; m_pos = 0; }; ~librock_CStreamPRCL() { }; protected: struct PRCL_s librock_PTR *m_pfirst; long m_pos; struct PRCL_s *hgraft(long size); public: /* IRWPStream Primitive Implementation */ virtual long Seek(long dist,int mode); virtual long GetSeg(long offset,char librock_PTR * librock_PTR *p, int cbToStore); virtual const char *pLine(char librock_PTR * librock_PTR *ppasz,long *pcb); virtual char *GetCont(long offset, int cbToStore); virtual void WrittenTo(long offset); /************************/ /* advanced overrides. Normally the base class implements using the primitives. Override for efficiency or implematation reasons */ private: virtual int readln(char librock_PTR *dest, int max) { /* Not implemented */ ;return 0;}; public: virtual unsigned Read(char librock_PTR *dest, unsigned max); virtual unsigned Write(const char librock_PTR *src, unsigned max); virtual long length(); long CopyTo(librock_IStream librock_PTR*dest,long max); public: /* Expose PRCLfreeall 2/28/2000 */ int PRCLfreeall(); }; class librock_CVsil; class librock_CWStream_CVsil : public librock_IMemoryStream { public: librock_CWStream_CVsil(librock_CVsil *pv) { m_pv = pv; }; ~librock_CWStream_CVsil() { }; private: librock_CWStream_CVsil() { /* Don't call this one */ }; protected: librock_CVsil librock_PTR *m_pv; public: private: virtual long seek(long dist,int mode) { /* not supported */; return -1; return -1; }; private: virtual unsigned read(char librock_PTR *dest, unsigned max) { /* not supported */; return 0; }; public: virtual unsigned write(const char librock_PTR *src, unsigned max); }; class librock_CStream_astr : public librock_IMemoryStream { /* Added 8/14/2000 */ public: librock_CStream_astr(char **ppasz) { m_ppasz = ppasz; }; ~librock_CStream_astr() { }; virtual long GetSeg(long offset,char librock_PTR * librock_PTR *p, int cbToStore); virtual char *GetCont(long offset, int cb); virtual void WrittenTo(long offset); private: librock_CStream_astr() { /* Don't call this one */ }; protected: char **m_ppasz; private: virtual long seek(long dist,int mode) { /* Seek not supported */ return -1; }; virtual unsigned read(char librock_PTR *dest, unsigned max) { /* not supported */ return 0; }; public: virtual unsigned write(const char librock_PTR *src, unsigned max); }; class librock_CVsb; class librock_CStream_CVsb : public librock_IMemoryStream { /* Added 9/27/99 */ public: librock_CStream_CVsb(librock_CVsb *pv) { m_pv = pv; }; virtual long GetSeg(long offset,char librock_PTR * librock_PTR *p, int cbToStore); virtual char *GetCont(long offset, int cb); virtual void WrittenTo(long offset); private: librock_CStream_CVsb() { /* Don't call this one */ }; protected: librock_CVsb librock_PTR *m_pv; public: /* IRWPStream Primitive Implementation */ virtual long seek(long dist,int mode) { /*Seek not supported */ return -1; }; private: virtual unsigned read(char librock_PTR *dest, unsigned max) { /* not supported */ return 0; }; public: virtual unsigned write(const char librock_PTR *src, unsigned max); }; /**************************************************************/ class librock_CAAR { /* 1-15-98 Ascii archive. Supports storing multiple sections into one ASCII line-oriented file. */ /* DEBUG: these should support error reporting */ protected: char *m_pszHead; public: long m_offBegin; /* Offset in m_f to start of current section */ long m_offNext; /* Offset to next section (when reading) */ librock_FILE *m_f; librock_CAAR() { m_pszHead = 0; m_f =0; }; ~librock_CAAR(); /* Reading */ void attach(librock_FILE *f); char *opennext(long *pcbLen); char *aReadLine(char **ppasz); /* Reads until end of section */ /* Writing */ void write(const char *ptr,int cbLen); /* Write. can just access m_f also */ void begin(const char *pszHeader); void end(void); }; #ifdef INADDR_ANY class librock_CStream_Socket : public librock_IStream { private: librock_CStream_Socket() { assert(0&&"Don't call constructor CStream_Socket(void)"); /* Don't call this one */ m_s = 0; }; public: librock_CStream_Socket(SOCKET s) { m_s = s; }; ~librock_CStream_Socket() { }; protected: SOCKET m_s; public: /* Primitive Implementation */ virtual unsigned Read(char librock_PTR *dest, unsigned max) { return (int) recv(m_s,dest,max,0); }; virtual long Seek(long pos,int mode) { if ((mode == SEEK_CUR)&&(pos==0)) {return -1;} assert(0&&"CStream_Socket::Seek not supported"); return -1; } virtual unsigned Write(const char librock_PTR *src, unsigned max) { return (int) send(m_s,src,max,0); }; }; #endif #endif /* EXPERIMENTAL_IStream */ #include #endif /* $Log: stream.h,v $ Revision 1.2 2002/03/18 16:11:16 forrest@mibsoftware.com rights=#1 Fix a comment Revision 1.1 2002/03/18 16:09:34 forrest@mibsoftware.com rights=#1 Initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */