1 #include <lib/base/buffer.h>
2 #include <lib/base/eerror.h>
8 void eIOBuffer::removeblock()
10 ASSERT(!buffer.empty());
11 eIOBufferData &b=buffer.front();
17 eIOBuffer::eIOBufferData &eIOBuffer::addblock()
20 s.data=new __u8[allocationsize];
26 eIOBuffer::~eIOBuffer()
31 void eIOBuffer::clear()
33 while (!buffer.empty())
37 int eIOBuffer::size() const
40 for (std::list<eIOBufferData>::const_iterator i(buffer.begin()); i != buffer.end(); ++i)
46 int eIOBuffer::empty() const
48 return buffer.empty();
51 int eIOBuffer::peek(void *dest, int len) const
53 __u8 *dst=(__u8*)dest;
54 std::list<eIOBufferData>::const_iterator i(buffer.begin());
59 if (i == buffer.end())
65 memcpy(dst, i->data+p, tc);
77 void eIOBuffer::skip(int len)
81 ASSERT(! buffer.empty());
83 if (tn > (buffer.front().len-ptr))
84 tn=buffer.front().len-ptr;
87 if (ptr == buffer.front().len)
93 int eIOBuffer::read(void *dest, int len)
95 __u8 *dst=(__u8*)dest;
101 void eIOBuffer::write(const void *source, int len)
103 const __u8 *src=(const __u8*)source;
107 if (buffer.empty() || (allocationsize == buffer.back().len))
109 if (tc > allocationsize-buffer.back().len)
110 tc=allocationsize-buffer.back().len;
111 memcpy(buffer.back().data+buffer.back().len, src, tc);
113 buffer.back().len+=tc;
118 int eIOBuffer::fromfile(int fd, int len)
125 if (buffer.empty() || (allocationsize == buffer.back().len))
127 if (tc > allocationsize-buffer.back().len)
128 tc=allocationsize-buffer.back().len;
129 r=::read(fd, buffer.back().data+buffer.back().len, tc);
130 buffer.back().len+=r;
133 if (errno != EWOULDBLOCK && errno != EBUSY && errno != EINTR)
134 eDebug("couldn't read: %m");
147 int eIOBuffer::tofile(int fd, int len)
151 while (len && !buffer.empty())
153 if (buffer.begin() == buffer.end())
155 int tc=buffer.front().len-ptr;
159 w=::write(fd, buffer.front().data+ptr, tc);
162 if (errno != EWOULDBLOCK && errno != EBUSY && errno != EINTR)
167 if (ptr == buffer.front().len)
178 int eIOBuffer::searchchr(char ch) const
180 std::list<eIOBufferData>::const_iterator i(buffer.begin());
185 if (i == buffer.end())
189 if (i->data[p] == ch)