File: streambuffer.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (402 lines) | stat: -rw-r--r-- 8,756 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
   FALCON - The Falcon Programming Language
   FILE: streambuffer.cpp

   Buffer wrapper for stream operations.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Mon, 02 Feb 2009 16:45:57 +0100

   -------------------------------------------------------------------
   (C) Copyright 2009: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Buffer wrapper for stream operations.
*/

#include <falcon/memory.h>
#include <falcon/streambuffer.h>
#include <falcon/fassert.h>

#include <string.h>
#include <cstring>

namespace Falcon {

StreamBuffer::StreamBuffer( Stream *underlying, bool bOwn, uint32 bufSize ):
   Stream( underlying->type() ),
   m_bufSize( bufSize ),
   m_changed( false ),
   m_bufPos(0),
   m_bufLen(0),
   m_filePos(0),
   m_bReseek( false ),
   m_stream( underlying ),
   m_streamOwner( bOwn )
{
   m_buffer = (byte *) memAlloc( m_bufSize );
}

StreamBuffer::StreamBuffer( const StreamBuffer &other ):
   Stream( other.m_streamType ),
   m_bufSize( other.m_bufSize ),
   m_changed( other.m_changed ),
   m_bufPos( other.m_bufPos ),
   m_bufLen( other.m_bufLen ),
   m_filePos( other.m_filePos ),
   m_bReseek( other.m_bReseek ),
   m_streamOwner( other.m_streamOwner )
{
   if( m_streamOwner )
      m_stream = dyncast<Stream*>(other.m_stream->clone());
   else
      m_stream = other.m_stream;

   m_buffer = (byte *) memAlloc( m_bufSize );
}


StreamBuffer::~StreamBuffer()
{
   flush();

   if( m_streamOwner )
      delete m_stream;

   memFree( m_buffer );
}


StreamBuffer *StreamBuffer::clone() const
{
   return new StreamBuffer( *this );
}

bool StreamBuffer::refill()
{
   if ( ! flush() )
      return false;

   if( m_bReseek )
   {
      m_stream->seekBegin( m_filePos );
      m_bReseek = false;
   }
   else {
      m_filePos += m_bufLen;
   }

   m_bufPos = 0;

   int32 readIn = m_stream->read( m_buffer, m_bufSize );
   if ( readIn < 0 )
   {
      m_bufLen = 0;
      return false;
   }

   // changed was set to false from flush
   m_bufLen = readIn;

   return true;
}

int32 StreamBuffer::read( void *b, int32 size )
{
   // minimal sanity check
   if ( size <= 0 )
      return 0;

   if( ! m_stream->good() || ! m_stream->open() )
      return -1;

   byte *buf = (byte*) b;

   int32 avail = m_bufLen - m_bufPos;

   // have we something to store in the buffer?
   if ( avail > 0 )
   {
      // enough to fill the buffer?
      if ( avail >= size )
      {
         memcpy( buf, m_buffer + m_bufPos, size );
         m_bufPos += size;
         return size;
      }
      else {
         // in the meanwhile, put the data in.
         memcpy( buf, m_buffer + m_bufPos, avail );
         m_bufPos = m_bufLen;  // declare we have consumed everything.
         // return a partial read in case of underlying networks
         if ( m_stream->type() == t_network )
            return avail;
      }
   }

   // if we're here, we need to refill the buffer, or eventually to read everything from the stream
   // the amount of data we still have to put in the buffer is size - avail.

   int32 toBeRead = size - avail;

   // would be a new buffer enough to store the data?
   if ( toBeRead <= m_bufSize )
   {
      if ( ! refill() )
      {
         // if the refill operation failed, return what we have read.
         return m_stream->bad() ? -1 : avail;
      }

      // if the refill operation succed, it is still possible that it has read less than toBeRead.
      if ( m_bufLen < toBeRead )
         toBeRead = m_bufLen;

      memcpy( buf + avail, m_buffer, toBeRead );
      m_bufPos = toBeRead;  // declare we have consumed the data.
      return toBeRead + avail;
   }
   else
   {
      // it's of no use to refill the buffer now. Just read the required size and update the file pointer.
      m_filePos += m_bufLen;  // a buffer is gone.

      int32 readin = m_stream->read( buf + avail, size - avail );
      if( readin > 0 )
      {
          m_filePos += readin;
          return readin + avail;
      }
      else {
         // we have an error, but return the read data.
         return avail;
      }
   }
}


int32 StreamBuffer::write( const void *b, int32 size )
{
   // minimal sanity check
   if ( size <= 0 )
      return 0;

   if( m_stream->status() != t_open )
      return -1;

   const byte *buf = (byte*) b;

   // first; is there any space left in the buffer for write?
   int32 avail = m_bufSize - m_bufPos;
   if( avail > 0 )
   {
      m_changed = true;

      // good; if we have enough space, advance and go away.
      if ( size <= avail )
      {
         memcpy( m_buffer + m_bufPos, buf, size );
         m_bufPos += size;
         if ( m_bufLen < m_bufPos )
         {
            m_bufLen = m_bufPos;
         }

         return size;
      }
      else {
         // nay, we can write only part of the stuff.
         memcpy( m_buffer + m_bufPos, buf, avail );
         m_bufPos = m_bufLen = m_bufSize;
      }
   }

   // we have still to write part or all the data.

   // now, if the rest of the data can be stored in the next buffer,
   // refill and write. Otherwise, just flush and try a single write out.
   int32 toBeWritten = size - avail;
   if( toBeWritten <= m_bufSize )
   {
      flush();
      m_changed = true; // ensure we declare this buffer changed anyhow

      memcpy( m_buffer, buf + avail, toBeWritten );
      m_bufPos = m_bufLen = toBeWritten;

      return avail + toBeWritten;
   }
   else
   {
      flush(); // but don't reload now

      toBeWritten = m_stream->write( buf + avail, toBeWritten );
      if( toBeWritten < 0 )
      {
         return avail;
      }
      m_filePos += toBeWritten;
      return avail + toBeWritten;
   }
}


bool StreamBuffer::close()
{
   flush();
   return m_stream->close();
}

int64 StreamBuffer::tell()
{
   return m_filePos + m_bufPos;
}

bool StreamBuffer::truncate( int64 pos )
{
   if ( pos == -1 )
   {
      // shorten the buffer to the current position
      m_bufLen = m_bufPos;
      // And truncate
      if(  m_stream->truncate( m_filePos + m_bufPos ) )
      {
         flush();
         return true;
      }

      return false;
   }
   else
   {
      int64 curpos = m_filePos + m_bufPos;
      flush();

      // and trunk at the desired position
      if ( pos < curpos )
      {
         m_filePos = pos;
         curpos = pos;
      }

      m_stream->seekBegin( curpos );
      return m_stream->truncate( pos );
   }
}

int32 StreamBuffer::readAvailable( int32 msecs_timeout, const Sys::SystemData *sysData )
{
   if ( m_bufPos < m_bufLen )
      return m_bufLen - m_bufPos;

   return m_stream->readAvailable( msecs_timeout, sysData );
}

int32 StreamBuffer::writeAvailable( int32 msecs_timeout, const Sys::SystemData *sysData )
{
   if ( m_bufPos < m_bufSize )
      return m_bufSize - m_bufPos;

   return m_stream->writeAvailable( msecs_timeout, sysData );
}

int64 StreamBuffer::seek( int64 pos, e_whence whence )
{
   // TODO: optimize and avoid re-buffering if we're still in the buffer.
   if( whence == ew_cur )
   {
      pos = m_filePos + m_bufPos + pos;
      whence = ew_begin;
   }

   flush();
   m_bufLen = m_bufPos = 0;

   m_filePos = m_stream->seek( pos, whence );
   m_bReseek = false;
   return m_filePos;
}

bool StreamBuffer::flush()
{
   if ( ! m_changed || m_bufLen == 0 )
      return true;

   int32 written = m_stream->write( m_buffer, m_bufLen );
   int32 count = written;
   while( written > 0 && count < m_bufLen )
   {
      written = m_stream->write( m_buffer + count, m_bufLen - count );
      count += written;
   }

   if( written < 0 )
      return false;

   m_filePos += m_bufPos;
   m_bReseek = m_bReseek || m_bufPos != m_bufLen;
   m_changed = false;
   m_bufPos = m_bufLen = 0;

   m_stream->flush();

   return true;
}

bool StreamBuffer::get( uint32 &chr )
{
   if ( popBuffer(chr) )
      return true;

   if ( m_bufPos == m_bufLen )
   {
      if ( ! refill() )
         return false;

      if( m_bufPos == m_bufLen ) // eof?
         return false;
   }

   chr = m_buffer[m_bufPos++ ];
   return true;
}

bool StreamBuffer::put( uint32 chr )
{
   if ( m_bufPos == m_bufSize )
   {
      if ( ! flush() )
         return false;

      m_buffer[ 0 ] = chr;
      m_bufPos = m_bufLen = 1;
      m_changed = true;
      return true;
   }

   m_buffer[ m_bufPos++ ] = chr;
   m_changed = true;
   if ( m_bufLen < m_bufPos )
      m_bufLen = m_bufPos;
   return true;
}

bool StreamBuffer::resizeBuffer( uint32 size )
{
   fassert( size > 0 );
   if ( ! flush() )
      return false;

   memFree( m_buffer );
   m_buffer = (byte*) memAlloc( size );
   m_bufSize = size;
   return true;
}

}


/* end of streambuffer.cpp */