diff options
Diffstat (limited to 'src/stable/buffer.cpp')
-rw-r--r-- | src/stable/buffer.cpp | 250 |
1 files changed, 125 insertions, 125 deletions
diff --git a/src/stable/buffer.cpp b/src/stable/buffer.cpp index 16c3e2f..c787499 100644 --- a/src/stable/buffer.cpp +++ b/src/stable/buffer.cpp | |||
@@ -8,26 +8,26 @@ | |||
8 | #include "bu/buffer.h" | 8 | #include "bu/buffer.h" |
9 | 9 | ||
10 | Bu::Buffer::Buffer( Bu::Stream &rNext, int iWhat, int iBufSize ) : | 10 | Bu::Buffer::Buffer( Bu::Stream &rNext, int iWhat, int iBufSize ) : |
11 | Bu::Filter( rNext ), | 11 | Bu::Filter( rNext ), |
12 | sSoFar( 0 ), | 12 | sSoFar( 0 ), |
13 | iBufSize( iBufSize ), | 13 | iBufSize( iBufSize ), |
14 | sReadBuf( NULL ), | 14 | sReadBuf( NULL ), |
15 | sWriteBuf( NULL ), | 15 | sWriteBuf( NULL ), |
16 | iReadBufFill( 0 ), | 16 | iReadBufFill( 0 ), |
17 | iReadPos( 0 ), | 17 | iReadPos( 0 ), |
18 | iWriteBufFill( 0 ), | 18 | iWriteBufFill( 0 ), |
19 | iWritePos( 0 ), | 19 | iWritePos( 0 ), |
20 | iWhat( iWhat ) | 20 | iWhat( iWhat ) |
21 | { | 21 | { |
22 | sReadBuf = new char[iBufSize]; | 22 | sReadBuf = new char[iBufSize]; |
23 | sWriteBuf = new char[iBufSize]; | 23 | sWriteBuf = new char[iBufSize]; |
24 | } | 24 | } |
25 | 25 | ||
26 | Bu::Buffer::~Buffer() | 26 | Bu::Buffer::~Buffer() |
27 | { | 27 | { |
28 | flush(); | 28 | flush(); |
29 | delete[] sReadBuf; | 29 | delete[] sReadBuf; |
30 | delete[] sWriteBuf; | 30 | delete[] sWriteBuf; |
31 | } | 31 | } |
32 | 32 | ||
33 | void Bu::Buffer::start() | 33 | void Bu::Buffer::start() |
@@ -36,135 +36,135 @@ void Bu::Buffer::start() | |||
36 | 36 | ||
37 | Bu::size Bu::Buffer::stop() | 37 | Bu::size Bu::Buffer::stop() |
38 | { | 38 | { |
39 | flush(); | 39 | flush(); |
40 | iReadBufFill = iReadPos = iWriteBufFill = iWritePos = 0; | 40 | iReadBufFill = iReadPos = iWriteBufFill = iWritePos = 0; |
41 | return sSoFar; | 41 | return sSoFar; |
42 | } | 42 | } |
43 | 43 | ||
44 | void Bu::Buffer::fillReadBuf() | 44 | void Bu::Buffer::fillReadBuf() |
45 | { | 45 | { |
46 | if( iReadBufFill+iReadPos < iBufSize ) | 46 | if( iReadBufFill+iReadPos < iBufSize ) |
47 | { | 47 | { |
48 | iReadBufFill += rNext.read( | 48 | iReadBufFill += rNext.read( |
49 | sReadBuf+iReadPos+iReadBufFill, | 49 | sReadBuf+iReadPos+iReadBufFill, |
50 | iBufSize-iReadBufFill-iReadPos | 50 | iBufSize-iReadBufFill-iReadPos |
51 | ); | 51 | ); |
52 | } | 52 | } |
53 | } | 53 | } |
54 | 54 | ||
55 | Bu::size Bu::Buffer::read( void *pBuf, Bu::size nBytes ) | 55 | Bu::size Bu::Buffer::read( void *pBuf, Bu::size nBytes ) |
56 | { | 56 | { |
57 | if( (iWhat&Read) == 0 ) | 57 | if( (iWhat&Read) == 0 ) |
58 | return rNext.read( pBuf, nBytes ); | 58 | return rNext.read( pBuf, nBytes ); |
59 | 59 | ||
60 | if( nBytes <= 0 ) | 60 | if( nBytes <= 0 ) |
61 | { | 61 | { |
62 | fillReadBuf(); | 62 | fillReadBuf(); |
63 | return 0; | 63 | return 0; |
64 | } | 64 | } |
65 | 65 | ||
66 | Bu::size nTotRead = 0; | 66 | Bu::size nTotRead = 0; |
67 | // fillReadBuf(); | 67 | // fillReadBuf(); |
68 | 68 | ||
69 | do | 69 | do |
70 | { | 70 | { |
71 | int iAmnt = nBytes-nTotRead; | 71 | int iAmnt = nBytes-nTotRead; |
72 | if( iAmnt > iReadBufFill ) | 72 | if( iAmnt > iReadBufFill ) |
73 | { | 73 | { |
74 | iAmnt = iReadBufFill; | 74 | iAmnt = iReadBufFill; |
75 | } | 75 | } |
76 | if( iAmnt > 0 ) | 76 | if( iAmnt > 0 ) |
77 | { | 77 | { |
78 | memcpy( ((char *)pBuf)+nTotRead, sReadBuf+iReadPos, iAmnt ); | 78 | memcpy( ((char *)pBuf)+nTotRead, sReadBuf+iReadPos, iAmnt ); |
79 | iReadPos += iAmnt; | 79 | iReadPos += iAmnt; |
80 | nTotRead += iAmnt; | 80 | nTotRead += iAmnt; |
81 | iReadBufFill -= iAmnt; | 81 | iReadBufFill -= iAmnt; |
82 | } | 82 | } |
83 | if( iReadBufFill == 0 ) | 83 | if( iReadBufFill == 0 ) |
84 | { | 84 | { |
85 | iReadPos = 0; | 85 | iReadPos = 0; |
86 | fillReadBuf(); | 86 | fillReadBuf(); |
87 | } | 87 | } |
88 | } | 88 | } |
89 | while( nTotRead < nBytes && iReadBufFill > 0 ); | 89 | while( nTotRead < nBytes && iReadBufFill > 0 ); |
90 | 90 | ||
91 | //printf("Buffer: %db returned, %db remain in buffer.\n", nTotRead, iReadBufFill ); | 91 | //printf("Buffer: %db returned, %db remain in buffer.\n", nTotRead, iReadBufFill ); |
92 | 92 | ||
93 | return nTotRead; | 93 | return nTotRead; |
94 | } | 94 | } |
95 | 95 | ||
96 | Bu::size Bu::Buffer::write( const void *pBuf, Bu::size nBytes ) | 96 | Bu::size Bu::Buffer::write( const void *pBuf, Bu::size nBytes ) |
97 | { | 97 | { |
98 | if( (iWhat&Write) == 0 ) | 98 | if( (iWhat&Write) == 0 ) |
99 | return rNext.write( pBuf, nBytes ); | 99 | return rNext.write( pBuf, nBytes ); |
100 | 100 | ||
101 | Bu::size nTotWrote = 0; | 101 | Bu::size nTotWrote = 0; |
102 | 102 | ||
103 | do | 103 | do |
104 | { | 104 | { |
105 | int iAmnt = nBytes-nTotWrote; | 105 | int iAmnt = nBytes-nTotWrote; |
106 | if( iAmnt > iBufSize-iWritePos-iWriteBufFill ) | 106 | if( iAmnt > iBufSize-iWritePos-iWriteBufFill ) |
107 | { | 107 | { |
108 | iAmnt = iBufSize-iWritePos-iWriteBufFill; | 108 | iAmnt = iBufSize-iWritePos-iWriteBufFill; |
109 | } | 109 | } |
110 | if( iAmnt > 0 ) | 110 | if( iAmnt > 0 ) |
111 | { | 111 | { |
112 | memcpy( | 112 | memcpy( |
113 | sWriteBuf+iWritePos+iWriteBufFill, | 113 | sWriteBuf+iWritePos+iWriteBufFill, |
114 | ((char *)pBuf)+nTotWrote, | 114 | ((char *)pBuf)+nTotWrote, |
115 | iAmnt | 115 | iAmnt |
116 | ); | 116 | ); |
117 | nTotWrote += iAmnt; | 117 | nTotWrote += iAmnt; |
118 | iWriteBufFill += iAmnt; | 118 | iWriteBufFill += iAmnt; |
119 | //printf("Buffer: Moved %db to write buffer, %db filled now.\n", | 119 | //printf("Buffer: Moved %db to write buffer, %db filled now.\n", |
120 | //iAmnt, iWriteBufFill ); | 120 | //iAmnt, iWriteBufFill ); |
121 | } | 121 | } |
122 | while( iWritePos+iWriteBufFill == iBufSize ) | 122 | while( iWritePos+iWriteBufFill == iBufSize ) |
123 | { | 123 | { |
124 | //printf("iWritePos = %d\n", iWritePos ); | 124 | //printf("iWritePos = %d\n", iWritePos ); |
125 | int iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill ); | 125 | int iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill ); |
126 | //printf("Buffer: Wrote %db from buffer to stream, %db filled now.\n", iWr, iWriteBufFill-iWr ); | 126 | //printf("Buffer: Wrote %db from buffer to stream, %db filled now.\n", iWr, iWriteBufFill-iWr ); |
127 | if( iWr == 0 ) | 127 | if( iWr == 0 ) |
128 | { | 128 | { |
129 | return nTotWrote; | 129 | return nTotWrote; |
130 | } | 130 | } |
131 | else if( iWr == iWriteBufFill ) | 131 | else if( iWr == iWriteBufFill ) |
132 | { | 132 | { |
133 | iWritePos = iWriteBufFill = 0; | 133 | iWritePos = iWriteBufFill = 0; |
134 | } | 134 | } |
135 | else | 135 | else |
136 | { | 136 | { |
137 | iWritePos += iWr; | 137 | iWritePos += iWr; |
138 | iWriteBufFill -= iWr; | 138 | iWriteBufFill -= iWr; |
139 | } | 139 | } |
140 | } | 140 | } |
141 | } | 141 | } |
142 | while( nTotWrote < nBytes && iWriteBufFill < iBufSize+iWritePos ); | 142 | while( nTotWrote < nBytes && iWriteBufFill < iBufSize+iWritePos ); |
143 | 143 | ||
144 | return nTotWrote; | 144 | return nTotWrote; |
145 | } | 145 | } |
146 | 146 | ||
147 | void Bu::Buffer::flush() | 147 | void Bu::Buffer::flush() |
148 | { | 148 | { |
149 | if( (iWhat&Write) == 0 ) | 149 | if( (iWhat&Write) == 0 ) |
150 | return rNext.flush(); | 150 | return rNext.flush(); |
151 | 151 | ||
152 | if( iWriteBufFill > 0 ) | 152 | if( iWriteBufFill > 0 ) |
153 | { | 153 | { |
154 | //printf("Buffer: Flushing remaining data, %db.\n", iWriteBufFill ); | 154 | //printf("Buffer: Flushing remaining data, %db.\n", iWriteBufFill ); |
155 | int iWr = 0; | 155 | int iWr = 0; |
156 | do | 156 | do |
157 | { | 157 | { |
158 | iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill ); | 158 | iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill ); |
159 | //printf("Buffer: %db written to stream.\n", iWr ); | 159 | //printf("Buffer: %db written to stream.\n", iWr ); |
160 | iWritePos += iWr; | 160 | iWritePos += iWr; |
161 | iWriteBufFill -= iWr; | 161 | iWriteBufFill -= iWr; |
162 | } while( iWriteBufFill > 0 && iWr > 0 ); | 162 | } while( iWriteBufFill > 0 && iWr > 0 ); |
163 | } | 163 | } |
164 | } | 164 | } |
165 | 165 | ||
166 | bool Bu::Buffer::isEos() | 166 | bool Bu::Buffer::isEos() |
167 | { | 167 | { |
168 | return (iReadPos >= (iReadBufFill-1)) && (rNext.isEos()); | 168 | return (iReadPos >= (iReadBufFill-1)) && (rNext.isEos()); |
169 | } | 169 | } |
170 | 170 | ||