File: stringstream.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 (571 lines) | stat: -rw-r--r-- 11,155 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
   FALCON - The Falcon Programming Language.
   FILE: file_StringStream.cpp

   Implementation of StringStream oriented streams.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: sab nov 13 2004

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

   See LICENSE file for licensing details.
*/

/** \file
   Implementation of StringStream oriented streams.
*/

#include <falcon/stringstream.h>
#include <falcon/memory.h>
#include <falcon/mt.h>
#include <cstring>
#include <string.h>

#include <stdio.h>

namespace Falcon {

class StringStream::Buffer {
public:
   String* m_str;
   int32 m_refcount;
   Mutex m_mtx;
   
   Buffer():
      m_str( new String() ),
      m_refcount(1)
   {}

   Buffer( const Buffer& other ):
      m_str( new String( *other.m_str ) ),
      m_refcount(1)
   {}

   void incref() { atomicInc(m_refcount); }
   void decref() { if (atomicDec(m_refcount) == 0) delete this; }

private:
   ~Buffer() {
      delete m_str;
   }
};

StringStream::StringStream( int32 size ):
   Stream( t_membuf ),
   m_b( new Buffer ),
   m_lastError(0),
   m_pos(0)
{
   m_pos = 0;
   if ( size <= 0 )
      size = 32;

   m_b->m_str->reserve(size);
}

StringStream::StringStream( const String &strbuf ):
   Stream( t_membuf ),
   m_b( new Buffer ),
   m_lastError(0),
   m_pos(0)
{
   *m_b->m_str = strbuf;
   m_b->m_str->bufferize();
}

StringStream::StringStream( const StringStream &strbuf ):
   Stream( strbuf ),
   m_lastError(0),
   m_pos( strbuf.m_pos )
{
   strbuf.m_b->incref();
   m_b = strbuf.m_b;
}


StringStream::~StringStream()
{
   m_b->decref();
}


void StringStream::setBuffer( const String &source )
{
   m_pos = 0;
   
   m_b->m_mtx.lock();
   if( m_b->m_str == 0 )
   {
      m_b->m_str = new String( source );
   }
   else
   {
      *m_b->m_str = source;
   }

   m_b->m_str->bufferize();
   m_lastError = 0;
   m_b->m_mtx.unlock();
}


void StringStream::setBuffer( const char* source, int size )
{
   m_b->m_mtx.lock();
   m_pos = 0;
   if( m_b->m_str == 0 )
   {
      m_b->m_str = new String;
   }
   m_b->m_str->reserve(size);
   memcpy( m_b->m_str->getRawStorage(), source, size );
   m_b->m_str->size( size );
   m_b->m_str->manipulator( &csh::handler_buffer );
   m_lastError = 0;
   m_b->m_mtx.unlock();
}


bool StringStream::detachBuffer()
{
   m_b->m_mtx.lock();
   if( m_b->m_str != 0 ) {
      m_b->m_str->setRawStorage(0);
      m_b->m_str->size(0);
      m_b->m_str->allocated(0);
      m_b->m_str->manipulator( &csh::handler_static );
      m_b->m_mtx.unlock();
      
      status( t_none );
      return true;
   }
   
   m_b->m_mtx.unlock();
   return false;
}

uint32 StringStream::length() const 
{ 
   return m_b->m_str->size();
}

uint32 StringStream::allocated() const 
{ 
   return m_b->m_str->allocated();
}

byte *StringStream::data() const
{ 
   return m_b->m_str->getRawStorage();
}


String *StringStream::closeToString()
{
   m_b->m_mtx.lock();
   String *s = m_b->m_str;
   m_b->m_str = 0;
   m_b->m_mtx.unlock();
   return s;
}


CoreString *StringStream::closeToCoreString()
{
   CoreString *temp = new CoreString;
   if (closeToString( *temp ))
      return temp;
   return 0; // CoreString is subject to GC
}


int64 StringStream::lastError() const
{ 
   return (int64) m_lastError;
}


void StringStream::transfer( StringStream &strbuf )
{
   strbuf.m_b->incref();
   m_b->decref();
   m_b = strbuf.m_b;
}

bool StringStream::errorDescription( String &description ) const
{
   switch( m_lastError )
   {
      case 0:  description = "None"; return true;
      case -1: description = "Out of Memory"; return true;
   }

   return false;
}

bool StringStream::close()
{
   m_b->m_mtx.lock();
   delete m_b->m_str;
   m_b->m_str = 0;
   m_b->m_mtx.unlock();
   return false;
}

int32 StringStream::read( void *buffer, int32 size )
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_status = t_error;
      m_b->m_mtx.unlock();

