aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-07-30 17:05:47 +0000
committerMike Buland <eichlan@xagasoft.com>2009-07-30 17:05:47 +0000
commit3f0f30442de297859cccc18f28db83b4e755d36f (patch)
tree6e9fdd9ff6febcdd87779f7663fecc0388e0dd73 /src/buffer.cpp
parent8551cdd3bbf66d8b7b0fea8aa1e0fbddd86bca47 (diff)
downloadlibbu++-3f0f30442de297859cccc18f28db83b4e755d36f.tar.gz
libbu++-3f0f30442de297859cccc18f28db83b4e755d36f.tar.bz2
libbu++-3f0f30442de297859cccc18f28db83b4e755d36f.tar.xz
libbu++-3f0f30442de297859cccc18f28db83b4e755d36f.zip
Bu::Buffer actually works, and works really well. I dig it. Bu::BZip2 now
follows the new filter guidelines, where read and write report the amount of data consumed, not the amount processed. I.e. when writing, it reports how much of your incoming data it used, not how many bytes it wrote on the other end.
Diffstat (limited to 'src/buffer.cpp')
-rw-r--r--src/buffer.cpp120
1 files changed, 118 insertions, 2 deletions
diff --git a/src/buffer.cpp b/src/buffer.cpp
index fef64b1..5e7fb9a 100644
--- a/src/buffer.cpp
+++ b/src/buffer.cpp
@@ -1,18 +1,31 @@
1/*
2 * Copyright (C) 2007-2008 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
1#include "bu/buffer.h" 8#include "bu/buffer.h"
2 9
3Bu::Buffer::Buffer( Bu::Stream &rNext, int iBufsize ) : 10Bu::Buffer::Buffer( Bu::Stream &rNext, int iBufSize ) :
4 Bu::Filter( rNext ), 11 Bu::Filter( rNext ),
5 sSoFar( 0 ), 12 sSoFar( 0 ),
6 iBufSize( iBufSize ), 13 iBufSize( iBufSize ),
7 sReadBuf( NULL ), 14 sReadBuf( NULL ),
8 sWriteBuf( NULL ), 15 sWriteBuf( NULL ),
9 iReadBufFill( 0 ), 16 iReadBufFill( 0 ),
10 iWriteBufFill( 0 ) 17 iReadPos( 0 ),
18 iWriteBufFill( 0 ),
19 iWritePos( 0 )
11{ 20{
21 sReadBuf = new char[iBufSize];
22 sWriteBuf = new char[iBufSize];
12} 23}
13 24
14Bu::Buffer::~Buffer() 25Bu::Buffer::~Buffer()
15{ 26{
27 delete[] sReadBuf;
28 delete[] sWriteBuf;
16} 29}
17 30
18void Bu::Buffer::start() 31void Bu::Buffer::start()
@@ -23,15 +36,118 @@ size_t Bu::Buffer::stop()
23{ 36{
24} 37}
25 38
39void Bu::Buffer::fillReadBuf()
40{
41 if( iReadBufFill+iReadPos < iBufSize )
42 {
43 printf("Buffer: Attempting to read %db.\n", iBufSize-iReadBufFill-iReadPos );
44 iReadBufFill += rNext.read(
45 sReadBuf+iReadPos+iReadBufFill,
46 iBufSize-iReadBufFill-iReadPos
47 );
48 printf("Buffer: Read from stream, %db now in buffer.\n", iReadBufFill );
49 }
50}
51
26size_t Bu::Buffer::read( void *pBuf, size_t nBytes ) 52size_t Bu::Buffer::read( void *pBuf, size_t nBytes )
27{ 53{
54 if( nBytes <= 0 )
55 {
56 fillReadBuf();
57 return 0;
58 }
59
60 size_t nTotRead = 0;
61// fillReadBuf();
62
63 do
64 {
65 int iAmnt = nBytes-nTotRead;
66 if( iAmnt > iReadBufFill )
67 {
68 iAmnt = iReadBufFill;
69 }
70 if( iAmnt > 0 )
71 {
72 memcpy( ((char *)pBuf)+nTotRead, sReadBuf+iReadPos, iAmnt );
73 iReadPos += iAmnt;
74 nTotRead += iAmnt;
75 iReadBufFill -= iAmnt;
76 }
77 if( iReadBufFill == 0 )
78 {
79 iReadPos = 0;
80 fillReadBuf();
81 }
82 }
83 while( nTotRead < nBytes && iReadBufFill > 0 );
84
85 printf("Buffer: %db returned, %db remain in buffer.\n", nTotRead, iReadBufFill );
86
87 return nTotRead;
28} 88}
29 89
30size_t Bu::Buffer::write( const void *pBuf, size_t nBytes ) 90size_t Bu::Buffer::write( const void *pBuf, size_t nBytes )
31{ 91{
92 size_t nTotWrote = 0;
93
94 do
95 {
96 int iAmnt = nBytes-nTotWrote;
97 if( iAmnt > iBufSize-iWritePos-iWriteBufFill )
98 {
99 iAmnt = iBufSize-iWritePos-iWriteBufFill;
100 }
101 if( iAmnt > 0 )
102 {
103 memcpy(
104 sWriteBuf+iWritePos+iWriteBufFill,
105 ((char *)pBuf)+nTotWrote,
106 iAmnt
107 );
108 nTotWrote += iAmnt;
109 iWriteBufFill += iAmnt;
110 printf("Buffer: Moved %db to write buffer, %db filled now.\n",
111 iAmnt, iWriteBufFill );
112 }
113 while( iWritePos+iWriteBufFill == iBufSize )
114 {
115 printf("iWritePos = %d\n", iWritePos );
116 int iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill );
117 printf("Buffer: Wrote %db from buffer to stream, %db filled now.\n", iWr, iWriteBufFill-iWr );
118 if( iWr == 0 )
119 {
120 return nTotWrote;
121 }
122 else if( iWr == iWriteBufFill )
123 {
124 iWritePos = iWriteBufFill = 0;
125 }
126 else
127 {
128 iWritePos += iWr;
129 iWriteBufFill -= iWr;
130 }
131 }
132 }
133 while( nTotWrote < nBytes && iWriteBufFill < iBufSize+iWritePos );
134
135 return nTotWrote;
32} 136}
33 137
34void Bu::Buffer::flush() 138void Bu::Buffer::flush()
35{ 139{
140 if( iWriteBufFill > 0 )
141 {
142 printf("Buffer: Flushing remaining data, %db.\n", iWriteBufFill );
143 int iWr = 0;
144 do
145 {
146 iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill );
147 printf("Buffer: %db written to stream.\n", iWr );
148 iWritePos += iWr;
149 iWriteBufFill -= iWr;
150 } while( iWriteBufFill > 0 && iWr > 0 );
151 }
36} 152}
37 153