1 #include "ftextstream.h"
4 //----------------------------------------------------------------------------
6 class QGStringBuffer : public QIODevice
9 QGStringBuffer( QGString* str );
17 int readBlock( char *, uint) { return -1; }
18 int writeBlock( const char *p, uint len );
19 int getch() { return -1; }
21 int ungetch( int ) { return -1; }
26 private: // Disabled copy constructor and operator=
27 QGStringBuffer( const QGStringBuffer & );
28 QGStringBuffer &operator=( const QGStringBuffer & );
31 QGStringBuffer::QGStringBuffer( QGString* str ) : m_str(str)
33 //printf("QGStringBuffer::QGStringBuffer(%p)\n",str);
36 QGStringBuffer::~QGStringBuffer()
40 bool QGStringBuffer::open( int m )
44 #if defined(CHECK_STATE)
45 qWarning( "QGStringBuffer::open: No string" );
50 { // buffer already open
51 #if defined(CHECK_STATE)
52 qWarning( "QGStringBuffer::open: Buffer already open" );
57 if ( m & IO_Truncate )
62 { // append to end of buffer
63 ioIndex = m_str->length();
74 void QGStringBuffer::close()
78 setFlags( IO_Direct );
83 void QGStringBuffer::flush()
87 uint QGStringBuffer::size() const
89 return m_str ? m_str->length() : 0;
92 int QGStringBuffer::at() const
97 bool QGStringBuffer::at( int pos )
99 #if defined(CHECK_STATE)
102 qWarning( "QGStringBuffer::at: Buffer is not open" );
106 if ( (uint)pos >= m_str->length() )
108 #if defined(CHECK_RANGE)
109 qWarning( "QGStringBuffer::at: Index %d out of range", pos );
118 int QGStringBuffer::writeBlock( const char *p, uint len )
120 //printf("QGStringBuffer::writeBlock(%p,%d) m_str=%p ioIndex=%d\n",p,len,
122 m_str->enlarge(ioIndex+len+1);
123 memcpy(m_str->data()+ioIndex,p,len);
125 m_str->data()[ioIndex]='\0';
126 m_str->setLen(ioIndex);
130 int QGStringBuffer::putch( int ch )
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;
137 m_str->data()[ioIndex] = '\0';
138 m_str->setLen(ioIndex);
143 //----------------------------------------------------------------------------
145 FTextStream::FTextStream()
151 FTextStream::FTextStream( QIODevice *dev )
157 FTextStream::FTextStream( QGString *s )
159 m_dev = new QGStringBuffer(s);
160 ((QGStringBuffer*)m_dev)->open( IO_WriteOnly );
164 FTextStream::FTextStream( FILE *fh )
167 ((QFile *)m_dev)->open( IO_WriteOnly, fh);
170 FTextStream::~FTextStream()
172 if (m_owndev) delete m_dev;
176 QIODevice *FTextStream::device() const
181 void FTextStream::setDevice( QIODevice *dev )
191 void FTextStream::unsetDevice()
196 FTextStream &FTextStream::output_int( ulong n, bool neg )
203 n = (ulong)(-(long)n);
207 *--p = ((int)(n%10)) + '0';
210 if ( neg ) *--p = '-';
211 return operator<<(p);
214 FTextStream &FTextStream::operator<<( signed short i )
216 return output_int( i, i < 0 );
219 FTextStream &FTextStream::operator<<( unsigned short i )
221 return output_int( i, FALSE );
224 FTextStream &FTextStream::operator<<( signed int i )
226 return output_int( i, i < 0 );
229 FTextStream &FTextStream::operator<<( unsigned int i )
231 return output_int( i, FALSE );
234 FTextStream &FTextStream::operator<<( signed long i )
236 return output_int( i, i < 0 );
239 FTextStream &FTextStream::operator<<( unsigned long i )
241 return output_int( i, FALSE );
244 FTextStream &FTextStream::operator<<( float f )
246 return *this << (double)f;
249 FTextStream &FTextStream::operator<<( double d )