aboutsummaryrefslogtreecommitdiff
path: root/src/stable/queuebuf.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/stable/queuebuf.cpp
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/stable/queuebuf.cpp')
-rw-r--r--src/stable/queuebuf.cpp278
1 files changed, 278 insertions, 0 deletions
diff --git a/src/stable/queuebuf.cpp b/src/stable/queuebuf.cpp
new file mode 100644
index 0000000..98d8ee0
--- /dev/null
+++ b/src/stable/queuebuf.cpp
@@ -0,0 +1,278 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/queuebuf.h"
9
10#include "bu/sio.h"
11using Bu::sio;
12
13Bu::QueueBuf::QueueBuf( int iBlockSize /*=256*/ ) :
14 iBlockSize( iBlockSize ),
15 iReadOffset( 0 ),
16 iWriteOffset( 0 ),
17 iTotalSize( 0 )
18{
19}
20
21Bu::QueueBuf::~QueueBuf()
22{
23 for( BlockList::iterator i = lBlocks.begin(); i; i++ )
24 delete[] *i;
25}
26
27void Bu::QueueBuf::close()
28{
29 for( BlockList::iterator i = lBlocks.begin(); i; i++ )
30 delete[] *i;
31 lBlocks.clear();
32 iReadOffset = iWriteOffset = iTotalSize = 0;
33}
34
35Bu::size Bu::QueueBuf::read( void *pRawBuf, Bu::size nBytes )
36{
37 if( nBytes <= 0 )
38 return 0;
39
40 if( lBlocks.isEmpty() )
41 return 0;
42
43 Bu::size iLeft = nBytes;
44 char *pBuf = (char *)pRawBuf;
45
46 while( iLeft > 0 && iTotalSize > 0 )
47 {
48 if( iReadOffset == iBlockSize )
49 {
50 removeBlock();
51 if( lBlocks.isEmpty() )
52 {
53 return nBytes-iLeft;
54 }
55 iReadOffset = 0;
56 }
57 char *pBlock = lBlocks.first();
58 Bu::size iCopy = iBlockSize-iReadOffset;
59 if( iLeft < iCopy )
60 iCopy = iLeft;
61 if( iTotalSize < iCopy )
62 iCopy = iTotalSize;
63 memcpy( pBuf, pBlock+iReadOffset, iCopy );
64 iReadOffset += iCopy;
65 iLeft -= iCopy;
66 pBuf += iCopy;
67 iTotalSize -= iCopy;
68// sio << "Read " << iCopy << " bytes, new size: " << iTotalSize << sio.nl;
69 }
70
71 return nBytes - iLeft;
72}
73
74Bu::size Bu::QueueBuf::peek( void *pBuf, Bu::size nBytes )
75{
76 return peek( pBuf, nBytes, 0 );
77}
78
79Bu::size Bu::QueueBuf::peek( void *pRawBuf, Bu::size nBytes, Bu::size nSkip )
80{
81 if( nBytes <= 0 )
82 return 0;
83
84 if( lBlocks.isEmpty() )
85 return 0;
86
87 Bu::size iLeft = nBytes;
88 char *pBuf = (char *)pRawBuf;
89
90 int iTmpReadOffset = iReadOffset + nSkip;
91 Bu::size iTmpRemSize = iTotalSize;
92 BlockList::iterator iBlock = lBlocks.begin();
93 while( iTmpReadOffset > iBlockSize )
94 {
95 iTmpReadOffset -= iBlockSize;
96 iBlock++;
97 }
98 while( iLeft > 0 && iTmpRemSize > 0 )
99 {
100 if( iTmpReadOffset == iBlockSize )
101 {
102 iBlock++;
103 if( iBlock == lBlocks.end() )
104 {
105 return nBytes-iLeft;
106 }
107 iTmpReadOffset = 0;
108 }
109 char *pBlock = *iBlock;
110 Bu::size iCopy = iBlockSize-iTmpReadOffset;
111 if( iLeft < iCopy )
112 iCopy = iLeft;
113 if( iTmpRemSize < iCopy )
114 iCopy = iTmpRemSize;
115 memcpy( pBuf, pBlock+iTmpReadOffset, iCopy );
116 iTmpReadOffset += iCopy;
117 iLeft -= iCopy;
118 pBuf += iCopy;
119 iTmpRemSize -= iCopy;
120// sio << "Read (peek) " << iCopy << " bytes, new temp size: "
121// << iTmpRemSize << sio.nl;
122 }
123
124 return nBytes - iLeft;
125}
126
127Bu::size Bu::QueueBuf::write( const void *pRawBuf, Bu::size nBytes )
128{
129 if( nBytes <= 0 )
130 return 0;
131
132 if( lBlocks.isEmpty() )
133 {
134 addBlock();
135 iWriteOffset = 0;
136 }
137 Bu::size iLeft = nBytes;
138 const char *pBuf = (const char *)pRawBuf;
139
140 while( iLeft > 0 )
141 {
142 if( iWriteOffset == iBlockSize )
143 {
144 addBlock();
145 iWriteOffset = 0;
146 }
147 char *pBlock = lBlocks.last();
148 Bu::size iCopy = iBlockSize-iWriteOffset;
149 if( iLeft < iCopy )
150 iCopy = iLeft;
151 memcpy( pBlock+iWriteOffset, pBuf, iCopy );
152 iWriteOffset += iCopy;
153 iLeft -= iCopy;
154 pBuf += iCopy;
155 iTotalSize += iCopy;
156// sio << "Wrote " << iCopy << " bytes, new size: " << iTotalSize
157// << sio.nl;
158 }
159
160 return nBytes;
161}
162
163Bu::size Bu::QueueBuf::tell()
164{
165 return -1;
166}
167
168void Bu::QueueBuf::seek( Bu::size iAmnt )
169{
170 if( iAmnt <= 0 )
171 return;
172
173 if( (Bu::size)iAmnt >= iTotalSize )
174 {
175// sio << "seek: clear all data (" << iAmnt << ">=" << iTotalSize
176// << ")." << sio.nl;
177 close();
178 return;
179 }
180
181 iReadOffset += iAmnt;
182 iTotalSize -= iAmnt;
183 while( iReadOffset >= iBlockSize )
184 {
185 removeBlock();
186 iReadOffset -= iBlockSize;
187// sio << "seek: removal step (" << iReadOffset << ")" << sio.nl;
188 }
189}
190
191void Bu::QueueBuf::setPos( Bu::size )
192{
193}
194
195void Bu::QueueBuf::setPosEnd( Bu::size )
196{
197}
198
199bool Bu::QueueBuf::isEos()
200{
201 return iTotalSize == 0;
202}
203
204bool Bu::QueueBuf::isOpen()
205{
206 return true;
207}
208
209void Bu::QueueBuf::flush()
210{
211}
212
213bool Bu::QueueBuf::canRead()
214{
215 return iTotalSize > 0;
216}
217
218bool Bu::QueueBuf::canWrite()
219{
220 return true;
221}
222
223bool Bu::QueueBuf::isReadable()
224{
225 return true;
226}
227
228bool Bu::QueueBuf::isWritable()
229{
230 return true;
231}
232
233bool Bu::QueueBuf::isSeekable()
234{
235 return false;
236}
237
238bool Bu::QueueBuf::isBlocking()
239{
240 return false;
241}
242
243void Bu::QueueBuf::setBlocking( bool )
244{
245}
246
247void Bu::QueueBuf::setSize( Bu::size )
248{
249}
250
251Bu::size Bu::QueueBuf::getSize() const
252{
253 return iTotalSize;
254}
255
256Bu::size Bu::QueueBuf::getBlockSize() const
257{
258 return iBlockSize;
259}
260
261Bu::String Bu::QueueBuf::getLocation() const
262{
263 return "";
264}
265
266void Bu::QueueBuf::addBlock()
267{
268 lBlocks.append( new char[iBlockSize] );
269// sio << "Added new block." << sio.nl;
270}
271
272void Bu::QueueBuf::removeBlock()
273{
274 delete[] lBlocks.first();
275 lBlocks.erase( lBlocks.begin() );
276// sio << "Removed block." << sio.nl;
277}
278