aboutsummaryrefslogtreecommitdiff
path: root/lib/base/buffer.cpp
blob: 07e9d7f1c086f55c68b6b0c6b7ace45cbce47da8 (plain)
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
195
196
#include <lib/base/buffer.h>
#include <lib/base/eerror.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

void eIOBuffer::removeblock()
{
	ASSERT(!buffer.empty());
	eIOBufferData &b=buffer.front();
	delete[] b.data;
	buffer.pop_front();
	ptr=0;
}

eIOBuffer::eIOBufferData &eIOBuffer::addblock()
{
	eIOBufferData s;
	s.data=new __u8[allocationsize];
	s.len=0;
	buffer.push_back(s);
	return buffer.back();
}

eIOBuffer::~eIOBuffer()
{
	clear();
}

void eIOBuffer::clear()
{
	while (!buffer.empty())
		removeblock();
}

int eIOBuffer::size() const
{
	int total=0;
	for (std::list<eIOBufferData>::const_iterator i(buffer.begin()); i != buffer.end(); ++i)
		total+=i->len;
	total-=ptr;
	return total;
}

int eIOBuffer::empty() const
{
	return buffer.empty();
}

int eIOBuffer::peek(void *dest, int len) const
{
	__u8 *dst=(__u8*)dest;
	std::list<eIOBufferData>::const_iterator i(buffer.begin());
	int p=ptr;
	int written=0;
	while (len)
	{	
		if (i == buffer.end())
			break;
		int tc=i->len-p;
		if (tc > len)
			tc = len;
	
		memcpy(dst, i->data+p, tc);
		dst+=tc;
		written+=tc;
	
		++i;
		p=0;
			
		len-=tc;
	}
	return written;
}

void eIOBuffer::skip(int len)
{
	while (len)
	{
		ASSERT(! buffer.empty());
		int tn=len;
		if (tn > (buffer.front().len-ptr))
			tn=buffer.front().len-ptr;

		ptr+=tn;
		if (ptr == buffer.front().len)
			removeblock();
		len-=tn;
	}
}

int eIOBuffer::read(void *dest, int len)
{
	__u8 *dst=(__u8*)dest;
	len=peek(dst, len);
	skip(len);
	return len;
}

void eIOBuffer::write(const void *source, int len)
{
	const __u8 *src=(const __u8*)source;
	while (len)
	{
		int tc=len;
		if (buffer.empty() || (allocationsize == buffer.back().len))
			addblock();
		if (tc > allocationsize-buffer.back().len)
			tc=allocationsize-buffer.back().len;
		memcpy(buffer.back().data+buffer.back().len, src, tc);
		src+=tc;
		buffer.back().len+=tc;
		len-=tc;
	}
}

int eIOBuffer::fromfile(int fd, int len)
{
	int re=0;
	while (len)
	{
		int tc=len;
		int r;
		if (buffer.empty() || (allocationsize == buffer.back().len))
			addblock();
		if (tc > allocationsize-buffer.back().len)
			tc=allocationsize-buffer.back().len;
		r=::read(fd, buffer.back().data+buffer.back().len, tc);
		buffer.back().len+=r;
		len-=r;
		if (r < 0)
		{
			if (errno != EWOULDBLOCK)
				eDebug("read: %m");
			r=0;
		}
		re+=r;
		if (r != tc)
			break;
	}
	return re;
}

int eIOBuffer::tofile(int fd, int len)
{
	int written=0;
	int w;
	while (len && !buffer.empty())
	{	
		if (buffer.begin() == buffer.end())
			break;
		int tc=buffer.front().len-ptr;
		if (tc > len)
			tc = len;
	
		w=::write(fd, buffer.front().data+ptr, tc);
		if (w < 0)
		{
			if (errno != EWOULDBLOCK)
				eDebug("write: %m");
			w=0;
		}
		ptr+=w;
		if (ptr == buffer.front().len)
			removeblock();
		written+=w;	

		len-=w;
		if (tc != w)
			break;
	}
	return written;
}

int eIOBuffer::searchchr(char ch) const
{
	std::list<eIOBufferData>::const_iterator i(buffer.begin());
	int p=ptr;
	int c=0;
	while (1)
	{	
		if (i == buffer.end())
			break;
		while (p < i->len)
		{
			if (i->data[p] == ch)
				return c;
			else
				c++, p++;
		}
		++i;
		p=0;
	}
	return -1;
}