Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
114 cycrow 1
/*
2
  basic file stream class
3
*/
4
 
5
#ifndef BOB_DOM_FILESTREAM_INCLUDED
6
#define BOB_DOM_FILESTREAM_INCLUDED
7
 
8
#include <string.h>
9
#include <stdlib.h>
10
#include <stdio.h>
11
 
12
#include "stream.h"
13
 
14
/*
15
	obinaryfilestream - binary output stream
16
	otextfilestream - text output stream
17
*/
18
 
19
 
20
class filestream
21
{
22
	protected:
23
		char *m_name;
24
 
25
		void name(const char *pszName)
26
		{
27
			delete[] m_name;
28
			m_name = _strdup(pszName);
29
		}
30
 
31
	public:
32
		enum mode{
33
			seekbof   =  1,
34
			seekeof   =  2,
35
			rdonly    =  4,
36
			rdwr      =  8,
37
			create    = 16,
38
			typepck   = 32,
39
			typeplain = 64
40
		};
41
 
42
		filestream() { m_name = _strdup(""); }
43
		filestream(const filestream& other) 
44
		{ 
45
			m_name = _strdup(other.name()); 
46
		}
47
 
48
		virtual ~filestream() { delete[] m_name; }
49
 
50
		const char *name() const { return m_name; }
51
 
52
		virtual bool open(const char *fileName, mode m = seekbof) = 0;
53
		virtual void close() = 0;
54
};
55
 
56
class obinaryfilestream : public ostream<char>, public filestream
57
{
58
	public:
59
		virtual size_t seek(int offset, seek_type origin)=0;
60
};
61
 
62
class otextfilestream : public ostream<char>, public filestream
63
{
64
	public:
65
		static const int bob_addsemicolon = 4;
66
 
67
};
68
 
69
class itextfilestream : public istream<char>, public filestream
70
{
71
 
72
};
73
 
74
class ibinaryfilestream : public istream<unsigned char>, public filestream
75
{
76
 
77
};
78
 
79
// binary input operators
80
inline
81
ibinaryfilestream& operator >> (ibinaryfilestream& is, int& right)
82
{
83
	is.read((unsigned char*)&right, sizeof(right));
84
	right=be2le((unsigned int)right);
85
	return is;
86
}
87
 
88
inline
89
ibinaryfilestream& operator >> (ibinaryfilestream& is, short& right)
90
{
91
	is.read((unsigned char*)&right, sizeof(right));
92
	right=be2le((unsigned short)right);
93
	return is;
94
}
95
 
96
inline
97
ibinaryfilestream& operator >> (ibinaryfilestream& is, double& right)
98
{
99
	static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65536
100
 
101
	int v;
102
	is >> v;
103
	right=v * MULTIPLIER;
104
	return is;
105
}
106
 
107
inline
108
ibinaryfilestream& operator >> (ibinaryfilestream& is, float& right)
109
{
110
	static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65536
111
 
112
	int v;
113
	is >> v;
114
	right=(float)(v * MULTIPLIER);
115
	return is;
116
}
117
 
118
inline
119
ibinaryfilestream& operator >> (ibinaryfilestream& is, char*& right)
120
{
121
	char *buff=0, *p;
122
	size_t size=250, readed;
123
	buff=new char[size];
124
 
125
	size_t start=is.tell();
126
	while(is.good()){
127
		readed=is.read((unsigned char*)buff, size);
128
		if(is.fail()) break;
129
		if(p=(char*)memchr(buff, 0, readed)) break;
130
	}
131
 
132
	if(p){
133
		size_t send=is.tell();
134
		size_t end=(send - readed) + (p - buff);
135
 
136
		delete buff;
137
		size=end - start + 1;
138
		buff=new char[size];
139
		is.advance( - ((int)send - (int)start));
140
		is.read((unsigned char*)buff, size);
141
	}
142
	else{
143
		delete buff;
144
		buff=0;
145
	}
146
	right=buff;
147
 
148
	return is;
149
}
150
 
151
/* binary output operators */
152
inline 
153
obinaryfilestream& operator << (obinaryfilestream& os, int right)
154
{
155
	right=be2le((unsigned int)right);
156
	os.write((obinaryfilestream::const_value_type_ptr)&right, sizeof(right));
157
	return os;
158
}
159
 
160
inline 
161
obinaryfilestream& operator << (obinaryfilestream& os, short right)
162
{
163
	right=be2le((unsigned short)right);
164
	os.write((obinaryfilestream::const_value_type_ptr)&right, sizeof(right));
165
	return os;
166
}
167
 
