Subversion Repositories spk

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

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