summaryrefslogtreecommitdiff
path: root/src/stable/membuf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/membuf.cpp')
-rw-r--r--src/stable/membuf.cpp177
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
10using namespace Bu;
11
12Bu::MemBuf::MemBuf() :
13 nPos( 0 )
14{
15}
16
17Bu::MemBuf::MemBuf( const Bu::String &str ) :
18 sBuf( str ),
19 nPos( 0 )
20{
21}
22
23Bu::MemBuf::~MemBuf()
24{
25}
26
27void Bu::MemBuf::close()
28{
29}
30
31size 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
42size 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
69size Bu::MemBuf::tell()
70{
71 return nPos;
72}
73
74void 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
81void 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
88void 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
95bool Bu::MemBuf::isEos()
96{
97 return (nPos == sBuf.getSize());
98}
99
100bool Bu::MemBuf::isOpen()
101{
102 return true;
103}
104
105void Bu::MemBuf::flush()
106{
107}
108
109bool Bu::MemBuf::canRead()
110{
111 return !isEos();
112}
113
114bool Bu::MemBuf::canWrite()
115{
116 return true;
117}
118
119bool Bu::MemBuf::isReadable()
120{
121 return true;
122}
123
124bool Bu::MemBuf::isWritable()
125{
126 return true;
127}
128
129bool Bu::MemBuf::isSeekable()
130{
131 return true;
132}
133
134bool Bu::MemBuf::isBlocking()
135{
136 return true;
137}
138
139void Bu::MemBuf::setBlocking( bool )
140{
141}
142
143void 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
152Bu::size Bu::MemBuf::getSize() const
153{
154 return sBuf.getSize();
155}
156
157Bu::size Bu::MemBuf::getBlockSize() const
158{
159 return sBuf.getSize();
160}
161
162Bu::String Bu::MemBuf::getLocation() const
163{
164 return "";
165}
166
167Bu::String &Bu::MemBuf::getString()
168{
169 return sBuf;
170}
171
172void Bu::MemBuf::setString( const Bu::String &sNewData )
173{
174 sBuf = sNewData;
175 nPos = 0;
176}
177