QtBase  v6.3.1
qtextstream.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QTEXTSTREAM_H
41 #define QTEXTSTREAM_H
42 
43 #include <QtCore/qiodevicebase.h>
44 #include <QtCore/qstring.h>
45 #include <QtCore/qchar.h>
46 #include <QtCore/qscopedpointer.h>
47 #include <QtCore/qstringconverter.h>
48 
49 #include <stdio.h>
50 
51 #ifdef Status
52 #error qtextstream.h must be included before any header file that defines Status
53 #endif
54 
56 
57 class QIODevice;
58 class QLocale;
59 
60 class QTextStreamPrivate;
61 class Q_CORE_EXPORT QTextStream : public QIODeviceBase
62 {
63  Q_DECLARE_PRIVATE(QTextStream)
64 
65 public:
69  ScientificNotation
70  };
75  AlignAccountingStyle
76  };
77  enum Status {
78  Ok,
81  WriteFailed
82  };
83  enum NumberFlag {
84  ShowBase = 0x1,
85  ForcePoint = 0x2,
86  ForceSign = 0x4,
87  UppercaseBase = 0x8,
88  UppercaseDigits = 0x10
89  };
90  Q_DECLARE_FLAGS(NumberFlags, NumberFlag)
91 
92  QTextStream();
93  explicit QTextStream(QIODevice *device);
94  explicit QTextStream(FILE *fileHandle, OpenMode openMode = ReadWrite);
95  explicit QTextStream(QString *string, OpenMode openMode = ReadWrite);
96  explicit QTextStream(QByteArray *array, OpenMode openMode = ReadWrite);
97  explicit QTextStream(const QByteArray &array, OpenMode openMode = ReadOnly);
98  virtual ~QTextStream();
99 
100  void setEncoding(QStringConverter::Encoding encoding);
101  QStringConverter::Encoding encoding() const;
102  void setAutoDetectUnicode(bool enabled);
103  bool autoDetectUnicode() const;
104  void setGenerateByteOrderMark(bool generate);
105  bool generateByteOrderMark() const;
106 
107  void setLocale(const QLocale &locale);
108  QLocale locale() const;
109 
110  void setDevice(QIODevice *device);
111  QIODevice *device() const;
112 
113  void setString(QString *string, OpenMode openMode = ReadWrite);
114  QString *string() const;
115 
116  Status status() const;
117  void setStatus(Status status);
118  void resetStatus();
119 
120  bool atEnd() const;
121  void reset();
122  void flush();
123  bool seek(qint64 pos);
124  qint64 pos() const;
125 
126  void skipWhiteSpace();
127 
128  QString readLine(qint64 maxlen = 0);
129  bool readLineInto(QString *line, qint64 maxlen = 0);
130  QString readAll();
131  QString read(qint64 maxlen);
132 
133  void setFieldAlignment(FieldAlignment alignment);
134  FieldAlignment fieldAlignment() const;
135 
136  void setPadChar(QChar ch);
137  QChar padChar() const;
138 
139  void setFieldWidth(int width);
140  int fieldWidth() const;
141 
142  void setNumberFlags(NumberFlags flags);
143  NumberFlags numberFlags() const;
144 
145  void setIntegerBase(int base);
146  int integerBase() const;
147 
148  void setRealNumberNotation(RealNumberNotation notation);
149  RealNumberNotation realNumberNotation() const;
150 
151  void setRealNumberPrecision(int precision);
152  int realNumberPrecision() const;
153 
155  QTextStream &operator>>(char &ch);
156  QTextStream &operator>>(signed short &i);
157  QTextStream &operator>>(unsigned short &i);
158  QTextStream &operator>>(signed int &i);
159  QTextStream &operator>>(unsigned int &i);
160  QTextStream &operator>>(signed long &i);
161  QTextStream &operator>>(unsigned long &i);
164  QTextStream &operator>>(float &f);
165  QTextStream &operator>>(double &f);
168  QTextStream &operator>>(char *c);
169 
171  QTextStream &operator<<(char ch);
172  QTextStream &operator<<(char16_t ch) { return *this << QChar(ch); }
173  QTextStream &operator<<(signed short i);
174  QTextStream &operator<<(unsigned short i);
175  QTextStream &operator<<(signed int i);
176  QTextStream &operator<<(unsigned int i);
177  QTextStream &operator<<(signed long i);
178  QTextStream &operator<<(unsigned long i);
181  QTextStream &operator<<(float f);
182  QTextStream &operator<<(double f);
183  QTextStream &operator<<(const QString &s);
187  QTextStream &operator<<(const char *c);
188  QTextStream &operator<<(const void *ptr);
189 
190 private:
193  friend class QDebug;
194 
196 };
197 
199 
200 /*****************************************************************************
201  QTextStream manipulators
202  *****************************************************************************/
203 
204 typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function
205 typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
206 typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
207 
208 
209 class Q_CORE_EXPORT QTextStreamManipulator
210 {
211 public:
212  constexpr QTextStreamManipulator(QTSMFI m, int a) noexcept : mf(m), mc(nullptr), arg(a), ch() {}
213  constexpr QTextStreamManipulator(QTSMFC m, QChar c) noexcept : mf(nullptr), mc(m), arg(-1), ch(c) {}
214  void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } }
215 
216 private:
217  QTSMFI mf; // QTextStream member function
218  QTSMFC mc; // QTextStream member function
219  int arg; // member function argument
220  QChar ch;
221 };
222 
224 { return (*f)(s); }
225 
227 { return (*f)(s); }
228 
230 { m.exec(s); return s; }
231 
232 namespace Qt {
233 Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
234 Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
235 Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
236 Q_CORE_EXPORT QTextStream &hex(QTextStream &s);
237 
238 Q_CORE_EXPORT QTextStream &showbase(QTextStream &s);
239 Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s);
240 Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s);
241 Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s);
242 Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s);
243 Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s);
244 
245 Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s);
246 Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s);
247 Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s);
248 Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s);
249 
250 Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
251 Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
252 
253 Q_CORE_EXPORT QTextStream &left(QTextStream &s);
254 Q_CORE_EXPORT QTextStream &right(QTextStream &s);
255 Q_CORE_EXPORT QTextStream &center(QTextStream &s);
256 
257 Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
258 Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
259 Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
260 
261 Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
262 
263 Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
264 
265 } // namespace Qt
266 
268 {
271 }
272 
274 {
276  return QTextStreamManipulator(func, ch);
277 }
278 
280 {
283 }
284 
286 
287 #endif // QTEXTSTREAM_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:84
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
Base class for QIODevice that provides flags describing the mode in which a device is opened.
Definition: qiodevicebase.h:48
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:70
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QStrin...
Definition: qstringview.h:122
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:62
QTextStream & operator<<(char16_t ch)
Definition: qtextstream.h:172
void setRealNumberPrecision(int precision)
void setPadChar(QChar ch)
void setFieldWidth(int width)
void exec(QTextStream &s)
Definition: qtextstream.h:214
constexpr QTextStreamManipulator(QTSMFC m, QChar c) noexcept
Definition: qtextstream.h:213
constexpr QTextStreamManipulator(QTSMFI m, int a) noexcept
Definition: qtextstream.h:212
Definition: base.h:37
uint alignment
int flush
Definition: zlib.h:309
auto generate(StringRef generatorName, SourceLineInfo const &lineInfo, L const &generatorExpression) -> decltype(std::declval< decltype(generatorExpression())>().get())
Definition: catch_p_p.h:4085
Definition: qnamespace.h:55
QTextStream & uppercasebase(QTextStream &stream)
QTextStream & bin(QTextStream &stream)
QTextStream & showbase(QTextStream &stream)
QTextStream & lowercasebase(QTextStream &stream)
QTextStream & noforcesign(QTextStream &stream)
QTextStream & uppercasedigits(QTextStream &stream)
QTextStream & bom(QTextStream &stream)
QTextStream & oct(QTextStream &stream)
QTextStream & lowercasedigits(QTextStream &stream)
QTextStream & noshowbase(QTextStream &stream)
QTextStream & noforcepoint(QTextStream &stream)
QTextStream & ws(QTextStream &stream)
QTextStream & fixed(QTextStream &stream)
QTextStream & forcesign(QTextStream &stream)
QTextStream & center(QTextStream &stream)
QTextStream & forcepoint(QTextStream &stream)
QTextStream & endl(QTextStream &stream)
QTextStream & scientific(QTextStream &stream)
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition: qflags.h:210
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition: qflags.h:227
quint64 qulonglong
Definition: qglobal.h:302
long long qint64
Definition: qglobal.h:298
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
qint64 qlonglong
Definition: qglobal.h:301
const GLfloat * m
GLboolean GLboolean GLboolean GLboolean a
[7]
GLdouble GLdouble right
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLfloat GLfloat f
GLint GLsizei width
GLint left
GLbitfield flags
GLboolean reset
Definition: qopenglext.h:2748
GLenum func
Definition: qopenglext.h:663
const GLubyte * c
Definition: qopenglext.h:12701
GLenum array
Definition: qopenglext.h:7028
GLdouble s
[6]
Definition: qopenglext.h:235
GLenum GLint GLint * precision
Definition: qopenglext.h:1890
GLsizei const GLchar *const * string
[0]
Definition: qopenglext.h:694
SSL_CTX int(*) void arg)
QTextStreamManipulator qSetPadChar(QChar ch)
Definition: qtextstream.h:273
QTextStream &(* QTextStreamFunction)(QTextStream &)
Definition: qtextstream.h:204
QTextStreamManipulator qSetRealNumberPrecision(int precision)
Definition: qtextstream.h:279
QTextStream & operator>>(QTextStream &s, QTextStreamFunction f)
Definition: qtextstream.h:223
QTextStream & operator<<(QTextStream &s, QTextStreamFunction f)
Definition: qtextstream.h:226
QTextStreamManipulator qSetFieldWidth(int width)
Definition: qtextstream.h:267
void(QTextStream::* QTSMFC)(QChar)
Definition: qtextstream.h:206
void(QTextStream::* QTSMFI)(int)
Definition: qtextstream.h:205
out setFieldAlignment(QTextStream::AlignCenter)
out setPadChar('-')
out setFieldWidth(10)