Fix for UBSan build
[platform/upstream/doxygen.git] / src / ftextstream.cpp
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 }
169
170 FTextStream::~FTextStream()
171 {
172   if (m_owndev) delete m_dev;
173   m_dev = 0;
174 }
175
176 QIODevice *FTextStream::device() const
177 {
178   return m_dev;
179 }
180
181 void FTextStream::setDevice( QIODevice *dev )
182 {
183   if (m_owndev) 
184   {
185     delete m_dev;
186     m_owndev = FALSE;
187   }
188   m_dev = dev;
189 }
190
191 void FTextStream::unsetDevice()
192 {
193   setDevice(0);
194 }
195
196 FTextStream &FTextStream::output_int( ulong n, bool neg )
197 {
198   char buf[20];
199   char *p = &buf[19];
200   *p = '\0';
201   if ( neg )
202   {
203     n = (ulong)(-(long)n);
204   }
205   do 
206   {
207     *--p = ((int)(n%10)) + '0';
208     n /= 10;
209   } while ( n );
210   if ( neg ) *--p = '-';
211   return operator<<(p);
212 }
213
214 FTextStream &FTextStream::operator<<( signed short i )
215 {
216     return output_int( i, i < 0 );
217 }
218
219 FTextStream &FTextStream::operator<<( unsigned short i )
220 {
221     return output_int( i, FALSE );
222 }
223
224 FTextStream &FTextStream::operator<<( signed int i )
225 {
226     return output_int( i, i < 0 );
227 }
228
229 FTextStream &FTextStream::operator<<( unsigned int i )
230 {
231     return output_int( i, FALSE );
232 }
233
234 FTextStream &FTextStream::operator<<( signed long i )
235 {
236     return output_int( i, i < 0 );
237 }
238
239 FTextStream &FTextStream::operator<<( unsigned long i )
240 {
241     return output_int( i, FALSE );
242 }
243
244 FTextStream &FTextStream::operator<<( float f )
245 {
246     return *this << (double)f;
247 }
248
249 FTextStream &FTextStream::operator<<( double d )
250 {
251     char buf[64];
252     sprintf(buf,"%f",d);
253     return *this << buf;
254 }
255
256
257
258
259