File: byteio.h

package info (click to toggle)
ibus-unikey 0.7.0~beta1-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 672 kB
  • sloc: cpp: 7,042; xml: 63; makefile: 6
file content (194 lines) | stat: -rw-r--r-- 3,943 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#ifndef BYTE_IO_STREAM_H
#define BYTE_IO_STREAM_H


//#include "vnconv.h"
#include <stdio.h>

typedef unsigned char UKBYTE;
typedef unsigned short UKWORD;
typedef unsigned int UKDWORD;

//----------------------------------------------------
class ByteStream {
 public:
  virtual ~ByteStream(){};
};

//----------------------------------------------------
class ByteInStream: public ByteStream
{
public:
	virtual int getNext(UKBYTE &b) = 0;
	virtual int peekNext(UKBYTE &b) = 0;
	virtual int unget(UKBYTE b) = 0;

	virtual int getNextW(UKWORD &w) = 0;
	virtual int peekNextW(UKWORD &w) = 0;

	virtual int getNextDW(UKDWORD &dw) = 0;

	virtual int bookmark() //no support for bookmark by default
	{
		return 0;
	}

	virtual int gotoBookmark()
	{
		return 0;
	}

	virtual int eos() = 0; //end of stream
	virtual int close() = 0;
};

//----------------------------------------------------
class ByteOutStream: public ByteStream
{
public:
	virtual int putB(UKBYTE b) = 0;
	virtual int putW(UKWORD w) = 0;
	virtual int puts(const char *s, int size = -1) = 0; // write an 8-bit string
	virtual int isOK() = 0;// get current stream state
};

//----------------------------------------------------
class StringBIStream : public ByteInStream
{
protected:
	int m_eos;
	UKBYTE *m_data, *m_current;
	int m_len, m_left;

	struct {
		int eos;
		UKBYTE *data, *current;
		int len, left;
	} m_bookmark;

	int m_didBookmark;
		
public:
	StringBIStream(UKBYTE *data, int len, int elementSize = 1);
	virtual int getNext(UKBYTE &b);
	virtual int peekNext(UKBYTE &b);
	virtual int unget(UKBYTE b);

	virtual int getNextW(UKWORD &w);
	virtual int peekNextW(UKWORD &w);

	virtual int getNextDW(UKDWORD &dw);

    virtual int eos(); //end of stream
	virtual int close();

	virtual int bookmark();
	virtual int gotoBookmark();

	void reopen();
	int left() {
		return m_left;
	}
};

//----------------------------------------------------
class FileBIStream : public ByteInStream
{
protected:
	FILE *m_file;
	int m_bufSize;
	char *m_buf;
	int m_own;
	int m_didBookmark;

	struct {
		long pos;
	} m_bookmark;

	//some systems don't have wide char IO functions
	//we have to use this variables to implement that
	UKBYTE m_readByte; 
	int m_readAhead;
	int m_lastIsAhead;

public:

	FileBIStream(int bufsize = 8192, char *buf = NULL);
//	FileBIStream(char *fileName, int bufsize = 8192, void *buf = NULL);

	int open(const char *fileName);
	void attach(FILE *f);
	virtual int close();

	virtual int getNext(UKBYTE &b);
	virtual int peekNext(UKBYTE &b);
	virtual int unget(UKBYTE b);

	virtual int getNextW(UKWORD &w);
	virtual int peekNextW(UKWORD &w);

    virtual int getNextDW(UKDWORD &dw);

    virtual int eos(); //end of stream

	virtual int bookmark();
	virtual int gotoBookmark();

	virtual ~FileBIStream();
};


//----------------------------------------------------
class StringBOStream : public ByteOutStream
{
protected:
	UKBYTE *m_buf, *m_current;
	int m_out;
	int m_len;
	int m_bad;
public:
	StringBOStream(UKBYTE *buf, int len);
	virtual int putB(UKBYTE b);
	virtual int putW(UKWORD w);
	virtual int puts(const char *s, int size = -1);
	virtual int isOK(); // get current stream state

	virtual int close()	
	{
		return 1;
	};

	void reopen();
	int getOutBytes() {
		return m_out;
	}
};

//----------------------------------------------------
class FileBOStream : public ByteOutStream
{
protected:
	FILE *m_file;
	int m_bufSize;
	char *m_buf;
	int m_own;
	int m_bad;

public:
	FileBOStream(int bufsize = 8192, char *buf = NULL);
//	FileBOStream(char *fileName, int bufsize = 8192, void *buf = NULL);

	int open(const char *fileName);
	void attach(FILE *);
	virtual int close();

	virtual int putB(UKBYTE b);
	virtual int putW(UKWORD w);
	virtual int puts(const char *s, int size = -1);
	virtual int isOK(); // get current stream state
	virtual ~FileBOStream();
};


#endif