summaryrefslogtreecommitdiff
path: root/src/unstable/myriadstream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/myriadstream.cpp')
-rw-r--r--src/unstable/myriadstream.cpp309
1 files changed, 309 insertions, 0 deletions
diff --git a/src/unstable/myriadstream.cpp b/src/unstable/myriadstream.cpp
new file mode 100644
index 0000000..e6e94bc
--- /dev/null
+++ b/src/unstable/myriadstream.cpp
@@ -0,0 +1,309 @@
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/myriadstream.h"
9
10#include <string.h>
11
12// #define MYRIAD_STREAM_DEBUG 1
13
14#ifdef MYRIAD_STREAM_DEBUG
15#include "bu/sio.h"
16
17using Bu::sio;
18using Bu::Fmt;
19#endif
20
21Bu::MyriadStream::MyriadStream( Bu::Myriad &rMyriad,
22 Bu::Myriad::Stream *pStream ) :
23 rMyriad( rMyriad ),
24 pStream( pStream ),
25 pCurBlock( NULL ),
26 iPos( 0 )
27{
28#ifdef MYRIAD_STREAM_DEBUG
29 sio << "MyriadStream: " << __LINE__ << ": Created, iId=" << pStream->iId << ", iSize="
30 << pStream->iSize << sio.nl;
31#endif
32 //pCurBlock = rMyriad.newBlock();
33 //rMyriad.getBlock( uStream, pCurBlock );
34 //uSize = pCurBlock->uBytesUsed;
35}
36
37Bu::MyriadStream::~MyriadStream()
38{
39 if( pCurBlock )
40 rMyriad.releaseBlock( pCurBlock );
41 //rMyriad.updateStreamSize( uStream, uSize );
42 //rMyriad.deleteBlock( pCurBlock );
43}
44
45void Bu::MyriadStream::close()
46{
47}
48
49Bu::size Bu::MyriadStream::read( void *pBuf, Bu::size nBytes )
50{
51#ifdef MYRIAD_STREAM_DEBUG
52 sio << "MyriadStream: read: " << __LINE__ << ": Started, asked to read " << nBytes << "b."
53 << sio.nl;
54#endif
55 if( nBytes > (Bu::size)pStream->iSize-iPos )
56 nBytes = pStream->iSize-iPos;
57 if( nBytes <= 0 )
58 return 0;
59 int iLeft = nBytes;
60#ifdef MYRIAD_STREAM_DEBUG
61 sio << "MyriadStream: read: " << __LINE__ << ": Started, going to read " << nBytes << "b."
62 << sio.nl;
63#endif
64 if( pCurBlock == NULL )
65 {
66#ifdef MYRIAD_STREAM_DEBUG
67 sio << "MyriadStream: read: " << __LINE__ << ": No block loaded, loading initial block."
68 << sio.nl;
69#endif
70 pCurBlock = rMyriad.getBlock(
71 pStream->aBlocks[iPos/rMyriad.iBlockSize]
72 );
73 }
74 while( iLeft > 0 )
75 {
76 int iCurBlock = pStream->aBlocks[iPos/rMyriad.iBlockSize];
77 if( pCurBlock->iBlockIndex != iCurBlock )
78 {
79#ifdef MYRIAD_STREAM_DEBUG
80 sio << "MyriadStream: read: " << __LINE__ << ": Loading new block " << iCurBlock << "."
81 << sio.nl;
82#endif
83 rMyriad.releaseBlock( pCurBlock );
84 pCurBlock = rMyriad.getBlock( iCurBlock );
85 }
86
87 int iAmnt = Bu::min(
88 Bu::min(
89 rMyriad.iBlockSize - iPos%rMyriad.iBlockSize,
90 iLeft
91 ),
92 pStream->iSize-iPos
93 );
94#ifdef MYRIAD_STREAM_DEBUG
95 sio << "MyriadStream: read: " << __LINE__ << ": Copying out bytes: "
96 << iPos << "(" << (iPos%rMyriad.iBlockSize) << ")+"
97 << iAmnt
98 << ", " << iLeft << "b left." << sio.nl;
99#endif
100 memcpy(
101 pBuf,
102 pCurBlock->pData+(iPos%rMyriad.iBlockSize),
103 iAmnt
104 );
105 iPos += iAmnt;
106 pBuf = &((char *)pBuf)[iAmnt];
107 iLeft -= iAmnt;
108 }
109 return nBytes;
110}
111
112Bu::size Bu::MyriadStream::write( const void *pBuf, Bu::size nBytes )
113{
114 if( nBytes <= 0 )
115 return 0;
116
117#ifdef MYRIAD_STREAM_DEBUG
118 sio << "MyriadStream: write: " << __LINE__ << ": Started, asked to write " << nBytes << "b."
119 << sio.nl;
120#endif
121 if( nBytes <= 0 )
122 return 0;
123 int iLeft = nBytes;
124 /*
125 if( pCurBlock == NULL )
126 {
127#ifdef MYRIAD_STREAM_DEBUG
128 sio << "MyriadStream: write: No block loaded, loading initial block."
129 << sio.nl;
130#endif
131 pCurBlock = rMyriad.getBlock(
132 pStream->aBlocks[iPos/rMyriad.iBlockSize]
133 );
134 }*/
135
136 while( iLeft > 0 )
137 {
138 int iCurBlock;
139 if( iPos/rMyriad.iBlockSize < pStream->aBlocks.getSize() )
140 {
141 iCurBlock = pStream->aBlocks[iPos/rMyriad.iBlockSize];
142 }
143 else
144 {
145 iCurBlock = rMyriad.streamAddBlock( pStream );
146#ifdef MYRIAD_STREAM_DEBUG
147 sio << "MyriadStream: write: " << __LINE__ << ": New block allocated and appended: "
148 << iCurBlock << "." << sio.nl;
149
150#endif
151 }
152 if( !pCurBlock || pCurBlock->iBlockIndex != iCurBlock )
153 {
154#ifdef MYRIAD_STREAM_DEBUG
155 sio << "MyriadStream: write: " << __LINE__ << ": Loading new block " << iCurBlock << "."
156 << sio.nl;
157#endif
158 rMyriad.releaseBlock( pCurBlock );
159 pCurBlock = rMyriad.getBlock( iCurBlock );
160 }
161 pCurBlock->bChanged = true;
162
163 // There are two main writing modes when it comes down to it.
164 // Overwrite mode and append mode. Append is what pretty much always
165 // happens when creating a new stream.
166 if( iPos < pStream->iSize )
167 {
168 int iAmnt = Bu::min(
169 Bu::min(
170 rMyriad.iBlockSize - iPos%rMyriad.iBlockSize,
171 iLeft
172 ),
173 pStream->iSize-iPos
174 );
175#ifdef MYRIAD_STREAM_DEBUG
176 sio << "MyriadStream: write (ovr): " << __LINE__ << ": Copying in bytes: "
177 << (iPos%rMyriad.iBlockSize) << "+"
178 << iAmnt
179 << ", " << iLeft << "b left." << sio.nl;
180#endif
181 memcpy(
182 pCurBlock->pData+(iPos%rMyriad.iBlockSize),
183 pBuf,
184 iAmnt
185 );
186 iPos += iAmnt;
187 pBuf = &((char *)pBuf)[iAmnt];
188 iLeft -= iAmnt;
189 }
190 else
191 {
192 int iAmnt = Bu::min(
193 rMyriad.iBlockSize - iPos%rMyriad.iBlockSize,
194 iLeft
195 );
196#ifdef MYRIAD_STREAM_DEBUG
197 sio << "MyriadStream: write (app): " << __LINE__ << ": Copying in bytes: "
198 << (iPos%rMyriad.iBlockSize) << "+"
199 << iAmnt
200 << ", " << iLeft << "b left." << sio.nl;
201#endif
202 memcpy(
203 pCurBlock->pData+(iPos%rMyriad.iBlockSize),
204 pBuf,
205 iAmnt
206 );
207 iPos += iAmnt;
208 pStream->iSize += iAmnt;
209 rMyriad.headerChanged();
210 pBuf = &((char *)pBuf)[iAmnt];
211 iLeft -= iAmnt;
212 }
213 }
214
215 return nBytes;
216}
217
218Bu::size Bu::MyriadStream::tell()
219{
220 return iPos;
221}
222
223void Bu::MyriadStream::seek( Bu::size offset )
224{
225 iPos += offset;
226}
227
228void Bu::MyriadStream::setPos( Bu::size pos )
229{
230 iPos = pos;
231}
232
233void Bu::MyriadStream::setPosEnd( Bu::size pos )
234{
235 iPos = pStream->iSize-pos;
236}
237
238bool Bu::MyriadStream::isEos()
239{
240 return iPos >= pStream->iSize;
241}
242
243bool Bu::MyriadStream::isOpen()
244{
245 return true;
246}
247
248void Bu::MyriadStream::flush()
249{
250}
251
252bool Bu::MyriadStream::canRead()
253{
254 return true;
255}
256
257bool Bu::MyriadStream::canWrite()
258{
259 return true;
260}
261
262bool Bu::MyriadStream::isReadable()
263{
264 return true;
265}
266
267bool Bu::MyriadStream::isWritable()
268{
269 return true;
270}
271
272bool Bu::MyriadStream::isSeekable()
273{
274 return true;
275}
276
277bool Bu::MyriadStream::isBlocking()
278{
279 return true;
280}
281
282void Bu::MyriadStream::setBlocking( bool /*bBlocking*/ )
283{
284}
285
286void Bu::MyriadStream::setSize( Bu::size iSize )
287{
288 if( iSize < 0 )
289 iSize = 0;
290 rMyriad.setStreamSize( pStream, iSize );
291 if( iPos > iSize )
292 iPos = iSize;
293}
294
295Bu::size Bu::MyriadStream::getSize() const
296{
297 return pStream->iSize;
298}
299
300Bu::size Bu::MyriadStream::getBlockSize() const
301{
302 return rMyriad.getBlockSize();
303}
304
305Bu::String Bu::MyriadStream::getLocation() const
306{
307 return Bu::String("%1").arg( pStream->iId );
308}
309