diff options
Diffstat (limited to 'src/membuf.cpp')
-rw-r--r-- | src/membuf.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/membuf.cpp b/src/membuf.cpp new file mode 100644 index 0000000..45ff5bd --- /dev/null +++ b/src/membuf.cpp | |||
@@ -0,0 +1,120 @@ | |||
1 | #include "bu/membuf.h" | ||
2 | |||
3 | using namespace Bu; | ||
4 | |||
5 | Bu::MemBuf::MemBuf() : | ||
6 | nPos( 0 ) | ||
7 | { | ||
8 | } | ||
9 | |||
10 | Bu::MemBuf::MemBuf( const Bu::FString &str ) : | ||
11 | sBuf( str ), | ||
12 | nPos( 0 ) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | Bu::MemBuf::~MemBuf() | ||
17 | { | ||
18 | } | ||
19 | |||
20 | void Bu::MemBuf::close() | ||
21 | { | ||
22 | } | ||
23 | |||
24 | size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) | ||
25 | { | ||
26 | if( (size_t)sBuf.getSize()-(size_t)nPos < nBytes ) | ||
27 | nBytes = sBuf.getSize()-nPos; | ||
28 | |||
29 | memcpy( pBuf, sBuf.getStr()+nPos, nBytes ); | ||
30 | nPos += nBytes; | ||
31 | |||
32 | return nBytes; | ||
33 | } | ||
34 | |||
35 | size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) | ||
36 | { | ||
37 | sBuf.append( (const char *)pBuf, nBytes ); | ||
38 | nPos += nBytes; | ||
39 | return nBytes; | ||
40 | } | ||
41 | |||
42 | long Bu::MemBuf::tell() | ||
43 | { | ||
44 | return nPos; | ||
45 | } | ||
46 | |||
47 | void Bu::MemBuf::seek( long offset ) | ||
48 | { | ||
49 | nPos += offset; | ||
50 | if( nPos < 0 ) nPos = 0; | ||
51 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
52 | } | ||
53 | |||
54 | void Bu::MemBuf::setPos( long pos ) | ||
55 | { | ||
56 | nPos = pos; | ||
57 | if( nPos < 0 ) nPos = 0; | ||
58 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
59 | } | ||
60 | |||
61 | void Bu::MemBuf::setPosEnd( long pos ) | ||
62 | { | ||
63 | nPos = sBuf.getSize()-pos; | ||
64 | if( nPos < 0 ) nPos = 0; | ||
65 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
66 | } | ||
67 | |||
68 | bool Bu::MemBuf::isEOS() | ||
69 | { | ||
70 | return (nPos == sBuf.getSize()); | ||
71 | } | ||
72 | |||
73 | bool Bu::MemBuf::isOpen() | ||
74 | { | ||
75 | return true; | ||
76 | } | ||
77 | |||
78 | void Bu::MemBuf::flush() | ||
79 | { | ||
80 | } | ||
81 | |||
82 | bool Bu::MemBuf::canRead() | ||
83 | { | ||
84 | return !isEOS(); | ||
85 | } | ||
86 | |||
87 | bool Bu::MemBuf::canWrite() | ||
88 | { | ||
89 | return isEOS(); | ||
90 | } | ||
91 | |||
92 | bool Bu::MemBuf::isReadable() | ||
93 | { | ||
94 | return true; | ||
95 | } | ||
96 | |||
97 | bool Bu::MemBuf::isWritable() | ||
98 | { | ||
99 | return true; | ||
100 | } | ||
101 | |||
102 | bool Bu::MemBuf::isSeekable() | ||
103 | { | ||
104 | return true; | ||
105 | } | ||
106 | |||
107 | bool Bu::MemBuf::isBlocking() | ||
108 | { | ||
109 | return true; | ||
110 | } | ||
111 | |||
112 | void Bu::MemBuf::setBlocking( bool bBlocking ) | ||
113 | { | ||
114 | } | ||
115 | |||
116 | Bu::FString &Bu::MemBuf::getString() | ||
117 | { | ||
118 | return sBuf; | ||
119 | } | ||
120 | |||