summaryrefslogtreecommitdiff
path: root/src/nidsstream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/nidsstream.cpp')
-rw-r--r--src/nidsstream.cpp237
1 files changed, 0 insertions, 237 deletions
diff --git a/src/nidsstream.cpp b/src/nidsstream.cpp
deleted file mode 100644
index 65c8962..0000000
--- a/src/nidsstream.cpp
+++ /dev/null
@@ -1,237 +0,0 @@
1/*
2 * Copyright (C) 2007-2010 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/nidsstream.h"
9
10#include <string.h>
11
12Bu::NidsStream::NidsStream( Nids &rNids, uint32_t uStream ) :
13 rNids( rNids ),
14 uStream( uStream ),
15 pCurBlock( NULL ),
16 uCurBlock( uStream ),
17 uSize( 0 ),
18 uBlockSize( rNids.iBlockSize-sizeof(Nids::Block) ),
19 uPos( 0 )
20{
21 //printf("NidsStream::allocated\n");
22 pCurBlock = rNids.newBlock();
23 rNids.getBlock( uStream, pCurBlock );
24 uSize = pCurBlock->uBytesUsed;
25}
26
27/*
28Bu::NidsStream::NidsStream( const Bu::NidsStream &rSrc ) :
29 Stream( rSrc ),
30 rNids( rSrc.rNids ),
31 uStream( rSrc.uStream ),
32 pCurBlock( NULL ),
33 uCurBlock( uStream ),
34 uSize( 0 ),
35 uBlockSize( rSrc.uBlockSize ),
36 iUsable( uBlockSize-sizeof(Nids::Block) ),
37 uPos( 0 )
38{
39 printf("NidsStream::copied\n");
40 pCurBlock = rNids.newBlock();
41 rNids.getBlock( uStream, pCurBlock );
42}*/
43
44Bu::NidsStream::~NidsStream()
45{
46 //printf("Destroying stream?\n");
47 rNids.updateStreamSize( uStream, uSize );
48 rNids.deleteBlock( pCurBlock );
49}
50
51void Bu::NidsStream::close()
52{
53}
54
55size_t Bu::NidsStream::read( void *pBuf, size_t nBytes )
56{
57 if( nBytes == 0 )
58 return 0;
59 if( nBytes + uPos > uSize )
60 nBytes = uSize - uPos;
61 if( (uPos%uBlockSize)+nBytes < uBlockSize )
62 {
63 size_t iRead = nBytes;
64 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) )
65 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
66 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead );
67 //printf("read buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u)",
68 // iRead, uPos, uPos+iRead-1, uSize, 0, iRead-1, nBytes, uCurBlock );
69 uPos += iRead;
70 //printf(" -- %u\n", uPos%uBlockSize );
71 //printf("ra: block %u = %ub:%u (%ub total)\n",
72 // uCurBlock, uPos, nBytes, uSize );
73
74 // This can't happen, if we're right on a boundery, it goes to the
75 // other case
76 //if( uPos%uBlockSize == 0 )
77 // uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock, false );
78 return iRead;
79 }
80 else
81 {
82 size_t nTotal = 0;
83 for(;;)
84 {
85 uint32_t iRead = nBytes;
86 if( iRead > uBlockSize-(uPos%uBlockSize) )
87 iRead = uBlockSize-(uPos%uBlockSize);
88 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) )
89 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
90 memcpy( ((char *)pBuf)+nTotal,
91 pCurBlock->pData+(uPos%uBlockSize), iRead );
92 //printf(" read buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u)\n",
93 // iRead, uPos, uPos+iRead-1, uSize,
94 // nTotal, nTotal+nBytes-1, nBytes, uCurBlock );
95 uPos += iRead;
96 nBytes -= iRead;
97 nTotal += iRead;
98 //printf("rb: block %u = %ub:%u (%ub total)\n",
99 // uCurBlock, uPos, iRead, uSize );
100 if( uPos%uBlockSize == 0 )
101 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock, false );
102 if( nBytes == 0 || uPos >= uSize )
103 return nTotal;
104 }
105 }
106 return 0;
107}
108
109size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
110{
111 if( nBytes == 0 )
112 return 0;
113/* if( pCurBlock->uBytesUsed >= uBlockSize )
114 {
115 // We're at the end of our current block, allocate another before we do
116 // anything.
117 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock );
118 } */
119 if( (uPos%uBlockSize)+nBytes < uBlockSize )
120 {
121 //printf("wa: %u:%u:%u:%u -> ", uPos, uPos%uBlockSize, uSize, pCurBlock->uBytesUsed );
122 memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes );
123 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u:%u)\n",
124 // nBytes, 0, nBytes-1, nBytes,
125 // uPos, uPos+nBytes-1, uSize, uCurBlock,
126 // pCurBlock->uBytesUsed );
127 if( (uPos%uBlockSize)+nBytes > pCurBlock->uBytesUsed )
128 pCurBlock->uBytesUsed = (uPos%uBlockSize)+nBytes;
129 rNids.setBlock( uCurBlock, pCurBlock );
130 uPos += nBytes;
131 if( uPos > uSize )
132 uSize = uPos;
133 //printf("block %u = %ub (%ub total) %d:%u\n",
134 // uCurBlock, pCurBlock->uBytesUsed, uSize, nBytes, uPos );
135 return nBytes;
136 }
137 else
138 {
139 size_t nTotal = 0;
140 for(;;)
141 {
142 uint32_t uNow = uBlockSize-(uPos%uBlockSize);
143 //printf("uNow: %u (%u-(%u%%%u)) %d req\n", uNow, uBlockSize, uPos, uBlockSize, nBytes );
144 if( nBytes < uNow )
145 uNow = nBytes;
146 memcpy( pCurBlock->pData+(uPos%uBlockSize),
147 &((char *)pBuf)[nTotal], uNow );
148 //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u:%u)\n",
149 // uNow, nTotal, nTotal+uNow-1, nBytes,
150 // uPos, uPos+uNow-1, uSize, uCurBlock, pCurBlock->uBytesUsed );
151 if( (uPos%uBlockSize)+uNow > pCurBlock->uBytesUsed )
152 pCurBlock->uBytesUsed = (uPos%uBlockSize)+uNow;
153 rNids.setBlock( uCurBlock, pCurBlock );
154 uPos += uNow;
155 if( uPos > uSize )
156 uSize = uPos;
157 nTotal += uNow;
158 nBytes -= uNow;
159 //printf("wb: block %u = %ub (%ub total)\n",
160 // uCurBlock, pCurBlock->uBytesUsed, uSize );
161 //if( pCurBlock->uBytesUsed == uBlockSize )
162 if( uPos%uBlockSize == 0 )
163 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock );
164 if( nBytes == 0 )
165 return nTotal;
166 }
167 }
168}
169
170long Bu::NidsStream::tell()
171{
172 return uPos;
173}
174
175void Bu::NidsStream::seek( long offset )
176{
177 uPos += offset;
178}
179
180void Bu::NidsStream::setPos( long pos )
181{
182 uPos = pos;
183}
184
185void Bu::NidsStream::setPosEnd( long pos )
186{
187 uPos = uSize-pos-1;
188}
189
190bool Bu::NidsStream::isEos()
191{
192 return true;
193}
194
195bool Bu::NidsStream::isOpen()
196{
197 return true;
198}
199
200void Bu::NidsStream::flush()
201{
202}
203
204bool Bu::NidsStream::canRead()
205{
206 return true;
207}
208
209bool Bu::NidsStream::canWrite()
210{
211 return true;
212}
213
214bool Bu::NidsStream::isReadable()
215{
216 return true;
217}
218
219bool Bu::NidsStream::isWritable()
220{
221 return true;
222}
223
224bool Bu::NidsStream::isSeekable()
225{
226 return true;
227}
228
229bool Bu::NidsStream::isBlocking()
230{
231 return true;
232}
233
234void Bu::NidsStream::setBlocking( bool /*bBlocking*/ )
235{
236}
237