diff options
-rw-r--r-- | src/nullstream.cpp | 114 | ||||
-rw-r--r-- | src/nullstream.h | 19 |
2 files changed, 113 insertions, 20 deletions
diff --git a/src/nullstream.cpp b/src/nullstream.cpp index 364b58a..40c636c 100644 --- a/src/nullstream.cpp +++ b/src/nullstream.cpp | |||
@@ -1,6 +1,8 @@ | |||
1 | #include "bu/nullstream.h" | 1 | #include "bu/nullstream.h" |
2 | 2 | ||
3 | Bu::NullStream::NullStream() | 3 | Bu::NullStream::NullStream() : |
4 | sRead( 0 ), | ||
5 | sWrote( 0 ) | ||
4 | { | 6 | { |
5 | } | 7 | } |
6 | 8 | ||
@@ -8,24 +10,96 @@ Bu::NullStream::~NullStream() | |||
8 | { | 10 | { |
9 | } | 11 | } |
10 | 12 | ||
11 | void Bu::NullStream::close(); | 13 | void Bu::NullStream::close() |
12 | size_t Bu::NullStream::read( void *pBuf, size_t nBytes ); | 14 | { |
13 | Bu::FString Bu::NullStream::readLine(); | 15 | sRead = sWrote = 0; |
14 | size_t Bu::NullStream::write( const void *pBuf, size_t nBytes ); | 16 | } |
15 | long Bu::NullStream::tell(); | 17 | |
16 | void Bu::NullStream::seek( long offset ); | 18 | size_t Bu::NullStream::read( void *pBuf, size_t nBytes ) |
17 | void Bu::NullStream::setPos( long pos ); | 19 | { |
18 | void Bu::NullStream::setPosEnd( long pos ); | 20 | memset( pBuf, 0, nBytes ); |
19 | bool Bu::NullStream::isEos(); | 21 | sRead += nBytes; |
20 | bool Bu::NullStream::isOpen(); | 22 | return nBytes; |
21 | void Bu::NullStream::flush(); | 23 | } |
22 | bool Bu::NullStream::canRead(); | 24 | |
23 | bool Bu::NullStream::canWrite(); | 25 | Bu::FString Bu::NullStream::readLine() |
24 | bool Bu::NullStream::isReadable(); | 26 | { |
25 | bool Bu::NullStream::isWritable(); | 27 | sRead++; |
26 | bool Bu::NullStream::isSeekable(); | 28 | return Bu::FString("\0", 1 ); |
27 | bool Bu::NullStream::isBlocking(); | 29 | } |
28 | void Bu::NullStream::setBlocking( bool bBlocking=true ); | 30 | |
29 | void Bu::NullStream::setSize( long iSize ); | 31 | size_t Bu::NullStream::write( const void *, size_t nBytes ) |
32 | { | ||
33 | sWrote += nBytes; | ||
34 | return nBytes; | ||
35 | } | ||
36 | |||
37 | long Bu::NullStream::tell() | ||
38 | { | ||
39 | return sRead + sWrote; | ||
40 | } | ||
30 | 41 | ||
42 | void Bu::NullStream::seek( long ) | ||
43 | { | ||
44 | } | ||
45 | |||
46 | void Bu::NullStream::setPos( long ) | ||
47 | { | ||
48 | } | ||
49 | |||
50 | void Bu::NullStream::setPosEnd( long ) | ||
51 | { | ||
52 | } | ||
53 | |||
54 | bool Bu::NullStream::isEos() | ||
55 | { | ||
56 | return false; | ||
57 | } | ||
58 | |||
59 | bool Bu::NullStream::isOpen() | ||
60 | { | ||
61 | return true; | ||
62 | } | ||
63 | |||
64 | void Bu::NullStream::flush() | ||
65 | { | ||
66 | } | ||
67 | |||
68 | bool Bu::NullStream::canRead() | ||
69 | { | ||
70 | return true; | ||
71 | } | ||
72 | |||
73 | bool Bu::NullStream::canWrite() | ||
74 | { | ||
75 | return true; | ||
76 | } | ||
77 | |||
78 | bool Bu::NullStream::isReadable() | ||
79 | { | ||
80 | return true; | ||
81 | } | ||
82 | |||
83 | bool Bu::NullStream::isWritable() | ||
84 | { | ||
85 | return true; | ||
86 | } | ||
87 | |||
88 | bool Bu::NullStream::isSeekable() | ||
89 | { | ||
90 | return false; | ||
91 | } | ||
92 | |||
93 | bool Bu::NullStream::isBlocking() | ||
94 | { | ||
95 | return true; | ||
96 | } | ||
97 | |||
98 | void Bu::NullStream::setBlocking( bool ) | ||
99 | { | ||
100 | } | ||
101 | |||
102 | void Bu::NullStream::setSize( long ) | ||
103 | { | ||
104 | } | ||
31 | 105 | ||
diff --git a/src/nullstream.h b/src/nullstream.h index 5440af6..1537ffb 100644 --- a/src/nullstream.h +++ b/src/nullstream.h | |||
@@ -5,6 +5,18 @@ | |||
5 | 5 | ||
6 | namespace Bu | 6 | namespace Bu |
7 | { | 7 | { |
8 | /** | ||
9 | * Works a lot like /dev/null on *nix style systems. This class allows | ||
10 | * infinite reading and writing. All operatorns "succeed" even if they | ||
11 | * don't seem to do anything. This is great for testing writing code or | ||
12 | * doing dry runs. When reading, it will produce NULL bytes, so any | ||
13 | * application that would like the ability to produce null streams as a | ||
14 | * snap-in replacement for any other Bu::Stream, this is the right option. | ||
15 | * | ||
16 | * As an added feature, the NullStream will track how many bytes it was | ||
17 | * asked to read and write, allowing you to use it to determine how many | ||
18 | * bytes a write opretion would use without actually writing anything. | ||
19 | */ | ||
8 | class NullStream : public Bu::Stream | 20 | class NullStream : public Bu::Stream |
9 | { | 21 | { |
10 | public: | 22 | public: |
@@ -31,6 +43,13 @@ namespace Bu | |||
31 | virtual bool isBlocking(); | 43 | virtual bool isBlocking(); |
32 | virtual void setBlocking( bool bBlocking=true ); | 44 | virtual void setBlocking( bool bBlocking=true ); |
33 | virtual void setSize( long iSize ); | 45 | virtual void setSize( long iSize ); |
46 | |||
47 | size_t getBytesRead() { return sRead; } | ||
48 | size_t getByetsWritten() { return sWrote; } | ||
49 | |||
50 | private: | ||
51 | size_t sRead; | ||
52 | size_t sWrote; | ||
34 | }; | 53 | }; |
35 | }; | 54 | }; |
36 | 55 | ||