      return -1;
   }

   uint32 bsize =  m_b->m_str->size();

   if ( m_pos >= bsize ) {
      m_status = m_status | t_eof;
      m_b->m_mtx.unlock();
      
      return 0;
   }

   int sret = size + m_pos < bsize ? size : bsize - m_pos;
   memcpy( buffer, m_b->m_str->getRawStorage() + m_pos, sret );
   m_pos += sret;
   m_lastMoved = sret;
   m_b->m_mtx.unlock();

   return sret;
}

bool StringStream::readString( String &target, uint32 size )
{
   uint32 chr;
   target.size(0);
   target.manipulator( &csh::handler_buffer );

   while ( size > 0 && popBuffer(chr) )
   {
      target += chr;
      --size;
   }

   if( size == 0 )
   {
      return true;
   }

   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return false;
   }

   String* src = m_b->m_str;
   int csize = src->manipulator()->charSize();
   uint32 tglen = src->length();
   uint32 start = (m_pos / csize);
   if ( start >= tglen )
   {
      m_status = m_status | t_eof;
      m_b->m_mtx.unlock();
      return false;
   }

   uint32 end = start + size;
   if ( end > tglen ) {
      end = tglen;
   }

   target = src->subString(start,end);
   m_pos = end * csize;

   m_b->m_mtx.unlock();
   return true;
}


int32 StringStream::write( const void *buffer, int32 size )
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return -1;
   }
   
   // be sure there is enough space to write
   m_b->m_str->reserve(m_pos+size);

   // effectively write
   memcpy( m_b->m_str->getRawStorage() + m_pos, buffer, size );
   m_pos += size;

   // are we writing at end? -- enlarge the string.
   if( m_pos > m_b->m_str->size() )
   {
      m_b->m_str->size( m_pos );
   }

   m_lastMoved = size;
   m_b->m_mtx.unlock();

   return size;
}

bool StringStream::writeString( const String &source, uint32 begin, uint32 end )
{
   if( begin != 0 || ( end != -1 && end < source.length() ) )
   {
      return StringStream::subWriteString( source.subString(begin, end) );
   }
   else
   {
      return StringStream::subWriteString( source );
   }
}


bool StringStream::subWriteString( const String &source )
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return false;
   }

   // writing at end?
   if ( m_pos >= m_b->m_str->size() )
   {
      // easy
      *m_b->m_str += source;
      m_pos = m_b->m_str->size();
   }
   else {
      // less easy
      int32 origCharSize = m_b->m_str->manipulator()->charSize();
      int32 startPos = m_pos / origCharSize;
      if ( source.length() + startPos < m_b->m_str->length() )
      {
         m_b->m_str->size(m_pos);
         *m_b->m_str += source;
      }
      else
      {
         // even less easy
         String part2 = m_b->m_str->subString( source.length() + startPos );
         m_b->m_str->size(m_pos);
         *m_b->m_str += source + part2;
      }

      // the manipulator may have changed, so pos may have changed as well.
      m_pos = (startPos + source.length()) * m_b->m_str->manipulator()->charSize();
   }

   m_b->m_mtx.unlock();
   return true;
}


