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:
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:
25 
26  private: // Disabled copy constructor and operator=
27  QGStringBuffer( const QGStringBuffer & );
29 };
30 
32 {
33  //printf("QGStringBuffer::QGStringBuffer(%p)\n",str);
34 }
35 
37 {
38 }
39 
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 
75 {
76  if ( isOpen() )
77  {
79  ioIndex = 0;
80  }
81 }
82 
84 {
85 }
86 
88 {
89  return m_str ? m_str->length() : 0;
90 }
91 
92 int QGStringBuffer::at() const
93 {
94  return ioIndex;
95 }
96 
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 
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 
146 {
147  m_dev = 0;
148  m_owndev = FALSE;
149 }
150 
152 {
153  m_dev = dev;
154  m_owndev = FALSE;
155 }
156 
158 {
159  m_dev = new QGStringBuffer(s);
160  ((QGStringBuffer*)m_dev)->open( IO_WriteOnly );
161  m_owndev = TRUE;
162 }
163 
165 {
166  m_dev = new QFile;
167  ((QFile *)m_dev)->open( IO_WriteOnly, fh);
168  m_owndev = TRUE;
169 }
170 
172 {
173  if (m_owndev) delete m_dev;
174  m_dev = 0;
175 }
176 
178 {
179  return m_dev;
180 }
181 
183 {
184  if (m_owndev)
185  {
186  delete m_dev;
187  m_owndev = FALSE;
188  }
189  m_dev = dev;
190 }
191 
193 {
194  setDevice(0);
195 }
196 
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 
216 {
217  return output_int( i, i < 0 );
218 }
219 
221 {
222  return output_int( i, FALSE );
223 }
224 
226 {
227  return output_int( i, i < 0 );
228 }
229 
231 {
232  return output_int( i, FALSE );
233 }
234 
236 {
237  return output_int( i, i < 0 );
238 }
239 
241 {
242  return output_int( i, FALSE );
243 }
244 
246 {
247  return *this << (double)f;
248 }
249 
251 {
252  char buf[64];
253  sprintf(buf,"%f",d);
254  return *this << buf;
255 }
256 
257 
258 
259 
260 
int putch(int ch)
char * data() const
Definition: qgstring.h:42
int readBlock(char *, uint)
Definition: ftextstream.cpp:17
uint length() const
Definition: qgstring.h:40
#define IO_WriteOnly
Definition: qiodevice.h:62
void setFlags(int f)
Definition: qiodevice.h:136
void setDevice(QIODevice *)
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setMode(int)
Definition: qiodevice.cpp:378
Simplified and optimized version of QTextStream.
Definition: ftextstream.h:11
void unsetDevice()
void setStatus(int)
Definition: qiodevice.cpp:408
#define IO_Direct
Definition: qiodevice.h:49
FTextStream & operator<<(char)
Definition: ftextstream.h:49
bool truncate(uint pos)
Definition: qgstring.h:43
bool open(int m)
Definition: ftextstream.cpp:40
std::void_t< T > n
bool open(int)
Definition: qfile_unix.cpp:134
int writeBlock(const char *p, uint len)
#define IO_Truncate
Definition: qiodevice.h:65
unsigned long ulong
Definition: qglobal.h:352
p
Definition: test.py:223
#define IO_Append
Definition: qiodevice.h:64
std::ostream & operator<<(std::ostream &os, Analyzer::Table< T > const &t)
Definition: Analyzer.h:136
QIODevice * device() const
QGString * m_str
Definition: ftextstream.cpp:24
QGStringBuffer & operator=(const QGStringBuffer &)
int ungetch(int)
Definition: ftextstream.cpp:21
The QFile class is an I/O device that operates on files.
Definition: qfile.h:50
void setState(int)
Definition: qiodevice.cpp:393
int at() const
Definition: ftextstream.cpp:92
FTextStream & output_int(ulong n, bool neg)
QGStringBuffer(QGString *str)
Definition: ftextstream.cpp:31
void setLen(uint newlen)
Definition: qgstring.cpp:156
bool enlarge(uint newlen)
Definition: qgstring.cpp:127
virtual ~FTextStream()
The QIODevice class is the base class of I/O devices.
Definition: qiodevice.h:88
bool isOpen() const
Definition: qiodevice.h:110
unsigned uint
Definition: qglobal.h:351
#define IO_Open
Definition: qiodevice.h:71
static QCString * s
Definition: config.cpp:1042
const bool TRUE
Definition: qglobal.h:371
static QCString str
uint size() const
Definition: ftextstream.cpp:87
int ioIndex
Definition: qiodevice.h:141