Qt 4.8
qtextstream.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QTEXTSTREAM_H
43 #define QTEXTSTREAM_H
44 
45 #include <QtCore/qiodevice.h>
46 #include <QtCore/qstring.h>
47 #include <QtCore/qchar.h>
48 #include <QtCore/qlocale.h>
49 #include <QtCore/qscopedpointer.h>
50 
51 #ifndef QT_NO_TEXTCODEC
52 # ifdef QT3_SUPPORT
53 # include <QtCore/qtextcodec.h>
54 # endif
55 #endif
56 
57 #include <stdio.h>
58 
59 #ifdef Status
60 #error qtextstream.h must be included before any header file that defines Status
61 #endif
62 
64 
66 
67 QT_MODULE(Core)
68 
69 class QTextCodec;
70 class QTextDecoder;
71 
72 class QTextStreamPrivate;
73 class Q_CORE_EXPORT QTextStream // text stream class
74 {
76 
77 public:
81  ScientificNotation
82  };
87  AlignAccountingStyle
88  };
89  enum Status {
90  Ok,
93  WriteFailed
94  };
95  enum NumberFlag {
96  ShowBase = 0x1,
97  ForcePoint = 0x2,
98  ForceSign = 0x4,
99  UppercaseBase = 0x8,
100  UppercaseDigits = 0x10
101  };
102  Q_DECLARE_FLAGS(NumberFlags, NumberFlag)
103 
104  QTextStream();
105  explicit QTextStream(QIODevice *device);
106  explicit QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
107  explicit QTextStream(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
108  explicit QTextStream(QByteArray *array, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
109  explicit QTextStream(const QByteArray &array, QIODevice::OpenMode openMode = QIODevice::ReadOnly);
110  virtual ~QTextStream();
111 
112 #ifndef QT_NO_TEXTCODEC
113  void setCodec(QTextCodec *codec);
114  void setCodec(const char *codecName);
115  QTextCodec *codec() const;
116  void setAutoDetectUnicode(bool enabled);
117  bool autoDetectUnicode() const;
118  void setGenerateByteOrderMark(bool generate);
119  bool generateByteOrderMark() const;
120 #endif
121 
122  void setLocale(const QLocale &locale);
123  QLocale locale() const;
124 
125  void setDevice(QIODevice *device);
126  QIODevice *device() const;
127 
128  void setString(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
129  QString *string() const;
130 
131  Status status() const;
132  void setStatus(Status status);
133  void resetStatus();
134 
135  bool atEnd() const;
136  void reset();
137  void flush();
138  bool seek(qint64 pos);
139  qint64 pos() const;
140 
141  void skipWhiteSpace();
142 
143  QString readLine(qint64 maxlen = 0);
144  QString readAll();
145  QString read(qint64 maxlen);
146 
147  void setFieldAlignment(FieldAlignment alignment);
148  FieldAlignment fieldAlignment() const;
149 
150  void setPadChar(QChar ch);
151  QChar padChar() const;
152 
153  void setFieldWidth(int width);
154  int fieldWidth() const;
155 
156  void setNumberFlags(NumberFlags flags);
157  NumberFlags numberFlags() const;
158 
159  void setIntegerBase(int base);
160  int integerBase() const;
161 
162  void setRealNumberNotation(RealNumberNotation notation);
163  RealNumberNotation realNumberNotation() const;
164 
165  void setRealNumberPrecision(int precision);
166  int realNumberPrecision() const;
167 
169  QTextStream &operator>>(char &ch);
170  QTextStream &operator>>(signed short &i);
171  QTextStream &operator>>(unsigned short &i);
172  QTextStream &operator>>(signed int &i);
173  QTextStream &operator>>(unsigned int &i);
174  QTextStream &operator>>(signed long &i);
175  QTextStream &operator>>(unsigned long &i);
178  QTextStream &operator>>(float &f);
179  QTextStream &operator>>(double &f);
182  QTextStream &operator>>(char *c);
183 
186  QTextStream &operator<<(char ch);
187  QTextStream &operator<<(signed short i);
188  QTextStream &operator<<(unsigned short i);
189  QTextStream &operator<<(signed int i);
190  QTextStream &operator<<(unsigned int i);
191  QTextStream &operator<<(signed long i);
192  QTextStream &operator<<(unsigned long i);
195  QTextStream &operator<<(float f);
196  QTextStream &operator<<(double f);
197  QTextStream &operator<<(const QString &s);
198  QTextStream &operator<<(const QByteArray &array);
199  QTextStream &operator<<(const char *c);
200  QTextStream &operator<<(const void *ptr);
201 
202 #ifdef QT3_SUPPORT
203  // not marked as QT3_SUPPORT to avoid double compiler warnings, as
204  // they are used in the QT3_SUPPORT functions below.
205  inline QT3_SUPPORT int flags() const { return flagsInternal(); }
206  inline QT3_SUPPORT int flags(int f) { return flagsInternal(f); }
207 
208  inline QT3_SUPPORT int setf(int bits)
209  { int old = flagsInternal(); flagsInternal(flagsInternal() | bits); return old; }
210  inline QT3_SUPPORT int setf(int bits, int mask)
211  { int old = flagsInternal(); flagsInternal(flagsInternal() | (bits & mask)); return old; }
212  inline QT3_SUPPORT int unsetf(int bits)
213  { int old = flagsInternal(); flagsInternal(flagsInternal() & ~bits); return old; }
214 
215  inline QT3_SUPPORT int width(int w)
216  { int old = fieldWidth(); setFieldWidth(w); return old; }
217  inline QT3_SUPPORT int fill(int f)
218  { QChar ch = padChar(); setPadChar(QChar(f)); return ch.unicode(); }
219  inline QT3_SUPPORT int precision(int p)
220  { int old = realNumberPrecision(); setRealNumberPrecision(p); return old; }
221 
222  enum {
223  skipws = 0x0001, // skip whitespace on input
224  left = 0x0002, // left-adjust output
225  right = 0x0004, // right-adjust output
226  internal = 0x0008, // pad after sign
227  bin = 0x0010, // binary format integer
228  oct = 0x0020, // octal format integer
229  dec = 0x0040, // decimal format integer
230  hex = 0x0080, // hex format integer
231  showbase = 0x0100, // show base indicator
232  showpoint = 0x0200, // force decimal point (float)
233  uppercase = 0x0400, // upper-case hex output
234  showpos = 0x0800, // add '+' to positive integers
235  scientific = 0x1000, // scientific float output
236  fixed = 0x2000 // fixed float output
237  };
238  enum {
239  basefield = bin | oct | dec | hex,
240  adjustfield = left | right | internal,
241  floatfield = scientific | fixed
242  };
243 
244 #ifndef QT_NO_TEXTCODEC
245  enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder,
246  UnicodeReverse, RawUnicode, UnicodeUTF8 };
247  QT3_SUPPORT void setEncoding(Encoding encoding);
248 #endif
249  inline QT3_SUPPORT QString read() { return readAll(); }
250  inline QT3_SUPPORT void unsetDevice() { setDevice(0); }
251 #endif
252 
253 private:
254 #ifdef QT3_SUPPORT
255  int flagsInternal() const;
256  int flagsInternal(int flags);
257 #endif
258 
260 
262 };
263 
265 
266 /*****************************************************************************
267  QTextStream manipulators
268  *****************************************************************************/
269 
270 typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function
271 typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
272 typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
273 
275 {
276 public:
277  QTextStreamManipulator(QTSMFI m, int a) { mf = m; mc = 0; arg = a; }
278  QTextStreamManipulator(QTSMFC m, QChar c) { mf = 0; mc = m; ch = c; arg = -1; }
279  void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } }
280 
281 private:
282  QTSMFI mf; // QTextStream member function
283  QTSMFC mc; // QTextStream member function
284  int arg; // member function argument
285  QChar ch;
286 };
287 
289 { return (*f)(s); }
290 
292 { return (*f)(s); }
293 
295 { m.exec(s); return s; }
296 
301 
308 
313 
316 
320 
324 
326 
328 
330 {
332  return QTextStreamManipulator(func,width);
333 }
334 
336 {
338  return QTextStreamManipulator(func, ch);
339 }
340 
342 {
344  return QTextStreamManipulator(func, precision);
345 }
346 
347 #ifdef QT3_SUPPORT
348 typedef QTextStream QTS;
349 
350 class Q_CORE_EXPORT QTextIStream : public QTextStream
351 {
352 public:
353  inline explicit QTextIStream(const QString *s) : QTextStream(const_cast<QString *>(s), QIODevice::ReadOnly) {}
354  inline explicit QTextIStream(QByteArray *a) : QTextStream(a, QIODevice::ReadOnly) {}
355  inline QTextIStream(FILE *f) : QTextStream(f, QIODevice::ReadOnly) {}
356 
357 private:
358  Q_DISABLE_COPY(QTextIStream)
359 };
360 
361 class Q_CORE_EXPORT QTextOStream : public QTextStream
362 {
363 public:
364  inline explicit QTextOStream(QString *s) : QTextStream(s, QIODevice::WriteOnly) {}
365  inline explicit QTextOStream(QByteArray *a) : QTextStream(a, QIODevice::WriteOnly) {}
366  inline QTextOStream(FILE *f) : QTextStream(f, QIODevice::WriteOnly) {}
367 
368 private:
369  Q_DISABLE_COPY(QTextOStream)
370 };
371 #endif
372 
374 
376 
377 #endif // QTEXTSTREAM_H
Q_CORE_EXPORT QTextStream & bom(QTextStream &s)
QTextStream & operator>>(QTextStream &s, QTextStreamFunction f)
Definition: qtextstream.h:288
QTextStream()
Constructs a QTextStream.
void(QTextStream::* QTSMFI)(int)
Definition: qtextstream.h:271
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
Q_CORE_EXPORT QTextStream & ws(QTextStream &s)
#define QT_MODULE(x)
Definition: qglobal.h:2783
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_DECLARE_FLAGS(Flags, Enum)
The Q_DECLARE_FLAGS() macro expands to.
Definition: qglobal.h:2348
Q_CORE_EXPORT QTextStream & oct(QTextStream &s)
void(QTextStream::* QTSMFC)(QChar)
Definition: qtextstream.h:272
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Q_CORE_EXPORT QTextStream & reset(QTextStream &s)
The QTextDecoder class provides a state-based decoder.
Definition: qtextcodec.h:177
FieldAlignment
This enum specifies how to align text in fields when the field is wider than the text that occupies i...
Definition: qtextstream.h:83
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
void setFieldWidth(int width)
Sets the current field width to width.
long ASN1_INTEGER_get ASN1_INTEGER * a
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT QTextStream & uppercasedigits(QTextStream &s)
static const uint base
Definition: qurl.cpp:268
Q_CORE_EXPORT QTextStream & lowercasebase(QTextStream &s)
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
Q_CORE_EXPORT QTextStream & hex(QTextStream &s)
Q_CORE_EXPORT QTextStream & dec(QTextStream &s)
Q_CORE_EXPORT QTextStream & forcepoint(QTextStream &s)
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
Q_CORE_EXPORT QTextStream & fixed(QTextStream &s)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags...
Definition: qglobal.h:2355
QTextStreamManipulator(QTSMFI m, int a)
Definition: qtextstream.h:277
Q_CORE_EXPORT QTextStream & bin(QTextStream &s)
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
void setRealNumberPrecision(int precision)
Sets the precision of real numbers to precision.
const T * ptr(const T &t)
__int64 qint64
Definition: qglobal.h:942
handler setDevice(device)
Q_CORE_EXPORT QTextStream & showbase(QTextStream &s)
QTextStreamManipulator qSetPadChar(QChar ch)
Definition: qtextstream.h:335
RealNumberNotation
This enum specifies which notations to use for expressing float and double as strings.
Definition: qtextstream.h:78
Q_CORE_EXPORT QTextStream & center(QTextStream &s)
QTextStream &(* QTextStreamFunction)(QTextStream &)
Definition: qtextstream.h:270
QTextStream & operator<<(QTextStream &s, QTextStreamFunction f)
Definition: qtextstream.h:291
Status
This enum describes the current status of the text stream.
Definition: qtextstream.h:89
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
void exec(QTextStream &s)
Definition: qtextstream.h:279
QTextStreamManipulator(QTSMFC m, QChar c)
Definition: qtextstream.h:278
Q_CORE_EXPORT QTextStream & forcesign(QTextStream &s)
Q_CORE_EXPORT QTextStream & noshowbase(QTextStream &s)
Q_CORE_EXPORT QTextStream & lowercasedigits(QTextStream &s)
Q_CORE_EXPORT QTextStream & flush(QTextStream &s)
QTextStreamManipulator qSetRealNumberPrecision(int precision)
Definition: qtextstream.h:341
Q_CORE_EXPORT QTextStream & scientific(QTextStream &s)
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
quint64 qulonglong
Definition: qglobal.h:952
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
#define class
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
QTextStreamManipulator qSetFieldWidth(int width)
Definition: qtextstream.h:329
qint64 qlonglong
Definition: qglobal.h:951
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
#define QT_END_HEADER
Definition: qglobal.h:137
Q_CORE_EXPORT QTextStream & noforcesign(QTextStream &s)
void setPadChar(QChar ch)
Sets the pad character to ch.
#define enabled
Q_CORE_EXPORT QTextStream & noforcepoint(QTextStream &s)
Q_CORE_EXPORT QTextStream & uppercasebase(QTextStream &s)
NumberFlag
This enum specifies various flags that can be set to affect the output of integers, floats, and doubles.
Definition: qtextstream.h:95
Q_CORE_EXPORT QTextStream & endl(QTextStream &s)