diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-11-02 06:14:41 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-11-02 06:14:41 +0000 |
commit | d1e486136db52b466b788c1d81a5c88b6f24acd9 (patch) | |
tree | 691724f082670407d386ab6f1e489ee766b592c9 /src | |
parent | 73cb610641b9d69aea0f75b6ccd26236e6c8c3fc (diff) | |
download | libbu++-d1e486136db52b466b788c1d81a5c88b6f24acd9.tar.gz libbu++-d1e486136db52b466b788c1d81a5c88b6f24acd9.tar.bz2 libbu++-d1e486136db52b466b788c1d81a5c88b6f24acd9.tar.xz libbu++-d1e486136db52b466b788c1d81a5c88b6f24acd9.zip |
Added the SBuffer class, a memory buffer that can be used like a stream. Yay!
Diffstat (limited to '')
-rw-r--r-- | src/sbuffer.cpp | 62 | ||||
-rw-r--r-- | src/sbuffer.h | 34 | ||||
-rw-r--r-- | src/tests/sbuffer.cpp | 27 |
3 files changed, 123 insertions, 0 deletions
diff --git a/src/sbuffer.cpp b/src/sbuffer.cpp new file mode 100644 index 0000000..00324b5 --- /dev/null +++ b/src/sbuffer.cpp | |||
@@ -0,0 +1,62 @@ | |||
1 | #include <string.h> | ||
2 | #include "sbuffer.h" | ||
3 | |||
4 | SBuffer::SBuffer() : | ||
5 | bOpen( true ), | ||
6 | nPos( 0 ) | ||
7 | { | ||
8 | } | ||
9 | |||
10 | SBuffer::~SBuffer() | ||
11 | { | ||
12 | } | ||
13 | |||
14 | void SBuffer::close() | ||
15 | { | ||
16 | bOpen = false; | ||
17 | fbData.clearData(); | ||
18 | } | ||
19 | |||
20 | size_t SBuffer::read( char *pBuf, size_t nBytes ) | ||
21 | { | ||
22 | long nLeft = fbData.getLength() - nPos; | ||
23 | if( nBytes > nLeft ) | ||
24 | nBytes = nLeft; | ||
25 | |||
26 | if( nLeft == 0 ) | ||
27 | return 0; | ||
28 | |||
29 | memcpy( pBuf, fbData.getData()+nPos, nBytes ); | ||
30 | nPos += nBytes; | ||
31 | |||
32 | return nBytes; | ||
33 | } | ||
34 | |||
35 | size_t SBuffer::write( const char *pBuf, size_t nBytes ) | ||
36 | { | ||
37 | fbData.appendData( pBuf, nBytes ); | ||
38 | nPos += nBytes; | ||
39 | |||
40 | return nBytes; | ||
41 | } | ||
42 | |||
43 | long SBuffer::tell() | ||
44 | { | ||
45 | return nPos; | ||
46 | } | ||
47 | |||
48 | void SBuffer::seek( long offset ) | ||
49 | { | ||
50 | nPos += offset; | ||
51 | } | ||
52 | |||
53 | void SBuffer::setPos( long pos ) | ||
54 | { | ||
55 | nPos = pos; | ||
56 | } | ||
57 | |||
58 | void SBuffer::setPosEnd( long pos ) | ||
59 | { | ||
60 | nPos = fbData.getLength() - pos; | ||
61 | } | ||
62 | |||
diff --git a/src/sbuffer.h b/src/sbuffer.h new file mode 100644 index 0000000..1dda33a --- /dev/null +++ b/src/sbuffer.h | |||
@@ -0,0 +1,34 @@ | |||
1 | #ifndef S_BUFFER_H | ||
2 | #define S_BUFFER_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | |||
6 | #include "stream.h" | ||
7 | #include "flexbuf.h" | ||
8 | |||
9 | class SBuffer : public Stream | ||
10 | { | ||
11 | public: | ||
12 | SBuffer(); | ||
13 | virtual ~SBuffer(); | ||
14 | |||
15 | virtual void close(); | ||
16 | virtual size_t read( char *pBuf, size_t nBytes ); | ||
17 | |||
18 | /** | ||
19 | *@todo Update this to write at nPos, not just append data. | ||
20 | */ | ||
21 | virtual size_t write( const char *pBuf, size_t nBytes ); | ||
22 | |||
23 | virtual long tell(); | ||
24 | virtual void seek( long offset ); | ||
25 | virtual void setPos( long pos ); | ||
26 | virtual void setPosEnd( long pos ); | ||
27 | |||
28 | private: | ||
29 | long nPos; | ||
30 | bool bOpen; | ||
31 | FlexBuf fbData; | ||
32 | }; | ||
33 | |||
34 | #endif | ||
diff --git a/src/tests/sbuffer.cpp b/src/tests/sbuffer.cpp new file mode 100644 index 0000000..d8bbe4d --- /dev/null +++ b/src/tests/sbuffer.cpp | |||
@@ -0,0 +1,27 @@ | |||
1 | #include "sbuffer.h" | ||
2 | |||
3 | int main() | ||
4 | { | ||
5 | SBuffer buf; | ||
6 | |||
7 | buf.write("abcdefg", 7 ); | ||
8 | |||
9 | printf("tell: %d\n", buf.tell() ); | ||
10 | |||
11 | char abuf[6]; | ||
12 | int nRead; | ||
13 | nRead = buf.read( abuf, 5 ); | ||
14 | abuf[nRead] = '\0'; | ||
15 | printf("Read %d bytes \"%s\"\n", nRead, abuf ); | ||
16 | |||
17 | buf.setPos( 0 ); | ||
18 | nRead = buf.read( abuf, 5 ); | ||
19 | abuf[nRead] = '\0'; | ||
20 | printf("Read %d bytes \"%s\"\n", nRead, abuf ); | ||
21 | |||
22 | nRead = buf.read( abuf, 5 ); | ||
23 | abuf[nRead] = '\0'; | ||
24 | printf("Read %d bytes \"%s\"\n", nRead, abuf ); | ||
25 | |||
26 | } | ||
27 | |||