My Project
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
ftextstream.cpp
Go to the documentation of this file.
1
#include "
ftextstream.h
"
2
#include <qfile.h>
3
4
//----------------------------------------------------------------------------
5
6
class
QGStringBuffer
:
public
QIODevice
7
{
8
public
:
9
QGStringBuffer
( QGString* str );
10
~QGStringBuffer
();
11
bool
open
(
int
m );
12
void
close
();
13
void
flush
();
14
uint
size
()
const
;
15
int
at
()
const
;
16
bool
at
(
int
pos );
17
int
readBlock
(
char
*, uint) {
return
-1; }
18
int
writeBlock
(
const
char
*p, uint len );
19
int
getch
() {
return
-1; }
20
int
putch
(
int
ch );
21
int
ungetch
(
int
) {
return
-1; }
22
23
protected
:
24
QGString*
m_str
;
25
26
private
:
// Disabled copy constructor and operator=
27
QGStringBuffer
(
const
QGStringBuffer
& );
28
QGStringBuffer
&
operator=
(
const
QGStringBuffer
& );
29
};
30
31
QGStringBuffer::QGStringBuffer
( QGString* str ) : m_str(str)
32
{
33
//printf("QGStringBuffer::QGStringBuffer(%p)\n",str);
34
}
35
36
QGStringBuffer::~QGStringBuffer
()
37
{
38
}
39
40
bool
QGStringBuffer::open
(
int
m )
41
{
42
if
( !
m_str
)
43
{
44
#if defined(CHECK_STATE)
45
qWarning(
"QGStringBuffer::open: No string"
);
46
#endif
47
return
FALSE;
48
}
49
if
( isOpen() )
50
{
// buffer already open
51
#if defined(CHECK_STATE)
52
qWarning(
"QGStringBuffer::open: Buffer already open"
);
53
#endif
54
return
FALSE;
55
}
56
setMode( m );
57
if
( m & IO_Truncate )
58
{
// truncate buffer
59
m_str
->truncate( 0 );
60
}
61
if
( m & IO_Append )
62
{
// append to end of buffer
63
ioIndex =
m_str
->length();
64
}
65
else
66
{
67
ioIndex = 0;
68
}
69
setState( IO_Open );
70
setStatus( 0 );
71
return
TRUE;
72
}
73
74
void
QGStringBuffer::close
()
75
{
76
if
( isOpen() )
77
{
78
setFlags( IO_Direct );
79
ioIndex = 0;
80
}
81
}
82
83
void
QGStringBuffer::flush
()
84
{
85
}
86
87
uint
QGStringBuffer::size
()
const
88
{
89
return
m_str
?
m_str
->length() : 0;
90
}
91
92
int
QGStringBuffer::at
()
const
93
{
94
return
ioIndex;
95
}
96
97
bool
QGStringBuffer::at
(
int
pos )
98
{
99
#if defined(CHECK_STATE)
100
if
( !isOpen() )
101
{
102
qWarning(
"QGStringBuffer::at: Buffer is not open"
);
103
return
FALSE;
104
}
105
#endif
106
if
( (uint)pos >=
m_str
->length() )
107
{
108
#if defined(CHECK_RANGE)
109
qWarning(
"QGStringBuffer::at: Index %d out of range"
, pos );
110
#endif
111
return
FALSE;
112
}
113
114
ioIndex = pos;
115
return
TRUE;
116
}
117
118
int
QGStringBuffer::writeBlock
(
const
char
*p, uint len )
119
{
120
//printf("QGStringBuffer::writeBlock(%p,%d) m_str=%p ioIndex=%d\n",p,len,
121
// m_str,ioIndex);
122
m_str
->enlarge(ioIndex+len+1);
123
memcpy(
m_str
->data()+ioIndex,p,len);
124
ioIndex+=len;
125
m_str
->data()[ioIndex]=
'\0'
;
126
m_str
->setLen(ioIndex);
127
return
len;
128
}
129
130
int
QGStringBuffer::putch
(
int
ch )
131
{
132
//printf("QGStringBuffer::putch(%d) m_str=%p ioIndex=%d\n",
133
// ch,m_str,ioIndex);
134
m_str
->enlarge(ioIndex+2);
135
m_str
->data()[ioIndex] = (char)ch;
136
ioIndex++;
137
m_str
->data()[ioIndex] =
'\0'
;
138
m_str
->setLen(ioIndex);
139
return
ch;
140
}
141
142
143
//----------------------------------------------------------------------------
144
145
FTextStream::FTextStream
()
146
{
147
m_dev
= 0;
148
m_owndev
= FALSE;
149
}
150
151
FTextStream::FTextStream
( QIODevice *dev )
152
{
153
m_dev
= dev;
154
m_owndev
= FALSE;
155
}
156
157
FTextStream::FTextStream
( QGString *s )
158
{
159
m_dev
=
new
QGStringBuffer
(s);
160
((
QGStringBuffer
*)
m_dev
)->open( IO_WriteOnly );
161
m_owndev
= TRUE;
162
}
163
164
FTextStream::FTextStream
( FILE *fh )
165
{
166
m_dev
=
new
QFile;
167
((QFile *)
m_dev
)->open( IO_WriteOnly, fh);
168
m_owndev
= TRUE;
169
}
170
171
FTextStream::~FTextStream
()
172
{
173
if
(
m_owndev
)
delete
m_dev
;
174
m_dev
= 0;
175
}
176
177
QIODevice *
FTextStream::device
()
const
178
{
179
return
m_dev
;
180
}
181
182
void
FTextStream::setDevice
( QIODevice *dev )
183
{
184
if
(
m_owndev
)
185
{
186
delete
m_dev
;
187
m_owndev
= FALSE;
188
}
189
m_dev
= dev;
190
}
191
192
void
FTextStream::unsetDevice
()
193
{
194
setDevice
(0);
195
}
196
197
FTextStream
&
FTextStream::output_int
( ulong n,
bool
neg )
198
{
199
char
buf[20];
200
char
*p = &buf[19];
201
*p =
'\0'
;
202
if
( neg )
203
{
204
n = (ulong)(-(
long
)n);
205
}
206
do
207
{
208
*--p = ((int)(n%10)) +
'0'
;
209
n /= 10;
210
}
while
( n );
211
if
( neg ) *--p =
'-'
;
212
return
operator<<
(p);
213
}
214
215
FTextStream
&
FTextStream::operator<<
(
signed
short
i )
216
{
217
return
output_int
( i, i < 0 );
218
}
219
220
FTextStream
&
FTextStream::operator<<
(
unsigned
short
i )
221
{
222
return
output_int
( i, FALSE );
223
}
224
225
FTextStream
&
FTextStream::operator<<
(
signed
int
i )
226
{
227
return
output_int
( i, i < 0 );
228
}
229
230
FTextStream
&
FTextStream::operator<<
(
unsigned
int
i )
231
{
232
return
output_int
( i, FALSE );
233
}
234
235
FTextStream
&
FTextStream::operator<<
(
signed
long
i )
236
{
237
return
output_int
( i, i < 0 );
238
}
239
240
FTextStream
&
FTextStream::operator<<
(
unsigned
long
i )
241
{
242
return
output_int
( i, FALSE );
243
}
244
245
FTextStream
&
FTextStream::operator<<
(
float
f )
246
{
247
return
*
this
<< (double)f;
248
}
249
250
FTextStream
&
FTextStream::operator<<
(
double
d )
251
{
252
char
buf[64];
253
sprintf(buf,
"%f"
,d);
254
return
*
this
<< buf;
255
}
256
257
258
259
260