Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
/*
2
  basic stream class - it's similar to STL streams
3
*/
4
 
5
#ifndef BOB_DOM_STREAM_INCLUDED
6
#define BOB_DOM_STREAM_INCLUDED
7
 
8
#include "../common/indian.h"
9
#include "../common/ext_stream.h"
10
 
11
/* 
12
	bob_dom_istream - input template stream
13
	bob_dom_ostream - output template stream
14
*/
15
 
16
template <class Ty>
17
class bob_istream : public ext::stream_base
18
{
19
	public:
20
		typedef size_t size_type;
21
		typedef int offset_type;
22
		typedef Ty value_type;
23
		typedef Ty* value_type_ptr;
24
		typedef ext::stream_base base;
25
		typedef const Ty* const_value_type_ptr;
26
 
27
		virtual size_type read(value_type_ptr buffer, size_type size)=0;
28
		virtual bool advance(offset_type off)=0;
29
		virtual size_type tell() const = 0;
30
};
31
 
32
template <class Ty>
33
class bob_dom_ibufferstream : public bob_istream<Ty>
34
{
35
	public:
36
		typedef bob_istream<Ty> base;
37
		typedef int offset_type;
38
 
39
	private:
40
		const_value_type_ptr m_buffer;
41
		const_value_type_ptr m_ptr;
42
		size_type m_size;
43
		size_type m_avail;
44
 
45
	public:
46
		bob_dom_ibufferstream() { rdbuff(0, 0); clear(); }
47
		bob_dom_ibufferstream(const_value_type_ptr *buffer, size_type size) { rdbuff(buffer, size); }
48
 
49
		const_value_type_ptr rdbuff() const { return m_ptr; }
50
		void rdbuff(const_value_type_ptr buffer, size_type size) 
51
		{ 
52
			m_buffer=m_ptr=buffer; 
53
			m_avail=m_size=size;
54
			clear();
55
		}
56
 
57
		void clear(state s=goodbit) 
58
		{ 
59
			base::clear(s);
60
			setstate(m_buffer ? goodbit : badbit); 
61
		}
62
 
63
 
64
		size_type read(value_type_ptr buffer, size_type size);
65
 
66
		size_type tell() const { return m_ptr - m_buffer; }
67
		size_type size() const { return m_size; }
68
		size_type avail() const { return m_avail; }
69
 
70
		bool advance(offset_type offset)
71
		{
72
			if((offset > (offset_type)avail()) || ((offset_type)tell() + offset < 0))
73
				setstate(failbit);
74
			else{
75
				m_ptr+=offset;
76
				m_avail-=offset;
77
			}
78
			if(avail()==0) 
79
				setstate(eofbit);
80
			else
81
				clear((state)(rdstate() & ~eofbit));
82
			return good();
83
		}
84
 
85
		void rewind()
86
		{
87
			m_ptr=m_buffer;
88
			m_avail=m_size;
89
			clear();
90
		}		
91
 
92
 
93
		bob_dom_ibufferstream& operator ++()
94
		{
95
			advance(1);
96
			return *this;
97
		}
98
 
99
		bob_dom_ibufferstream& operator --()
100
		{
101
			advance(-1);
102
			return *this;
103
		}
104
 
105
		bob_dom_ibufferstream& operator +=(offset_type offset)
106
		{
107
			advance(offset);
108
			return *this;
109
		}
110
 
111
		bob_dom_ibufferstream& operator -=(offset_type offset)
112
		{
113
			advance(offset * -1);
114
			return *this;
115
		}
116
};
117
 
118
template <class Ty>
119
typename bob_dom_ibufferstream<Ty>::size_type bob_dom_ibufferstream<Ty>::read(value_type_ptr buffer, size_type s)
120
{
121
	if(!good()) return -1;
122
	if(avail() < s){
123
		setstate(failbit);
124
		return -1;
125
	}
126
	else{
127
		memcpy(buffer, rdbuff(), s);
128
		advance((offset_type)s);
129
		return s;
130
	}
131
}
132
 
133
typedef bob_dom_ibufferstream<unsigned char> bob_dom_ibytestream;
134
 
135
inline 
136
bob_dom_ibytestream& operator >> (bob_dom_ibytestream& is, int& right)
137
{
138
	is.read((bob_dom_ibytestream::value_type_ptr)&right, sizeof(right));
139
	right=be2le((unsigned int)right);
140
	return is;
141
}
142
 
143
inline
144
bob_dom_ibytestream& operator >> (bob_dom_ibytestream& is, short& right)
145
{
146
	is.read((bob_dom_ibytestream::value_type_ptr)&right, sizeof(right));
147
	right=be2le((unsigned short) right);
148
	return is;
149
}
150
 
151
#include <memory.h>
152
 
153
inline
154
bob_dom_ibytestream& operator >> (bob_dom_ibytestream& is, char*& right)
155
{
156
	bob_dom_ibytestream::const_value_type_ptr data=is.rdbuff();
157
	bob_dom_ibytestream::const_value_type_ptr pos=(bob_dom_ibytestream::const_value_type_ptr)memchr(data, 0, is.avail());
158
	if(pos){
159
		bob_dom_ibytestream::offset_type s=(bob_dom_ibytestream::offset_type)(pos - data + 1);
160
		right=new char[s];
161
		memcpy(right, data, s);
162
		is.advance(s);
163
	}
164
	else 
165
		right=0;
166
	return is;
167
}
168
 
169
template <class Ty>
170
class bob_ostream : public ext::stream_base
171
{
172
	public:
173
		typedef Ty value_type;
174
		typedef Ty* value_type_ptr;
175
		typedef const Ty const_value_type;
176
		typedef const Ty* const_value_type_ptr;
177
		typedef size_t size_type;
178
 
179
		// flags for formating output
180
		static const int hex=1;
181
 
182
		virtual size_type write(const_value_type_ptr data, size_type size)=0;
183
		virtual bool put(const_value_type &ch)=0;
184
 
185
		virtual size_t tell()=0;
186
};
187
 
188
 
189
 
190
#endif // !defined(BOB_DOM_STREAM_INCLUDED)