summaryrefslogtreecommitdiff
path: root/src/myriadstream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/myriadstream.cpp')
-rw-r--r--src/myriadstream.cpp157
1 files changed, 79 insertions, 78 deletions
diff --git a/src/myriadstream.cpp b/src/myriadstream.cpp
index 04f98ed..0e6fc89 100644
--- a/src/myriadstream.cpp
+++ b/src/myriadstream.cpp
@@ -6,19 +6,22 @@
6 */ 6 */
7 7
8#include "bu/myriadstream.h" 8#include "bu/myriadstream.h"
9#include "bu/sio.h"
10
11using Bu::sio;
12using Bu::Fmt;
9 13
10#include <string.h> 14#include <string.h>
11 15
12Bu::MyriadStream::MyriadStream( Myriad &rMyriad, uint32_t uStream ) : 16Bu::MyriadStream::MyriadStream( Bu::Myriad &rMyriad,
17 Bu::Myriad::Stream *pStream ) :
13 rMyriad( rMyriad ), 18 rMyriad( rMyriad ),
14 uStream( uStream ), 19 pStream( pStream ),
15 pCurBlock( NULL ), 20 pCurBlock( NULL ),
16 uCurBlock( uStream ), 21 iPos( 0 )
17 uSize( 0 ),
18 uBlockSize( rMyriad.iBlockSize ),
19 uPos( 0 )
20{ 22{
21 //printf("MyriadStream::allocated\n"); 23 sio << "MyriadStream: Created, iId=" << pStream->iId << ", iSize="
24 << pStream->iSize << sio.nl;
22 //pCurBlock = rMyriad.newBlock(); 25 //pCurBlock = rMyriad.newBlock();
23 //rMyriad.getBlock( uStream, pCurBlock ); 26 //rMyriad.getBlock( uStream, pCurBlock );
24 //uSize = pCurBlock->uBytesUsed; 27 //uSize = pCurBlock->uBytesUsed;
@@ -26,7 +29,8 @@ Bu::MyriadStream::MyriadStream( Myriad &rMyriad, uint32_t uStream ) :
26 29
27Bu::MyriadStream::~MyriadStream() 30Bu::MyriadStream::~MyriadStream()
28{ 31{
29 //printf("Destroying stream?\n"); 32 if( pCurBlock )
33 rMyriad.releaseBlock( pCurBlock );
30 //rMyriad.updateStreamSize( uStream, uSize ); 34 //rMyriad.updateStreamSize( uStream, uSize );
31 //rMyriad.deleteBlock( pCurBlock ); 35 //rMyriad.deleteBlock( pCurBlock );
32} 36}
@@ -37,57 +41,54 @@ void Bu::MyriadStream::close()
37 41
38size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes ) 42size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes )
39{ 43{
40 /* 44 sio << "MyriadStream: Read: Started, asked to read " << nBytes << "b."
41 if( nBytes == 0 ) 45 << sio.nl;
46 if( nBytes <= 0 )
42 return 0; 47 return 0;
43 if( nBytes + uPos > uSize ) 48 if( nBytes > pStream->iSize-iPos )
44 nBytes = uSize - uPos; 49 nBytes = pStream->iSize-iPos;
45 if( (uPos%uBlockSize)+nBytes < uBlockSize ) 50 int iLeft = nBytes;
51 sio << "MyriadStream: Read: Started, going to read " << nBytes << "b."
52 << sio.nl;
53 if( pCurBlock == NULL )
46 { 54 {
47 size_t iRead = nBytes; 55 sio << "MyriadStream: Read: No block loaded, loading initial block."
48 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) ) 56 << sio.nl;
49 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize); 57 pCurBlock = rMyriad.getBlock(
50 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead ); 58 pStream->aBlocks[iPos/rMyriad.iBlockSize]
51 //printf("read buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u)", 59 );
52 // iRead, uPos, uPos+iRead-1, uSize, 0, iRead-1, nBytes, uCurBlock );
53 uPos += iRead;
54 //printf(" -- %u\n", uPos%uBlockSize );
55 //printf("ra: block %u = %ub:%u (%ub total)\n",
56 // uCurBlock, uPos, nBytes, uSize );
57
58 // This can't happen, if we're right on a boundery, it goes to the
59 // other case
60 //if( uPos%uBlockSize == 0 )
61 // uCurBlock = rMyriad.getNextBlock( uCurBlock, pCurBlock, false );
62 return iRead;
63 } 60 }
64 else 61 while( iLeft > 0 )
65 { 62 {
66 size_t nTotal = 0; 63 int iCurBlock = pStream->aBlocks[iPos/rMyriad.iBlockSize];
67 for(;;) 64 if( pCurBlock->iBlockIndex != iCurBlock )
68 { 65 {
69 uint32_t iRead = nBytes; 66 sio << "MyriadStream: Read: Loading new block " << iCurBlock << "."
70 if( iRead > uBlockSize-(uPos%uBlockSize) ) 67 << sio.nl;
71 iRead = uBlockSize-(uPos%uBlockSize); 68 rMyriad.releaseBlock( pCurBlock );
72 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) ) 69 pCurBlock = rMyriad.getBlock( iCurBlock );
73 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
74 memcpy( ((char *)pBuf)+nTotal,
75 pCurBlock->pData+(uPos%uBlockSize), iRead );
76 //printf(" read buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u)\n",
77 // iRead, uPos, uPos+iRead-1, uSize,
78 // nTotal, nTotal+nBytes-1, nBytes, uCurBlock );
79 uPos += iRead;
80 nBytes -= iRead;
81 nTotal += iRead;
82 //printf("rb: block %u = %ub:%u (%ub total)\n",
83 // uCurBlock, uPos, iRead, uSize );
84 if( uPos%uBlockSize == 0 )
85 uCurBlock = rMyriad.getNextBlock( uCurBlock, pCurBlock, false );
86 if( nBytes == 0 || uPos >= uSize )
87 return nTotal;
88 } 70 }
89 }*/ 71
90 return 0; 72 int iAmnt = Bu::min(
73 rMyriad.iBlockSize - iPos%rMyriad.iBlockSize,
74 iLeft
75 );
76// if( iLeft > iAmnt )
77// iAmnt = iLeft;
78 sio << "MyriadStream: Read: Copying out bytes: "
79 << (iPos%rMyriad.iBlockSize) << " - "
80 << iAmnt
81 << ", " << iLeft << "b left." << sio.nl;
82 memcpy(
83 pBuf,
84 pCurBlock->pData+iPos%rMyriad.iBlockSize,
85 iAmnt
86 );
87 iPos += iAmnt;
88 pBuf = &((char *)pBuf)[iAmnt];
89 iLeft -= iAmnt;
90 }
91 return nBytes;
91} 92}
92 93
93size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes ) 94size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
@@ -101,22 +102,22 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
101 uCurBlock = rMyriad.getNextBlock( uCurBlock, pCurBlock ); 102 uCurBlock = rMyriad.getNextBlock( uCurBlock, pCurBlock );
102 } */ 103 } */
103 /* 104 /*
104 if( (uPos%uBlockSize)+nBytes < uBlockSize ) 105 if( (iPos%uBlockSize)+nBytes < uBlockSize )
105 { 106 {
106 //printf("wa: %u:%u:%u:%u -> ", uPos, uPos%uBlockSize, uSize, pCurBlock->uBytesUsed ); 107 //printf("wa: %u:%u:%u:%u -> ", iPos, iPos%uBlockSize, uSize, pCurBlock->uBytesUsed );
107 memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes ); 108 memcpy( pCurBlock->pData+(iPos%uBlockSize), pBuf, nBytes );
108 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u:%u)\n", 109 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u:%u)\n",
109 // nBytes, 0, nBytes-1, nBytes, 110 // nBytes, 0, nBytes-1, nBytes,
110 // uPos, uPos+nBytes-1, uSize, uCurBlock, 111 // iPos, iPos+nBytes-1, uSize, uCurBlock,
111 // pCurBlock->uBytesUsed ); 112 // pCurBlock->uBytesUsed );
112 if( (uPos%uBlockSize)+nBytes > pCurBlock->uBytesUsed ) 113 if( (iPos%uBlockSize)+nBytes > pCurBlock->uBytesUsed )
113 pCurBlock->uBytesUsed = (uPos%uBlockSize)+nBytes; 114 pCurBlock->uBytesUsed = (iPos%uBlockSize)+nBytes;
114 rMyriad.setBlock( uCurBlock, pCurBlock ); 115 rMyriad.setBlock( uCurBlock, pCurBlock );
115 uPos += nBytes; 116 iPos += nBytes;
116 if( uPos > uSize ) 117 if( iPos > uSize )
117 uSize = uPos; 118 uSize = iPos;
118 //printf("block %u = %ub (%ub total) %d:%u\n", 119 //printf("block %u = %ub (%ub total) %d:%u\n",
119 // uCurBlock, pCurBlock->uBytesUsed, uSize, nBytes, uPos ); 120 // uCurBlock, pCurBlock->uBytesUsed, uSize, nBytes, iPos );
120 return nBytes; 121 return nBytes;
121 } 122 }
122 else 123 else
@@ -124,27 +125,27 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
124 size_t nTotal = 0; 125 size_t nTotal = 0;
125 for(;;) 126 for(;;)
126 { 127 {
127 uint32_t uNow = uBlockSize-(uPos%uBlockSize); 128 uint32_t uNow = uBlockSize-(iPos%uBlockSize);
128 //printf("uNow: %u (%u-(%u%%%u)) %d req\n", uNow, uBlockSize, uPos, uBlockSize, nBytes ); 129 //printf("uNow: %u (%u-(%u%%%u)) %d req\n", uNow, uBlockSize, iPos, uBlockSize, nBytes );
129 if( nBytes < uNow ) 130 if( nBytes < uNow )
130 uNow = nBytes; 131 uNow = nBytes;
131 memcpy( pCurBlock->pData+(uPos%uBlockSize), 132 memcpy( pCurBlock->pData+(iPos%uBlockSize),
132 &((char *)pBuf)[nTotal], uNow ); 133 &((char *)pBuf)[nTotal], uNow );
133 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u:%u)\n", 134 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u:%u)\n",
134 // uNow, nTotal, nTotal+uNow-1, nBytes, 135 // uNow, nTotal, nTotal+uNow-1, nBytes,
135 // uPos, uPos+uNow-1, uSize, uCurBlock, pCurBlock->uBytesUsed ); 136 // iPos, iPos+uNow-1, uSize, uCurBlock, pCurBlock->uBytesUsed );
136 if( (uPos%uBlockSize)+uNow > pCurBlock->uBytesUsed ) 137 if( (iPos%uBlockSize)+uNow > pCurBlock->uBytesUsed )
137 pCurBlock->uBytesUsed = (uPos%uBlockSize)+uNow; 138 pCurBlock->uBytesUsed = (iPos%uBlockSize)+uNow;
138 rMyriad.setBlock( uCurBlock, pCurBlock ); 139 rMyriad.setBlock( uCurBlock, pCurBlock );
139 uPos += uNow; 140 iPos += uNow;
140 if( uPos > uSize ) 141 if( iPos > uSize )
141 uSize = uPos; 142 uSize = iPos;
142 nTotal += uNow; 143 nTotal += uNow;
143 nBytes -= uNow; 144 nBytes -= uNow;
144 //printf("wb: block %u = %ub (%ub total)\n", 145 //printf("wb: block %u = %ub (%ub total)\n",
145 // uCurBlock, pCurBlock->uBytesUsed, uSize ); 146 // uCurBlock, pCurBlock->uBytesUsed, uSize );
146 //if( pCurBlock->uBytesUsed == uBlockSize ) 147 //if( pCurBlock->uBytesUsed == uBlockSize )
147 if( uPos%uBlockSize == 0 ) 148 if( iPos%uBlockSize == 0 )
148 uCurBlock = rMyriad.getNextBlock( uCurBlock, pCurBlock ); 149 uCurBlock = rMyriad.getNextBlock( uCurBlock, pCurBlock );
149 if( nBytes == 0 ) 150 if( nBytes == 0 )
150 return nTotal; 151 return nTotal;
@@ -155,27 +156,27 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
155 156
156long Bu::MyriadStream::tell() 157long Bu::MyriadStream::tell()
157{ 158{
158 return uPos; 159 return iPos;
159} 160}
160 161
161void Bu::MyriadStream::seek( long offset ) 162void Bu::MyriadStream::seek( long offset )
162{ 163{
163 uPos += offset; 164 iPos += offset;
164} 165}
165 166
166void Bu::MyriadStream::setPos( long pos ) 167void Bu::MyriadStream::setPos( long pos )
167{ 168{
168 uPos = pos; 169 iPos = pos;
169} 170}
170 171
171void Bu::MyriadStream::setPosEnd( long pos ) 172void Bu::MyriadStream::setPosEnd( long pos )
172{ 173{
173 uPos = uSize-pos-1; 174 iPos = pStream->iSize-pos-1;
174} 175}
175 176
176bool Bu::MyriadStream::isEos() 177bool Bu::MyriadStream::isEos()
177{ 178{
178 return true; 179 return iPos >= pStream->iSize;
179} 180}
180 181
181bool Bu::MyriadStream::isOpen() 182bool Bu::MyriadStream::isOpen()