File: stream.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 (264 lines) | stat: -rw-r--r-- 4,506 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
/*
   FALCON - The Falcon Programming Language
   FILE: stream.cpp

   Implementation of common stream utility functions
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: ven ago 25 2006

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

   See LICENSE file for licensing details.
*/

/** \file
   Implementation of common stream utility functions
*/

#include <falcon/stream.h>
#include <falcon/memory.h>

#include <string.h>

namespace Falcon {

Stream::Stream( const Stream &other ):
   m_rhBufferSize( other.m_rhBufferSize ),
   m_rhBufferPos( other.m_rhBufferPos ),
   m_streamType( other.m_streamType ),
   m_status( other.m_status ),
   m_lastMoved( other.m_lastMoved )
{
   if ( m_rhBufferSize != 0 )
   {
      m_rhBuffer = (uint32 *) memAlloc( m_rhBufferSize * sizeof( uint32 ) );
      memcpy( m_rhBuffer, other.m_rhBuffer, m_rhBufferSize * sizeof( uint32 ) );
   }
   else
      m_rhBuffer = 0;
}

bool Stream::errorDescription( ::Falcon::String &description ) const
{
   if ( m_status == t_unsupported ) {
      description = "Unsupported operation for this stream";
      return true;
   }

	return false;
}

Stream::~Stream()
{
   if ( m_rhBuffer != 0 )
      memFree( m_rhBuffer );
}

//=================
// Private members

void Stream::pushBuffer( uint32 chr )
{
   if ( m_rhBufferPos == m_rhBufferSize )
   {
      m_rhBufferSize += FALCON_READAHEAD_BUFFER_BLOCK;
      uint32 *buf = (uint32 *) memRealloc( m_rhBuffer, m_rhBufferSize *sizeof(uint32) );
      m_rhBuffer = buf;
   }
   m_rhBuffer[ m_rhBufferPos ] = chr;
   m_rhBufferPos ++;
   reset();
}

bool Stream::popBuffer( uint32 &chr )
{
   if( m_rhBufferPos == 0 )
      return false;
   m_rhBufferPos--;
   chr = m_rhBuffer[ m_rhBufferPos ];
   return true;
}

//======================
// Public members


void Stream::unget( const String &target )
{
   uint32 pos = target.length();
   while( pos > 0 )
   {
      pos--;
      unget( target.getCharAt( pos ) );
   }
}

bool Stream::readAhead( uint32 &chr )
{
   if( popBuffer( chr ) )
      return true;

   if ( ! get( chr ) )
      return false;

   unget( chr );
   return true;
}

bool Stream::readAhead( String &target, uint32 size )
{
   if ( readString( target, size ) )
      return false;
   unget( target );
   return true;
}

void Stream::discardReadAhead( uint32 count )
{
   if( count == 0 || count >= m_rhBufferPos )
      m_rhBufferPos = 0;
   else
      m_rhBufferPos -= count;
}

bool Stream::flush()
{
   // does nothing
   return true;
}

bool Stream::readString( String &target, uint32 size )
{
   if ( size == 0 )
      return true;

   uint32 chr;

   // if we can't get EVEN a char, return false.
   if( ! get( chr ) || ! good() )
      return false;

   target.append( chr );
   size --;
   while( size > 0 )
   {
      // if we can't get a char, return false on stream error.
      if ( ! get( chr ) )
         return good();

      target.append( chr );
      size --;
   }

   return true;
}

bool Stream::writeString( const String &source, uint32 begin, uint32 end )
{
   uint32 pos = begin;
   if ( end > source.length() )
      end = source.length();

   while( pos < end ) {
      // some error in writing?
      if ( ! put( source.getCharAt( pos ) ) )
         return false;

      ++pos;
   }

   return true;
}




//======================================
// Overridables
//

Stream *Stream::clone() const
{
   return 0;
}

bool Stream::close()
{
   status( t_unsupported );
   return false;
}


int32 Stream::read( void *, int32 )
{
   status( t_unsupported );
   return -1;
}


int32 Stream::write( const void *, int32 )
{
   status( t_unsupported );
   return -1;
}


int64 Stream::tell()
{
   status( t_unsupported );
   return -1;
}


bool Stream::truncate( int64 )
{
   status( t_unsupported );
   return false;
}


int32 Stream::readAvailable( int32, const Sys::SystemData *sysData )
{
   status( t_unsupported );
   return -1;
}


int32 Stream::writeAvailable( int32, const Sys::SystemData *sysData )
{
   status( t_unsupported );
   return -1;
}

bool Stream::put( uint32 chr )
{
   status( t_unsupported );
   return false;
}


int64 Stream::seek( int64 , e_whence )
{
   status( t_unsupported );
   return -1;
}


int64 Stream::lastError() const
{
   return -1;
}


bool Stream::get( uint32 &chr )
{
   status( t_unsupported );
   return false;
}

}


/* end of stream.cpp */