bool StringStream::put( uint32 chr )
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return false;
   }

   if( m_pos == m_b->m_str->size() )
   {
      m_b->m_str->append(chr);
      // manipulator may have changed.
      m_pos = m_b->m_str->size();
   }
   else
   {
      int32 pos = m_pos / m_b->m_str->manipulator()->charSize();
      m_b->m_str->setCharAt( pos , chr );
      // manipulator may have changed.
      m_pos = (pos+1) * m_b->m_str->manipulator()->charSize();
   }
   m_b->m_mtx.unlock();

   return true;
}

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

   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return false;
   }

   if( m_pos == m_b->m_str->size() )
   {
      m_status = m_status | t_eof;
      m_b->m_mtx.unlock();
      return false;
   }
   else
   {
      int32 cs = m_b->m_str->manipulator()->charSize();
      chr = m_b->m_str->getCharAt( m_pos/cs );
      // manipulator may have changed.
      m_pos += cs;
   }
   m_b->m_mtx.unlock();
   return true;
}

int64 StringStream::seek( int64 pos, Stream::e_whence w )
{
   m_b->m_mtx.lock();

   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return -1;
   }

   switch( w ) {
      case Stream::ew_begin: m_pos = (int32) pos; break;
      case Stream::ew_cur: m_pos += (int32) pos; break;
      case Stream::ew_end: m_pos = (int32) (m_b->m_str->size() + (pos-1)); break;
   }

   if ( m_pos > m_b->m_str->size() )
   {
      m_pos = m_b->m_str->size();
      m_status = t_eof;
   }
   else if ( m_pos < 0 )
   {
      m_pos = 0;
      m_status = t_none;
   }
   else
      m_status = t_none;

   pos = m_pos;
   m_b->m_mtx.unlock();

   return pos;
}

int64 StringStream::tell()
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return -1;
   }
   
   int32 pos = m_pos;
   m_b->m_mtx.unlock();
   return pos;
}

bool StringStream::truncate( int64 pos )
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 ) {
      m_b->m_mtx.unlock();
      m_status = t_error;
      return false;
   }

   if ( pos <= 0 )
      m_b->m_str->size( 0 );
   else
      m_b->m_str->size( (int32) pos );

   m_b->m_mtx.unlock();
   return true;
}

int32 StringStream::readAvailable( int32, const Sys::SystemData * )
{
   return 1;
}

int32 StringStream::writeAvailable( int32, const Sys::SystemData * )
{
   return 1;
}

void StringStream::getString( String &target ) const
{
   m_b->m_mtx.lock();
   // as the string is always buffered, this operation is safe
   target = *m_b->m_str;
   m_b->m_mtx.unlock();
}

bool StringStream::closeToString( String &target )
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 )
   {
      m_b->m_mtx.unlock();
      return false;
   }
   
   // adopt original string buffer.
   target.adopt( (char*)m_b->m_str->getRawStorage(), m_b->m_str->size(), m_b->m_str->allocated() );
   target.manipulator( (csh::Base*) m_b->m_str->manipulator() ); // apply correct manipulator


   // dispose the old string without disposing of its memory
   m_b->m_str->allocated( 0 );
   m_b->m_str->setRawStorage( 0 );
   delete m_b->m_str;
   m_b->m_str = 0;
   m_b->m_mtx.unlock();

   return true;
}

byte * StringStream::closeToBuffer()
{
   m_b->m_mtx.lock();
   if ( m_b->m_str == 0 || m_b->m_str->size() == 0)
   {
      m_b->m_mtx.unlock();
      return 0;
   }
   byte *retbuf = m_b->m_str->getRawStorage();

   // dispose the old string without disposing of its memory
   m_b->m_str->allocated( 0 );
   m_b->m_str->setRawStorage( 0 );
   delete m_b->m_str;
   m_b->m_str = 0;

   m_b->m_mtx.unlock();
   
   return retbuf;
}

StringStream *StringStream::clone() const
{
   StringStream *sstr = new StringStream( *this );
   return sstr;
}

}


/* end of file_StringStream.cpp */