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