168
#include "conversion.h"
169
 
170
inline
171
obinaryfilestream& operator << (obinaryfilestream& os, double right)
172
{
173
	os << conversion::double2int(right);
174
	return os;
175
}
176
 
177
inline
178
obinaryfilestream& operator << (obinaryfilestream& os, float right)
179
{
180
	os << conversion::double2int(right);
181
	return os;
182
}
183
 
184
inline 
185
obinaryfilestream& operator << (obinaryfilestream& os, const char *right)
186
{
187
	os.write((obinaryfilestream::const_value_type_ptr)right, strlen(right) + 1);
188
	return os;
189
}
190
 
191
inline 
192
obinaryfilestream& operator << (obinaryfilestream& os, char *right)
193
{
194
	os << const_cast<const char*>(right);
195
	return os;
196
}
197
 
198
/* text output operators */
199
inline 
200
otextfilestream& operator << (otextfilestream& os, const char *right)
201
{
202
	os.write(right, strlen(right));
203
	if(os.flags() & otextfilestream::bob_addsemicolon)
204
		os.write("; ", 2);
205
 
206
	return os;
207
}
208
 
209
inline 
210
otextfilestream& operator << (otextfilestream& os, char *right)
211
{
212
	os << const_cast<const char*>(right);
213
	return os;
214
}
215
 
216
 
217
inline 
218
otextfilestream& operator << (otextfilestream& os, int right)
219
{
220
	char ch[20];
221
	if(os.flags() & otextfilestream::hex)
222
		sprintf(ch, "%X", right);
223
	else
224
		_itoa(right, ch, 10);
225
	os << ch;
226
	return os;
227
}
228
 
229
inline 
230
otextfilestream& operator << (otextfilestream& os, float right)
231
{
232
	char ch[20];
233
	sprintf(ch, "%f", right);
234
	os << ch;
235
	return os;
236
}
237
 
238
inline
239
otextfilestream& operator << (otextfilestream& os, double right)
240
{
241
	char ch[20];
242
	sprintf(ch, "%f", (float)right);
243
	os << ch;
244
	return os;
245
}
246
 
247
inline 
248
otextfilestream& operator << (otextfilestream& os, char right)
249
{
250
	os.put(right);
251
	return os;
252
}
253
 
254
inline 
255
otextfilestream& operator << (otextfilestream& os, short right)
256
{
257
	os << static_cast<int>(right);
258
	return os;
259
}
260
 
261
inline 
262
otextfilestream& operator << (otextfilestream& os, bool right)
263
{
264
	os << static_cast<int>(right);
265
	return os;
266
}
267
 
268
// text manipulators
269
 
270
inline
271
otextfilestream& endl(otextfilestream& os)
272
{
273
	os.put('\r');
274
	os.put('\n');
275
	return os;
276
}
277
 
278
inline 
279
otextfilestream& autoSemicolons(otextfilestream& os)
280
{
281
	os.setf(otextfilestream::bob_addsemicolon);
282
	return os;
283
}
284
 
285
inline 
286
otextfilestream& noSemicolons(otextfilestream& os)
287
{
288
	os.unsetf(otextfilestream::bob_addsemicolon);
289
	return os;
290
}
291
 
292
inline
293
otextfilestream& hex(otextfilestream& os)
294
{
295
	os.setf(otextfilestream::hex);
296
	return os;
297
}
298
 
299
inline
300
otextfilestream& dec(otextfilestream& os)
301
{
302
	os.unsetf(otextfilestream::hex);
303
	return os;
304
}
305
 
306
inline
307
otextfilestream& operator << (otextfilestream& os, otextfilestream& f(otextfilestream& os))
308
{
309
	return f(os);
310
}
311
 
312
//************************
313
 
314
template <class Ty>
315
ibinaryfilestream& operator >> (ibinaryfilestream& is, Ty& o)
316
{
317
	o.load(is);
318
	return is;
319
}
320
 
321
template <class Ty>
322
obinaryfilestream& operator << (obinaryfilestream& os, Ty& o)
323
{
324
	o.toFile(os);
325
	return os;
326
}
327
 
328
template <class Ty>
329
otextfilestream& operator << (otextfilestream& os, Ty& o)
330
{
331
	o.toFile(os);
332
	return os;
333
}
334
 
335
//*************************
336
 
337
#endif // !defined(BOB_DOM_FILESTREAM_INCLUDED)