| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** All rights reserved. |
| 5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
| 6 |
** |
| 7 |
** This file is part of the QtCore module of the Qt Toolkit. |
| 8 |
** |
| 9 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 10 |
** No Commercial Usage |
| 11 |
** This file contains pre-release code and may not be distributed. |
| 12 |
** You may use this file in accordance with the terms and conditions |
| 13 |
** contained in the Technology Preview License Agreement accompanying |
| 14 |
** this package. |
| 15 |
** |
| 16 |
** GNU Lesser General Public License Usage |
| 17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
| 18 |
** General Public License version 2.1 as published by the Free Software |
| 19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
| 20 |
** packaging of this file. Please review the following information to |
| 21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
| 22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 23 |
** |
| 24 |
** In addition, as a special exception, Nokia gives you certain additional |
| 25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 27 |
** |
| 28 |
** If you have questions regarding the use of this file, please contact |
| 29 |
** Nokia at qt-info@nokia.com. |
| 30 |
** |
| 31 |
** |
| 32 |
** |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 37 |
** |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
//#define QTEXTSTREAM_DEBUG |
| 43 |
static const int QTEXTSTREAM_BUFFERSIZE = 16384; |
| 44 |
|
| 45 |
/*! |
| 46 |
\class QTextStream |
| 47 |
|
| 48 |
\brief The QTextStream class provides a convenient interface for |
| 49 |
reading and writing text. |
| 50 |
|
| 51 |
\ingroup io |
| 52 |
\ingroup text |
| 53 |
\reentrant |
| 54 |
|
| 55 |
QTextStream can operate on a QIODevice, a QByteArray or a |
| 56 |
QString. Using QTextStream's streaming operators, you can |
| 57 |
conveniently read and write words, lines and numbers. For |
| 58 |
generating text, QTextStream supports formatting options for field |
| 59 |
padding and alignment, and formatting of numbers. Example: |
| 60 |
|
| 61 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 0 |
| 62 |
|
| 63 |
It's also common to use QTextStream to read console input and write |
| 64 |
console output. QTextStream is locale aware, and will automatically decode |
| 65 |
standard input using the correct codec. Example: |
| 66 |
|
| 67 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 1 |
| 68 |
|
| 69 |
Note that you cannot use QTextStream::atEnd(), which returns true when you |
| 70 |
have reached the end of the data stream, with stdin. The reason for this is |
| 71 |
that as long as stdin doesn't give any input to the QTextStream, \c atEnd() |
| 72 |
will return true even if the stdin is open and waiting for more characters. |
| 73 |
|
| 74 |
Besides using QTextStream's constructors, you can also set the |
| 75 |
device or string QTextStream operates on by calling setDevice() or |
| 76 |
setString(). You can seek to a position by calling seek(), and |
| 77 |
atEnd() will return true when there is no data left to be read. If |
| 78 |
you call flush(), QTextStream will empty all data from its write |
| 79 |
buffer into the device and call flush() on the device. |
| 80 |
|
| 81 |
Internally, QTextStream uses a Unicode based buffer, and |
| 82 |
QTextCodec is used by QTextStream to automatically support |
| 83 |
different character sets. By default, QTextCodec::codecForLocale() |
| 84 |
is used for reading and writing, but you can also set the codec by |
| 85 |
calling setCodec(). Automatic Unicode detection is also |
| 86 |
supported. When this feature is enabled (the default behavior), |
| 87 |
QTextStream will detect the UTF-16 or the UTF-32 BOM (Byte Order Mark) and |
| 88 |
switch to the appropriate UTF codec when reading. QTextStream |
| 89 |
does not write a BOM by default, but you can enable this by calling |
| 90 |
setGenerateByteOrderMark(true). When QTextStream operates on a QString |
| 91 |
directly, the codec is disabled. |
| 92 |
|
| 93 |
There are three general ways to use QTextStream when reading text |
| 94 |
files: |
| 95 |
|
| 96 |
\list |
| 97 |
|
| 98 |
\o Chunk by chunk, by calling readLine() or readAll(). |
| 99 |
|
| 100 |
\o Word by word. QTextStream supports streaming into QStrings, |
| 101 |
QByteArrays and char* buffers. Words are delimited by space, and |
| 102 |
leading white space is automatically skipped. |
| 103 |
|
| 104 |
\o Character by character, by streaming into QChar or char types. |
| 105 |
This method is often used for convenient input handling when |
| 106 |
parsing files, independent of character encoding and end-of-line |
| 107 |
semantics. To skip white space, call skipWhiteSpace(). |
| 108 |
|
| 109 |
\endlist |
| 110 |
|
| 111 |
Since the text stream uses a buffer, you should not read from |
| 112 |
the stream using the implementation of a superclass. For instance, |
| 113 |
if you have a QFile and read from it directly using |
| 114 |
QFile::readLine() instead of using the stream, the text stream's |
| 115 |
internal position will be out of sync with the file's position. |
| 116 |
|
| 117 |
By default, when reading numbers from a stream of text, |
| 118 |
QTextStream will automatically detect the number's base |
| 119 |
representation. For example, if the number starts with "0x", it is |
| 120 |
assumed to be in hexadecimal form. If it starts with the digits |
| 121 |
1-9, it is assumed to be in decimal form, and so on. You can set |
| 122 |
the integer base, thereby disabling the automatic detection, by |
| 123 |
calling setIntegerBase(). Example: |
| 124 |
|
| 125 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 2 |
| 126 |
|
| 127 |
QTextStream supports many formatting options for generating text. |
| 128 |
You can set the field width and pad character by calling |
| 129 |
setFieldWidth() and setPadChar(). Use setFieldAlignment() to set |
| 130 |
the alignment within each field. For real numbers, call |
| 131 |
setRealNumberNotation() and setRealNumberPrecision() to set the |
| 132 |
notation (SmartNotation, ScientificNotation, FixedNotation) and precision in |
| 133 |
digits of the generated number. Some extra number formatting |
| 134 |
options are also available through setNumberFlags(). |
| 135 |
|
| 136 |
\keyword QTextStream manipulators |
| 137 |
|
| 138 |
Like \c <iostream> in the standard C++ library, QTextStream also |
| 139 |
defines several global manipulator functions: |
| 140 |
|
| 141 |
\table |
| 142 |
\header \o Manipulator \o Description |
| 143 |
\row \o \c bin \o Same as setIntegerBase(2). |
| 144 |
\row \o \c oct \o Same as setIntegerBase(8). |
| 145 |
\row \o \c dec \o Same as setIntegerBase(10). |
| 146 |
\row \o \c hex \o Same as setIntegerBase(16). |
| 147 |
\row \o \c showbase \o Same as setNumberFlags(numberFlags() | ShowBase). |
| 148 |
\row \o \c forcesign \o Same as setNumberFlags(numberFlags() | ForceSign). |
| 149 |
\row \o \c forcepoint \o Same as setNumberFlags(numberFlags() | ForcePoint). |
| 150 |
\row \o \c noshowbase \o Same as setNumberFlags(numberFlags() & ~ShowBase). |
| 151 |
\row \o \c noforcesign \o Same as setNumberFlags(numberFlags() & ~ForceSign). |
| 152 |
\row \o \c noforcepoint \o Same as setNumberFlags(numberFlags() & ~ForcePoint). |
| 153 |
\row \o \c uppercasebase \o Same as setNumberFlags(numberFlags() | UppercaseBase). |
| 154 |
\row \o \c uppercasedigits \o Same as setNumberFlags(numberFlags() | UppercaseDigits). |
| 155 |
\row \o \c lowercasebase \o Same as setNumberFlags(numberFlags() & ~UppercaseBase). |
| 156 |
\row \o \c lowercasedigits \o Same as setNumberFlags(numberFlags() & ~UppercaseDigits). |
| 157 |
\row \o \c fixed \o Same as setRealNumberNotation(FixedNotation). |
| 158 |
\row \o \c scientific \o Same as setRealNumberNotation(ScientificNotation). |
| 159 |
\row \o \c left \o Same as setFieldAlignment(AlignLeft). |
| 160 |
\row \o \c right \o Same as setFieldAlignment(AlignRight). |
| 161 |
\row \o \c center \o Same as setFieldAlignment(AlignCenter). |
| 162 |
\row \o \c endl \o Same as operator<<('\n') and flush(). |
| 163 |
\row \o \c flush \o Same as flush(). |
| 164 |
\row \o \c reset \o Same as reset(). |
| 165 |
\row \o \c ws \o Same as skipWhiteSpace(). |
| 166 |
\row \o \c bom \o Same as setGenerateByteOrderMark(true). |
| 167 |
\endtable |
| 168 |
|
| 169 |
In addition, Qt provides three global manipulators that take a |
| 170 |
parameter: qSetFieldWidth(), qSetPadChar(), and |
| 171 |
qSetRealNumberPrecision(). |
| 172 |
|
| 173 |
\sa QDataStream, QIODevice, QFile, QBuffer, QTcpSocket, {Codecs Example} |
| 174 |
*/ |
| 175 |
|
| 176 |
/*! \enum QTextStream::RealNumberNotation |
| 177 |
|
| 178 |
This enum specifies which notations to use for expressing \c |
| 179 |
float and \c double as strings. |
| 180 |
|
| 181 |
\value ScientificNotation Scientific notation (\c{printf()}'s \c %e flag). |
| 182 |
\value FixedNotation Fixed-point notation (\c{printf()}'s \c %f flag). |
| 183 |
\value SmartNotation Scientific or fixed-point notation, depending on which makes most sense (\c{printf()}'s \c %g flag). |
| 184 |
|
| 185 |
\sa setRealNumberNotation() |
| 186 |
*/ |
| 187 |
|
| 188 |
/*! \enum QTextStream::FieldAlignment |
| 189 |
|
| 190 |
This enum specifies how to align text in fields when the field is |
| 191 |
wider than the text that occupies it. |
| 192 |
|
| 193 |
\value AlignLeft Pad on the right side of fields. |
| 194 |
\value AlignRight Pad on the left side of fields. |
| 195 |
\value AlignCenter Pad on both sides of field. |
| 196 |
\value AlignAccountingStyle Same as AlignRight, except that the |
| 197 |
sign of a number is flush left. |
| 198 |
|
| 199 |
\sa setFieldAlignment() |
| 200 |
*/ |
| 201 |
|
| 202 |
/*! \enum QTextStream::NumberFlag |
| 203 |
|
| 204 |
This enum specifies various flags that can be set to affect the |
| 205 |
output of integers, \c{float}s, and \c{double}s. |
| 206 |
|
| 207 |
\value ShowBase Show the base as a prefix if the base |
| 208 |
is 16 ("0x"), 8 ("0"), or 2 ("0b"). |
| 209 |
\value ForcePoint Always put the decimal separator in numbers, even if |
| 210 |
there are no decimals. |
| 211 |
\value ForceSign Always put the sign in numbers, even for positive numbers. |
| 212 |
\value UppercaseBase Use uppercase versions of base prefixes ("0X", "0B"). |
| 213 |
\value UppercaseDigits Use uppercase letters for expressing |
| 214 |
digits 10 to 35 instead of lowercase. |
| 215 |
|
| 216 |
\sa setNumberFlags() |
| 217 |
*/ |
| 218 |
|
| 219 |
/*! \enum QTextStream::Status |
| 220 |
|
| 221 |
This enum describes the current status of the text stream. |
| 222 |
|
| 223 |
\value Ok The text stream is operating normally. |
| 224 |
\value ReadPastEnd The text stream has read past the end of the |
| 225 |
data in the underlying device. |
| 226 |
\value ReadCorruptData The text stream has read corrupt data. |
| 227 |
|
| 228 |
\sa status() |
| 229 |
*/ |
| 230 |
|
| 231 |
#include "qtextstream.h" |
| 232 |
#include "qbuffer.h" |
| 233 |
#include "qfile.h" |
| 234 |
#include "qnumeric.h" |
| 235 |
#ifndef QT_NO_TEXTCODEC |
| 236 |
#include "qtextcodec.h" |
| 237 |
#endif |
| 238 |
#ifndef Q_OS_WINCE |
| 239 |
#include <locale.h> |
| 240 |
#endif |
| 241 |
#include "private/qlocale_p.h" |
| 242 |
|
| 243 |
#include <stdlib.h> |
| 244 |
#include <new> |
| 245 |
|
| 246 |
#if defined QTEXTSTREAM_DEBUG |
| 247 |
#include <ctype.h> |
| 248 |
|
| 249 |
QT_BEGIN_NAMESPACE |
| 250 |
|
| 251 |
// Returns a human readable representation of the first \a len |
| 252 |
// characters in \a data. |
| 253 |
static QByteArray qt_prettyDebug(const char *data, int len, int maxSize) |
| 254 |
{ |
| 255 |
if (!data) return "(null)"; |
| 256 |
QByteArray out; |
| 257 |
for (int i = 0; i < len; ++i) { |
| 258 |
char c = data[i]; |
| 259 |
if (isprint(int(uchar(c)))) { |
| 260 |
out += c; |
| 261 |
} else switch (c) { |
| 262 |
case '\n': out += "\\n"; break; |
| 263 |
case '\r': out += "\\r"; break; |
| 264 |
case '\t': out += "\\t"; break; |
| 265 |
default: |
| 266 |
QString tmp; |
| 267 |
tmp.sprintf("\\x%x", (unsigned int)(unsigned char)c); |
| 268 |
out += tmp.toLatin1(); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if (len < maxSize) |
| 273 |
out += "..."; |
| 274 |
|
| 275 |
return out; |
| 276 |
} |
| 277 |
QT_END_NAMESPACE |
| 278 |
|
| 279 |
#endif |
| 280 |
|
| 281 |
// A precondition macro |
| 282 |
#define Q_VOID |
| 283 |
#define CHECK_VALID_STREAM(x) do { \ |
| 284 |
if (!d->string && !d->device) { \ |
| 285 |
qWarning("QTextStream: No device"); \ |
| 286 |
return x; \ |
| 287 |
} } while (0) |
| 288 |
|
| 289 |
// Base implementations of operator>> for ints and reals |
| 290 |
#define IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(type) do { \ |
| 291 |
Q_D(QTextStream); \ |
| 292 |
CHECK_VALID_STREAM(*this); \ |
| 293 |
qulonglong tmp; \ |
| 294 |
switch (d->getNumber(&tmp)) { \ |
| 295 |
case QTextStreamPrivate::npsOk: \ |
| 296 |
i = (type)tmp; \ |
| 297 |
break; \ |
| 298 |
case QTextStreamPrivate::npsMissingDigit: \ |
| 299 |
case QTextStreamPrivate::npsInvalidPrefix: \ |
| 300 |
i = (type)0; \ |
| 301 |
setStatus(atEnd() ? QTextStream::ReadPastEnd : QTextStream::ReadCorruptData); \ |
| 302 |
break; \ |
| 303 |
} \ |
| 304 |
return *this; } while (0) |
| 305 |
|
| 306 |
#define IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(type) do { \ |
| 307 |
Q_D(QTextStream); \ |
| 308 |
CHECK_VALID_STREAM(*this); \ |
| 309 |
double tmp; \ |
| 310 |
if (d->getReal(&tmp)) { \ |
| 311 |
f = (type)tmp; \ |
| 312 |
} else { \ |
| 313 |
f = (type)0; \ |
| 314 |
setStatus(atEnd() ? QTextStream::ReadPastEnd : QTextStream::ReadCorruptData); \ |
| 315 |
} \ |
| 316 |
return *this; } while (0) |
| 317 |
|
| 318 |
QT_BEGIN_NAMESPACE |
| 319 |
|
| 320 |
#ifndef QT_NO_QOBJECT |
| 321 |
class QDeviceClosedNotifier : public QObject |
| 322 |
{ |
| 323 |
Q_OBJECT |
| 324 |
public: |
| 325 |
inline QDeviceClosedNotifier() |
| 326 |
{ } |
| 327 |
|
| 328 |
inline void setupDevice(QTextStream *stream, QIODevice *device) |
| 329 |
{ |
| 330 |
disconnect(); |
| 331 |
if (device) |
| 332 |
connect(device, SIGNAL(aboutToClose()), this, SLOT(flushStream())); |
| 333 |
this->stream = stream; |
| 334 |
} |
| 335 |
|
| 336 |
public slots: |
| 337 |
inline void flushStream() { stream->flush(); } |
| 338 |
|
| 339 |
private: |
| 340 |
QTextStream *stream; |
| 341 |
}; |
| 342 |
#endif |
| 343 |
|
| 344 |
//------------------------------------------------------------------- |
| 345 |
class QTextStreamPrivate |
| 346 |
{ |
| 347 |
Q_DECLARE_PUBLIC(QTextStream) |
| 348 |
public: |
| 349 |
QTextStreamPrivate(QTextStream *q_ptr); |
| 350 |
~QTextStreamPrivate(); |
| 351 |
void reset(); |
| 352 |
|
| 353 |
// device |
| 354 |
QIODevice *device; |
| 355 |
#ifndef QT_NO_QOBJECT |
| 356 |
QDeviceClosedNotifier deviceClosedNotifier; |
| 357 |
#endif |
| 358 |
bool deleteDevice; |
| 359 |
|
| 360 |
// string |
| 361 |
QString *string; |
| 362 |
int stringOffset; |
| 363 |
QIODevice::OpenMode stringOpenMode; |
| 364 |
|
| 365 |
#ifndef QT_NO_TEXTCODEC |
| 366 |
// codec |
| 367 |
QTextCodec *codec; |
| 368 |
QTextCodec::ConverterState readConverterState; |
| 369 |
QTextCodec::ConverterState writeConverterState; |
| 370 |
QTextCodec::ConverterState *readConverterSavedState; |
| 371 |
bool autoDetectUnicode; |
| 372 |
#endif |
| 373 |
|
| 374 |
// i/o |
| 375 |
enum TokenDelimiter { |
| 376 |
Space, |
| 377 |
NotSpace, |
| 378 |
EndOfLine, |
| 379 |
EndOfFile |
| 380 |
}; |
| 381 |
|
| 382 |
bool scan(const QChar **ptr, int *tokenLength, |
| 383 |
int maxlen, TokenDelimiter delimiter); |
| 384 |
inline const QChar *readPtr() const; |
| 385 |
inline void consumeLastToken(); |
| 386 |
inline void consume(int nchars); |
| 387 |
void saveConverterState(qint64 newPos); |
| 388 |
void restoreToSavedConverterState(); |
| 389 |
int lastTokenSize; |
| 390 |
|
| 391 |
// Return value type for getNumber() |
| 392 |
enum NumberParsingStatus { |
| 393 |
npsOk, |
| 394 |
npsMissingDigit, |
| 395 |
npsInvalidPrefix |
| 396 |
}; |
| 397 |
|
| 398 |
inline bool write(const QString &data); |
| 399 |
inline bool getChar(QChar *ch); |
| 400 |
inline void ungetChar(const QChar &ch); |
| 401 |
NumberParsingStatus getNumber(qulonglong *l); |
| 402 |
bool getReal(double *f); |
| 403 |
|
| 404 |
bool putNumber(qulonglong number, bool negative); |
| 405 |
inline bool putString(const QString &ch, bool number = false); |
| 406 |
|
| 407 |
// buffers |
| 408 |
bool fillReadBuffer(qint64 maxBytes = -1); |
| 409 |
void resetReadBuffer(); |
| 410 |
bool flushWriteBuffer(); |
| 411 |
QString writeBuffer; |
| 412 |
QString readBuffer; |
| 413 |
int readBufferOffset; |
| 414 |
qint64 readBufferStartDevicePos; |
| 415 |
|
| 416 |
// streaming parameters |
| 417 |
int realNumberPrecision; |
| 418 |
int integerBase; |
| 419 |
int fieldWidth; |
| 420 |
QChar padChar; |
| 421 |
QTextStream::FieldAlignment fieldAlignment; |
| 422 |
QTextStream::RealNumberNotation realNumberNotation; |
| 423 |
QTextStream::NumberFlags numberFlags; |
| 424 |
|
| 425 |
// status |
| 426 |
QTextStream::Status status; |
| 427 |
|
| 428 |
QLocale locale; |
| 429 |
|
| 430 |
QTextStream *q_ptr; |
| 431 |
}; |
| 432 |
|
| 433 |
/*! \internal |
| 434 |
*/ |
| 435 |
QTextStreamPrivate::QTextStreamPrivate(QTextStream *q_ptr) |
| 436 |
: |
| 437 |
#ifndef QT_NO_TEXTCODEC |
| 438 |
readConverterSavedState(0), |
| 439 |
#endif |
| 440 |
locale(QLocale::C) |
| 441 |
{ |
| 442 |
this->q_ptr = q_ptr; |
| 443 |
reset(); |
| 444 |
} |
| 445 |
|
| 446 |
/*! \internal |
| 447 |
*/ |
| 448 |
QTextStreamPrivate::~QTextStreamPrivate() |
| 449 |
{ |
| 450 |
if (deleteDevice) { |
| 451 |
#ifndef QT_NO_QOBJECT |
| 452 |
device->blockSignals(true); |
| 453 |
#endif |
| 454 |
delete device; |
| 455 |
} |
| 456 |
#ifndef QT_NO_TEXTCODEC |
| 457 |
delete readConverterSavedState; |
| 458 |
#endif |
| 459 |
} |
| 460 |
|
| 461 |
#ifndef QT_NO_TEXTCODEC |
| 462 |
static void resetCodecConverterStateHelper(QTextCodec::ConverterState *state) |
| 463 |
{ |
| 464 |
state->~ConverterState(); |
| 465 |
new (state) QTextCodec::ConverterState; |
| 466 |
} |
| 467 |
|
| 468 |
static void copyConverterStateHelper(QTextCodec::ConverterState *dest, |
| 469 |
const QTextCodec::ConverterState *src) |
| 470 |
{ |
| 471 |
// ### QTextCodec::ConverterState's copy constructors and assignments are |
| 472 |
// private. This function copies the structure manually. |
| 473 |
Q_ASSERT(!src->d); |
| 474 |
dest->flags = src->flags; |
| 475 |
dest->invalidChars = src->invalidChars; |
| 476 |
dest->state_data[0] = src->state_data[0]; |
| 477 |
dest->state_data[1] = src->state_data[1]; |
| 478 |
dest->state_data[2] = src->state_data[2]; |
| 479 |
} |
| 480 |
#endif |
| 481 |
|
| 482 |
/*! \internal |
| 483 |
*/ |
| 484 |
void QTextStreamPrivate::reset() |
| 485 |
{ |
| 486 |
realNumberPrecision = 6; |
| 487 |
integerBase = 0; |
| 488 |
fieldWidth = 0; |
| 489 |
padChar = QLatin1Char(' '); |
| 490 |
fieldAlignment = QTextStream::AlignRight; |
| 491 |
realNumberNotation = QTextStream::SmartNotation; |
| 492 |
numberFlags = 0; |
| 493 |
|
| 494 |
device = 0; |
| 495 |
deleteDevice = false; |
| 496 |
string = 0; |
| 497 |
stringOffset = 0; |
| 498 |
stringOpenMode = QIODevice::NotOpen; |
| 499 |
|
| 500 |
readBufferOffset = 0; |
| 501 |
readBufferStartDevicePos = 0; |
| 502 |
lastTokenSize = 0; |
| 503 |
|
| 504 |
#ifndef QT_NO_TEXTCODEC |
| 505 |
codec = QTextCodec::codecForLocale(); |
| 506 |
resetCodecConverterStateHelper(&readConverterState); |
| 507 |
resetCodecConverterStateHelper(&writeConverterState); |
| 508 |
delete readConverterSavedState; |
| 509 |
readConverterSavedState = 0; |
| 510 |
writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 511 |
autoDetectUnicode = true; |
| 512 |
#endif |
| 513 |
} |
| 514 |
|
| 515 |
/*! \internal |
| 516 |
*/ |
| 517 |
bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes) |
| 518 |
{ |
| 519 |
// no buffer next to the QString itself; this function should only |
| 520 |
// be called internally, for devices. |
| 521 |
Q_ASSERT(!string); |
| 522 |
Q_ASSERT(device); |
| 523 |
|
| 524 |
// handle text translation and bypass the Text flag in the device. |
| 525 |
bool textModeEnabled = device->isTextModeEnabled(); |
| 526 |
if (textModeEnabled) |
| 527 |
device->setTextModeEnabled(false); |
| 528 |
|
| 529 |
// read raw data into a temporary buffer |
| 530 |
char buf[QTEXTSTREAM_BUFFERSIZE]; |
| 531 |
qint64 bytesRead = 0; |
| 532 |
#if defined(Q_OS_WIN) |
| 533 |
// On Windows, there is no non-blocking stdin - so we fall back to reading |
| 534 |
// lines instead. If there is no QOBJECT, we read lines for all sequential |
| 535 |
// devices; otherwise, we read lines only for stdin. |
| 536 |
QFile *file = 0; |
| 537 |
Q_UNUSED(file); |
| 538 |
if (device->isSequential() |
| 539 |
#if !defined(QT_NO_QOBJECT) |
| 540 |
&& (file = qobject_cast<QFile *>(device)) && file->handle() == 0 |
| 541 |
#endif |
| 542 |
) { |
| 543 |
if (maxBytes != -1) |
| 544 |
bytesRead = device->readLine(buf, qMin<qint64>(sizeof(buf), maxBytes)); |
| 545 |
else |
| 546 |
bytesRead = device->readLine(buf, sizeof(buf)); |
| 547 |
} else |
| 548 |
#endif |
| 549 |
{ |
| 550 |
if (maxBytes != -1) |
| 551 |
bytesRead = device->read(buf, qMin<qint64>(sizeof(buf), maxBytes)); |
| 552 |
else |
| 553 |
bytesRead = device->read(buf, sizeof(buf)); |
| 554 |
} |
| 555 |
|
| 556 |
#ifndef QT_NO_TEXTCODEC |
| 557 |
// codec auto detection, explicitly defaults to locale encoding if the |
| 558 |
// codec has been set to 0. |
| 559 |
if (!codec || autoDetectUnicode) { |
| 560 |
autoDetectUnicode = false; |
| 561 |
|
| 562 |
if (bytesRead >= 4 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe && uchar(buf[2]) == 0 && uchar(buf[3]) == 0) |
| 563 |
|| (uchar(buf[0]) == 0 && uchar(buf[1]) == 0 && uchar(buf[2]) == 0xfe && uchar(buf[3]) == 0xff))) { |
| 564 |
codec = QTextCodec::codecForName("UTF-32"); |
| 565 |
} else if (bytesRead >= 2 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe) |
| 566 |
|| (uchar(buf[0]) == 0xfe && uchar(buf[1]) == 0xff))) { |
| 567 |
codec = QTextCodec::codecForName("UTF-16"); |
| 568 |
} else if (!codec) { |
| 569 |
codec = QTextCodec::codecForLocale(); |
| 570 |
writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 571 |
} |
| 572 |
} |
| 573 |
#if defined (QTEXTSTREAM_DEBUG) |
| 574 |
qDebug("QTextStreamPrivate::fillReadBuffer(), using %s codec", |
| 575 |
codec->name().constData()); |
| 576 |
#endif |
| 577 |
#endif |
| 578 |
|
| 579 |
#if defined (QTEXTSTREAM_DEBUG) |
| 580 |
qDebug("QTextStreamPrivate::fillReadBuffer(), device->read(\"%s\", %d) == %d", |
| 581 |
qt_prettyDebug(buf, qMin(32,int(bytesRead)) , int(bytesRead)).constData(), sizeof(buf), int(bytesRead)); |
| 582 |
#endif |
| 583 |
|
| 584 |
if (bytesRead <= 0) |
| 585 |
return false; |
| 586 |
|
| 587 |
int oldReadBufferSize = readBuffer.size(); |
| 588 |
#ifndef QT_NO_TEXTCODEC |
| 589 |
// convert to unicode |
| 590 |
readBuffer += codec->toUnicode(buf, bytesRead, &readConverterState); |
| 591 |
#else |
| 592 |
readBuffer += QString::fromLatin1(QByteArray(buf, bytesRead).constData()); |
| 593 |
#endif |
| 594 |
|
| 595 |
// reset the Text flag. |
| 596 |
if (textModeEnabled) |
| 597 |
device->setTextModeEnabled(true); |
| 598 |
|
| 599 |
// remove all '\r\n' in the string. |
| 600 |
if (readBuffer.size() > oldReadBufferSize && textModeEnabled) { |
| 601 |
QChar CR = QLatin1Char('\r'); |
| 602 |
QChar *writePtr = readBuffer.data() + oldReadBufferSize; |
| 603 |
QChar *readPtr = readBuffer.data() + oldReadBufferSize; |
| 604 |
QChar *endPtr = readBuffer.data() + readBuffer.size(); |
| 605 |
|
| 606 |
int n = oldReadBufferSize; |
| 607 |
if (readPtr < endPtr) { |
| 608 |
// Cut-off to avoid unnecessary self-copying. |
| 609 |
while (*readPtr++ != CR) { |
| 610 |
++n; |
| 611 |
if (++writePtr == endPtr) |
| 612 |
break; |
| 613 |
} |
| 614 |
} |
| 615 |
while (readPtr < endPtr) { |
| 616 |
QChar ch = *readPtr++; |
| 617 |
if (ch != CR) { |
| 618 |
*writePtr++ = ch; |
| 619 |
} else { |
| 620 |
if (n < readBufferOffset) |
| 621 |
--readBufferOffset; |
| 622 |
--bytesRead; |
| 623 |
} |
| 624 |
++n; |
| 625 |
} |
| 626 |
readBuffer.resize(writePtr - readBuffer.data()); |
| 627 |
} |
| 628 |
|
| 629 |
#if defined (QTEXTSTREAM_DEBUG) |
| 630 |
qDebug("QTextStreamPrivate::fillReadBuffer() read %d bytes from device. readBuffer = [%s]", int(bytesRead), |
| 631 |
qt_prettyDebug(readBuffer.toLatin1(), readBuffer.size(), readBuffer.size()).data()); |
| 632 |
#endif |
| 633 |
return true; |
| 634 |
} |
| 635 |
|
| 636 |
/*! \internal |
| 637 |
*/ |
| 638 |
void QTextStreamPrivate::resetReadBuffer() |
| 639 |
{ |
| 640 |
readBuffer.clear(); |
| 641 |
readBufferOffset = 0; |
| 642 |
readBufferStartDevicePos = (device ? device->pos() : 0); |
| 643 |
} |
| 644 |
|
| 645 |
/*! \internal |
| 646 |
*/ |
| 647 |
bool QTextStreamPrivate::flushWriteBuffer() |
| 648 |
{ |
| 649 |
// no buffer next to the QString itself; this function should only |
| 650 |
// be called internally, for devices. |
| 651 |
if (string || !device) |
| 652 |
return false; |
| 653 |
if (writeBuffer.isEmpty()) |
| 654 |
return true; |
| 655 |
|
| 656 |
#if defined (Q_OS_WIN) |
| 657 |
// handle text translation and bypass the Text flag in the device. |
| 658 |
bool textModeEnabled = device->isTextModeEnabled(); |
| 659 |
if (textModeEnabled) { |
| 660 |
device->setTextModeEnabled(false); |
| 661 |
writeBuffer.replace(QLatin1Char('\n'), QLatin1String("\r\n")); |
| 662 |
} |
| 663 |
#endif |
| 664 |
|
| 665 |
#ifndef QT_NO_TEXTCODEC |
| 666 |
if (!codec) |
| 667 |
codec = QTextCodec::codecForLocale(); |
| 668 |
#if defined (QTEXTSTREAM_DEBUG) |
| 669 |
qDebug("QTextStreamPrivate::flushWriteBuffer(), using %s codec (%s generating BOM)", |
| 670 |
codec->name().constData(), writeConverterState.flags & QTextCodec::IgnoreHeader ? "not" : ""); |
| 671 |
#endif |
| 672 |
|
| 673 |
// convert from unicode to raw data |
| 674 |
QByteArray data = codec->fromUnicode(writeBuffer.data(), writeBuffer.size(), &writeConverterState); |
| 675 |
#else |
| 676 |
QByteArray data = writeBuffer.toLocal8Bit(); |
| 677 |
#endif |
| 678 |
writeBuffer.clear(); |
| 679 |
|
| 680 |
// write raw data to the device |
| 681 |
qint64 bytesWritten = device->write(data); |
| 682 |
#if defined (QTEXTSTREAM_DEBUG) |
| 683 |
qDebug("QTextStreamPrivate::flushWriteBuffer(), device->write(\"%s\") == %d", |
| 684 |
qt_prettyDebug(data.constData(), qMin(data.size(),32), data.size()).constData(), int(bytesWritten)); |
| 685 |
#endif |
| 686 |
if (bytesWritten <= 0) |
| 687 |
return false; |
| 688 |
|
| 689 |
#if defined (Q_OS_WIN) |
| 690 |
// replace the text flag |
| 691 |
if (textModeEnabled) |
| 692 |
device->setTextModeEnabled(true); |
| 693 |
#endif |
| 694 |
|
| 695 |
// flush the file |
| 696 |
#ifndef QT_NO_QOBJECT |
| 697 |
QFile *file = qobject_cast<QFile *>(device); |
| 698 |
bool flushed = file && file->flush(); |
| 699 |
#else |
| 700 |
bool flushed = true; |
| 701 |
#endif |
| 702 |
|
| 703 |
#if defined (QTEXTSTREAM_DEBUG) |
| 704 |
qDebug("QTextStreamPrivate::flushWriteBuffer() wrote %d bytes", |
| 705 |
int(bytesWritten)); |
| 706 |
#endif |
| 707 |
return flushed && bytesWritten == qint64(data.size()); |
| 708 |
} |
| 709 |
|
| 710 |
/*! \internal |
| 711 |
|
| 712 |
Scans no more than \a maxlen QChars in the current buffer for the |
| 713 |
first \a delimiter. Stores a pointer to the start offset of the |
| 714 |
token in \a ptr, and the length in QChars in \a length. |
| 715 |
*/ |
| 716 |
bool QTextStreamPrivate::scan(const QChar **ptr, int *length, int maxlen, TokenDelimiter delimiter) |
| 717 |
{ |
| 718 |
int totalSize = 0; |
| 719 |
int delimSize = 0; |
| 720 |
bool consumeDelimiter = false; |
| 721 |
bool foundToken = false; |
| 722 |
int startOffset = device ? readBufferOffset : stringOffset; |
| 723 |
QChar lastChar; |
| 724 |
|
| 725 |
bool canStillReadFromDevice = true; |
| 726 |
do { |
| 727 |
int endOffset; |
| 728 |
const QChar *chPtr; |
| 729 |
if (device) { |
| 730 |
chPtr = readBuffer.constData(); |
| 731 |
endOffset = readBuffer.size(); |
| 732 |
} else { |
| 733 |
chPtr = string->constData(); |
| 734 |
endOffset = string->size(); |
| 735 |
} |
| 736 |
chPtr += startOffset; |
| 737 |
|
| 738 |
for (; !foundToken && startOffset < endOffset && (!maxlen || totalSize < maxlen); ++startOffset) { |
| 739 |
const QChar ch = *chPtr++; |
| 740 |
++totalSize; |
| 741 |
|
| 742 |
if (delimiter == Space && ch.isSpace()) { |
| 743 |
foundToken = true; |
| 744 |
delimSize = 1; |
| 745 |
} else if (delimiter == NotSpace && !ch.isSpace()) { |
| 746 |
foundToken = true; |
| 747 |
delimSize = 1; |
| 748 |
} else if (delimiter == EndOfLine && ch == QLatin1Char('\n')) { |
| 749 |
foundToken = true; |
| 750 |
delimSize = (lastChar == QLatin1Char('\r')) ? 2 : 1; |
| 751 |
consumeDelimiter = true; |
| 752 |
} |
| 753 |
|
| 754 |
lastChar = ch; |
| 755 |
} |
| 756 |
} while (!foundToken |
| 757 |
&& (!maxlen || totalSize < maxlen) |
| 758 |
&& (device && (canStillReadFromDevice = fillReadBuffer()))); |
| 759 |
|
| 760 |
// if the token was not found, but we reached the end of input, |
| 761 |
// then we accept what we got. if we are not at the end of input, |
| 762 |
// we return false. |
| 763 |
if (!foundToken && (!maxlen || totalSize < maxlen) |
| 764 |
&& (totalSize == 0 |
| 765 |
|| (string && stringOffset + totalSize < string->size()) |
| 766 |
|| (device && !device->atEnd() && canStillReadFromDevice))) { |
| 767 |
#if defined (QTEXTSTREAM_DEBUG) |
| 768 |
qDebug("QTextStreamPrivate::scan() did not find the token."); |
| 769 |
#endif |
| 770 |
return false; |
| 771 |
} |
| 772 |
|
| 773 |
// if we find a '\r' at the end of the data when reading lines, |
| 774 |
// don't make it part of the line. |
| 775 |
if (totalSize > 0 && !foundToken && delimiter == EndOfLine) { |
| 776 |
if (((string && stringOffset + totalSize == string->size()) || (device && device->atEnd())) |
| 777 |
&& lastChar == QLatin1Char('\r')) { |
| 778 |
consumeDelimiter = true; |
| 779 |
++delimSize; |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
// set the read offset and length of the token |
| 784 |
if (length) |
| 785 |
*length = totalSize - delimSize; |
| 786 |
if (ptr) |
| 787 |
*ptr = readPtr(); |
| 788 |
|
| 789 |
// update last token size. the callee will call consumeLastToken() when |
| 790 |
// done. |
| 791 |
lastTokenSize = totalSize; |
| 792 |
if (!consumeDelimiter) |
| 793 |
lastTokenSize -= delimSize; |
| 794 |
|
| 795 |
#if defined (QTEXTSTREAM_DEBUG) |
| 796 |
qDebug("QTextStreamPrivate::scan(%p, %p, %d, %x) token length = %d, delimiter = %d", |
| 797 |
ptr, length, maxlen, (int)delimiter, totalSize - delimSize, delimSize); |
| 798 |
#endif |
| 799 |
return true; |
| 800 |
} |
| 801 |
|
| 802 |
/*! \internal |
| 803 |
*/ |
| 804 |
inline const QChar *QTextStreamPrivate::readPtr() const |
| 805 |
{ |
| 806 |
Q_ASSERT(readBufferOffset <= readBuffer.size()); |
| 807 |
if (string) |
| 808 |
return string->constData() + stringOffset; |
| 809 |
return readBuffer.constData() + readBufferOffset; |
| 810 |
} |
| 811 |
|
| 812 |
/*! \internal |
| 813 |
*/ |
| 814 |
inline void QTextStreamPrivate::consumeLastToken() |
| 815 |
{ |
| 816 |
if (lastTokenSize) |
| 817 |
consume(lastTokenSize); |
| 818 |
lastTokenSize = 0; |
| 819 |
} |
| 820 |
|
| 821 |
/*! \internal |
| 822 |
*/ |
| 823 |
inline void QTextStreamPrivate::consume(int size) |
| 824 |
{ |
| 825 |
#if defined (QTEXTSTREAM_DEBUG) |
| 826 |
qDebug("QTextStreamPrivate::consume(%d)", size); |
| 827 |
#endif |
| 828 |
if (string) { |
| 829 |
stringOffset += size; |
| 830 |
if (stringOffset > string->size()) |
| 831 |
stringOffset = string->size(); |
| 832 |
} else { |
| 833 |
readBufferOffset += size; |
| 834 |
if (readBufferOffset >= readBuffer.size()) { |
| 835 |
readBufferOffset = 0; |
| 836 |
readBuffer.clear(); |
| 837 |
saveConverterState(device->pos()); |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
/*! \internal |
| 843 |
*/ |
| 844 |
inline void QTextStreamPrivate::saveConverterState(qint64 newPos) |
| 845 |
{ |
| 846 |
#ifndef QT_NO_TEXTCODEC |
| 847 |
if (readConverterState.d) { |
| 848 |
// converter cannot be copied, so don't save anything |
| 849 |
// don't update readBufferStartDevicePos either |
| 850 |
return; |
| 851 |
} |
| 852 |
|
| 853 |
if (!readConverterSavedState) |
| 854 |
readConverterSavedState = new QTextCodec::ConverterState; |
| 855 |
copyConverterStateHelper(readConverterSavedState, &readConverterState); |
| 856 |
#endif |
| 857 |
|
| 858 |
readBufferStartDevicePos = newPos; |
| 859 |
} |
| 860 |
|
| 861 |
/*! \internal |
| 862 |
*/ |
| 863 |
inline void QTextStreamPrivate::restoreToSavedConverterState() |
| 864 |
{ |
| 865 |
#ifndef QT_NO_TEXTCODEC |
| 866 |
if (readConverterSavedState) { |
| 867 |
// we have a saved state |
| 868 |
// that means the converter can be copied |
| 869 |
copyConverterStateHelper(&readConverterState, readConverterSavedState); |
| 870 |
} else { |
| 871 |
// the only state we could save was the initial |
| 872 |
// so reset to that |
| 873 |
resetCodecConverterStateHelper(&readConverterState); |
| 874 |
} |
| 875 |
#endif |
| 876 |
} |
| 877 |
|
| 878 |
/*! \internal |
| 879 |
*/ |
| 880 |
inline bool QTextStreamPrivate::write(const QString &data) |
| 881 |
{ |
| 882 |
if (string) { |
| 883 |
// ### What about seek()?? |
| 884 |
string->append(data); |
| 885 |
} else { |
| 886 |
writeBuffer += data; |
| 887 |
if (writeBuffer.size() > QTEXTSTREAM_BUFFERSIZE) |
| 888 |
return flushWriteBuffer(); |
| 889 |
} |
| 890 |
return true; |
| 891 |
} |
| 892 |
|
| 893 |
/*! \internal |
| 894 |
*/ |
| 895 |
inline bool QTextStreamPrivate::getChar(QChar *ch) |
| 896 |
{ |
| 897 |
if ((string && stringOffset == string->size()) |
| 898 |
|| (device && readBuffer.isEmpty() && !fillReadBuffer())) { |
| 899 |
if (ch) |
| 900 |
*ch = 0; |
| 901 |
return false; |
| 902 |
} |
| 903 |
if (ch) |
| 904 |
*ch = *readPtr(); |
| 905 |
consume(1); |
| 906 |
return true; |
| 907 |
} |
| 908 |
|
| 909 |
/*! \internal |
| 910 |
*/ |
| 911 |
inline void QTextStreamPrivate::ungetChar(const QChar &ch) |
| 912 |
{ |
| 913 |
if (string) { |
| 914 |
if (stringOffset == 0) |
| 915 |
string->prepend(ch); |
| 916 |
else |
| 917 |
(*string)[--stringOffset] = ch; |
| 918 |
return; |
| 919 |
} |
| 920 |
|
| 921 |
if (readBufferOffset == 0) { |
| 922 |
readBuffer.prepend(ch); |
| 923 |
return; |
| 924 |
} |
| 925 |
|
| 926 |
readBuffer[--readBufferOffset] = ch; |
| 927 |
} |
| 928 |
|
| 929 |
/*! \internal |
| 930 |
*/ |
| 931 |
inline bool QTextStreamPrivate::putString(const QString &s, bool number) |
| 932 |
{ |
| 933 |
QString tmp = s; |
| 934 |
|
| 935 |
// handle padding |
| 936 |
int padSize = fieldWidth - s.size(); |
| 937 |
if (padSize > 0) { |
| 938 |
QString pad(padSize > 0 ? padSize : 0, padChar); |
| 939 |
if (fieldAlignment == QTextStream::AlignLeft) { |
| 940 |
tmp.append(QString(padSize, padChar)); |
| 941 |
} else if (fieldAlignment == QTextStream::AlignRight |
| 942 |
|| fieldAlignment == QTextStream::AlignAccountingStyle) { |
| 943 |
tmp.prepend(QString(padSize, padChar)); |
| 944 |
if (fieldAlignment == QTextStream::AlignAccountingStyle && number) { |
| 945 |
const QChar sign = s.size() > 0 ? s.at(0) : QChar(); |
| 946 |
if (sign == locale.negativeSign() || sign == locale.positiveSign()) { |
| 947 |
QChar *data = tmp.data(); |
| 948 |
data[padSize] = tmp.at(0); |
| 949 |
data[0] = sign; |
| 950 |
} |
| 951 |
} |
| 952 |
} else if (fieldAlignment == QTextStream::AlignCenter) { |
| 953 |
tmp.prepend(QString(padSize/2, padChar)); |
| 954 |
tmp.append(QString(padSize - padSize/2, padChar)); |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
#if defined (QTEXTSTREAM_DEBUG) |
| 959 |
QByteArray a = s.toUtf8(); |
| 960 |
QByteArray b = tmp.toUtf8(); |
| 961 |
qDebug("QTextStreamPrivate::putString(\"%s\") calls write(\"%s\")", |
| 962 |
qt_prettyDebug(a.constData(), a.size(), qMax(16, a.size())).constData(), |
| 963 |
qt_prettyDebug(b.constData(), b.size(), qMax(16, b.size())).constData()); |
| 964 |
#endif |
| 965 |
return write(tmp); |
| 966 |
} |
| 967 |
|
| 968 |
/*! |
| 969 |
Constructs a QTextStream. Before you can use it for reading or |
| 970 |
writing, you must assign a device or a string. |
| 971 |
|
| 972 |
\sa setDevice(), setString() |
| 973 |
*/ |
| 974 |
QTextStream::QTextStream() |
| 975 |
: d_ptr(new QTextStreamPrivate(this)) |
| 976 |
{ |
| 977 |
#if defined (QTEXTSTREAM_DEBUG) |
| 978 |
qDebug("QTextStream::QTextStream()"); |
| 979 |
#endif |
| 980 |
Q_D(QTextStream); |
| 981 |
d->status = Ok; |
| 982 |
} |
| 983 |
|
| 984 |
/*! |
| 985 |
Constructs a QTextStream that operates on \a device. |
| 986 |
*/ |
| 987 |
QTextStream::QTextStream(QIODevice *device) |
| 988 |
: d_ptr(new QTextStreamPrivate(this)) |
| 989 |
{ |
| 990 |
#if defined (QTEXTSTREAM_DEBUG) |
| 991 |
qDebug("QTextStream::QTextStream(QIODevice *device == *%p)", |
| 992 |
device); |
| 993 |
#endif |
| 994 |
Q_D(QTextStream); |
| 995 |
d->device = device; |
| 996 |
#ifndef QT_NO_QOBJECT |
| 997 |
d->deviceClosedNotifier.setupDevice(this, d->device); |
| 998 |
#endif |
| 999 |
d->status = Ok; |
| 1000 |
} |
| 1001 |
|
| 1002 |
/*! |
| 1003 |
Constructs a QTextStream that operates on \a string, using \a |
| 1004 |
openMode to define the open mode. |
| 1005 |
*/ |
| 1006 |
QTextStream::QTextStream(QString *string, QIODevice::OpenMode openMode) |
| 1007 |
: d_ptr(new QTextStreamPrivate(this)) |
| 1008 |
{ |
| 1009 |
#if defined (QTEXTSTREAM_DEBUG) |
| 1010 |
qDebug("QTextStream::QTextStream(QString *string == *%p, openMode = %d)", |
| 1011 |
string, int(openMode)); |
| 1012 |
#endif |
| 1013 |
Q_D(QTextStream); |
| 1014 |
d->string = string; |
| 1015 |
d->stringOpenMode = openMode; |
| 1016 |
d->status = Ok; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/*! |
| 1020 |
Constructs a QTextStream that operates on \a array, using \a |
| 1021 |
openMode to define the open mode. Internally, the array is wrapped |
| 1022 |
by a QBuffer. |
| 1023 |
*/ |
| 1024 |
QTextStream::QTextStream(QByteArray *array, QIODevice::OpenMode openMode) |
| 1025 |
: d_ptr(new QTextStreamPrivate(this)) |
| 1026 |
{ |
| 1027 |
#if defined (QTEXTSTREAM_DEBUG) |
| 1028 |
qDebug("QTextStream::QTextStream(QByteArray *array == *%p, openMode = %d)", |
| 1029 |
array, int(openMode)); |
| 1030 |
#endif |
| 1031 |
Q_D(QTextStream); |
| 1032 |
d->device = new QBuffer(array); |
| 1033 |
d->device->open(openMode); |
| 1034 |
d->deleteDevice = true; |
| 1035 |
#ifndef QT_NO_QOBJECT |
| 1036 |
d->deviceClosedNotifier.setupDevice(this, d->device); |
| 1037 |
#endif |
| 1038 |
d->status = Ok; |
| 1039 |
} |
| 1040 |
|
| 1041 |
/*! |
| 1042 |
Constructs a QTextStream that operates on \a array, using \a |
| 1043 |
openMode to define the open mode. The array is accessed as |
| 1044 |
read-only, regardless of the values in \a openMode. |
| 1045 |
|
| 1046 |
This constructor is convenient for working on constant |
| 1047 |
strings. Example: |
| 1048 |
|
| 1049 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 3 |
| 1050 |
*/ |
| 1051 |
QTextStream::QTextStream(const QByteArray &array, QIODevice::OpenMode openMode) |
| 1052 |
: d_ptr(new QTextStreamPrivate(this)) |
| 1053 |
{ |
| 1054 |
#if defined (QTEXTSTREAM_DEBUG) |
| 1055 |
qDebug("QTextStream::QTextStream(const QByteArray &array == *(%p), openMode = %d)", |
| 1056 |
&array, int(openMode)); |
| 1057 |
#endif |
| 1058 |
QBuffer *buffer = new QBuffer; |
| 1059 |
buffer->setData(array); |
| 1060 |
buffer->open(openMode); |
| 1061 |
|
| 1062 |
Q_D(QTextStream); |
| 1063 |
d->device = buffer; |
| 1064 |
d->deleteDevice = true; |
| 1065 |
#ifndef QT_NO_QOBJECT |
| 1066 |
d->deviceClosedNotifier.setupDevice(this, d->device); |
| 1067 |
#endif |
| 1068 |
d->status = Ok; |
| 1069 |
} |
| 1070 |
|
| 1071 |
/*! |
| 1072 |
Constructs a QTextStream that operates on \a fileHandle, using \a |
| 1073 |
openMode to define the open mode. Internally, a QFile is created |
| 1074 |
to handle the FILE pointer. |
| 1075 |
|
| 1076 |
This constructor is useful for working directly with the common |
| 1077 |
FILE based input and output streams: stdin, stdout and stderr. Example: |
| 1078 |
|
| 1079 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 4 |
| 1080 |
*/ |
| 1081 |
|
| 1082 |
QTextStream::QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode) |
| 1083 |
: d_ptr(new QTextStreamPrivate(this)) |
| 1084 |
{ |
| 1085 |
#if defined (QTEXTSTREAM_DEBUG) |
| 1086 |
qDebug("QTextStream::QTextStream(FILE *fileHandle = %p, openMode = %d)", |
| 1087 |
fileHandle, int(openMode)); |
| 1088 |
#endif |
| 1089 |
QFile *file = new QFile; |
| 1090 |
file->open(fileHandle, openMode); |
| 1091 |
|
| 1092 |
Q_D(QTextStream); |
| 1093 |
d->device = file; |
| 1094 |
d->deleteDevice = true; |
| 1095 |
#ifndef QT_NO_QOBJECT |
| 1096 |
d->deviceClosedNotifier.setupDevice(this, d->device); |
| 1097 |
#endif |
| 1098 |
d->status = Ok; |
| 1099 |
} |
| 1100 |
|
| 1101 |
/*! |
| 1102 |
Destroys the QTextStream. |
| 1103 |
|
| 1104 |
If the stream operates on a device, flush() will be called |
| 1105 |
implicitly. Otherwise, the device is unaffected. |
| 1106 |
*/ |
| 1107 |
QTextStream::~QTextStream() |
| 1108 |
{ |
| 1109 |
Q_D(QTextStream); |
| 1110 |
#if defined (QTEXTSTREAM_DEBUG) |
| 1111 |
qDebug("QTextStream::~QTextStream()"); |
| 1112 |
#endif |
| 1113 |
if (!d->writeBuffer.isEmpty()) |
| 1114 |
d->flushWriteBuffer(); |
| 1115 |
|
| 1116 |
delete d; |
| 1117 |
d_ptr = 0; |
| 1118 |
} |
| 1119 |
|
| 1120 |
/*! |
| 1121 |
Resets QTextStream's formatting options, bringing it back to its |
| 1122 |
original constructed state. The device, string and any buffered |
| 1123 |
data is left untouched. |
| 1124 |
*/ |
| 1125 |
void QTextStream::reset() |
| 1126 |
{ |
| 1127 |
Q_D(QTextStream); |
| 1128 |
|
| 1129 |
d->realNumberPrecision = 6; |
| 1130 |
d->integerBase = 0; |
| 1131 |
d->fieldWidth = 0; |
| 1132 |
d->padChar = QLatin1Char(' '); |
| 1133 |
d->fieldAlignment = QTextStream::AlignRight; |
| 1134 |
d->realNumberNotation = QTextStream::SmartNotation; |
| 1135 |
d->numberFlags = 0; |
| 1136 |
} |
| 1137 |
|
| 1138 |
/*! |
| 1139 |
Flushes any buffered data waiting to be written to the device. |
| 1140 |
|
| 1141 |
If QTextStream operates on a string, this function does nothing. |
| 1142 |
*/ |
| 1143 |
void QTextStream::flush() |
| 1144 |
{ |
| 1145 |
Q_D(QTextStream); |
| 1146 |
d->flushWriteBuffer(); |
| 1147 |
} |
| 1148 |
|
| 1149 |
/*! |
| 1150 |
Seeks to the position \a pos in the device. Returns true on |
| 1151 |
success; otherwise returns false. |
| 1152 |
*/ |
| 1153 |
bool QTextStream::seek(qint64 pos) |
| 1154 |
{ |
| 1155 |
Q_D(QTextStream); |
| 1156 |
d->lastTokenSize = 0; |
| 1157 |
|
| 1158 |
if (d->device) { |
| 1159 |
// Empty the write buffer |
| 1160 |
d->flushWriteBuffer(); |
| 1161 |
if (!d->device->seek(pos)) |
| 1162 |
return false; |
| 1163 |
d->resetReadBuffer(); |
| 1164 |
|
| 1165 |
#ifndef QT_NO_TEXTCODEC |
| 1166 |
// Reset the codec converter states. |
| 1167 |
resetCodecConverterStateHelper(&d->readConverterState); |
| 1168 |
resetCodecConverterStateHelper(&d->writeConverterState); |
| 1169 |
delete d->readConverterSavedState; |
| 1170 |
d->readConverterSavedState = 0; |
| 1171 |
#endif |
| 1172 |
return true; |
| 1173 |
} |
| 1174 |
|
| 1175 |
// string |
| 1176 |
if (d->string && pos <= d->string->size()) { |
| 1177 |
d->stringOffset = int(pos); |
| 1178 |
return true; |
| 1179 |
} |
| 1180 |
return false; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/*! |
| 1184 |
\since 4.2 |
| 1185 |
|
| 1186 |
Returns the device position corresponding to the current position of the |
| 1187 |
stream, or -1 if an error occurs (e.g., if there is no device or string, |
| 1188 |
or if there's a device error). |
| 1189 |
|
| 1190 |
Because QTextStream is buffered, this function may have to |
| 1191 |
seek the device to reconstruct a valid device position. This |
| 1192 |
operation can be expensive, so you may want to avoid calling this |
| 1193 |
function in a tight loop. |
| 1194 |
|
| 1195 |
\sa seek() |
| 1196 |
*/ |
| 1197 |
qint64 QTextStream::pos() const |
| 1198 |
{ |
| 1199 |
Q_D(const QTextStream); |
| 1200 |
if (d->device) { |
| 1201 |
// Cutoff |
| 1202 |
if (d->readBuffer.isEmpty()) |
| 1203 |
return d->device->pos(); |
| 1204 |
if (d->device->isSequential()) |
| 1205 |
return 0; |
| 1206 |
|
| 1207 |
// Seek the device |
| 1208 |
if (!d->device->seek(d->readBufferStartDevicePos)) |
| 1209 |
return qint64(-1); |
| 1210 |
|
| 1211 |
// Reset the read buffer |
| 1212 |
QTextStreamPrivate *thatd = const_cast<QTextStreamPrivate *>(d); |
| 1213 |
thatd->readBuffer.clear(); |
| 1214 |
|
| 1215 |
#ifndef QT_NO_TEXTCODEC |
| 1216 |
thatd->restoreToSavedConverterState(); |
| 1217 |
if (d->readBufferStartDevicePos == 0) |
| 1218 |
thatd->autoDetectUnicode = true; |
| 1219 |
#endif |
| 1220 |
|
| 1221 |
// Rewind the device to get to the current position Ensure that |
| 1222 |
// readBufferOffset is unaffected by fillReadBuffer() |
| 1223 |
int oldReadBufferOffset = d->readBufferOffset; |
| 1224 |
while (d->readBuffer.size() < oldReadBufferOffset) { |
| 1225 |
if (!thatd->fillReadBuffer(1)) |
| 1226 |
return qint64(-1); |
| 1227 |
} |
| 1228 |
thatd->readBufferOffset = oldReadBufferOffset; |
| 1229 |
|
| 1230 |
// Return the device position. |
| 1231 |
return d->device->pos(); |
| 1232 |
} |
| 1233 |
|
| 1234 |
if (d->string) |
| 1235 |
return d->stringOffset; |
| 1236 |
|
| 1237 |
qWarning("QTextStream::pos: no device"); |
| 1238 |
return qint64(-1); |
| 1239 |
} |
| 1240 |
|
| 1241 |
/*! |
| 1242 |
Reads and discards whitespace from the stream until either a |
| 1243 |
non-space character is detected, or until atEnd() returns |
| 1244 |
true. This function is useful when reading a stream character by |
| 1245 |
character. |
| 1246 |
|
| 1247 |
Whitespace characters are all characters for which |
| 1248 |
QChar::isSpace() returns true. |
| 1249 |
|
| 1250 |
\sa operator>>() |
| 1251 |
*/ |
| 1252 |
void QTextStream::skipWhiteSpace() |
| 1253 |
{ |
| 1254 |
Q_D(QTextStream); |
| 1255 |
CHECK_VALID_STREAM(Q_VOID); |
| 1256 |
d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); |
| 1257 |
d->consumeLastToken(); |
| 1258 |
} |
| 1259 |
|
| 1260 |
/*! |
| 1261 |
Sets the current device to \a device. If a device has already been |
| 1262 |
assigned, QTextStream will call flush() before the old device is |
| 1263 |
replaced. |
| 1264 |
|
| 1265 |
\note This function resets locale to the default locale ('C') |
| 1266 |
and codec to the default codec, QTextCodec::codecForLocale(). |
| 1267 |
|
| 1268 |
\sa device(), setString() |
| 1269 |
*/ |
| 1270 |
void QTextStream::setDevice(QIODevice *device) |
| 1271 |
{ |
| 1272 |
Q_D(QTextStream); |
| 1273 |
flush(); |
| 1274 |
if (d->deleteDevice) { |
| 1275 |
#ifndef QT_NO_QOBJECT |
| 1276 |
d->deviceClosedNotifier.disconnect(); |
| 1277 |
#endif |
| 1278 |
delete d->device; |
| 1279 |
d->deleteDevice = false; |
| 1280 |
} |
| 1281 |
|
| 1282 |
d->reset(); |
| 1283 |
d->status = Ok; |
| 1284 |
d->device = device; |
| 1285 |
d->resetReadBuffer(); |
| 1286 |
#ifndef QT_NO_QOBJECT |
| 1287 |
d->deviceClosedNotifier.setupDevice(this, d->device); |
| 1288 |
#endif |
| 1289 |
} |
| 1290 |
|
| 1291 |
/*! |
| 1292 |
Returns the current device associated with the QTextStream, |
| 1293 |
or 0 if no device has been assigned. |
| 1294 |
|
| 1295 |
\sa setDevice(), string() |
| 1296 |
*/ |
| 1297 |
QIODevice *QTextStream::device() const |
| 1298 |
{ |
| 1299 |
Q_D(const QTextStream); |
| 1300 |
return d->device; |
| 1301 |
} |
| 1302 |
|
| 1303 |
/*! |
| 1304 |
Sets the current string to \a string, using the given \a |
| 1305 |
openMode. If a device has already been assigned, QTextStream will |
| 1306 |
call flush() before replacing it. |
| 1307 |
|
| 1308 |
\sa string(), setDevice() |
| 1309 |
*/ |
| 1310 |
void QTextStream::setString(QString *string, QIODevice::OpenMode openMode) |
| 1311 |
{ |
| 1312 |
Q_D(QTextStream); |
| 1313 |
flush(); |
| 1314 |
if (d->deleteDevice) { |
| 1315 |
#ifndef QT_NO_QOBJECT |
| 1316 |
d->deviceClosedNotifier.disconnect(); |
| 1317 |
d->device->blockSignals(true); |
| 1318 |
#endif |
| 1319 |
delete d->device; |
| 1320 |
d->deleteDevice = false; |
| 1321 |
} |
| 1322 |
|
| 1323 |
d->reset(); |
| 1324 |
d->status = Ok; |
| 1325 |
d->string = string; |
| 1326 |
d->stringOpenMode = openMode; |
| 1327 |
} |
| 1328 |
|
| 1329 |
/*! |
| 1330 |
Returns the current string assigned to the QTextStream, or 0 if no |
| 1331 |
string has been assigned. |
| 1332 |
|
| 1333 |
\sa setString(), device() |
| 1334 |
*/ |
| 1335 |
QString *QTextStream::string() const |
| 1336 |
{ |
| 1337 |
Q_D(const QTextStream); |
| 1338 |
return d->string; |
| 1339 |
} |
| 1340 |
|
| 1341 |
/*! |
| 1342 |
Sets the field alignment to \a mode. When used together with |
| 1343 |
setFieldWidth(), this function allows you to generate formatted |
| 1344 |
output with text aligned to the left, to the right or center |
| 1345 |
aligned. |
| 1346 |
|
| 1347 |
\sa fieldAlignment(), setFieldWidth() |
| 1348 |
*/ |
| 1349 |
void QTextStream::setFieldAlignment(FieldAlignment mode) |
| 1350 |
{ |
| 1351 |
Q_D(QTextStream); |
| 1352 |
d->fieldAlignment = mode; |
| 1353 |
} |
| 1354 |
|
| 1355 |
/*! |
| 1356 |
Returns the current field alignment. |
| 1357 |
|
| 1358 |
\sa setFieldAlignment(), fieldWidth() |
| 1359 |
*/ |
| 1360 |
QTextStream::FieldAlignment QTextStream::fieldAlignment() const |
| 1361 |
{ |
| 1362 |
Q_D(const QTextStream); |
| 1363 |
return d->fieldAlignment; |
| 1364 |
} |
| 1365 |
|
| 1366 |
/*! |
| 1367 |
Sets the pad character to \a ch. The default value is the ASCII |
| 1368 |
space character (' '), or QChar(0x20). This character is used to |
| 1369 |
fill in the space in fields when generating text. |
| 1370 |
|
| 1371 |
Example: |
| 1372 |
|
| 1373 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 5 |
| 1374 |
|
| 1375 |
The string \c s contains: |
| 1376 |
|
| 1377 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 6 |
| 1378 |
|
| 1379 |
\sa padChar(), setFieldWidth() |
| 1380 |
*/ |
| 1381 |
void QTextStream::setPadChar(QChar ch) |
| 1382 |
{ |
| 1383 |
Q_D(QTextStream); |
| 1384 |
d->padChar = ch; |
| 1385 |
} |
| 1386 |
|
| 1387 |
/*! |
| 1388 |
Returns the current pad character. |
| 1389 |
|
| 1390 |
\sa setPadChar(), setFieldWidth() |
| 1391 |
*/ |
| 1392 |
QChar QTextStream::padChar() const |
| 1393 |
{ |
| 1394 |
Q_D(const QTextStream); |
| 1395 |
return d->padChar; |
| 1396 |
} |
| 1397 |
|
| 1398 |
/*! |
| 1399 |
Sets the current field width to \a width. If \a width is 0 (the |
| 1400 |
default), the field width is equal to the length of the generated |
| 1401 |
text. |
| 1402 |
|
| 1403 |
\note The field width applies to every element appended to this |
| 1404 |
stream after this function has been called (e.g., it also pads |
| 1405 |
endl). This behavior is different from similar classes in the STL, |
| 1406 |
where the field width only applies to the next element. |
| 1407 |
|
| 1408 |
\sa fieldWidth(), setPadChar() |
| 1409 |
*/ |
| 1410 |
void QTextStream::setFieldWidth(int width) |
| 1411 |
{ |
| 1412 |
Q_D(QTextStream); |
| 1413 |
d->fieldWidth = width; |
| 1414 |
} |
| 1415 |
|
| 1416 |
/*! |
| 1417 |
Returns the current field width. |
| 1418 |
|
| 1419 |
\sa setFieldWidth() |
| 1420 |
*/ |
| 1421 |
int QTextStream::fieldWidth() const |
| 1422 |
{ |
| 1423 |
Q_D(const QTextStream); |
| 1424 |
return d->fieldWidth; |
| 1425 |
} |
| 1426 |
|
| 1427 |
/*! |
| 1428 |
Sets the current number flags to \a flags. \a flags is a set of |
| 1429 |
flags from the NumberFlag enum, and describes options for |
| 1430 |
formatting generated code (e.g., whether or not to always write |
| 1431 |
the base or sign of a number). |
| 1432 |
|
| 1433 |
\sa numberFlags(), setIntegerBase(), setRealNumberNotation() |
| 1434 |
*/ |
| 1435 |
void QTextStream::setNumberFlags(NumberFlags flags) |
| 1436 |
{ |
| 1437 |
Q_D(QTextStream); |
| 1438 |
d->numberFlags = flags; |
| 1439 |
} |
| 1440 |
|
| 1441 |
/*! |
| 1442 |
Returns the current number flags. |
| 1443 |
|
| 1444 |
\sa setNumberFlags(), integerBase(), realNumberNotation() |
| 1445 |
*/ |
| 1446 |
QTextStream::NumberFlags QTextStream::numberFlags() const |
| 1447 |
{ |
| 1448 |
Q_D(const QTextStream); |
| 1449 |
return d->numberFlags; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/*! |
| 1453 |
Sets the base of integers to \a base, both for reading and for |
| 1454 |
generating numbers. \a base can be either 2 (binary), 8 (octal), |
| 1455 |
10 (decimal) or 16 (hexadecimal). If \a base is 0, QTextStream |
| 1456 |
will attempt to detect the base by inspecting the data on the |
| 1457 |
stream. When generating numbers, QTextStream assumes base is 10 |
| 1458 |
unless the base has been set explicitly. |
| 1459 |
|
| 1460 |
\sa integerBase(), QString::number(), setNumberFlags() |
| 1461 |
*/ |
| 1462 |
void QTextStream::setIntegerBase(int base) |
| 1463 |
{ |
| 1464 |
Q_D(QTextStream); |
| 1465 |
d->integerBase = base; |
| 1466 |
} |
| 1467 |
|
| 1468 |
/*! |
| 1469 |
Returns the current base of integers. 0 means that the base is |
| 1470 |
detected when reading, or 10 (decimal) when generating numbers. |
| 1471 |
|
| 1472 |
\sa setIntegerBase(), QString::number(), numberFlags() |
| 1473 |
*/ |
| 1474 |
int QTextStream::integerBase() const |
| 1475 |
{ |
| 1476 |
Q_D(const QTextStream); |
| 1477 |
return d->integerBase; |
| 1478 |
} |
| 1479 |
|
| 1480 |
/*! |
| 1481 |
Sets the real number notation to \a notation (SmartNotation, |
| 1482 |
FixedNotation, ScientificNotation). When reading and generating |
| 1483 |
numbers, QTextStream uses this value to detect the formatting of |
| 1484 |
real numbers. |
| 1485 |
|
| 1486 |
\sa realNumberNotation(), setRealNumberPrecision(), setNumberFlags(), setIntegerBase() |
| 1487 |
*/ |
| 1488 |
void QTextStream::setRealNumberNotation(RealNumberNotation notation) |
| 1489 |
{ |
| 1490 |
Q_D(QTextStream); |
| 1491 |
d->realNumberNotation = notation; |
| 1492 |
} |
| 1493 |
|
| 1494 |
/*! |
| 1495 |
Returns the current real number notation. |
| 1496 |
|
| 1497 |
\sa setRealNumberNotation(), realNumberPrecision(), numberFlags(), integerBase() |
| 1498 |
*/ |
| 1499 |
QTextStream::RealNumberNotation QTextStream::realNumberNotation() const |
| 1500 |
{ |
| 1501 |
Q_D(const QTextStream); |
| 1502 |
return d->realNumberNotation; |
| 1503 |
} |
| 1504 |
|
| 1505 |
/*! |
| 1506 |
Sets the precision of real numbers to \a precision. This value |
| 1507 |
describes the number of fraction digits QTextStream should |
| 1508 |
write when generating real numbers. |
| 1509 |
|
| 1510 |
The precision cannot be a negative value. The default value is 6. |
| 1511 |
|
| 1512 |
\sa realNumberPrecision(), setRealNumberNotation() |
| 1513 |
*/ |
| 1514 |
void QTextStream::setRealNumberPrecision(int precision) |
| 1515 |
{ |
| 1516 |
Q_D(QTextStream); |
| 1517 |
if (precision < 0) { |
| 1518 |
qWarning("QTextStream::setRealNumberPrecision: Invalid precision (%d)", precision); |
| 1519 |
d->realNumberPrecision = 6; |
| 1520 |
return; |
| 1521 |
} |
| 1522 |
d->realNumberPrecision = precision; |
| 1523 |
} |
| 1524 |
|
| 1525 |
/*! |
| 1526 |
Returns the current real number precision, or the number of fraction |
| 1527 |
digits QTextStream will write when generating real numbers. |
| 1528 |
|
| 1529 |
\sa setRealNumberNotation(), realNumberNotation(), numberFlags(), integerBase() |
| 1530 |
*/ |
| 1531 |
int QTextStream::realNumberPrecision() const |
| 1532 |
{ |
| 1533 |
Q_D(const QTextStream); |
| 1534 |
return d->realNumberPrecision; |
| 1535 |
} |
| 1536 |
|
| 1537 |
/*! |
| 1538 |
Returns the status of the text stream. |
| 1539 |
|
| 1540 |
\sa QTextStream::Status, setStatus(), resetStatus() |
| 1541 |
*/ |
| 1542 |
|
| 1543 |
QTextStream::Status QTextStream::status() const |
| 1544 |
{ |
| 1545 |
Q_D(const QTextStream); |
| 1546 |
return d->status; |
| 1547 |
} |
| 1548 |
|
| 1549 |
/*! |
| 1550 |
\since 4.1 |
| 1551 |
|
| 1552 |
Resets the status of the text stream. |
| 1553 |
|
| 1554 |
\sa QTextStream::Status, status(), setStatus() |
| 1555 |
*/ |
| 1556 |
void QTextStream::resetStatus() |
| 1557 |
{ |
| 1558 |
Q_D(QTextStream); |
| 1559 |
d->status = Ok; |
| 1560 |
} |
| 1561 |
|
| 1562 |
/*! |
| 1563 |
\since 4.1 |
| 1564 |
|
| 1565 |
Sets the status of the text stream to the \a status given. |
| 1566 |
|
| 1567 |
\sa Status status() resetStatus() |
| 1568 |
*/ |
| 1569 |
void QTextStream::setStatus(Status status) |
| 1570 |
{ |
| 1571 |
Q_D(QTextStream); |
| 1572 |
if (d->status == Ok) |
| 1573 |
d->status = status; |
| 1574 |
} |
| 1575 |
|
| 1576 |
/*! |
| 1577 |
Returns true if there is no more data to be read from the |
| 1578 |
QTextStream; otherwise returns false. This is similar to, but not |
| 1579 |
the same as calling QIODevice::atEnd(), as QTextStream also takes |
| 1580 |
into account its internal Unicode buffer. |
| 1581 |
*/ |
| 1582 |
bool QTextStream::atEnd() const |
| 1583 |
{ |
| 1584 |
Q_D(const QTextStream); |
| 1585 |
CHECK_VALID_STREAM(true); |
| 1586 |
|
| 1587 |
if (d->string) |
| 1588 |
return d->string->size() == d->stringOffset; |
| 1589 |
return d->readBuffer.isEmpty() && d->device->atEnd(); |
| 1590 |
} |
| 1591 |
|
| 1592 |
/*! |
| 1593 |
Reads the entire content of the stream, and returns it as a |
| 1594 |
QString. Avoid this function when working on large files, as it |
| 1595 |
will consume a significant amount of memory. |
| 1596 |
|
| 1597 |
Calling readLine() is better if you do not know how much data is |
| 1598 |
available. |
| 1599 |
|
| 1600 |
\sa readLine() |
| 1601 |
*/ |
| 1602 |
QString QTextStream::readAll() |
| 1603 |
{ |
| 1604 |
Q_D(QTextStream); |
| 1605 |
CHECK_VALID_STREAM(QString()); |
| 1606 |
|
| 1607 |
const QChar *readPtr; |
| 1608 |
int length; |
| 1609 |
if (!d->scan(&readPtr, &length, /* maxlen = */ 0, QTextStreamPrivate::EndOfFile)) |
| 1610 |
return QString(); |
| 1611 |
|
| 1612 |
QString tmp = QString(readPtr, length); |
| 1613 |
d->consumeLastToken(); |
| 1614 |
return tmp; |
| 1615 |
} |
| 1616 |
|
| 1617 |
/*! |
| 1618 |
Reads one line of text from the stream, and returns it as a |
| 1619 |
QString. The maximum allowed line length is set to \a maxlen. If |
| 1620 |
the stream contains lines longer than this, then the lines will be |
| 1621 |
split after \a maxlen characters and returned in parts. |
| 1622 |
|
| 1623 |
If \a maxlen is 0, the lines can be of any length. A common value |
| 1624 |
for \a maxlen is 75. |
| 1625 |
|
| 1626 |
The returned line has no trailing end-of-line characters ("\\n" |
| 1627 |
or "\\r\\n"), so calling QString::trimmed() is unnecessary. |
| 1628 |
|
| 1629 |
If the stream has read to the end of the file, readLine() will return a |
| 1630 |
null QString. For strings, or for devices that support it, you can |
| 1631 |
explicitly test for the end of the stream using atEnd(). |
| 1632 |
|
| 1633 |
\sa readAll(), QIODevice::readLine() |
| 1634 |
*/ |
| 1635 |
QString QTextStream::readLine(qint64 maxlen) |
| 1636 |
{ |
| 1637 |
Q_D(QTextStream); |
| 1638 |
CHECK_VALID_STREAM(QString()); |
| 1639 |
|
| 1640 |
const QChar *readPtr; |
| 1641 |
int length; |
| 1642 |
if (!d->scan(&readPtr, &length, int(maxlen), QTextStreamPrivate::EndOfLine)) |
| 1643 |
return QString(); |
| 1644 |
|
| 1645 |
QString tmp = QString(readPtr, length); |
| 1646 |
d->consumeLastToken(); |
| 1647 |
return tmp; |
| 1648 |
} |
| 1649 |
|
| 1650 |
/*! |
| 1651 |
\since 4.1 |
| 1652 |
|
| 1653 |
Reads at most \a maxlen characters from the stream, and returns the data |
| 1654 |
read as a QString. |
| 1655 |
|
| 1656 |
\sa readAll(), readLine(), QIODevice::read() |
| 1657 |
*/ |
| 1658 |
QString QTextStream::read(qint64 maxlen) |
| 1659 |
{ |
| 1660 |
Q_D(QTextStream); |
| 1661 |
CHECK_VALID_STREAM(QString()); |
| 1662 |
|
| 1663 |
if (maxlen <= 0) |
| 1664 |
return QString::fromLatin1(""); // empty, not null |
| 1665 |
|
| 1666 |
const QChar *readPtr; |
| 1667 |
int length; |
| 1668 |
if (!d->scan(&readPtr, &length, int(maxlen), QTextStreamPrivate::EndOfFile)) |
| 1669 |
return QString(); |
| 1670 |
|
| 1671 |
QString tmp = QString(readPtr, length); |
| 1672 |
d->consumeLastToken(); |
| 1673 |
return tmp; |
| 1674 |
} |
| 1675 |
|
| 1676 |
/*! \internal |
| 1677 |
*/ |
| 1678 |
QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong *ret) |
| 1679 |
{ |
| 1680 |
scan(0, 0, 0, NotSpace); |
| 1681 |
consumeLastToken(); |
| 1682 |
|
| 1683 |
// detect int encoding |
| 1684 |
int base = integerBase; |
| 1685 |
if (base == 0) { |
| 1686 |
QChar ch; |
| 1687 |
if (!getChar(&ch)) |
| 1688 |
return npsInvalidPrefix; |
| 1689 |
if (ch == QLatin1Char('0')) { |
| 1690 |
QChar ch2; |
| 1691 |
if (!getChar(&ch2)) { |
| 1692 |
// Result is the number 0 |
| 1693 |
*ret = 0; |
| 1694 |
return npsOk; |
| 1695 |
} |
| 1696 |
ch2 = ch2.toLower(); |
| 1697 |
|
| 1698 |
if (ch2 == QLatin1Char('x')) { |
| 1699 |
base = 16; |
| 1700 |
} else if (ch2 == QLatin1Char('b')) { |
| 1701 |
base = 2; |
| 1702 |
} else if (ch2.isDigit() && ch2.digitValue() >= 0 && ch2.digitValue() <= 7) { |
| 1703 |
base = 8; |
| 1704 |
} else { |
| 1705 |
base = 10; |
| 1706 |
} |
| 1707 |
ungetChar(ch2); |
| 1708 |
} else if (ch == locale.negativeSign() || ch == locale.positiveSign() || ch.isDigit()) { |
| 1709 |
base = 10; |
| 1710 |
} else { |
| 1711 |
ungetChar(ch); |
| 1712 |
return npsInvalidPrefix; |
| 1713 |
} |
| 1714 |
ungetChar(ch); |
| 1715 |
// State of the stream is now the same as on entry |
| 1716 |
// (cursor is at prefix), |
| 1717 |
// and local variable 'base' has been set appropriately. |
| 1718 |
} |
| 1719 |
|
| 1720 |
qulonglong val=0; |
| 1721 |
switch (base) { |
| 1722 |
case 2: { |
| 1723 |
QChar pf1, pf2, dig; |
| 1724 |
// Parse prefix '0b' |
| 1725 |
if (!getChar(&pf1) || pf1 != QLatin1Char('0')) |
| 1726 |
return npsInvalidPrefix; |
| 1727 |
if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('b')) |
| 1728 |
return npsInvalidPrefix; |
| 1729 |
// Parse digits |
| 1730 |
int ndigits = 0; |
| 1731 |
while (getChar(&dig)) { |
| 1732 |
int n = dig.toLower().unicode(); |
| 1733 |
if (n == '0' || n == '1') { |
| 1734 |
val <<= 1; |
| 1735 |
val += n - '0'; |
| 1736 |
} else { |
| 1737 |
ungetChar(dig); |
| 1738 |
break; |
| 1739 |
} |
| 1740 |
ndigits++; |
| 1741 |
} |
| 1742 |
if (ndigits == 0) { |
| 1743 |
// Unwind the prefix and abort |
| 1744 |
ungetChar(pf2); |
| 1745 |
ungetChar(pf1); |
| 1746 |
return npsMissingDigit; |
| 1747 |
} |
| 1748 |
break; |
| 1749 |
} |
| 1750 |
case 8: { |
| 1751 |
QChar pf, dig; |
| 1752 |
// Parse prefix '0' |
| 1753 |
if (!getChar(&pf) || pf != QLatin1Char('0')) |
| 1754 |
return npsInvalidPrefix; |
| 1755 |
// Parse digits |
| 1756 |
int ndigits = 0; |
| 1757 |
while (getChar(&dig)) { |
| 1758 |
int n = dig.toLower().unicode(); |
| 1759 |
if (n >= '0' && n <= '7') { |
| 1760 |
val *= 8; |
| 1761 |
val += n - '0'; |
| 1762 |
} else { |
| 1763 |
ungetChar(dig); |
| 1764 |
break; |
| 1765 |
} |
| 1766 |
ndigits++; |
| 1767 |
} |
| 1768 |
if (ndigits == 0) { |
| 1769 |
// Unwind the prefix and abort |
| 1770 |
ungetChar(pf); |
| 1771 |
return npsMissingDigit; |
| 1772 |
} |
| 1773 |
break; |
| 1774 |
} |
| 1775 |
case 10: { |
| 1776 |
// Parse sign (or first digit) |
| 1777 |
QChar sign; |
| 1778 |
int ndigits = 0; |
| 1779 |
if (!getChar(&sign)) |
| 1780 |
return npsMissingDigit; |
| 1781 |
if (sign != locale.negativeSign() && sign != locale.positiveSign()) { |
| 1782 |
if (!sign.isDigit()) { |
| 1783 |
ungetChar(sign); |
| 1784 |
return npsMissingDigit; |
| 1785 |
} |
| 1786 |
val += sign.digitValue(); |
| 1787 |
ndigits++; |
| 1788 |
} |
| 1789 |
// Parse digits |
| 1790 |
QChar ch; |
| 1791 |
while (getChar(&ch)) { |
| 1792 |
if (ch.isDigit()) { |
| 1793 |
val *= 10; |
| 1794 |
val += ch.digitValue(); |
| 1795 |
} else if (locale.language() != QLocale::C |
| 1796 |
&& ch == locale.groupSeparator()) { |
| 1797 |
continue; |
| 1798 |
} else { |
| 1799 |
ungetChar(ch); |
| 1800 |
break; |
| 1801 |
} |
| 1802 |
ndigits++; |
| 1803 |
} |
| 1804 |
if (ndigits == 0) |
| 1805 |
return npsMissingDigit; |
| 1806 |
if (sign == locale.negativeSign()) { |
| 1807 |
qlonglong ival = qlonglong(val); |
| 1808 |
if (ival > 0) |
| 1809 |
ival = -ival; |
| 1810 |
val = qulonglong(ival); |
| 1811 |
} |
| 1812 |
break; |
| 1813 |
} |
| 1814 |
case 16: { |
| 1815 |
QChar pf1, pf2, dig; |
| 1816 |
// Parse prefix ' 0x' |
| 1817 |
if (!getChar(&pf1) || pf1 != QLatin1Char('0')) |
| 1818 |
return npsInvalidPrefix; |
| 1819 |
if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('x')) |
| 1820 |
return npsInvalidPrefix; |
| 1821 |
// Parse digits |
| 1822 |
int ndigits = 0; |
| 1823 |
while (getChar(&dig)) { |
| 1824 |
int n = dig.toLower().unicode(); |
| 1825 |
if (n >= '0' && n <= '9') { |
| 1826 |
val <<= 4; |
| 1827 |
val += n - '0'; |
| 1828 |
} else if (n >= 'a' && n <= 'f') { |
| 1829 |
val <<= 4; |
| 1830 |
val += 10 + (n - 'a'); |
| 1831 |
} else { |
| 1832 |
ungetChar(dig); |
| 1833 |
break; |
| 1834 |
} |
| 1835 |
ndigits++; |
| 1836 |
} |
| 1837 |
if (ndigits == 0) { |
| 1838 |
return npsMissingDigit; |
| 1839 |
} |
| 1840 |
break; |
| 1841 |
} |
| 1842 |
default: |
| 1843 |
// Unsupported integerBase |
| 1844 |
return npsInvalidPrefix; |
| 1845 |
} |
| 1846 |
|
| 1847 |
if (ret) |
| 1848 |
*ret = val; |
| 1849 |
return npsOk; |
| 1850 |
} |
| 1851 |
|
| 1852 |
/*! \internal |
| 1853 |
(hihi) |
| 1854 |
*/ |
| 1855 |
bool QTextStreamPrivate::getReal(double *f) |
| 1856 |
{ |
| 1857 |
// We use a table-driven FSM to parse floating point numbers |
| 1858 |
// strtod() cannot be used directly since we may be reading from a |
| 1859 |
// QIODevice. |
| 1860 |
enum ParserState { |
| 1861 |
Init = 0, |
| 1862 |
Sign = 1, |
| 1863 |
Mantissa = 2, |
| 1864 |
Dot = 3, |
| 1865 |
Abscissa = 4, |
| 1866 |
ExpMark = 5, |
| 1867 |
ExpSign = 6, |
| 1868 |
Exponent = 7, |
| 1869 |
Nan1 = 8, |
| 1870 |
Nan2 = 9, |
| 1871 |
Inf1 = 10, |
| 1872 |
Inf2 = 11, |
| 1873 |
NanInf = 12, |
| 1874 |
Done = 13 |
| 1875 |
}; |
| 1876 |
enum InputToken { |
| 1877 |
None = 0, |
| 1878 |
InputSign = 1, |
| 1879 |
InputDigit = 2, |
| 1880 |
InputDot = 3, |
| 1881 |
InputExp = 4, |
| 1882 |
InputI = 5, |
| 1883 |
InputN = 6, |
| 1884 |
InputF = 7, |
| 1885 |
InputA = 8, |
| 1886 |
InputT = 9 |
| 1887 |
}; |
| 1888 |
|
| 1889 |
static const uchar table[13][10] = { |
| 1890 |
// None InputSign InputDigit InputDot InputExp InputI InputN InputF InputA InputT |
| 1891 |
{ 0, Sign, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 }, // 0 Init |
| 1892 |
{ 0, 0, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 }, // 1 Sign |
| 1893 |
{ Done, Done, Mantissa, Dot, ExpMark, 0, 0, 0, 0, 0 }, // 2 Mantissa |
| 1894 |
{ 0, 0, Abscissa, 0, 0, 0, 0, 0, 0, 0 }, // 3 Dot |
| 1895 |
{ Done, Done, Abscissa, Done, ExpMark, 0, 0, 0, 0, 0 }, // 4 Abscissa |
| 1896 |
{ 0, ExpSign, Exponent, 0, 0, 0, 0, 0, 0, 0 }, // 5 ExpMark |
| 1897 |
{ 0, 0, Exponent, 0, 0, 0, 0, 0, 0, 0 }, // 6 ExpSign |
| 1898 |
{ Done, Done, Exponent, Done, Done, 0, 0, 0, 0, 0 }, // 7 Exponent |
| 1899 |
{ 0, 0, 0, 0, 0, 0, 0, 0, Nan2, 0 }, // 8 Nan1 |
| 1900 |
{ 0, 0, 0, 0, 0, 0, NanInf, 0, 0, 0 }, // 9 Nan2 |
| 1901 |
{ 0, 0, 0, 0, 0, 0, Inf2, 0, 0, 0 }, // 10 Inf1 |
| 1902 |
{ 0, 0, 0, 0, 0, 0, 0, NanInf, 0, 0 }, // 11 Inf2 |
| 1903 |
{ Done, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 11 NanInf |
| 1904 |
}; |
| 1905 |
|
| 1906 |
ParserState state = Init; |
| 1907 |
InputToken input = None; |
| 1908 |
|
| 1909 |
scan(0, 0, 0, NotSpace); |
| 1910 |
consumeLastToken(); |
| 1911 |
|
| 1912 |
const int BufferSize = 128; |
| 1913 |
char buf[BufferSize]; |
| 1914 |
int i = 0; |
| 1915 |
|
| 1916 |
QChar c; |
| 1917 |
while (getChar(&c)) { |
| 1918 |
switch (c.unicode()) { |
| 1919 |
case '0': case '1': case '2': case '3': case '4': |
| 1920 |
case '5': case '6': case '7': case '8': case '9': |
| 1921 |
input = InputDigit; |
| 1922 |
break; |
| 1923 |
case 'i': case 'I': |
| 1924 |
input = InputI; |
| 1925 |
break; |
| 1926 |
case 'n': case 'N': |
| 1927 |
input = InputN; |
| 1928 |
break; |
| 1929 |
case 'f': case 'F': |
| 1930 |
input = InputF; |
| 1931 |
break; |
| 1932 |
case 'a': case 'A': |
| 1933 |
input = InputA; |
| 1934 |
break; |
| 1935 |
case 't': case 'T': |
| 1936 |
input = InputT; |
| 1937 |
break; |
| 1938 |
default: { |
| 1939 |
QChar lc = c.toLower(); |
| 1940 |
if (lc == locale.decimalPoint().toLower()) |
| 1941 |
input = InputDot; |
| 1942 |
else if (lc == locale.exponential().toLower()) |
| 1943 |
input = InputExp; |
| 1944 |
else if (lc == locale.negativeSign().toLower() |
| 1945 |
|| lc == locale.positiveSign().toLower()) |
| 1946 |
input = InputSign; |
| 1947 |
else if (locale.language() != QLocale::C // backward-compatibility |
| 1948 |
&& lc == locale.groupSeparator().toLower()) |
| 1949 |
input = InputDigit; // well, it isn't a digit, but no one cares. |
| 1950 |
else |
| 1951 |
input = None; |
| 1952 |
} |
| 1953 |
break; |
| 1954 |
} |
| 1955 |
|
| 1956 |
state = ParserState(table[state][input]); |
| 1957 |
|
| 1958 |
if (state == Init || state == Done || i > (BufferSize - 5)) { |
| 1959 |
ungetChar(c); |
| 1960 |
if (i > (BufferSize - 5)) { // ignore rest of digits |
| 1961 |
while (getChar(&c)) { |
| 1962 |
if (!c.isDigit()) { |
| 1963 |
ungetChar(c); |
| 1964 |
break; |
| 1965 |
} |
| 1966 |
} |
| 1967 |
} |
| 1968 |
break; |
| 1969 |
} |
| 1970 |
|
| 1971 |
buf[i++] = c.toLatin1(); |
| 1972 |
} |
| 1973 |
|
| 1974 |
if (i == 0) |
| 1975 |
return false; |
| 1976 |
if (!f) |
| 1977 |
return true; |
| 1978 |
buf[i] = '\0'; |
| 1979 |
|
| 1980 |
// backward-compatibility. Old implmentation supported +nan/-nan |
| 1981 |
// for some reason. QLocale only checks for lower-case |
| 1982 |
// nan/+inf/-inf, so here we also check for uppercase and mixed |
| 1983 |
// case versions. |
| 1984 |
if (!qstricmp(buf, "nan") || !qstricmp(buf, "+nan") || !qstricmp(buf, "-nan")) { |
| 1985 |
*f = qSNaN(); |
| 1986 |
return true; |
| 1987 |
} else if (!qstricmp(buf, "+inf") || !qstricmp(buf, "inf")) { |
| 1988 |
*f = qInf(); |
| 1989 |
return true; |
| 1990 |
} else if (!qstricmp(buf, "-inf")) { |
| 1991 |
*f = -qInf(); |
| 1992 |
return true; |
| 1993 |
} |
| 1994 |
bool ok; |
| 1995 |
*f = locale.toDouble(QString::fromLatin1(buf), &ok); |
| 1996 |
return ok; |
| 1997 |
} |
| 1998 |
|
| 1999 |
/*! |
| 2000 |
Reads a character from the stream and stores it in \a c. Returns a |
| 2001 |
reference to the QTextStream, so several operators can be |
| 2002 |
nested. Example: |
| 2003 |
|
| 2004 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 7 |
| 2005 |
|
| 2006 |
Whitespace is \e not skipped. |
| 2007 |
*/ |
| 2008 |
|
| 2009 |
QTextStream &QTextStream::operator>>(QChar &c) |
| 2010 |
{ |
| 2011 |
Q_D(QTextStream); |
| 2012 |
CHECK_VALID_STREAM(*this); |
| 2013 |
d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); |
| 2014 |
if (!d->getChar(&c)) |
| 2015 |
setStatus(ReadPastEnd); |
| 2016 |
return *this; |
| 2017 |
} |
| 2018 |
|
| 2019 |
/*! |
| 2020 |
\overload |
| 2021 |
|
| 2022 |
Reads a character from the stream and stores it in \a c. The |
| 2023 |
character from the stream is converted to ISO-5589-1 before it is |
| 2024 |
stored. |
| 2025 |
|
| 2026 |
\sa QChar::toLatin1() |
| 2027 |
*/ |
| 2028 |
QTextStream &QTextStream::operator>>(char &c) |
| 2029 |
{ |
| 2030 |
QChar ch; |
| 2031 |
*this >> ch; |
| 2032 |
c = ch.toLatin1(); |
| 2033 |
return *this; |
| 2034 |
} |
| 2035 |
|
| 2036 |
/*! |
| 2037 |
Reads an integer from the stream and stores it in \a i, then |
| 2038 |
returns a reference to the QTextStream. The number is cast to |
| 2039 |
the correct type before it is stored. If no number was detected on |
| 2040 |
the stream, \a i is set to 0. |
| 2041 |
|
| 2042 |
By default, QTextStream will attempt to detect the base of the |
| 2043 |
number using the following rules: |
| 2044 |
|
| 2045 |
\table |
| 2046 |
\header \o Prefix \o Base |
| 2047 |
\row \o "0b" or "0B" \o 2 (binary) |
| 2048 |
\row \o "0" followed by "0-7" \o 8 (octal) |
| 2049 |
\row \o "0" otherwise \o 10 (decimal) |
| 2050 |
\row \o "0x" or "0X" \o 16 (hexadecimal) |
| 2051 |
\row \o "1" to "9" \o 10 (decimal) |
| 2052 |
\endtable |
| 2053 |
|
| 2054 |
By calling setIntegerBase(), you can specify the integer base |
| 2055 |
explicitly. This will disable the auto-detection, and speed up |
| 2056 |
QTextStream slightly. |
| 2057 |
|
| 2058 |
Leading whitespace is skipped. |
| 2059 |
*/ |
| 2060 |
QTextStream &QTextStream::operator>>(signed short &i) |
| 2061 |
{ |
| 2062 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(signed short); |
| 2063 |
} |
| 2064 |
|
| 2065 |
/*! |
| 2066 |
\overload |
| 2067 |
|
| 2068 |
Stores the integer in the unsigned short \a i. |
| 2069 |
*/ |
| 2070 |
QTextStream &QTextStream::operator>>(unsigned short &i) |
| 2071 |
{ |
| 2072 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned short); |
| 2073 |
} |
| 2074 |
|
| 2075 |
/*! |
| 2076 |
\overload |
| 2077 |
|
| 2078 |
Stores the integer in the signed int \a i. |
| 2079 |
*/ |
| 2080 |
QTextStream &QTextStream::operator>>(signed int &i) |
| 2081 |
{ |
| 2082 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(signed int); |
| 2083 |
} |
| 2084 |
|
| 2085 |
/*! |
| 2086 |
\overload |
| 2087 |
|
| 2088 |
Stores the integer in the unsigned int \a i. |
| 2089 |
*/ |
| 2090 |
QTextStream &QTextStream::operator>>(unsigned int &i) |
| 2091 |
{ |
| 2092 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned int); |
| 2093 |
} |
| 2094 |
|
| 2095 |
/*! |
| 2096 |
\overload |
| 2097 |
|
| 2098 |
Stores the integer in the signed long \a i. |
| 2099 |
*/ |
| 2100 |
QTextStream &QTextStream::operator>>(signed long &i) |
| 2101 |
{ |
| 2102 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(signed long); |
| 2103 |
} |
| 2104 |
|
| 2105 |
/*! |
| 2106 |
\overload |
| 2107 |
|
| 2108 |
Stores the integer in the unsigned long \a i. |
| 2109 |
*/ |
| 2110 |
QTextStream &QTextStream::operator>>(unsigned long &i) |
| 2111 |
{ |
| 2112 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned long); |
| 2113 |
} |
| 2114 |
|
| 2115 |
/*! |
| 2116 |
\overload |
| 2117 |
|
| 2118 |
Stores the integer in the qlonglong \a i. |
| 2119 |
*/ |
| 2120 |
QTextStream &QTextStream::operator>>(qlonglong &i) |
| 2121 |
{ |
| 2122 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(qlonglong); |
| 2123 |
} |
| 2124 |
|
| 2125 |
/*! |
| 2126 |
\overload |
| 2127 |
|
| 2128 |
Stores the integer in the qulonglong \a i. |
| 2129 |
*/ |
| 2130 |
QTextStream &QTextStream::operator>>(qulonglong &i) |
| 2131 |
{ |
| 2132 |
IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(qulonglong); |
| 2133 |
} |
| 2134 |
|
| 2135 |
/*! |
| 2136 |
Reads a real number from the stream and stores it in \a f, then |
| 2137 |
returns a reference to the QTextStream. The number is cast to |
| 2138 |
the correct type. If no real number is detect on the stream, \a f |
| 2139 |
is set to 0.0. |
| 2140 |
|
| 2141 |
As a special exception, QTextStream allows the strings "nan" and "inf" to |
| 2142 |
represent NAN and INF floats or doubles. |
| 2143 |
|
| 2144 |
Leading whitespace is skipped. |
| 2145 |
*/ |
| 2146 |
QTextStream &QTextStream::operator>>(float &f) |
| 2147 |
{ |
| 2148 |
IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(float); |
| 2149 |
} |
| 2150 |
|
| 2151 |
/*! |
| 2152 |
\overload |
| 2153 |
|
| 2154 |
Stores the real number in the double \a f. |
| 2155 |
*/ |
| 2156 |
QTextStream &QTextStream::operator>>(double &f) |
| 2157 |
{ |
| 2158 |
IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(double); |
| 2159 |
} |
| 2160 |
|
| 2161 |
/*! |
| 2162 |
Reads a word from the stream and stores it in \a str, then returns |
| 2163 |
a reference to the stream. Words are separated by whitespace |
| 2164 |
(i.e., all characters for which QChar::isSpace() returns true). |
| 2165 |
|
| 2166 |
Leading whitespace is skipped. |
| 2167 |
*/ |
| 2168 |
QTextStream &QTextStream::operator>>(QString &str) |
| 2169 |
{ |
| 2170 |
Q_D(QTextStream); |
| 2171 |
CHECK_VALID_STREAM(*this); |
| 2172 |
|
| 2173 |
str.clear(); |
| 2174 |
d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); |
| 2175 |
d->consumeLastToken(); |
| 2176 |
|
| 2177 |
const QChar *ptr; |
| 2178 |
int length; |
| 2179 |
if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) { |
| 2180 |
setStatus(ReadPastEnd); |
| 2181 |
return *this; |
| 2182 |
} |
| 2183 |
|
| 2184 |
str = QString(ptr, length); |
| 2185 |
d->consumeLastToken(); |
| 2186 |
return *this; |
| 2187 |
} |
| 2188 |
|
| 2189 |
/*! |
| 2190 |
\overload |
| 2191 |
|
| 2192 |
Converts the word to ISO-8859-1, then stores it in \a array. |
| 2193 |
|
| 2194 |
\sa QString::toLatin1() |
| 2195 |
*/ |
| 2196 |
QTextStream &QTextStream::operator>>(QByteArray &array) |
| 2197 |
{ |
| 2198 |
Q_D(QTextStream); |
| 2199 |
CHECK_VALID_STREAM(*this); |
| 2200 |
|
| 2201 |
array.clear(); |
| 2202 |
d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); |
| 2203 |
d->consumeLastToken(); |
| 2204 |
|
| 2205 |
const QChar *ptr; |
| 2206 |
int length; |
| 2207 |
if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) { |
| 2208 |
setStatus(ReadPastEnd); |
| 2209 |
return *this; |
| 2210 |
} |
| 2211 |
|
| 2212 |
for (int i = 0; i < length; ++i) |
| 2213 |
array += ptr[i].toLatin1(); |
| 2214 |
|
| 2215 |
d->consumeLastToken(); |
| 2216 |
return *this; |
| 2217 |
} |
| 2218 |
|
| 2219 |
/*! |
| 2220 |
\overload |
| 2221 |
|
| 2222 |
Stores the word in \a c, terminated by a '\0' character. If no word is |
| 2223 |
available, only the '\0' character is stored. |
| 2224 |
|
| 2225 |
Warning: Although convenient, this operator is dangerous and must |
| 2226 |
be used with care. QTextStream assumes that \a c points to a |
| 2227 |
buffer with enough space to hold the word. If the buffer is too |
| 2228 |
small, your application may crash. |
| 2229 |
|
| 2230 |
If possible, use the QByteArray operator instead. |
| 2231 |
*/ |
| 2232 |
QTextStream &QTextStream::operator>>(char *c) |
| 2233 |
{ |
| 2234 |
Q_D(QTextStream); |
| 2235 |
*c = 0; |
| 2236 |
CHECK_VALID_STREAM(*this); |
| 2237 |
d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); |
| 2238 |
d->consumeLastToken(); |
| 2239 |
|
| 2240 |
const QChar *ptr; |
| 2241 |
int length; |
| 2242 |
if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) { |
| 2243 |
setStatus(ReadPastEnd); |
| 2244 |
return *this; |
| 2245 |
} |
| 2246 |
|
| 2247 |
for (int i = 0; i < length; ++i) |
| 2248 |
*c++ = ptr[i].toLatin1(); |
| 2249 |
*c = '\0'; |
| 2250 |
d->consumeLastToken(); |
| 2251 |
return *this; |
| 2252 |
} |
| 2253 |
|
| 2254 |
/*! \internal |
| 2255 |
*/ |
| 2256 |
bool QTextStreamPrivate::putNumber(qulonglong number, bool negative) |
| 2257 |
{ |
| 2258 |
QString result; |
| 2259 |
|
| 2260 |
unsigned flags = 0; |
| 2261 |
if (numberFlags & QTextStream::ShowBase) |
| 2262 |
flags |= QLocalePrivate::ShowBase; |
| 2263 |
if (numberFlags & QTextStream::ForceSign) |
| 2264 |
flags |= QLocalePrivate::AlwaysShowSign; |
| 2265 |
if (numberFlags & QTextStream::UppercaseBase) |
| 2266 |
flags |= QLocalePrivate::UppercaseBase; |
| 2267 |
if (numberFlags & QTextStream::UppercaseDigits) |
| 2268 |
flags |= QLocalePrivate::CapitalEorX; |
| 2269 |
|
| 2270 |
// add thousands group separators. For backward compatibility we |
| 2271 |
// don't add a group separator for C locale. |
| 2272 |
if (locale.language() != QLocale::C) |
| 2273 |
flags |= QLocalePrivate::ThousandsGroup; |
| 2274 |
|
| 2275 |
const QLocalePrivate *dd = locale.d(); |
| 2276 |
int base = integerBase ? integerBase : 10; |
| 2277 |
if (negative && base == 10) { |
| 2278 |
result = dd->longLongToString(-static_cast<qlonglong>(number), -1, |
| 2279 |
base, -1, flags); |
| 2280 |
} else if (negative) { |
| 2281 |
// Workaround for backward compatibility for writing negative |
| 2282 |
// numbers in octal and hex: |
| 2283 |
// QTextStream(result) << showbase << hex << -1 << oct << -1 |
| 2284 |
// should output: -0x1 -0b1 |
| 2285 |
result = dd->unsLongLongToString(number, -1, base, -1, flags); |
| 2286 |
result.prepend(locale.negativeSign()); |
| 2287 |
} else { |
| 2288 |
result = dd->unsLongLongToString(number, -1, base, -1, flags); |
| 2289 |
// workaround for backward compatibility - in octal form with |
| 2290 |
// ShowBase flag set zero should be written as '00' |
| 2291 |
if (number == 0 && base == 8 && numberFlags & QTextStream::ShowBase |
| 2292 |
&& result == QLatin1String("0")) { |
| 2293 |
result.prepend(QLatin1String("0")); |
| 2294 |
} |
| 2295 |
} |
| 2296 |
return putString(result, true); |
| 2297 |
} |
| 2298 |
|
| 2299 |
/*! |
| 2300 |
\internal |
| 2301 |
\overload |
| 2302 |
*/ |
| 2303 |
QTextStream &QTextStream::operator<<(QBool b) |
| 2304 |
{ |
| 2305 |
return *this << bool(b); |
| 2306 |
} |
| 2307 |
|
| 2308 |
/*! |
| 2309 |
Writes the character \a c to the stream, then returns a reference |
| 2310 |
to the QTextStream. |
| 2311 |
|
| 2312 |
\sa setFieldWidth() |
| 2313 |
*/ |
| 2314 |
QTextStream &QTextStream::operator<<(QChar c) |
| 2315 |
{ |
| 2316 |
Q_D(QTextStream); |
| 2317 |
CHECK_VALID_STREAM(*this); |
| 2318 |
d->putString(QString(c)); |
| 2319 |
return *this; |
| 2320 |
} |
| 2321 |
|
| 2322 |
/*! |
| 2323 |
\overload |
| 2324 |
|
| 2325 |
Converts \a c from ASCII to a QChar, then writes it to the stream. |
| 2326 |
*/ |
| 2327 |
QTextStream &QTextStream::operator<<(char c) |
| 2328 |
{ |
| 2329 |
Q_D(QTextStream); |
| 2330 |
CHECK_VALID_STREAM(*this); |
| 2331 |
d->putString(QString(QChar::fromAscii(c))); |
| 2332 |
return *this; |
| 2333 |
} |
| 2334 |
|
| 2335 |
/*! |
| 2336 |
Writes the integer number \a i to the stream, then returns a |
| 2337 |
reference to the QTextStream. By default, the number is stored in |
| 2338 |
decimal form, but you can also set the base by calling |
| 2339 |
setIntegerBase(). |
| 2340 |
|
| 2341 |
\sa setFieldWidth(), setNumberFlags() |
| 2342 |
*/ |
| 2343 |
QTextStream &QTextStream::operator<<(signed short i) |
| 2344 |
{ |
| 2345 |
Q_D(QTextStream); |
| 2346 |
CHECK_VALID_STREAM(*this); |
| 2347 |
d->putNumber((qulonglong)qAbs(qlonglong(i)), i < 0); |
| 2348 |
return *this; |
| 2349 |
} |
| 2350 |
|
| 2351 |
/*! |
| 2352 |
\overload |
| 2353 |
|
| 2354 |
Writes the unsigned short \a i to the stream. |
| 2355 |
*/ |
| 2356 |
QTextStream &QTextStream::operator<<(unsigned short i) |
| 2357 |
{ |
| 2358 |
Q_D(QTextStream); |
| 2359 |
CHECK_VALID_STREAM(*this); |
| 2360 |
d->putNumber((qulonglong)i, false); |
| 2361 |
return *this; |
| 2362 |
} |
| 2363 |
|
| 2364 |
/*! |
| 2365 |
\overload |
| 2366 |
|
| 2367 |
Writes the signed int \a i to the stream. |
| 2368 |
*/ |
| 2369 |
QTextStream &QTextStream::operator<<(signed int i) |
| 2370 |
{ |
| 2371 |
Q_D(QTextStream); |
| 2372 |
CHECK_VALID_STREAM(*this); |
| 2373 |
d->putNumber((qulonglong)qAbs(qlonglong(i)), i < 0); |
| 2374 |
return *this; |
| 2375 |
} |
| 2376 |
|
| 2377 |
/*! |
| 2378 |
\overload |
| 2379 |
|
| 2380 |
Writes the unsigned int \a i to the stream. |
| 2381 |
*/ |
| 2382 |
QTextStream &QTextStream::operator<<(unsigned int i) |
| 2383 |
{ |
| 2384 |
Q_D(QTextStream); |
| 2385 |
CHECK_VALID_STREAM(*this); |
| 2386 |
d->putNumber((qulonglong)i, false); |
| 2387 |
return *this; |
| 2388 |
} |
| 2389 |
|
| 2390 |
/*! |
| 2391 |
\overload |
| 2392 |
|
| 2393 |
Writes the signed long \a i to the stream. |
| 2394 |
*/ |
| 2395 |
QTextStream &QTextStream::operator<<(signed long i) |
| 2396 |
{ |
| 2397 |
Q_D(QTextStream); |
| 2398 |
CHECK_VALID_STREAM(*this); |
| 2399 |
d->putNumber((qulonglong)qAbs(qlonglong(i)), i < 0); |
| 2400 |
return *this; |
| 2401 |
} |
| 2402 |
|
| 2403 |
/*! |
| 2404 |
\overload |
| 2405 |
|
| 2406 |
Writes the unsigned long \a i to the stream. |
| 2407 |
*/ |
| 2408 |
QTextStream &QTextStream::operator<<(unsigned long i) |
| 2409 |
{ |
| 2410 |
Q_D(QTextStream); |
| 2411 |
CHECK_VALID_STREAM(*this); |
| 2412 |
d->putNumber((qulonglong)i, false); |
| 2413 |
return *this; |
| 2414 |
} |
| 2415 |
|
| 2416 |
/*! |
| 2417 |
\overload |
| 2418 |
|
| 2419 |
Writes the qlonglong \a i to the stream. |
| 2420 |
*/ |
| 2421 |
QTextStream &QTextStream::operator<<(qlonglong i) |
| 2422 |
{ |
| 2423 |
Q_D(QTextStream); |
| 2424 |
CHECK_VALID_STREAM(*this); |
| 2425 |
d->putNumber((qulonglong)qAbs(i), i < 0); |
| 2426 |
return *this; |
| 2427 |
} |
| 2428 |
|
| 2429 |
/*! |
| 2430 |
\overload |
| 2431 |
|
| 2432 |
Writes the qulonglong \a i to the stream. |
| 2433 |
*/ |
| 2434 |
QTextStream &QTextStream::operator<<(qulonglong i) |
| 2435 |
{ |
| 2436 |
Q_D(QTextStream); |
| 2437 |
CHECK_VALID_STREAM(*this); |
| 2438 |
d->putNumber(i, false); |
| 2439 |
return *this; |
| 2440 |
} |
| 2441 |
|
| 2442 |
/*! |
| 2443 |
Writes the real number \a f to the stream, then returns a |
| 2444 |
reference to the QTextStream. By default, QTextStream stores it |
| 2445 |
using SmartNotation, with up to 6 digits of precision. You can |
| 2446 |
change the textual representation QTextStream will use for real |
| 2447 |
numbers by calling setRealNumberNotation(), |
| 2448 |
setRealNumberPrecision() and setNumberFlags(). |
| 2449 |
|
| 2450 |
\sa setFieldWidth(), setRealNumberNotation(), |
| 2451 |
setRealNumberPrecision(), setNumberFlags() |
| 2452 |
*/ |
| 2453 |
QTextStream &QTextStream::operator<<(float f) |
| 2454 |
{ |
| 2455 |
return *this << double(f); |
| 2456 |
} |
| 2457 |
|
| 2458 |
/*! |
| 2459 |
\overload |
| 2460 |
|
| 2461 |
Writes the double \a f to the stream. |
| 2462 |
*/ |
| 2463 |
QTextStream &QTextStream::operator<<(double f) |
| 2464 |
{ |
| 2465 |
Q_D(QTextStream); |
| 2466 |
CHECK_VALID_STREAM(*this); |
| 2467 |
|
| 2468 |
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal; |
| 2469 |
switch (realNumberNotation()) { |
| 2470 |
case FixedNotation: |
| 2471 |
form = QLocalePrivate::DFDecimal; |
| 2472 |
break; |
| 2473 |
case ScientificNotation: |
| 2474 |
form = QLocalePrivate::DFExponent; |
| 2475 |
break; |
| 2476 |
case SmartNotation: |
| 2477 |
form = QLocalePrivate::DFSignificantDigits; |
| 2478 |
break; |
| 2479 |
} |
| 2480 |
|
| 2481 |
uint flags = 0; |
| 2482 |
if (numberFlags() & ShowBase) |
| 2483 |
flags |= QLocalePrivate::ShowBase; |
| 2484 |
if (numberFlags() & ForceSign) |
| 2485 |
flags |= QLocalePrivate::AlwaysShowSign; |
| 2486 |
if (numberFlags() & UppercaseBase) |
| 2487 |
flags |= QLocalePrivate::UppercaseBase; |
| 2488 |
if (numberFlags() & UppercaseDigits) |
| 2489 |
flags |= QLocalePrivate::CapitalEorX; |
| 2490 |
if (numberFlags() & ForcePoint) |
| 2491 |
flags |= QLocalePrivate::Alternate; |
| 2492 |
|
| 2493 |
const QLocalePrivate *dd = d->locale.d(); |
| 2494 |
QString num = dd->doubleToString(f, d->realNumberPrecision, form, -1, flags); |
| 2495 |
d->putString(num, true); |
| 2496 |
return *this; |
| 2497 |
} |
| 2498 |
|
| 2499 |
/*! |
| 2500 |
Writes the string \a string to the stream, and returns a reference |
| 2501 |
to the QTextStream. The string is first encoded using the assigned |
| 2502 |
codec (the default codec is QTextCodec::codecForLocale()) before |
| 2503 |
it is written to the stream. |
| 2504 |
|
| 2505 |
\sa setFieldWidth(), setCodec() |
| 2506 |
*/ |
| 2507 |
QTextStream &QTextStream::operator<<(const QString &string) |
| 2508 |
{ |
| 2509 |
Q_D(QTextStream); |
| 2510 |
CHECK_VALID_STREAM(*this); |
| 2511 |
d->putString(string); |
| 2512 |
return *this; |
| 2513 |
} |
| 2514 |
|
| 2515 |
/*! |
| 2516 |
\overload |
| 2517 |
|
| 2518 |
Writes \a array to the stream. The contents of \a array are |
| 2519 |
converted with QString::fromAscii(). |
| 2520 |
*/ |
| 2521 |
QTextStream &QTextStream::operator<<(const QByteArray &array) |
| 2522 |
{ |
| 2523 |
Q_D(QTextStream); |
| 2524 |
CHECK_VALID_STREAM(*this); |
| 2525 |
d->putString(QString::fromAscii(array.constData(), array.length())); |
| 2526 |
return *this; |
| 2527 |
} |
| 2528 |
|
| 2529 |
/*! |
| 2530 |
\overload |
| 2531 |
|
| 2532 |
Writes the constant string pointed to by \a string to the stream. \a |
| 2533 |
string is assumed to be in ISO-8859-1 encoding. This operator |
| 2534 |
is convenient when working with constant string data. Example: |
| 2535 |
|
| 2536 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 8 |
| 2537 |
|
| 2538 |
Warning: QTextStream assumes that \a string points to a string of |
| 2539 |
text, terminated by a '\0' character. If there is no terminating |
| 2540 |
'\0' character, your application may crash. |
| 2541 |
*/ |
| 2542 |
QTextStream &QTextStream::operator<<(const char *string) |
| 2543 |
{ |
| 2544 |
Q_D(QTextStream); |
| 2545 |
CHECK_VALID_STREAM(*this); |
| 2546 |
d->putString(QLatin1String(string)); |
| 2547 |
return *this; |
| 2548 |
} |
| 2549 |
|
| 2550 |
/*! |
| 2551 |
\overload |
| 2552 |
|
| 2553 |
Writes \a ptr to the stream as a hexadecimal number with a base. |
| 2554 |
*/ |
| 2555 |
|
| 2556 |
QTextStream &QTextStream::operator<<(const void *ptr) |
| 2557 |
{ |
| 2558 |
Q_D(QTextStream); |
| 2559 |
CHECK_VALID_STREAM(*this); |
| 2560 |
int oldBase = d->integerBase; |
| 2561 |
NumberFlags oldFlags = d->numberFlags; |
| 2562 |
d->integerBase = 16; |
| 2563 |
d->numberFlags |= ShowBase; |
| 2564 |
d->putNumber(reinterpret_cast<quintptr>(ptr), false); |
| 2565 |
d->integerBase = oldBase; |
| 2566 |
d->numberFlags = oldFlags; |
| 2567 |
return *this; |
| 2568 |
} |
| 2569 |
|
| 2570 |
/*! |
| 2571 |
\relates QTextStream |
| 2572 |
|
| 2573 |
Calls QTextStream::setIntegerBase(2) on \a stream and returns \a |
| 2574 |
stream. |
| 2575 |
|
| 2576 |
\sa oct(), dec(), hex(), {QTextStream manipulators} |
| 2577 |
*/ |
| 2578 |
QTextStream &bin(QTextStream &stream) |
| 2579 |
{ |
| 2580 |
stream.setIntegerBase(2); |
| 2581 |
return stream; |
| 2582 |
} |
| 2583 |
|
| 2584 |
/*! |
| 2585 |
\relates QTextStream |
| 2586 |
|
| 2587 |
Calls QTextStream::setIntegerBase(8) on \a stream and returns \a |
| 2588 |
stream. |
| 2589 |
|
| 2590 |
\sa bin(), dec(), hex(), {QTextStream manipulators} |
| 2591 |
*/ |
| 2592 |
QTextStream &oct(QTextStream &stream) |
| 2593 |
{ |
| 2594 |
stream.setIntegerBase(8); |
| 2595 |
return stream; |
| 2596 |
} |
| 2597 |
|
| 2598 |
/*! |
| 2599 |
\relates QTextStream |
| 2600 |
|
| 2601 |
Calls QTextStream::setIntegerBase(10) on \a stream and returns \a |
| 2602 |
stream. |
| 2603 |
|
| 2604 |
\sa bin(), oct(), hex(), {QTextStream manipulators} |
| 2605 |
*/ |
| 2606 |
QTextStream &dec(QTextStream &stream) |
| 2607 |
{ |
| 2608 |
stream.setIntegerBase(10); |
| 2609 |
return stream; |
| 2610 |
} |
| 2611 |
|
| 2612 |
/*! |
| 2613 |
\relates QTextStream |
| 2614 |
|
| 2615 |
Calls QTextStream::setIntegerBase(16) on \a stream and returns \a |
| 2616 |
stream. |
| 2617 |
|
| 2618 |
\note The hex modifier can only be used for writing to streams. |
| 2619 |
\sa bin(), oct(), dec(), {QTextStream manipulators} |
| 2620 |
*/ |
| 2621 |
QTextStream &hex(QTextStream &stream) |
| 2622 |
{ |
| 2623 |
stream.setIntegerBase(16); |
| 2624 |
return stream; |
| 2625 |
} |
| 2626 |
|
| 2627 |
/*! |
| 2628 |
\relates QTextStream |
| 2629 |
|
| 2630 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | |
| 2631 |
QTextStream::ShowBase) on \a stream and returns \a stream. |
| 2632 |
|
| 2633 |
\sa noshowbase(), forcesign(), forcepoint(), {QTextStream manipulators} |
| 2634 |
*/ |
| 2635 |
QTextStream &showbase(QTextStream &stream) |
| 2636 |
{ |
| 2637 |
stream.setNumberFlags(stream.numberFlags() | QTextStream::ShowBase); |
| 2638 |
return stream; |
| 2639 |
} |
| 2640 |
|
| 2641 |
/*! |
| 2642 |
\relates QTextStream |
| 2643 |
|
| 2644 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | |
| 2645 |
QTextStream::ForceSign) on \a stream and returns \a stream. |
| 2646 |
|
| 2647 |
\sa noforcesign(), forcepoint(), showbase(), {QTextStream manipulators} |
| 2648 |
*/ |
| 2649 |
QTextStream &forcesign(QTextStream &stream) |
| 2650 |
{ |
| 2651 |
stream.setNumberFlags(stream.numberFlags() | QTextStream::ForceSign); |
| 2652 |
return stream; |
| 2653 |
} |
| 2654 |
|
| 2655 |
/*! |
| 2656 |
\relates QTextStream |
| 2657 |
|
| 2658 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | |
| 2659 |
QTextStream::ForcePoint) on \a stream and returns \a stream. |
| 2660 |
|
| 2661 |
\sa noforcepoint(), forcesign(), showbase(), {QTextStream manipulators} |
| 2662 |
*/ |
| 2663 |
QTextStream &forcepoint(QTextStream &stream) |
| 2664 |
{ |
| 2665 |
stream.setNumberFlags(stream.numberFlags() | QTextStream::ForcePoint); |
| 2666 |
return stream; |
| 2667 |
} |
| 2668 |
|
| 2669 |
/*! |
| 2670 |
\relates QTextStream |
| 2671 |
|
| 2672 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & |
| 2673 |
~QTextStream::ShowBase) on \a stream and returns \a stream. |
| 2674 |
|
| 2675 |
\sa showbase(), noforcesign(), noforcepoint(), {QTextStream manipulators} |
| 2676 |
*/ |
| 2677 |
QTextStream &noshowbase(QTextStream &stream) |
| 2678 |
{ |
| 2679 |
stream.setNumberFlags(stream.numberFlags() &= ~QTextStream::ShowBase); |
| 2680 |
return stream; |
| 2681 |
} |
| 2682 |
|
| 2683 |
/*! |
| 2684 |
\relates QTextStream |
| 2685 |
|
| 2686 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & |
| 2687 |
~QTextStream::ForceSign) on \a stream and returns \a stream. |
| 2688 |
|
| 2689 |
\sa forcesign(), noforcepoint(), noshowbase(), {QTextStream manipulators} |
| 2690 |
*/ |
| 2691 |
QTextStream &noforcesign(QTextStream &stream) |
| 2692 |
{ |
| 2693 |
stream.setNumberFlags(stream.numberFlags() &= ~QTextStream::ForceSign); |
| 2694 |
return stream; |
| 2695 |
} |
| 2696 |
|
| 2697 |
/*! |
| 2698 |
\relates QTextStream |
| 2699 |
|
| 2700 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & |
| 2701 |
~QTextStream::ForcePoint) on \a stream and returns \a stream. |
| 2702 |
|
| 2703 |
\sa forcepoint(), noforcesign(), noshowbase(), {QTextStream manipulators} |
| 2704 |
*/ |
| 2705 |
QTextStream &noforcepoint(QTextStream &stream) |
| 2706 |
{ |
| 2707 |
stream.setNumberFlags(stream.numberFlags() &= ~QTextStream::ForcePoint); |
| 2708 |
return stream; |
| 2709 |
} |
| 2710 |
|
| 2711 |
/*! |
| 2712 |
\relates QTextStream |
| 2713 |
|
| 2714 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | |
| 2715 |
QTextStream::UppercaseBase) on \a stream and returns \a stream. |
| 2716 |
|
| 2717 |
\sa lowercasebase(), uppercasedigits(), {QTextStream manipulators} |
| 2718 |
*/ |
| 2719 |
QTextStream &uppercasebase(QTextStream &stream) |
| 2720 |
{ |
| 2721 |
stream.setNumberFlags(stream.numberFlags() | QTextStream::UppercaseBase); |
| 2722 |
return stream; |
| 2723 |
} |
| 2724 |
|
| 2725 |
/*! |
| 2726 |
\relates QTextStream |
| 2727 |
|
| 2728 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | |
| 2729 |
QTextStream::UppercaseDigits) on \a stream and returns \a stream. |
| 2730 |
|
| 2731 |
\sa lowercasedigits(), uppercasebase(), {QTextStream manipulators} |
| 2732 |
*/ |
| 2733 |
QTextStream &uppercasedigits(QTextStream &stream) |
| 2734 |
{ |
| 2735 |
stream.setNumberFlags(stream.numberFlags() | QTextStream::UppercaseDigits); |
| 2736 |
return stream; |
| 2737 |
} |
| 2738 |
|
| 2739 |
/*! |
| 2740 |
\relates QTextStream |
| 2741 |
|
| 2742 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & |
| 2743 |
~QTextStream::UppercaseBase) on \a stream and returns \a stream. |
| 2744 |
|
| 2745 |
\sa uppercasebase(), lowercasedigits(), {QTextStream manipulators} |
| 2746 |
*/ |
| 2747 |
QTextStream &lowercasebase(QTextStream &stream) |
| 2748 |
{ |
| 2749 |
stream.setNumberFlags(stream.numberFlags() & ~QTextStream::UppercaseBase); |
| 2750 |
return stream; |
| 2751 |
} |
| 2752 |
|
| 2753 |
/*! |
| 2754 |
\relates QTextStream |
| 2755 |
|
| 2756 |
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & |
| 2757 |
~QTextStream::UppercaseDigits) on \a stream and returns \a stream. |
| 2758 |
|
| 2759 |
\sa uppercasedigits(), lowercasebase(), {QTextStream manipulators} |
| 2760 |
*/ |
| 2761 |
QTextStream &lowercasedigits(QTextStream &stream) |
| 2762 |
{ |
| 2763 |
stream.setNumberFlags(stream.numberFlags() & ~QTextStream::UppercaseDigits); |
| 2764 |
return stream; |
| 2765 |
} |
| 2766 |
|
| 2767 |
/*! |
| 2768 |
\relates QTextStream |
| 2769 |
|
| 2770 |
Calls QTextStream::setRealNumberNotation(QTextStream::FixedNotation) |
| 2771 |
on \a stream and returns \a stream. |
| 2772 |
|
| 2773 |
\sa scientific(), {QTextStream manipulators} |
| 2774 |
*/ |
| 2775 |
QTextStream &fixed(QTextStream &stream) |
| 2776 |
{ |
| 2777 |
stream.setRealNumberNotation(QTextStream::FixedNotation); |
| 2778 |
return stream; |
| 2779 |
} |
| 2780 |
|
| 2781 |
/*! |
| 2782 |
\relates QTextStream |
| 2783 |
|
| 2784 |
Calls QTextStream::setRealNumberNotation(QTextStream::ScientificNotation) |
| 2785 |
on \a stream and returns \a stream. |
| 2786 |
|
| 2787 |
\sa fixed(), {QTextStream manipulators} |
| 2788 |
*/ |
| 2789 |
QTextStream &scientific(QTextStream &stream) |
| 2790 |
{ |
| 2791 |
stream.setRealNumberNotation(QTextStream::ScientificNotation); |
| 2792 |
return stream; |
| 2793 |
} |
| 2794 |
|
| 2795 |
/*! |
| 2796 |
\relates QTextStream |
| 2797 |
|
| 2798 |
Calls QTextStream::setFieldAlignment(QTextStream::AlignLeft) |
| 2799 |
on \a stream and returns \a stream. |
| 2800 |
|
| 2801 |
\sa right(), center(), {QTextStream manipulators} |
| 2802 |
*/ |
| 2803 |
QTextStream &left(QTextStream &stream) |
| 2804 |
{ |
| 2805 |
stream.setFieldAlignment(QTextStream::AlignLeft); |
| 2806 |
return stream; |
| 2807 |
} |
| 2808 |
|
| 2809 |
/*! |
| 2810 |
\relates QTextStream |
| 2811 |
|
| 2812 |
Calls QTextStream::setFieldAlignment(QTextStream::AlignRight) |
| 2813 |
on \a stream and returns \a stream. |
| 2814 |
|
| 2815 |
\sa left(), center(), {QTextStream manipulators} |
| 2816 |
*/ |
| 2817 |
QTextStream &right(QTextStream &stream) |
| 2818 |
{ |
| 2819 |
stream.setFieldAlignment(QTextStream::AlignRight); |
| 2820 |
return stream; |
| 2821 |
} |
| 2822 |
|
| 2823 |
/*! |
| 2824 |
\relates QTextStream |
| 2825 |
|
| 2826 |
Calls QTextStream::setFieldAlignment(QTextStream::AlignCenter) |
| 2827 |
on \a stream and returns \a stream. |
| 2828 |
|
| 2829 |
\sa left(), right(), {QTextStream manipulators} |
| 2830 |
*/ |
| 2831 |
QTextStream ¢er(QTextStream &stream) |
| 2832 |
{ |
| 2833 |
stream.setFieldAlignment(QTextStream::AlignCenter); |
| 2834 |
return stream; |
| 2835 |
} |
| 2836 |
|
| 2837 |
/*! |
| 2838 |
\relates QTextStream |
| 2839 |
|
| 2840 |
Writes '\n' to the \a stream and flushes the stream. |
| 2841 |
|
| 2842 |
Equivalent to |
| 2843 |
|
| 2844 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 9 |
| 2845 |
|
| 2846 |
Note: On Windows, all '\n' characters are written as '\r\n' if |
| 2847 |
QTextStream's device or string is opened using the QIODevice::Text flag. |
| 2848 |
|
| 2849 |
\sa flush(), reset(), {QTextStream manipulators} |
| 2850 |
*/ |
| 2851 |
QTextStream &endl(QTextStream &stream) |
| 2852 |
{ |
| 2853 |
return stream << QLatin1Char('\n') << flush; |
| 2854 |
} |
| 2855 |
|
| 2856 |
/*! |
| 2857 |
\relates QTextStream |
| 2858 |
|
| 2859 |
Calls QTextStream::flush() on \a stream and returns \a stream. |
| 2860 |
|
| 2861 |
\sa endl(), reset(), {QTextStream manipulators} |
| 2862 |
*/ |
| 2863 |
QTextStream &flush(QTextStream &stream) |
| 2864 |
{ |
| 2865 |
stream.flush(); |
| 2866 |
return stream; |
| 2867 |
} |
| 2868 |
|
| 2869 |
/*! |
| 2870 |
\relates QTextStream |
| 2871 |
|
| 2872 |
Calls QTextStream::reset() on \a stream and returns \a stream. |
| 2873 |
|
| 2874 |
\sa flush(), {QTextStream manipulators} |
| 2875 |
*/ |
| 2876 |
QTextStream &reset(QTextStream &stream) |
| 2877 |
{ |
| 2878 |
stream.reset(); |
| 2879 |
return stream; |
| 2880 |
} |
| 2881 |
|
| 2882 |
/*! |
| 2883 |
\relates QTextStream |
| 2884 |
|
| 2885 |
Calls skipWhiteSpace() on \a stream and returns \a stream. |
| 2886 |
|
| 2887 |
\sa {QTextStream manipulators} |
| 2888 |
*/ |
| 2889 |
QTextStream &ws(QTextStream &stream) |
| 2890 |
{ |
| 2891 |
stream.skipWhiteSpace(); |
| 2892 |
return stream; |
| 2893 |
} |
| 2894 |
|
| 2895 |
/*! |
| 2896 |
\fn QTextStreamManipulator qSetFieldWidth(int width) |
| 2897 |
\relates QTextStream |
| 2898 |
|
| 2899 |
Equivalent to QTextStream::setFieldWidth(\a width). |
| 2900 |
*/ |
| 2901 |
|
| 2902 |
/*! |
| 2903 |
\fn QTextStreamManipulator qSetPadChar(QChar ch) |
| 2904 |
\relates QTextStream |
| 2905 |
|
| 2906 |
Equivalent to QTextStream::setPadChar(\a ch). |
| 2907 |
*/ |
| 2908 |
|
| 2909 |
/*! |
| 2910 |
\fn QTextStreamManipulator qSetRealNumberPrecision(int precision) |
| 2911 |
\relates QTextStream |
| 2912 |
|
| 2913 |
Equivalent to QTextStream::setRealNumberPrecision(\a precision). |
| 2914 |
*/ |
| 2915 |
|
| 2916 |
#ifndef QT_NO_TEXTCODEC |
| 2917 |
/*! |
| 2918 |
\relates QTextStream |
| 2919 |
|
| 2920 |
Toggles insertion of the Byte Order Mark on \a stream when QTextStream is |
| 2921 |
used with a UTF codec. |
| 2922 |
|
| 2923 |
\sa QTextStream::setGenerateByteOrderMark(), {QTextStream manipulators} |
| 2924 |
*/ |
| 2925 |
QTextStream &bom(QTextStream &stream) |
| 2926 |
{ |
| 2927 |
stream.setGenerateByteOrderMark(true); |
| 2928 |
return stream; |
| 2929 |
} |
| 2930 |
|
| 2931 |
/*! |
| 2932 |
Sets the codec for this stream to \a codec. The codec is used for |
| 2933 |
decoding any data that is read from the assigned device, and for |
| 2934 |
encoding any data that is written. By default, |
| 2935 |
QTextCodec::codecForLocale() is used, and automatic unicode |
| 2936 |
detection is enabled. |
| 2937 |
|
| 2938 |
If QTextStream operates on a string, this function does nothing. |
| 2939 |
|
| 2940 |
\warning If you call this function while the text stream is reading |
| 2941 |
from an open sequential socket, the internal buffer may still contain |
| 2942 |
text decoded using the old codec. |
| 2943 |
|
| 2944 |
\sa codec(), setAutoDetectUnicode(), setLocale() |
| 2945 |
*/ |
| 2946 |
void QTextStream::setCodec(QTextCodec *codec) |
| 2947 |
{ |
| 2948 |
Q_D(QTextStream); |
| 2949 |
qint64 seekPos = -1; |
| 2950 |
if (!d->readBuffer.isEmpty()) { |
| 2951 |
if (!d->device->isSequential()) { |
| 2952 |
seekPos = pos(); |
| 2953 |
} |
| 2954 |
} |
| 2955 |
d->codec = codec; |
| 2956 |
if (seekPos >=0 && !d->readBuffer.isEmpty()) |
| 2957 |
seek(seekPos); |
| 2958 |
} |
| 2959 |
|
| 2960 |
/*! |
| 2961 |
Sets the codec for this stream to the QTextCodec for the encoding |
| 2962 |
specified by \a codecName. Common values for \c codecName include |
| 2963 |
"ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't |
| 2964 |
recognized, nothing happens. |
| 2965 |
|
| 2966 |
Example: |
| 2967 |
|
| 2968 |
\snippet doc/src/snippets/code/src_corelib_io_qtextstream.cpp 10 |
| 2969 |
|
| 2970 |
\sa QTextCodec::codecForName(), setLocale() |
| 2971 |
*/ |
| 2972 |
void QTextStream::setCodec(const char *codecName) |
| 2973 |
{ |
| 2974 |
QTextCodec *codec = QTextCodec::codecForName(codecName); |
| 2975 |
if (codec) |
| 2976 |
setCodec(codec); |
| 2977 |
} |
| 2978 |
|
| 2979 |
/*! |
| 2980 |
Returns the codec that is current assigned to the stream. |
| 2981 |
|
| 2982 |
\sa setCodec(), setAutoDetectUnicode(), locale() |
| 2983 |
*/ |
| 2984 |
QTextCodec *QTextStream::codec() const |
| 2985 |
{ |
| 2986 |
Q_D(const QTextStream); |
| 2987 |
return d->codec; |
| 2988 |
} |
| 2989 |
|
| 2990 |
/*! |
| 2991 |
If \a enabled is true, QTextStream will attempt to detect Unicode |
| 2992 |
encoding by peeking into the stream data to see if it can find the |
| 2993 |
UTF-16 or UTF-32 BOM (Byte Order Mark). If this mark is found, QTextStream |
| 2994 |
will replace the current codec with the UTF codec. |
| 2995 |
|
| 2996 |
This function can be used together with setCodec(). It is common |
| 2997 |
to set the codec to UTF-8, and then enable UTF-16 detection. |
| 2998 |
|
| 2999 |
\sa autoDetectUnicode(), setCodec() |
| 3000 |
*/ |
| 3001 |
void QTextStream::setAutoDetectUnicode(bool enabled) |
| 3002 |
{ |
| 3003 |
Q_D(QTextStream); |
| 3004 |
d->autoDetectUnicode = enabled; |
| 3005 |
} |
| 3006 |
|
| 3007 |
/*! |
| 3008 |
Returns true if automatic Unicode detection is enabled; otherwise |
| 3009 |
returns false. |
| 3010 |
|
| 3011 |
\sa setAutoDetectUnicode(), setCodec() |
| 3012 |
*/ |
| 3013 |
bool QTextStream::autoDetectUnicode() const |
| 3014 |
{ |
| 3015 |
Q_D(const QTextStream); |
| 3016 |
return d->autoDetectUnicode; |
| 3017 |
} |
| 3018 |
|
| 3019 |
/*! |
| 3020 |
If \a generate is true and a UTF codec is used, QTextStream will insert |
| 3021 |
the BOM (Byte Order Mark) before any data has been written to the |
| 3022 |
device. If \a generate is false, no BOM will be inserted. This function |
| 3023 |
must be called before any data is written. Otherwise, it does nothing. |
| 3024 |
|
| 3025 |
\sa generateByteOrderMark(), bom() |
| 3026 |
*/ |
| 3027 |
void QTextStream::setGenerateByteOrderMark(bool generate) |
| 3028 |
{ |
| 3029 |
Q_D(QTextStream); |
| 3030 |
if (d->writeBuffer.isEmpty()) { |
| 3031 |
if (generate) |
| 3032 |
d->writeConverterState.flags &= ~QTextCodec::IgnoreHeader; |
| 3033 |
else |
| 3034 |
d->writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3035 |
} |
| 3036 |
} |
| 3037 |
|
| 3038 |
/*! |
| 3039 |
Returns true if QTextStream is set to generate the UTF BOM (Byte Order |
| 3040 |
Mark) when using a UTF codec; otherwise returns false. |
| 3041 |
|
| 3042 |
\sa setGenerateByteOrderMark() |
| 3043 |
*/ |
| 3044 |
bool QTextStream::generateByteOrderMark() const |
| 3045 |
{ |
| 3046 |
Q_D(const QTextStream); |
| 3047 |
return (d->writeConverterState.flags & QTextCodec::IgnoreHeader) == 0; |
| 3048 |
} |
| 3049 |
|
| 3050 |
#endif |
| 3051 |
|
| 3052 |
/*! |
| 3053 |
\since 4.5 |
| 3054 |
|
| 3055 |
Sets the locale for this stream to \a locale. The specified locale is |
| 3056 |
used for conversions between numbers and their string representations. |
| 3057 |
|
| 3058 |
The default locale is C and it is a special case - the thousands |
| 3059 |
group separator is not used for backward compatibility reasons. |
| 3060 |
|
| 3061 |
\sa locale() |
| 3062 |
*/ |
| 3063 |
void QTextStream::setLocale(const QLocale &locale) |
| 3064 |
{ |
| 3065 |
Q_D(QTextStream); |
| 3066 |
d->locale = locale; |
| 3067 |
} |
| 3068 |
|
| 3069 |
/*! |
| 3070 |
\since 4.5 |
| 3071 |
|
| 3072 |
Returns the locale for this stream. The default locale is C. |
| 3073 |
|
| 3074 |
\sa setLocale() |
| 3075 |
*/ |
| 3076 |
QLocale QTextStream::locale() const |
| 3077 |
{ |
| 3078 |
Q_D(const QTextStream); |
| 3079 |
return d->locale; |
| 3080 |
} |
| 3081 |
|
| 3082 |
#ifdef QT3_SUPPORT |
| 3083 |
/*! |
| 3084 |
\class QTextIStream |
| 3085 |
\brief The QTextIStream class is a convenience class for input streams. |
| 3086 |
|
| 3087 |
\compat |
| 3088 |
\reentrant |
| 3089 |
\ingroup io |
| 3090 |
\ingroup text |
| 3091 |
|
| 3092 |
Use QTextStream instead. |
| 3093 |
*/ |
| 3094 |
|
| 3095 |
/*! |
| 3096 |
\fn QTextIStream::QTextIStream(const QString *string) |
| 3097 |
|
| 3098 |
Use QTextStream(&\a{string}, QIODevice::ReadOnly) instead. |
| 3099 |
*/ |
| 3100 |
/*! |
| 3101 |
\fn QTextIStream::QTextIStream(QByteArray *byteArray) |
| 3102 |
|
| 3103 |
Use QTextStream(&\a{byteArray}, QIODevice::ReadOnly) instead. |
| 3104 |
*/ |
| 3105 |
/*! |
| 3106 |
\fn QTextIStream::QTextIStream(FILE *file) |
| 3107 |
|
| 3108 |
Use QTextStream(\a{file}, QIODevice::ReadOnly) instead. |
| 3109 |
*/ |
| 3110 |
|
| 3111 |
/*! |
| 3112 |
\class QTextOStream |
| 3113 |
\brief The QTextOStream class is a convenience class for output streams. |
| 3114 |
|
| 3115 |
\compat |
| 3116 |
\reentrant |
| 3117 |
\ingroup io |
| 3118 |
\ingroup text |
| 3119 |
|
| 3120 |
Use QTextStream instead. |
| 3121 |
*/ |
| 3122 |
|
| 3123 |
/*! |
| 3124 |
\fn QTextOStream::QTextOStream(QString *string) |
| 3125 |
|
| 3126 |
Use QTextStream(&\a{string}, QIODevice::WriteOnly) instead. |
| 3127 |
*/ |
| 3128 |
/*! |
| 3129 |
\fn QTextOStream::QTextOStream(QByteArray *byteArray) |
| 3130 |
|
| 3131 |
Use QTextStream(&\a{byteArray}, QIODevice::WriteOnly) instead. |
| 3132 |
*/ |
| 3133 |
/*! |
| 3134 |
\fn QTextOStream::QTextOStream(FILE *file) |
| 3135 |
|
| 3136 |
Use QTextStream(\a{file}, QIODevice::WriteOnly) instead. |
| 3137 |
*/ |
| 3138 |
|
| 3139 |
/*! \internal |
| 3140 |
*/ |
| 3141 |
int QTextStream::flagsInternal() const |
| 3142 |
{ |
| 3143 |
Q_D(const QTextStream); |
| 3144 |
|
| 3145 |
int f = 0; |
| 3146 |
switch (d->fieldAlignment) { |
| 3147 |
case AlignLeft: f |= left; break; |
| 3148 |
case AlignRight: f |= right; break; |
| 3149 |
case AlignCenter: f |= internal; break; |
| 3150 |
default: |
| 3151 |
break; |
| 3152 |
} |
| 3153 |
switch (d->integerBase) { |
| 3154 |
case 2: f |= bin; break; |
| 3155 |
case 8: f |= oct; break; |
| 3156 |
case 10: f |= dec; break; |
| 3157 |
case 16: f |= hex; break; |
| 3158 |
default: |
| 3159 |
break; |
| 3160 |
} |
| 3161 |
switch (d->realNumberNotation) { |
| 3162 |
case FixedNotation: f |= fixed; break; |
| 3163 |
case ScientificNotation: f |= scientific; break; |
| 3164 |
default: |
| 3165 |
break; |
| 3166 |
} |
| 3167 |
if (d->numberFlags & ShowBase) |
| 3168 |
f |= showbase; |
| 3169 |
if (d->numberFlags & ForcePoint) |
| 3170 |
f |= showpoint; |
| 3171 |
if (d->numberFlags & ForceSign) |
| 3172 |
f |= showpos; |
| 3173 |
if (d->numberFlags & UppercaseBase) |
| 3174 |
f |= uppercase; |
| 3175 |
return f; |
| 3176 |
} |
| 3177 |
|
| 3178 |
/*! \internal |
| 3179 |
*/ |
| 3180 |
int QTextStream::flagsInternal(int newFlags) |
| 3181 |
{ |
| 3182 |
int oldFlags = flagsInternal(); |
| 3183 |
|
| 3184 |
if (newFlags & left) |
| 3185 |
setFieldAlignment(AlignLeft); |
| 3186 |
else if (newFlags & right) |
| 3187 |
setFieldAlignment(AlignRight); |
| 3188 |
else if (newFlags & internal) |
| 3189 |
setFieldAlignment(AlignCenter); |
| 3190 |
|
| 3191 |
if (newFlags & bin) |
| 3192 |
setIntegerBase(2); |
| 3193 |
else if (newFlags & oct) |
| 3194 |
setIntegerBase(8); |
| 3195 |
else if (newFlags & dec) |
| 3196 |
setIntegerBase(10); |
| 3197 |
else if (newFlags & hex) |
| 3198 |
setIntegerBase(16); |
| 3199 |
|
| 3200 |
if (newFlags & showbase) |
| 3201 |
setNumberFlags(numberFlags() | ShowBase); |
| 3202 |
if (newFlags & showpos) |
| 3203 |
setNumberFlags(numberFlags() | ForceSign); |
| 3204 |
if (newFlags & showpoint) |
| 3205 |
setNumberFlags(numberFlags() | ForcePoint); |
| 3206 |
if (newFlags & uppercase) |
| 3207 |
setNumberFlags(numberFlags() | UppercaseBase); |
| 3208 |
|
| 3209 |
if (newFlags & fixed) |
| 3210 |
setRealNumberNotation(FixedNotation); |
| 3211 |
else if (newFlags & scientific) |
| 3212 |
setRealNumberNotation(ScientificNotation); |
| 3213 |
|
| 3214 |
return oldFlags; |
| 3215 |
} |
| 3216 |
|
| 3217 |
#ifndef QT_NO_TEXTCODEC |
| 3218 |
/*! |
| 3219 |
Use setCodec() and setAutoDetectUnicode() instead. |
| 3220 |
*/ |
| 3221 |
void QTextStream::setEncoding(Encoding encoding) |
| 3222 |
{ |
| 3223 |
Q_D(QTextStream); |
| 3224 |
resetCodecConverterStateHelper(&d->readConverterState); |
| 3225 |
resetCodecConverterStateHelper(&d->writeConverterState); |
| 3226 |
|
| 3227 |
switch (encoding) { |
| 3228 |
case Locale: |
| 3229 |
d->writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3230 |
setCodec(QTextCodec::codecForLocale()); |
| 3231 |
d->autoDetectUnicode = true; |
| 3232 |
break; |
| 3233 |
case Latin1: |
| 3234 |
d->readConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3235 |
d->writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3236 |
setCodec(QTextCodec::codecForName("ISO-8859-1")); |
| 3237 |
d->autoDetectUnicode = false; |
| 3238 |
break; |
| 3239 |
case Unicode: |
| 3240 |
setCodec(QTextCodec::codecForName("UTF-16")); |
| 3241 |
d->autoDetectUnicode = false; |
| 3242 |
break; |
| 3243 |
case RawUnicode: |
| 3244 |
d->readConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3245 |
d->writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3246 |
setCodec(QTextCodec::codecForName("UTF-16")); |
| 3247 |
d->autoDetectUnicode = false; |
| 3248 |
break; |
| 3249 |
case UnicodeNetworkOrder: |
| 3250 |
d->readConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3251 |
d->writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3252 |
setCodec(QTextCodec::codecForName("UTF-16BE")); |
| 3253 |
d->autoDetectUnicode = false; |
| 3254 |
break; |
| 3255 |
case UnicodeReverse: |
| 3256 |
d->readConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3257 |
d->writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3258 |
setCodec(QTextCodec::codecForName("UTF-16LE")); |
| 3259 |
d->autoDetectUnicode = false; |
| 3260 |
break; |
| 3261 |
case UnicodeUTF8: |
| 3262 |
d->writeConverterState.flags |= QTextCodec::IgnoreHeader; |
| 3263 |
setCodec(QTextCodec::codecForName("UTF-8")); |
| 3264 |
d->autoDetectUnicode = true; |
| 3265 |
break; |
| 3266 |
} |
| 3267 |
} |
| 3268 |
#endif |
| 3269 |
|
| 3270 |
/*! |
| 3271 |
\enum QTextStream::Encoding |
| 3272 |
\compat |
| 3273 |
|
| 3274 |
\value Latin1 Use setCodec(QTextCodec::codecForName("ISO-8859-1")) instead. |
| 3275 |
\value Locale Use setCodec(QTextCodec::codecForLocale()) instead. |
| 3276 |
\value RawUnicode Use setCodec(QTextCodec::codecForName("UTF-16")) instead. |
| 3277 |
\value Unicode Use setCodec(QTextCodec::codecForName("UTF-16")) instead. |
| 3278 |
\value UnicodeNetworkOrder Use setCodec(QTextCodec::codecForName("UTF-16BE")) instead. |
| 3279 |
\value UnicodeReverse Use setCodec(QTextCodec::codecForName("UTF-16LE")) instead. |
| 3280 |
\value UnicodeUTF8 Use setCodec(QTextCodec::codecForName("UTF-8")) instead. |
| 3281 |
|
| 3282 |
Also, for all encodings except QTextStream::Latin1 and |
| 3283 |
QTextStream::UTF8, you need to call setAutoDetectUnicode(false) |
| 3284 |
to obtain the Qt 3 behavior in addition to the setCodec() call. |
| 3285 |
|
| 3286 |
\sa setCodec(), setAutoDetectUnicode() |
| 3287 |
*/ |
| 3288 |
|
| 3289 |
/*! |
| 3290 |
\fn int QTextStream::flags() const |
| 3291 |
|
| 3292 |
Use fieldAlignment(), padChar(), fieldWidth(), numberFlags(), |
| 3293 |
integerBase(), realNumberNotation(), and realNumberNotation |
| 3294 |
instead. |
| 3295 |
*/ |
| 3296 |
|
| 3297 |
/*! |
| 3298 |
\fn int QTextStream::flags(int) |
| 3299 |
|
| 3300 |
Use setFieldAlignment(), setPadChar(), setFieldWidth(), |
| 3301 |
setNumberFlags(), setIntegerBase(), setRealNumberNotation(), and |
| 3302 |
setRealNumberNotation instead. |
| 3303 |
*/ |
| 3304 |
|
| 3305 |
/*! |
| 3306 |
\fn int QTextStream::setf(int) |
| 3307 |
|
| 3308 |
Use setFieldAlignment(), setPadChar(), setFieldWidth(), |
| 3309 |
setNumberFlags(), setIntegerBase(), setRealNumberNotation(), and |
| 3310 |
setRealNumberNotation instead. |
| 3311 |
*/ |
| 3312 |
|
| 3313 |
/*! |
| 3314 |
\fn int QTextStream::setf(int, int) |
| 3315 |
|
| 3316 |
Use setFieldAlignment(), setPadChar(), setFieldWidth(), |
| 3317 |
setNumberFlags(), setIntegerBase(), setRealNumberNotation(), and |
| 3318 |
setRealNumberNotation instead. |
| 3319 |
*/ |
| 3320 |
|
| 3321 |
/*! |
| 3322 |
\fn int QTextStream::unsetf(int) |
| 3323 |
|
| 3324 |
Use setFieldAlignment(), setPadChar(), setFieldWidth(), |
| 3325 |
setNumberFlags(), setIntegerBase(), setRealNumberNotation(), and |
| 3326 |
setRealNumberNotation instead. |
| 3327 |
*/ |
| 3328 |
|
| 3329 |
/*! |
| 3330 |
\fn int QTextStream::width(int) |
| 3331 |
|
| 3332 |
Use setFieldWidth() instead. |
| 3333 |
*/ |
| 3334 |
|
| 3335 |
/*! |
| 3336 |
\fn int QTextStream::fill(int) |
| 3337 |
|
| 3338 |
Use setPadChar() instead. |
| 3339 |
*/ |
| 3340 |
|
| 3341 |
/*! |
| 3342 |
\fn int QTextStream::precision(int) |
| 3343 |
|
| 3344 |
Use setRealNumberPrecision() instead. |
| 3345 |
*/ |
| 3346 |
|
| 3347 |
/*! |
| 3348 |
\fn int QTextStream::read() |
| 3349 |
|
| 3350 |
Use readAll() or readLine() instead. |
| 3351 |
*/ |
| 3352 |
|
| 3353 |
/*! |
| 3354 |
\fn int QTextStream::unsetDevice() |
| 3355 |
|
| 3356 |
Use setDevice(0) instead. |
| 3357 |
*/ |
| 3358 |
|
| 3359 |
/*! |
| 3360 |
\variable QTextStream::skipws |
| 3361 |
\variable QTextStream::left |
| 3362 |
\variable QTextStream::right |
| 3363 |
\variable QTextStream::internal |
| 3364 |
\variable QTextStream::bin |
| 3365 |
\variable QTextStream::oct |
| 3366 |
\variable QTextStream::dec |
| 3367 |
\variable QTextStream::hex |
| 3368 |
\variable QTextStream::showbase |
| 3369 |
\variable QTextStream::showpoint |
| 3370 |
\variable QTextStream::uppercase |
| 3371 |
\variable QTextStream::showpos |
| 3372 |
\variable QTextStream::scientific |
| 3373 |
\variable QTextStream::fixed |
| 3374 |
\variable QTextStream::basefield |
| 3375 |
\variable QTextStream::adjustfield |
| 3376 |
\variable QTextStream::floatfield |
| 3377 |
\compat |
| 3378 |
|
| 3379 |
Use the new \l{QTextStream manipulators} instead. |
| 3380 |
*/ |
| 3381 |
|
| 3382 |
#endif |
| 3383 |
|
| 3384 |
QT_END_NAMESPACE |
| 3385 |
|
| 3386 |
#ifndef QT_NO_QOBJECT |
| 3387 |
#include "qtextstream.moc" |
| 3388 |
#endif |