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