diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-06-27 15:42:50 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-06-27 15:42:50 +0000 |
commit | 5ec9a131e12d021c42b46b601f5e79502485bebb (patch) | |
tree | 31d63d293a6c5bc6ea1d677474380ea48976add4 /src | |
parent | 01ecf54b07e75c17ca5f7039084daeefaea9a1c7 (diff) | |
download | libbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.tar.gz libbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.tar.bz2 libbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.tar.xz libbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.zip |
The MemBuf works just fine, although it still can't over-write data in the
buffer.
Diffstat (limited to '')
-rw-r--r-- | src/membuf.cpp | 30 | ||||
-rw-r--r-- | src/membuf.h | 7 | ||||
-rw-r--r-- | src/unit/entities/unit | 30 | ||||
-rw-r--r-- | src/unit/file.cpp | 10 | ||||
-rw-r--r-- | src/unit/hash.cpp | 4 | ||||
-rw-r--r-- | src/unit/membuf.cpp | 37 |
6 files changed, 113 insertions, 5 deletions
diff --git a/src/membuf.cpp b/src/membuf.cpp index fdb4366..3c394b0 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp | |||
@@ -2,7 +2,8 @@ | |||
2 | 2 | ||
3 | using namespace Bu; | 3 | using namespace Bu; |
4 | 4 | ||
5 | Bu::MemBuf::MemBuf() | 5 | Bu::MemBuf::MemBuf() : |
6 | nPos( 0 ) | ||
6 | { | 7 | { |
7 | } | 8 | } |
8 | 9 | ||
@@ -16,35 +17,56 @@ void Bu::MemBuf::close() | |||
16 | 17 | ||
17 | size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) | 18 | size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) |
18 | { | 19 | { |
20 | if( (size_t)sBuf.getSize()-(size_t)nPos < nBytes ) | ||
21 | nBytes = sBuf.getSize()-nPos; | ||
19 | 22 | ||
23 | memcpy( pBuf, sBuf.getStr()+nPos, nBytes ); | ||
24 | nPos += nBytes; | ||
25 | |||
26 | return nBytes; | ||
20 | } | 27 | } |
21 | 28 | ||
22 | size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) | 29 | size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) |
23 | { | 30 | { |
31 | sBuf.append( (const char *)pBuf, nBytes ); | ||
32 | nPos += nBytes; | ||
33 | return nBytes; | ||
24 | } | 34 | } |
25 | 35 | ||
26 | long Bu::MemBuf::tell() | 36 | long Bu::MemBuf::tell() |
27 | { | 37 | { |
38 | return nPos; | ||
28 | } | 39 | } |
29 | 40 | ||
30 | void Bu::MemBuf::seek( long offset ) | 41 | void Bu::MemBuf::seek( long offset ) |
31 | { | 42 | { |
43 | nPos += offset; | ||
44 | if( nPos < 0 ) nPos = 0; | ||
45 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
32 | } | 46 | } |
33 | 47 | ||
34 | void Bu::MemBuf::setPos( long pos ) | 48 | void Bu::MemBuf::setPos( long pos ) |
35 | { | 49 | { |
50 | nPos = pos; | ||
51 | if( nPos < 0 ) nPos = 0; | ||
52 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
36 | } | 53 | } |
37 | 54 | ||
38 | void Bu::MemBuf::setPosEnd( long pos ) | 55 | void Bu::MemBuf::setPosEnd( long pos ) |
39 | { | 56 | { |
57 | nPos = sBuf.getSize()-pos; | ||
58 | if( nPos < 0 ) nPos = 0; | ||
59 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
40 | } | 60 | } |
41 | 61 | ||
42 | bool Bu::MemBuf::isEOS() | 62 | bool Bu::MemBuf::isEOS() |
43 | { | 63 | { |
64 | return (nPos == sBuf.getSize()); | ||
44 | } | 65 | } |
45 | 66 | ||
46 | bool Bu::MemBuf::isOpen() | 67 | bool Bu::MemBuf::isOpen() |
47 | { | 68 | { |
69 | return true; | ||
48 | } | 70 | } |
49 | 71 | ||
50 | void Bu::MemBuf::flush() | 72 | void Bu::MemBuf::flush() |
@@ -53,26 +75,32 @@ void Bu::MemBuf::flush() | |||
53 | 75 | ||
54 | bool Bu::MemBuf::canRead() | 76 | bool Bu::MemBuf::canRead() |
55 | { | 77 | { |
78 | return !isEOS(); | ||
56 | } | 79 | } |
57 | 80 | ||
58 | bool Bu::MemBuf::canWrite() | 81 | bool Bu::MemBuf::canWrite() |
59 | { | 82 | { |
83 | return isEOS(); | ||
60 | } | 84 | } |
61 | 85 | ||
62 | bool Bu::MemBuf::isReadable() | 86 | bool Bu::MemBuf::isReadable() |
63 | { | 87 | { |
88 | return true; | ||
64 | } | 89 | } |
65 | 90 | ||
66 | bool Bu::MemBuf::isWritable() | 91 | bool Bu::MemBuf::isWritable() |
67 | { | 92 | { |
93 | return true; | ||
68 | } | 94 | } |
69 | 95 | ||
70 | bool Bu::MemBuf::isSeekable() | 96 | bool Bu::MemBuf::isSeekable() |
71 | { | 97 | { |
98 | return true; | ||
72 | } | 99 | } |
73 | 100 | ||
74 | bool Bu::MemBuf::isBlocking() | 101 | bool Bu::MemBuf::isBlocking() |
75 | { | 102 | { |
103 | return true; | ||
76 | } | 104 | } |
77 | 105 | ||
78 | void Bu::MemBuf::setBlocking( bool bBlocking ) | 106 | void Bu::MemBuf::setBlocking( bool bBlocking ) |
diff --git a/src/membuf.h b/src/membuf.h index 2cbbbdc..877b35e 100644 --- a/src/membuf.h +++ b/src/membuf.h | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <stdint.h> | 4 | #include <stdint.h> |
5 | 5 | ||
6 | #include "bu/stream.h" | 6 | #include "bu/stream.h" |
7 | #include "bu/fstring.h" | ||
7 | 8 | ||
8 | namespace Bu | 9 | namespace Bu |
9 | { | 10 | { |
@@ -18,6 +19,12 @@ namespace Bu | |||
18 | 19 | ||
19 | virtual void close(); | 20 | virtual void close(); |
20 | virtual size_t read( void *pBuf, size_t nBytes ); | 21 | virtual size_t read( void *pBuf, size_t nBytes ); |
22 | |||
23 | /** | ||
24 | *@todo Allow writes at the current position, not just appending to | ||
25 | * the current buffer. This is a silly way to do it, but it covers all | ||
26 | * of our bases for now. | ||
27 | */ | ||
21 | virtual size_t write( const void *pBuf, size_t nBytes ); | 28 | virtual size_t write( const void *pBuf, size_t nBytes ); |
22 | virtual long tell(); | 29 | virtual long tell(); |
23 | virtual void seek( long offset ); | 30 | virtual void seek( long offset ); |
diff --git a/src/unit/entities/unit b/src/unit/entities/unit new file mode 100644 index 0000000..28db45f --- /dev/null +++ b/src/unit/entities/unit | |||
@@ -0,0 +1,30 @@ | |||
1 | <?xml version="1.1" ?> | ||
2 | <entity desc="Unit test framework"> | ||
3 | <param name="name" required="yes" desc="Name of the class"/> | ||
4 | <file | ||
5 | name="source" | ||
6 | filename="{=name:%tolower}.cpp" | ||
7 | >#include "bu/unitsuite.h" | ||
8 | |||
9 | class Unit : public Bu::UnitSuite | ||
10 | { | ||
11 | public: | ||
12 | Unit() | ||
13 | { | ||
14 | setName("{=name}"); | ||
15 | addTest( Unit::test01 ); | ||
16 | } | ||
17 | |||
18 | virtual ~Unit() | ||
19 | { | ||
20 | } | ||
21 | |||
22 | void test01() | ||
23 | { | ||
24 | unitTest( 0 == 5 ); | ||
25 | } | ||
26 | }; | ||
27 | |||
28 | int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); } | ||
29 | </file> | ||
30 | </entity> | ||
diff --git a/src/unit/file.cpp b/src/unit/file.cpp index a45b8d7..1eaaf36 100644 --- a/src/unit/file.cpp +++ b/src/unit/file.cpp | |||
@@ -87,8 +87,14 @@ public: | |||
87 | unitTest( sf.isEOS() == false ); | 87 | unitTest( sf.isEOS() == false ); |
88 | try | 88 | try |
89 | { | 89 | { |
90 | sf.read( buf, 5 ); | 90 | if( sf.read( buf, 5 ) > 0 ) |
91 | unitFailed("No exception thrown"); | 91 | { |
92 | unitFailed("Non-zero read result"); | ||
93 | } | ||
94 | else | ||
95 | { | ||
96 | sf.close(); | ||
97 | } | ||
92 | } | 98 | } |
93 | catch( Bu::FileException &e ) | 99 | catch( Bu::FileException &e ) |
94 | { | 100 | { |
diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp index 588e687..9ea933f 100644 --- a/src/unit/hash.cpp +++ b/src/unit/hash.cpp | |||
@@ -1,6 +1,6 @@ | |||
1 | #include "bu/fstring.h" | 1 | #include "bu/fstring.h" |
2 | #include "bu/hash.h" | 2 | #include "bu/hash.h" |
3 | #include "unitsuite.h" | 3 | #include "bu/unitsuite.h" |
4 | 4 | ||
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | 6 | ||
@@ -23,7 +23,7 @@ public: | |||
23 | { | 23 | { |
24 | StrIntHash h; | 24 | StrIntHash h; |
25 | char buf[20]; | 25 | char buf[20]; |
26 | for(int i=0;i<10000;i++) | 26 | for(int i=1;i<10000;i++) |
27 | { | 27 | { |
28 | sprintf(buf,"%d",i); | 28 | sprintf(buf,"%d",i); |
29 | Bu::FString sTmp(buf); | 29 | Bu::FString sTmp(buf); |
diff --git a/src/unit/membuf.cpp b/src/unit/membuf.cpp new file mode 100644 index 0000000..65ba82a --- /dev/null +++ b/src/unit/membuf.cpp | |||
@@ -0,0 +1,37 @@ | |||
1 | #include "bu/unitsuite.h" | ||
2 | #include "bu/membuf.h" | ||
3 | |||
4 | class Unit : public Bu::UnitSuite | ||
5 | { | ||
6 | public: | ||
7 | Unit() | ||
8 | { | ||
9 | setName("MemBuf"); | ||
10 | addTest( Unit::testWriteRead01 ); | ||
11 | } | ||
12 | |||
13 | virtual ~Unit() | ||
14 | { | ||
15 | } | ||
16 | |||
17 | void testWriteRead01() | ||
18 | { | ||
19 | Bu::MemBuf mb; | ||
20 | unitTest( mb.write("ab", 2 ) == 2 ); | ||
21 | unitTest( mb.write("cde", 3 ) == 3 ); | ||
22 | unitTest( mb.write("FG", 2 ) == 2 ); | ||
23 | |||
24 | mb.setPos( 0 ); | ||
25 | |||
26 | char buf[8]; | ||
27 | buf[7] = '\0'; | ||
28 | unitTest( mb.read( buf, 7 ) == 7 ); | ||
29 | unitTest( !strncmp( buf, "abcdeFG", 7 ) ); | ||
30 | unitTest( mb.read( buf, 7 ) == 0 ); | ||
31 | mb.seek( -3 ); | ||
32 | unitTest( mb.read( buf, 7 ) == 3 ); | ||
33 | unitTest( !strncmp( buf, "eFG", 3 ) ); | ||
34 | } | ||
35 | }; | ||
36 | |||
37 | int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); } | ||