diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
commit | 469bbcf0701e1eb8a6670c23145b0da87357e178 (patch) | |
tree | b5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/stable/membuf.cpp | |
parent | ee1b79396076edc4e30aefb285fada03bb45e80d (diff) | |
download | libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2 libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip |
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/stable/membuf.cpp')
-rw-r--r-- | src/stable/membuf.cpp | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/src/stable/membuf.cpp b/src/stable/membuf.cpp new file mode 100644 index 0000000..14d0d58 --- /dev/null +++ b/src/stable/membuf.cpp | |||
@@ -0,0 +1,177 @@ | |||
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/membuf.h" | ||
9 | |||
10 | using namespace Bu; | ||
11 | |||
12 | Bu::MemBuf::MemBuf() : | ||
13 | nPos( 0 ) | ||
14 | { | ||
15 | } | ||
16 | |||
17 | Bu::MemBuf::MemBuf( const Bu::String &str ) : | ||
18 | sBuf( str ), | ||
19 | nPos( 0 ) | ||
20 | { | ||
21 | } | ||
22 | |||
23 | Bu::MemBuf::~MemBuf() | ||
24 | { | ||
25 | } | ||
26 | |||
27 | void Bu::MemBuf::close() | ||
28 | { | ||
29 | } | ||
30 | |||
31 | size Bu::MemBuf::read( void *pBuf, size nBytes ) | ||
32 | { | ||
33 | if( (size)sBuf.getSize()-(size)nPos < nBytes ) | ||
34 | nBytes = sBuf.getSize()-nPos; | ||
35 | |||
36 | memcpy( pBuf, sBuf.getStr()+nPos, nBytes ); | ||
37 | nPos += nBytes; | ||
38 | |||
39 | return nBytes; | ||
40 | } | ||
41 | |||
42 | size Bu::MemBuf::write( const void *pBuf, size nBytes ) | ||
43 | { | ||
44 | if( nPos == sBuf.getSize() ) | ||
45 | { | ||
46 | // Easiest, just append the data. | ||
47 | sBuf.append( (const char *)pBuf, nBytes ); | ||
48 | nPos += nBytes; | ||
49 | return nBytes; | ||
50 | } | ||
51 | else | ||
52 | { | ||
53 | // Trickier, we must do this in two parts, overwrite, then append | ||
54 | // Frist, overwrite. | ||
55 | size iOver = sBuf.getSize() - nPos; | ||
56 | if( iOver > nBytes ) | ||
57 | iOver = nBytes; | ||
58 | memcpy( sBuf.getStr()+nPos, pBuf, iOver ); | ||
59 | // Then append | ||
60 | if( iOver < nBytes ) | ||
61 | { | ||
62 | sBuf.append( ((const char *)pBuf)+iOver, nBytes-iOver ); | ||
63 | } | ||
64 | nPos += nBytes; | ||
65 | return nBytes; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | size Bu::MemBuf::tell() | ||
70 | { | ||
71 | return nPos; | ||
72 | } | ||
73 | |||
74 | void Bu::MemBuf::seek( size offset ) | ||
75 | { | ||
76 | nPos += offset; | ||
77 | if( nPos < 0 ) nPos = 0; | ||
78 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
79 | } | ||
80 | |||
81 | void Bu::MemBuf::setPos( size pos ) | ||
82 | { | ||
83 | nPos = pos; | ||
84 | if( nPos < 0 ) nPos = 0; | ||
85 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
86 | } | ||
87 | |||
88 | void Bu::MemBuf::setPosEnd( size pos ) | ||
89 | { | ||
90 | nPos = sBuf.getSize()-pos; | ||
91 | if( nPos < 0 ) nPos = 0; | ||
92 | else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); | ||
93 | } | ||
94 | |||
95 | bool Bu::MemBuf::isEos() | ||
96 | { | ||
97 | return (nPos == sBuf.getSize()); | ||
98 | } | ||
99 | |||
100 | bool Bu::MemBuf::isOpen() | ||
101 | { | ||
102 | return true; | ||
103 | } | ||
104 | |||
105 | void Bu::MemBuf::flush() | ||
106 | { | ||
107 | } | ||
108 | |||
109 | bool Bu::MemBuf::canRead() | ||
110 | { | ||
111 | return !isEos(); | ||
112 | } | ||
113 | |||
114 | bool Bu::MemBuf::canWrite() | ||
115 | { | ||
116 | return true; | ||
117 | } | ||
118 | |||
119 | bool Bu::MemBuf::isReadable() | ||
120 | { | ||
121 | return true; | ||
122 | } | ||
123 | |||
124 | bool Bu::MemBuf::isWritable() | ||
125 | { | ||
126 | return true; | ||
127 | } | ||
128 | |||
129 | bool Bu::MemBuf::isSeekable() | ||
130 | { | ||
131 | return true; | ||
132 | } | ||
133 | |||
134 | bool Bu::MemBuf::isBlocking() | ||
135 | { | ||
136 | return true; | ||
137 | } | ||
138 | |||
139 | void Bu::MemBuf::setBlocking( bool ) | ||
140 | { | ||
141 | } | ||
142 | |||
143 | void Bu::MemBuf::setSize( size iSize ) | ||
144 | { | ||
145 | if( iSize < 0 ) | ||
146 | iSize = 0; | ||
147 | sBuf.setSize( iSize ); | ||
148 | if( nPos > iSize ) | ||
149 | nPos = iSize; | ||
150 | } | ||
151 | |||
152 | Bu::size Bu::MemBuf::getSize() const | ||
153 | { | ||
154 | return sBuf.getSize(); | ||
155 | } | ||
156 | |||
157 | Bu::size Bu::MemBuf::getBlockSize() const | ||
158 | { | ||
159 | return sBuf.getSize(); | ||
160 | } | ||
161 | |||
162 | Bu::String Bu::MemBuf::getLocation() const | ||
163 | { | ||
164 | return ""; | ||
165 | } | ||
166 | |||
167 | Bu::String &Bu::MemBuf::getString() | ||
168 | { | ||
169 | return sBuf; | ||
170 | } | ||
171 | |||
172 | void Bu::MemBuf::setString( const Bu::String &sNewData ) | ||
173 | { | ||
174 | sBuf = sNewData; | ||
175 | nPos = 0; | ||
176 | } | ||
177 | |||