summaryrefslogtreecommitdiffstats
path: root/src/ftextstream.cpp
blob: 3038af34e0cdb91e1141d28bd79d76b200a90437 (plain)
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
#include "ftextstream.h"
#include <qfile.h>

//----------------------------------------------------------------------------

class QGStringBuffer : public QIODevice 
{
  public:
    QGStringBuffer( QGString* str );
    ~QGStringBuffer();
    bool  open( int m );
    void  close();
    void  flush();
    uint  size() const;
    int   at()   const;
    bool  at( int pos );
    int   readBlock( char *, uint) { return -1; }
    int   writeBlock( const char *p, uint len );
    int   getch() { return -1; }
    int   putch( int ch );
    int   ungetch( int ) { return -1; }

  protected:
    QGString* m_str;

  private:        // Disabled copy constructor and operator=
    QGStringBuffer( const QGStringBuffer & );
    QGStringBuffer &operator=( const QGStringBuffer & );
};

QGStringBuffer::QGStringBuffer( QGString* str ) : m_str(str)
{
  //printf("QGStringBuffer::QGStringBuffer(%p)\n",str);
}

QGStringBuffer::~QGStringBuffer()
{
}

bool  QGStringBuffer::open( int m )
{
    if ( !m_str ) 
    {
#if defined(CHECK_STATE)
	qWarning( "QGStringBuffer::open: No string" );
#endif
	return FALSE;
    }
    if ( isOpen() ) 
    {                           // buffer already open
#if defined(CHECK_STATE)
	qWarning( "QGStringBuffer::open: Buffer already open" );
#endif
	return FALSE;
    }
    setMode( m );
    if ( m & IO_Truncate ) 
    {                    // truncate buffer
	m_str->truncate( 0 );
    }
    if ( m & IO_Append ) 
    {                      // append to end of buffer
	ioIndex = m_str->length();
    } 
    else 
    {
	ioIndex = 0;
    }
    setState( IO_Open );
    setStatus( 0 );
    return TRUE;
}

void  QGStringBuffer::close()
{
  if ( isOpen() ) 
  {
    setFlags( IO_Direct );
    ioIndex = 0;
  }
}

void  QGStringBuffer::flush()
{
}

uint  QGStringBuffer::size() const
{
  return m_str ? m_str->length() : 0;
}

int   QGStringBuffer::at()   const
{
  return ioIndex;
}

bool  QGStringBuffer::at( int pos )
{
#if defined(CHECK_STATE)
  if ( !isOpen() ) 
  {
    qWarning( "QGStringBuffer::at: Buffer is not open" );
    return FALSE;
  }
#endif
  if ( (uint)pos >= m_str->length() ) 
  {
#if defined(CHECK_RANGE)
    qWarning( "QGStringBuffer::at: Index %d out of range", pos );
#endif
    return FALSE;
  }

  ioIndex = pos;
  return TRUE;
}

int QGStringBuffer::writeBlock( const char *p, uint len )
{
  //printf("QGStringBuffer::writeBlock(%p,%d) m_str=%p ioIndex=%d\n",p,len,
  //    m_str,ioIndex);
  m_str->enlarge(ioIndex+len+1);
  memcpy(m_str->data()+ioIndex,p,len);
  ioIndex+=len;
  m_str->data()[ioIndex]='\0';
  m_str->setLen(ioIndex);
  return len;
}

int QGStringBuffer::putch( int ch )
{
  //printf("QGStringBuffer::putch(%d) m_str=%p ioIndex=%d\n",
  //    ch,m_str,ioIndex);
  m_str->enlarge(ioIndex+2);
  m_str->data()[ioIndex] = (char)ch;
  ioIndex++;
  m_str->data()[ioIndex] = '\0';
  m_str->setLen(ioIndex);
  return ch;
}


//----------------------------------------------------------------------------

FTextStream::FTextStream()
{
  m_dev = 0;
  m_owndev = FALSE;
}

FTextStream::FTextStream( QIODevice *dev )
{
  m_dev = dev;
  m_owndev = FALSE;
}

FTextStream::FTextStream( QGString *s )
{
  m_dev = new QGStringBuffer(s);
  ((QGStringBuffer*)m_dev)->open( IO_WriteOnly );
  m_owndev = TRUE;
}

FTextStream::FTextStream( FILE *fh )
{
  m_dev = new QFile;
  ((QFile *)m_dev)->open( IO_WriteOnly, fh);
  m_owndev = TRUE;
}

FTextStream::~FTextStream()
{
  if (m_owndev) delete m_dev;
  m_dev = 0;
}

QIODevice *FTextStream::device() const
{
  return m_dev;
}

void FTextStream::setDevice( QIODevice *dev )
{
  if (m_owndev) 
  {
    delete m_dev;
    m_owndev = FALSE;
  }
  m_dev = dev;
}

void FTextStream::unsetDevice()
{
  setDevice(0);
}

FTextStream &FTextStream::output_int( ulong n, bool neg )
{
  char buf[20];
  char *p = &buf[19];
  *p = '\0';
  if ( neg )
  {
    n = (ulong)(-(long)n);
  }
  do 
  {
    *--p = ((int)(n%10)) + '0';
    n /= 10;
  } while ( n );
  if ( neg ) *--p = '-';
  return operator<<(p);
}

FTextStream &FTextStream::operator<<( signed short i )
{
    return output_int( i, i < 0 );
}

FTextStream &FTextStream::operator<<( unsigned short i )
{
    return output_int( i, FALSE );
}

FTextStream &FTextStream::operator<<( signed int i )
{
    return output_int( i, i < 0 );
}

FTextStream &FTextStream::operator<<( unsigned int i )
{
    return output_int( i, FALSE );
}

FTextStream &FTextStream::operator<<( signed long i )
{
    return output_int( i, i < 0 );
}

FTextStream &FTextStream::operator<<( unsigned long i )
{
    return output_int( i, FALSE );
}

FTextStream &FTextStream::operator<<( float f )
{
    return *this << (double)f;
}

FTextStream &FTextStream::operator<<( double d )
{
    char buf[64];
    sprintf(buf,"%f",d);
    return *this << buf;
}