aboutsummaryrefslogtreecommitdiff
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
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 '')
-rw-r--r--src/buffer.cpp120
-rw-r--r--src/buffer.h15
-rw-r--r--src/bzip2.cpp2
-rw-r--r--src/tests/buffer.cpp38
4 files changed, 172 insertions, 3 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
diff --git a/src/buffer.h b/src/buffer.h
index beb4b08..a18be11 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -1,3 +1,10 @@
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#ifndef BU_BUFFER_H 8#ifndef BU_BUFFER_H
2#define BU_BUFFER_H 9#define BU_BUFFER_H
3 10
@@ -18,15 +25,23 @@ namespace Bu
18 virtual size_t write( const void *pBuf, size_t nBytes ); 25 virtual size_t write( const void *pBuf, size_t nBytes );
19 using Stream::write; 26 using Stream::write;
20 27
28 size_t getReadFill() { return iReadBufFill; }
29 bool isWritePending() { return iWriteBufFill > 0; }
30
21 virtual void flush(); 31 virtual void flush();
22 32
23 private: 33 private:
34 void fillReadBuf();
35
36 private:
24 size_t sSoFar; 37 size_t sSoFar;
25 int iBufSize; 38 int iBufSize;
26 char *sReadBuf; 39 char *sReadBuf;
27 char *sWriteBuf; 40 char *sWriteBuf;
28 int iReadBufFill; 41 int iReadBufFill;
42 int iReadPos;
29 int iWriteBufFill; 43 int iWriteBufFill;
44 int iWritePos;
30 }; 45 };
31}; 46};
32 47
diff --git a/src/bzip2.cpp b/src/bzip2.cpp
index 10cfe8a..0a56f83 100644
--- a/src/bzip2.cpp
+++ b/src/bzip2.cpp
@@ -200,7 +200,7 @@ size_t Bu::BZip2::write( const void *pData, size_t nBytes )
200 break; 200 break;
201 } 201 }
202 202
203 return sTotalOut; 203 return nBytes;
204} 204}
205 205
206bool Bu::BZip2::isOpen() 206bool Bu::BZip2::isOpen()
diff --git a/src/tests/buffer.cpp b/src/tests/buffer.cpp
new file mode 100644
index 0000000..a1a1105
--- /dev/null
+++ b/src/tests/buffer.cpp
@@ -0,0 +1,38 @@
1#include "bu/membuf.h"
2#include "bu/buffer.h"
3#include "bu/file.h"
4
5using namespace Bu;
6
7int main( int argc, char *argv[] )
8{
9 argc--,argv++;
10 if( argc == 0 )
11 {
12 MemBuf mOut;
13 Buffer bOut( mOut, 10 );
14
15 for( int j = 0; j < 4; j++ )
16 bOut.write("hi ", 3 );
17
18 printf("Preflush: \"%s\"\n", mOut.getString().getStr() );
19 bOut.flush();
20
21 printf("Final: \"%s\"\n", mOut.getString().getStr() );
22 }
23 else
24 {
25 File fIn( *argv, File::Read );
26 Buffer bIn( fIn, 10 );
27
28 char buf[5];
29 for( int j = 0; j < 5; j++ )
30 {
31 buf[bIn.read( buf, 4 )] = '\0';
32 printf("Four chars: \"%s\"\n", buf );
33 }
34 }
35
36 return 0;
37}
38