| 1 |
// -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- |
| 2 |
#ifndef BYTE_IO_STREAM_H |
| 3 |
#define BYTE_IO_STREAM_H |
| 4 |
|
| 5 |
|
| 6 |
//#include "vnconv.h" |
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
typedef unsigned char UKBYTE; |
| 10 |
typedef unsigned short UKWORD; |
| 11 |
typedef unsigned int UKDWORD; |
| 12 |
|
| 13 |
//---------------------------------------------------- |
| 14 |
class ByteStream { |
| 15 |
public: |
| 16 |
virtual ~ByteStream(){}; |
| 17 |
}; |
| 18 |
|
| 19 |
//---------------------------------------------------- |
| 20 |
class ByteInStream: public ByteStream |
| 21 |
{ |
| 22 |
public: |
| 23 |
virtual int getNext(UKBYTE &b) = 0; |
| 24 |
virtual int peekNext(UKBYTE &b) = 0; |
| 25 |
virtual int unget(UKBYTE b) = 0; |
| 26 |
|
| 27 |
virtual int getNextW(UKWORD &w) = 0; |
| 28 |
virtual int peekNextW(UKWORD &w) = 0; |
| 29 |
|
| 30 |
virtual int getNextDW(UKDWORD &dw) = 0; |
| 31 |
|
| 32 |
virtual int bookmark() //no support for bookmark by default |
| 33 |
{ |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
virtual int gotoBookmark() |
| 38 |
{ |
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
virtual int eos() = 0; //end of stream |
| 43 |
virtual int close() = 0; |
| 44 |
}; |
| 45 |
|
| 46 |
//---------------------------------------------------- |
| 47 |
class ByteOutStream: public ByteStream |
| 48 |
{ |
| 49 |
public: |
| 50 |
virtual int putB(UKBYTE b) = 0; |
| 51 |
virtual int putW(UKWORD w) = 0; |
| 52 |
virtual int puts(const char *s, int size = -1) = 0; // write an 8-bit string |
| 53 |
virtual int isOK() = 0;// get current stream state |
| 54 |
}; |
| 55 |
|
| 56 |
//---------------------------------------------------- |
| 57 |
class StringBIStream : public ByteInStream |
| 58 |
{ |
| 59 |
protected: |
| 60 |
int m_eos; |
| 61 |
UKBYTE *m_data, *m_current; |
| 62 |
int m_len, m_left; |
| 63 |
|
| 64 |
struct { |
| 65 |
int eos; |
| 66 |
UKBYTE *data, *current; |
| 67 |
int len, left; |
| 68 |
} m_bookmark; |
| 69 |
|
| 70 |
int m_didBookmark; |
| 71 |
|
| 72 |
public: |
| 73 |
StringBIStream(UKBYTE *data, int len, int elementSize = 1); |
| 74 |
virtual int getNext(UKBYTE &b); |
| 75 |
virtual int peekNext(UKBYTE &b); |
| 76 |
virtual int unget(UKBYTE b); |
| 77 |
|
| 78 |
virtual int getNextW(UKWORD &w); |
| 79 |
virtual int peekNextW(UKWORD &w); |
| 80 |
|
| 81 |
virtual int getNextDW(UKDWORD &dw); |
| 82 |
|
| 83 |
virtual int eos(); //end of stream |
| 84 |
virtual int close(); |
| 85 |
|
| 86 |
virtual int bookmark(); |
| 87 |
virtual int gotoBookmark(); |
| 88 |
|
| 89 |
void reopen(); |
| 90 |
int left() { |
| 91 |
return m_left; |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
//---------------------------------------------------- |
| 96 |
class FileBIStream : public ByteInStream |
| 97 |
{ |
| 98 |
protected: |
| 99 |
FILE *m_file; |
| 100 |
int m_bufSize; |
| 101 |
char *m_buf; |
| 102 |
int m_own; |
| 103 |
int m_didBookmark; |
| 104 |
|
| 105 |
struct { |
| 106 |
long pos; |
| 107 |
} m_bookmark; |
| 108 |
|
| 109 |
//some systems don't have wide char IO functions |
| 110 |
//we have to use this variables to implement that |
| 111 |
UKBYTE m_readByte; |
| 112 |
int m_readAhead; |
| 113 |
int m_lastIsAhead; |
| 114 |
|
| 115 |
public: |
| 116 |
|
| 117 |
FileBIStream(int bufsize = 8192, char *buf = NULL); |
| 118 |
// FileBIStream(char *fileName, int bufsize = 8192, void *buf = NULL); |
| 119 |
|
| 120 |
int open(const char *fileName); |
| 121 |
void attach(FILE *f); |
| 122 |
virtual int close(); |
| 123 |
|
| 124 |
virtual int getNext(UKBYTE &b); |
| 125 |
virtual int peekNext(UKBYTE &b); |
| 126 |
virtual int unget(UKBYTE b); |
| 127 |
|
| 128 |
virtual int getNextW(UKWORD &w); |
| 129 |
virtual int peekNextW(UKWORD &w); |
| 130 |
|
| 131 |
virtual int getNextDW(UKDWORD &dw); |
| 132 |
|
| 133 |
virtual int eos(); //end of stream |
| 134 |
|
| 135 |
virtual int bookmark(); |
| 136 |
virtual int gotoBookmark(); |
| 137 |
|
| 138 |
virtual ~FileBIStream(); |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
//---------------------------------------------------- |
| 143 |
class StringBOStream : public ByteOutStream |
| 144 |
{ |
| 145 |
protected: |
| 146 |
UKBYTE *m_buf, *m_current; |
| 147 |
int m_out; |
| 148 |
int m_len; |
| 149 |
int m_bad; |
| 150 |
public: |
| 151 |
StringBOStream(UKBYTE *buf, int len); |
| 152 |
virtual int putB(UKBYTE b); |
| 153 |
virtual int putW(UKWORD w); |
| 154 |
virtual int puts(const char *s, int size = -1); |
| 155 |
virtual int isOK(); // get current stream state |
| 156 |
|
| 157 |
virtual int close() |
| 158 |
{ |
| 159 |
return 1; |
| 160 |
}; |
| 161 |
|
| 162 |
void reopen(); |
| 163 |
int getOutBytes() { |
| 164 |
return m_out; |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
//---------------------------------------------------- |
| 169 |
class FileBOStream : public ByteOutStream |
| 170 |
{ |
| 171 |
protected: |
| 172 |
FILE *m_file; |
| 173 |
int m_bufSize; |
| 174 |
char *m_buf; |
| 175 |
int m_own; |
| 176 |
int m_bad; |
| 177 |
|
| 178 |
public: |
| 179 |
FileBOStream(int bufsize = 8192, char *buf = NULL); |
| 180 |
// FileBOStream(char *fileName, int bufsize = 8192, void *buf = NULL); |
| 181 |
|
| 182 |
int open(const char *fileName); |
| 183 |
void attach(FILE *); |
| 184 |
virtual int close(); |
| 185 |
|
| 186 |
virtual int putB(UKBYTE b); |
| 187 |
virtual int putW(UKWORD w); |
| 188 |
virtual int puts(const char *s, int size = -1); |
| 189 |
virtual int isOK(); // get current stream state |
| 190 |
virtual ~FileBOStream(); |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
#endif |