Qt 4.8
qtextstream.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //#define QTEXTSTREAM_DEBUG
43 static const int QTEXTSTREAM_BUFFERSIZE = 16384;
44 
242 #include "qtextstream.h"
243 #include "qbuffer.h"
244 #include "qfile.h"
245 #include "qnumeric.h"
246 #ifndef QT_NO_TEXTCODEC
247 #include "qtextcodec.h"
248 #endif
249 #ifndef Q_OS_WINCE
250 #include <locale.h>
251 #endif
252 #include "private/qlocale_p.h"
253 
254 #include <stdlib.h>
255 #include <limits.h>
256 #include <new>
257 
258 #if defined QTEXTSTREAM_DEBUG
259 #include <ctype.h>
260 
262 
263 // Returns a human readable representation of the first \a len
264 // characters in \a data.
265 static QByteArray qt_prettyDebug(const char *data, int len, int maxSize)
266 {
267  if (!data) return "(null)";
268  QByteArray out;
269  for (int i = 0; i < len; ++i) {
270  char c = data[i];
271  if (isprint(int(uchar(c)))) {
272  out += c;
273  } else switch (c) {
274  case '\n': out += "\\n"; break;
275  case '\r': out += "\\r"; break;
276  case '\t': out += "\\t"; break;
277  default:
278  QString tmp;
279  tmp.sprintf("\\x%x", (unsigned int)(unsigned char)c);
280  out += tmp.toLatin1();
281  }
282  }
283 
284  if (len < maxSize)
285  out += "...";
286 
287  return out;
288 }
290 
291 #endif
292 
293 // A precondition macro
294 #define Q_VOID
295 #define CHECK_VALID_STREAM(x) do { \
296  if (!d->string && !d->device) { \
297  qWarning("QTextStream: No device"); \
298  return x; \
299  } } while (0)
300 
301 // Base implementations of operator>> for ints and reals
302 #define IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(type) do { \
303  Q_D(QTextStream); \
304  CHECK_VALID_STREAM(*this); \
305  qulonglong tmp; \
306  switch (d->getNumber(&tmp)) { \
307  case QTextStreamPrivate::npsOk: \
308  i = (type)tmp; \
309  break; \
310  case QTextStreamPrivate::npsMissingDigit: \
311  case QTextStreamPrivate::npsInvalidPrefix: \
312  i = (type)0; \
313  setStatus(atEnd() ? QTextStream::ReadPastEnd : QTextStream::ReadCorruptData); \
314  break; \
315  } \
316  return *this; } while (0)
317 
318 #define IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(type) do { \
319  Q_D(QTextStream); \
320  CHECK_VALID_STREAM(*this); \
321  double tmp; \
322  if (d->getReal(&tmp)) { \
323  f = (type)tmp; \
324  } else { \
325  f = (type)0; \
326  setStatus(atEnd() ? QTextStream::ReadPastEnd : QTextStream::ReadCorruptData); \
327  } \
328  return *this; } while (0)
329 
331 
332 #ifndef QT_NO_QOBJECT
334 {
335  Q_OBJECT
336 public:
338  { }
339 
340  inline void setupDevice(QTextStream *stream, QIODevice *device)
341  {
342  disconnect();
343  if (device)
344  connect(device, SIGNAL(aboutToClose()), this, SLOT(flushStream()));
345  this->stream = stream;
346  }
347 
348 public Q_SLOTS:
349  inline void flushStream() { stream->flush(); }
350 
351 private:
353 };
354 #endif
355 
356 //-------------------------------------------------------------------
358 {
360 public:
363  void reset();
364 
365  // device
367 #ifndef QT_NO_QOBJECT
369 #endif
371 
372  // string
375  QIODevice::OpenMode stringOpenMode;
376 
377 #ifndef QT_NO_TEXTCODEC
378  // codec
384 #endif
385 
386  // i/o
390  EndOfLine
391  };
392 
393  QString read(int maxlen);
394  bool scan(const QChar **ptr, int *tokenLength,
395  int maxlen, TokenDelimiter delimiter);
396  inline const QChar *readPtr() const;
397  inline void consumeLastToken();
398  inline void consume(int nchars);
399  void saveConverterState(qint64 newPos);
400  void restoreToSavedConverterState();
402 
403  // Return value type for getNumber()
407  npsInvalidPrefix
408  };
409 
410  inline bool getChar(QChar *ch);
411  inline void ungetChar(const QChar &ch);
412  NumberParsingStatus getNumber(qulonglong *l);
413  bool getReal(double *f);
414 
415  inline void write(const QString &data);
416  inline void putString(const QString &ch, bool number = false);
417  void putNumber(qulonglong number, bool negative);
418 
419  // buffers
420  bool fillReadBuffer(qint64 maxBytes = -1);
421  void resetReadBuffer();
422  void flushWriteBuffer();
426  int readConverterSavedStateOffset; //the offset between readBufferStartDevicePos and that start of the buffer
428 
429  // streaming parameters
436  QTextStream::NumberFlags numberFlags;
437 
438  // status
440 
442 
444 };
445 
449  :
450 #ifndef QT_NO_TEXTCODEC
451  readConverterSavedState(0),
452 #endif
453  readConverterSavedStateOffset(0),
454  locale(QLocale::c())
455 {
456  this->q_ptr = q_ptr;
457  reset();
458 }
459 
463 {
464  if (deleteDevice) {
465 #ifndef QT_NO_QOBJECT
466  device->blockSignals(true);
467 #endif
468  delete device;
469  }
470 #ifndef QT_NO_TEXTCODEC
472 #endif
473 }
474 
475 #ifndef QT_NO_TEXTCODEC
477 {
478  state->~ConverterState();
479  new (state) QTextCodec::ConverterState;
480 }
481 
483  const QTextCodec::ConverterState *src)
484 {
485  // ### QTextCodec::ConverterState's copy constructors and assignments are
486  // private. This function copies the structure manually.
487  Q_ASSERT(!src->d);
488  dest->flags = src->flags;
489  dest->invalidChars = src->invalidChars;
490  dest->state_data[0] = src->state_data[0];
491  dest->state_data[1] = src->state_data[1];
492  dest->state_data[2] = src->state_data[2];
493 }
494 #endif
495 
499 {
501  integerBase = 0;
502  fieldWidth = 0;
503  padChar = QLatin1Char(' ');
506  numberFlags = 0;
507 
508  device = 0;
509  deleteDevice = false;
510  string = 0;
511  stringOffset = 0;
513 
514  readBufferOffset = 0;
516  lastTokenSize = 0;
517 
518 #ifndef QT_NO_TEXTCODEC
525  autoDetectUnicode = true;
526 #endif
527 }
528 
532 {
533  // no buffer next to the QString itself; this function should only
534  // be called internally, for devices.
535  Q_ASSERT(!string);
536  Q_ASSERT(device);
537 
538  // handle text translation and bypass the Text flag in the device.
539  bool textModeEnabled = device->isTextModeEnabled();
540  if (textModeEnabled)
541  device->setTextModeEnabled(false);
542 
543  // read raw data into a temporary buffer
544  char buf[QTEXTSTREAM_BUFFERSIZE];
545  qint64 bytesRead = 0;
546 #if defined(Q_OS_WIN)
547  // On Windows, there is no non-blocking stdin - so we fall back to reading
548  // lines instead. If there is no QOBJECT, we read lines for all sequential
549  // devices; otherwise, we read lines only for stdin.
550  QFile *file = 0;
551  Q_UNUSED(file);
552  if (device->isSequential()
553 #if !defined(QT_NO_QOBJECT)
554  && (file = qobject_cast<QFile *>(device)) && file->handle() == 0
555 #endif
556  ) {
557  if (maxBytes != -1)
558  bytesRead = device->readLine(buf, qMin<qint64>(sizeof(buf), maxBytes));
559  else
560  bytesRead = device->readLine(buf, sizeof(buf));
561  } else
562 #endif
563  {
564  if (maxBytes != -1)
565  bytesRead = device->read(buf, qMin<qint64>(sizeof(buf), maxBytes));
566  else
567  bytesRead = device->read(buf, sizeof(buf));
568  }
569 
570 #ifndef QT_NO_TEXTCODEC
571  // codec auto detection, explicitly defaults to locale encoding if the
572  // codec has been set to 0.
573  if (!codec || autoDetectUnicode) {
574  autoDetectUnicode = false;
575 
577  if (!codec) {
580  }
581  }
582 #if defined (QTEXTSTREAM_DEBUG)
583  qDebug("QTextStreamPrivate::fillReadBuffer(), using %s codec",
584  codec->name().constData());
585 #endif
586 #endif
587 
588 #if defined (QTEXTSTREAM_DEBUG)
589  qDebug("QTextStreamPrivate::fillReadBuffer(), device->read(\"%s\", %d) == %d",
590  qt_prettyDebug(buf, qMin(32,int(bytesRead)) , int(bytesRead)).constData(), sizeof(buf), int(bytesRead));
591 #endif
592 
593  if (bytesRead <= 0)
594  return false;
595 
596  int oldReadBufferSize = readBuffer.size();
597 #ifndef QT_NO_TEXTCODEC
598  // convert to unicode
599  readBuffer += codec->toUnicode(buf, bytesRead, &readConverterState);
600 #else
601  readBuffer += QString::fromLatin1(QByteArray(buf, bytesRead).constData());
602 #endif
603 
604  // reset the Text flag.
605  if (textModeEnabled)
606  device->setTextModeEnabled(true);
607 
608  // remove all '\r\n' in the string.
609  if (readBuffer.size() > oldReadBufferSize && textModeEnabled) {
610  QChar CR = QLatin1Char('\r');
611  QChar *writePtr = readBuffer.data() + oldReadBufferSize;
612  QChar *readPtr = readBuffer.data() + oldReadBufferSize;
613  QChar *endPtr = readBuffer.data() + readBuffer.size();
614 
615  int n = oldReadBufferSize;
616  if (readPtr < endPtr) {
617  // Cut-off to avoid unnecessary self-copying.
618  while (*readPtr++ != CR) {
619  ++n;
620  if (++writePtr == endPtr)
621  break;
622  }
623  }
624  while (readPtr < endPtr) {
625  QChar ch = *readPtr++;
626  if (ch != CR) {
627  *writePtr++ = ch;
628  } else {
629  if (n < readBufferOffset)
631  --bytesRead;
632  }
633  ++n;
634  }
635  readBuffer.resize(writePtr - readBuffer.data());
636  }
637 
638 #if defined (QTEXTSTREAM_DEBUG)
639  qDebug("QTextStreamPrivate::fillReadBuffer() read %d bytes from device. readBuffer = [%s]", int(bytesRead),
640  qt_prettyDebug(readBuffer.toLatin1(), readBuffer.size(), readBuffer.size()).data());
641 #endif
642  return true;
643 }
644 
648 {
649  readBuffer.clear();
650  readBufferOffset = 0;
652 }
653 
657 {
658  // no buffer next to the QString itself; this function should only
659  // be called internally, for devices.
660  if (string || !device)
661  return;
662 
663  // Stream went bye-bye already. Appending further data may succeed again,
664  // but would create a corrupted stream anyway.
665  if (status != QTextStream::Ok)
666  return;
667 
668  if (writeBuffer.isEmpty())
669  return;
670 
671 #if defined (Q_OS_WIN)
672  // handle text translation and bypass the Text flag in the device.
673  bool textModeEnabled = device->isTextModeEnabled();
674  if (textModeEnabled) {
675  device->setTextModeEnabled(false);
677  }
678 #endif
679 
680 #ifndef QT_NO_TEXTCODEC
681  if (!codec)
683 #if defined (QTEXTSTREAM_DEBUG)
684  qDebug("QTextStreamPrivate::flushWriteBuffer(), using %s codec (%s generating BOM)",
686 #endif
687 
688  // convert from unicode to raw data
690 #else
692 #endif
693  writeBuffer.clear();
694 
695  // write raw data to the device
696  qint64 bytesWritten = device->write(data);
697 #if defined (QTEXTSTREAM_DEBUG)
698  qDebug("QTextStreamPrivate::flushWriteBuffer(), device->write(\"%s\") == %d",
699  qt_prettyDebug(data.constData(), qMin(data.size(),32), data.size()).constData(), int(bytesWritten));
700 #endif
701  if (bytesWritten <= 0) {
703  return;
704  }
705 
706 #if defined (Q_OS_WIN)
707  // replace the text flag
708  if (textModeEnabled)
709  device->setTextModeEnabled(true);
710 #endif
711 
712  // flush the file
713 #ifndef QT_NO_QOBJECT
714  QFile *file = qobject_cast<QFile *>(device);
715  bool flushed = !file || file->flush();
716 #else
717  bool flushed = true;
718 #endif
719 
720 #if defined (QTEXTSTREAM_DEBUG)
721  qDebug("QTextStreamPrivate::flushWriteBuffer() wrote %d bytes",
722  int(bytesWritten));
723 #endif
724  if (!flushed || bytesWritten != qint64(data.size()))
726 }
727 
729 {
730  QString ret;
731  if (string) {
732  lastTokenSize = qMin(maxlen, string->size() - stringOffset);
733  ret = string->mid(stringOffset, lastTokenSize);
734  } else {
735  while (readBuffer.size() - readBufferOffset < maxlen && fillReadBuffer()) ;
738  }
740 
741 #if defined (QTEXTSTREAM_DEBUG)
742  qDebug("QTextStreamPrivate::read() maxlen = %d, token length = %d", maxlen, ret.length());
743 #endif
744  return ret;
745 }
746 
756 bool QTextStreamPrivate::scan(const QChar **ptr, int *length, int maxlen, TokenDelimiter delimiter)
757 {
758  int totalSize = 0;
759  int delimSize = 0;
760  bool consumeDelimiter = false;
761  bool foundToken = false;
762  int startOffset = device ? readBufferOffset : stringOffset;
763  QChar lastChar;
764 
765  bool canStillReadFromDevice = true;
766  do {
767  int endOffset;
768  const QChar *chPtr;
769  if (device) {
770  chPtr = readBuffer.constData();
771  endOffset = readBuffer.size();
772  } else {
773  chPtr = string->constData();
774  endOffset = string->size();
775  }
776  chPtr += startOffset;
777 
778  for (; !foundToken && startOffset < endOffset && (!maxlen || totalSize < maxlen); ++startOffset) {
779  const QChar ch = *chPtr++;
780  ++totalSize;
781 
782  switch (delimiter) {
783  case Space:
784  if (ch.isSpace()) {
785  foundToken = true;
786  delimSize = 1;
787  }
788  break;
789  case NotSpace:
790  if (!ch.isSpace()) {
791  foundToken = true;
792  delimSize = 1;
793  }
794  break;
795  case EndOfLine:
796  if (ch == QLatin1Char('\n')) {
797  foundToken = true;
798  delimSize = (lastChar == QLatin1Char('\r')) ? 2 : 1;
799  consumeDelimiter = true;
800  }
801  lastChar = ch;
802  break;
803  }
804  }
805  } while (!foundToken
806  && (!maxlen || totalSize < maxlen)
807  && (device && (canStillReadFromDevice = fillReadBuffer())));
808 
809  // if the token was not found, but we reached the end of input,
810  // then we accept what we got. if we are not at the end of input,
811  // we return false.
812  if (!foundToken && (!maxlen || totalSize < maxlen)
813  && (totalSize == 0
814  || (string && stringOffset + totalSize < string->size())
815  || (device && !device->atEnd() && canStillReadFromDevice))) {
816 #if defined (QTEXTSTREAM_DEBUG)
817  qDebug("QTextStreamPrivate::scan() did not find the token.");
818 #endif
819  return false;
820  }
821 
822  // if we find a '\r' at the end of the data when reading lines,
823  // don't make it part of the line.
824  if (delimiter == EndOfLine && totalSize > 0 && !foundToken) {
825  if (((string && stringOffset + totalSize == string->size()) || (device && device->atEnd()))
826  && lastChar == QLatin1Char('\r')) {
827  consumeDelimiter = true;
828  ++delimSize;
829  }
830  }
831 
832  // set the read offset and length of the token
833  if (length)
834  *length = totalSize - delimSize;
835  if (ptr)
836  *ptr = readPtr();
837 
838  // update last token size. the callee will call consumeLastToken() when
839  // done.
840  lastTokenSize = totalSize;
841  if (!consumeDelimiter)
842  lastTokenSize -= delimSize;
843 
844 #if defined (QTEXTSTREAM_DEBUG)
845  qDebug("QTextStreamPrivate::scan(%p, %p, %d, %x) token length = %d, delimiter = %d",
846  ptr, length, maxlen, (int)delimiter, totalSize - delimSize, delimSize);
847 #endif
848  return true;
849 }
850 
853 inline const QChar *QTextStreamPrivate::readPtr() const
854 {
856  if (string)
857  return string->constData() + stringOffset;
859 }
860 
864 {
865  if (lastTokenSize)
867  lastTokenSize = 0;
868 }
869 
872 inline void QTextStreamPrivate::consume(int size)
873 {
874 #if defined (QTEXTSTREAM_DEBUG)
875  qDebug("QTextStreamPrivate::consume(%d)", size);
876 #endif
877  if (string) {
878  stringOffset += size;
879  if (stringOffset > string->size())
880  stringOffset = string->size();
881  } else {
882  readBufferOffset += size;
883  if (readBufferOffset >= readBuffer.size()) {
884  readBufferOffset = 0;
885  readBuffer.clear();
890  readBufferOffset = 0;
891  }
892  }
893 }
894 
898 {
899 #ifndef QT_NO_TEXTCODEC
900  if (readConverterState.d) {
901  // converter cannot be copied, so don't save anything
902  // don't update readBufferStartDevicePos either
903  return;
904  }
905 
909 #endif
910 
911  readBufferStartDevicePos = newPos;
913 }
914 
918 {
919 #ifndef QT_NO_TEXTCODEC
921  // we have a saved state
922  // that means the converter can be copied
924  } else {
925  // the only state we could save was the initial
926  // so reset to that
928  }
929 #endif
930 }
931 
935 {
936  if (string) {
937  // ### What about seek()??
938  string->append(data);
939  } else {
940  writeBuffer += data;
943  }
944 }
945 
949 {
950  if ((string && stringOffset == string->size())
951  || (device && readBuffer.isEmpty() && !fillReadBuffer())) {
952  if (ch)
953  *ch = 0;
954  return false;
955  }
956  if (ch)
957  *ch = *readPtr();
958  consume(1);
959  return true;
960 }
961 
964 inline void QTextStreamPrivate::ungetChar(const QChar &ch)
965 {
966  if (string) {
967  if (stringOffset == 0)
968  string->prepend(ch);
969  else
970  (*string)[--stringOffset] = ch;
971  return;
972  }
973 
974  if (readBufferOffset == 0) {
975  readBuffer.prepend(ch);
976  return;
977  }
978 
980 }
981 
984 inline void QTextStreamPrivate::putString(const QString &s, bool number)
985 {
986  QString tmp = s;
987 
988  // handle padding
989  int padSize = fieldWidth - s.size();
990  if (padSize > 0) {
991  QString pad(padSize, padChar);
993  tmp.append(QString(padSize, padChar));
996  tmp.prepend(QString(padSize, padChar));
998  const QChar sign = s.size() > 0 ? s.at(0) : QChar();
999  if (sign == locale.negativeSign() || sign == locale.positiveSign()) {
1000  QChar *data = tmp.data();
1001  data[padSize] = tmp.at(0);
1002  data[0] = sign;
1003  }
1004  }
1005  } else if (fieldAlignment == QTextStream::AlignCenter) {
1006  tmp.prepend(QString(padSize/2, padChar));
1007  tmp.append(QString(padSize - padSize/2, padChar));
1008  }
1009  }
1010 
1011 #if defined (QTEXTSTREAM_DEBUG)
1012  QByteArray a = s.toUtf8();
1013  QByteArray b = tmp.toUtf8();
1014  qDebug("QTextStreamPrivate::putString(\"%s\") calls write(\"%s\")",
1015  qt_prettyDebug(a.constData(), a.size(), qMax(16, a.size())).constData(),
1016  qt_prettyDebug(b.constData(), b.size(), qMax(16, b.size())).constData());
1017 #endif
1018  write(tmp);
1019 }
1020 
1028  : d_ptr(new QTextStreamPrivate(this))
1029 {
1030 #if defined (QTEXTSTREAM_DEBUG)
1031  qDebug("QTextStream::QTextStream()");
1032 #endif
1033  Q_D(QTextStream);
1034  d->status = Ok;
1035 }
1036 
1041  : d_ptr(new QTextStreamPrivate(this))
1042 {
1043 #if defined (QTEXTSTREAM_DEBUG)
1044  qDebug("QTextStream::QTextStream(QIODevice *device == *%p)",
1045  device);
1046 #endif
1047  Q_D(QTextStream);
1048  d->device = device;
1049 #ifndef QT_NO_QOBJECT
1050  d->deviceClosedNotifier.setupDevice(this, d->device);
1051 #endif
1052  d->status = Ok;
1053 }
1054 
1059 QTextStream::QTextStream(QString *string, QIODevice::OpenMode openMode)
1060  : d_ptr(new QTextStreamPrivate(this))
1061 {
1062 #if defined (QTEXTSTREAM_DEBUG)
1063  qDebug("QTextStream::QTextStream(QString *string == *%p, openMode = %d)",
1064  string, int(openMode));
1065 #endif
1066  Q_D(QTextStream);
1067  d->string = string;
1068  d->stringOpenMode = openMode;
1069  d->status = Ok;
1070 }
1071 
1077 QTextStream::QTextStream(QByteArray *array, QIODevice::OpenMode openMode)
1078  : d_ptr(new QTextStreamPrivate(this))
1079 {
1080 #if defined (QTEXTSTREAM_DEBUG)
1081  qDebug("QTextStream::QTextStream(QByteArray *array == *%p, openMode = %d)",
1082  array, int(openMode));
1083 #endif
1084  Q_D(QTextStream);
1085  d->device = new QBuffer(array);
1086  d->device->open(openMode);
1087  d->deleteDevice = true;
1088 #ifndef QT_NO_QOBJECT
1089  d->deviceClosedNotifier.setupDevice(this, d->device);
1090 #endif
1091  d->status = Ok;
1092 }
1093 
1104 QTextStream::QTextStream(const QByteArray &array, QIODevice::OpenMode openMode)
1105  : d_ptr(new QTextStreamPrivate(this))
1106 {
1107 #if defined (QTEXTSTREAM_DEBUG)
1108  qDebug("QTextStream::QTextStream(const QByteArray &array == *(%p), openMode = %d)",
1109  &array, int(openMode));
1110 #endif
1111  QBuffer *buffer = new QBuffer;
1112  buffer->setData(array);
1113  buffer->open(openMode);
1114 
1115  Q_D(QTextStream);
1116  d->device = buffer;
1117  d->deleteDevice = true;
1118 #ifndef QT_NO_QOBJECT
1119  d->deviceClosedNotifier.setupDevice(this, d->device);
1120 #endif
1121  d->status = Ok;
1122 }
1123 
1135 QTextStream::QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode)
1136  : d_ptr(new QTextStreamPrivate(this))
1137 {
1138 #if defined (QTEXTSTREAM_DEBUG)
1139  qDebug("QTextStream::QTextStream(FILE *fileHandle = %p, openMode = %d)",
1140  fileHandle, int(openMode));
1141 #endif
1142  QFile *file = new QFile;
1143  file->open(fileHandle, openMode);
1144 
1145  Q_D(QTextStream);
1146  d->device = file;
1147  d->deleteDevice = true;
1148 #ifndef QT_NO_QOBJECT
1149  d->deviceClosedNotifier.setupDevice(this, d->device);
1150 #endif
1151  d->status = Ok;
1152 }
1153 
1161 {
1162  Q_D(QTextStream);
1163 #if defined (QTEXTSTREAM_DEBUG)
1164  qDebug("QTextStream::~QTextStream()");
1165 #endif
1166  if (!d->writeBuffer.isEmpty())
1167  d->flushWriteBuffer();
1168 }
1169 
1176 {
1177  Q_D(QTextStream);
1178 
1179  d->realNumberPrecision = 6;
1180  d->integerBase = 0;
1181  d->fieldWidth = 0;
1182  d->padChar = QLatin1Char(' ');
1183  d->fieldAlignment = QTextStream::AlignRight;
1184  d->realNumberNotation = QTextStream::SmartNotation;
1185  d->numberFlags = 0;
1186 }
1187 
1194 {
1195  Q_D(QTextStream);
1196  d->flushWriteBuffer();
1197 }
1198 
1204 {
1205  Q_D(QTextStream);
1206  d->lastTokenSize = 0;
1207 
1208  if (d->device) {
1209  // Empty the write buffer
1210  d->flushWriteBuffer();
1211  if (!d->device->seek(pos))
1212  return false;
1213  d->resetReadBuffer();
1214 
1215 #ifndef QT_NO_TEXTCODEC
1216  // Reset the codec converter states.
1217  resetCodecConverterStateHelper(&d->readConverterState);
1218  resetCodecConverterStateHelper(&d->writeConverterState);
1219  delete d->readConverterSavedState;
1220  d->readConverterSavedState = 0;
1221  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
1222 #endif
1223  return true;
1224  }
1225 
1226  // string
1227  if (d->string && pos <= d->string->size()) {
1228  d->stringOffset = int(pos);
1229  return true;
1230  }
1231  return false;
1232 }
1233 
1252 {
1253  Q_D(const QTextStream);
1254  if (d->device) {
1255  // Cutoff
1256  if (d->readBuffer.isEmpty())
1257  return d->device->pos();
1258  if (d->device->isSequential())
1259  return 0;
1260 
1261  // Seek the device
1262  if (!d->device->seek(d->readBufferStartDevicePos))
1263  return qint64(-1);
1264 
1265  // Reset the read buffer
1266  QTextStreamPrivate *thatd = const_cast<QTextStreamPrivate *>(d);
1267  thatd->readBuffer.clear();
1268 
1269 #ifndef QT_NO_TEXTCODEC
1271  if (d->readBufferStartDevicePos == 0)
1272  thatd->autoDetectUnicode = true;
1273 #endif
1274 
1275  // Rewind the device to get to the current position Ensure that
1276  // readBufferOffset is unaffected by fillReadBuffer()
1277  int oldReadBufferOffset = d->readBufferOffset + d->readConverterSavedStateOffset;
1278  while (d->readBuffer.size() < oldReadBufferOffset) {
1279  if (!thatd->fillReadBuffer(1))
1280  return qint64(-1);
1281  }
1282  thatd->readBufferOffset = oldReadBufferOffset;
1283  thatd->readConverterSavedStateOffset = 0;
1284 
1285  // Return the device position.
1286  return d->device->pos();
1287  }
1288 
1289  if (d->string)
1290  return d->stringOffset;
1291 
1292  qWarning("QTextStream::pos: no device");
1293  return qint64(-1);
1294 }
1295 
1308 {
1309  Q_D(QTextStream);
1311  d->scan(0, 0, 0, QTextStreamPrivate::NotSpace);
1312  d->consumeLastToken();
1313 }
1314 
1326 {
1327  Q_D(QTextStream);
1328  flush();
1329  if (d->deleteDevice) {
1330 #ifndef QT_NO_QOBJECT
1331  d->deviceClosedNotifier.disconnect();
1332 #endif
1333  delete d->device;
1334  d->deleteDevice = false;
1335  }
1336 
1337  d->reset();
1338  d->status = Ok;
1339  d->device = device;
1340  d->resetReadBuffer();
1341 #ifndef QT_NO_QOBJECT
1342  d->deviceClosedNotifier.setupDevice(this, d->device);
1343 #endif
1344 }
1345 
1353 {
1354  Q_D(const QTextStream);
1355  return d->device;
1356 }
1357 
1365 void QTextStream::setString(QString *string, QIODevice::OpenMode openMode)
1366 {
1367  Q_D(QTextStream);
1368  flush();
1369  if (d->deleteDevice) {
1370 #ifndef QT_NO_QOBJECT
1371  d->deviceClosedNotifier.disconnect();
1372  d->device->blockSignals(true);
1373 #endif
1374  delete d->device;
1375  d->deleteDevice = false;
1376  }
1377 
1378  d->reset();
1379  d->status = Ok;
1380  d->string = string;
1381  d->stringOpenMode = openMode;
1382 }
1383 
1391 {
1392  Q_D(const QTextStream);
1393  return d->string;
1394 }
1395 
1405 {
1406  Q_D(QTextStream);
1407  d->fieldAlignment = mode;
1408 }
1409 
1416 {
1417  Q_D(const QTextStream);
1418  return d->fieldAlignment;
1419 }
1420 
1437 {
1438  Q_D(QTextStream);
1439  d->padChar = ch;
1440 }
1441 
1448 {
1449  Q_D(const QTextStream);
1450  return d->padChar;
1451 }
1452 
1466 {
1467  Q_D(QTextStream);
1468  d->fieldWidth = width;
1469 }
1470 
1477 {
1478  Q_D(const QTextStream);
1479  return d->fieldWidth;
1480 }
1481 
1490 void QTextStream::setNumberFlags(NumberFlags flags)
1491 {
1492  Q_D(QTextStream);
1493  d->numberFlags = flags;
1494 }
1495 
1501 QTextStream::NumberFlags QTextStream::numberFlags() const
1502 {
1503  Q_D(const QTextStream);
1504  return d->numberFlags;
1505 }
1506 
1518 {
1519  Q_D(QTextStream);
1520  d->integerBase = base;
1521 }
1522 
1530 {
1531  Q_D(const QTextStream);
1532  return d->integerBase;
1533 }
1534 
1544 {
1545  Q_D(QTextStream);
1546  d->realNumberNotation = notation;
1547 }
1548 
1555 {
1556  Q_D(const QTextStream);
1557  return d->realNumberNotation;
1558 }
1559 
1570 {
1571  Q_D(QTextStream);
1572  if (precision < 0) {
1573  qWarning("QTextStream::setRealNumberPrecision: Invalid precision (%d)", precision);
1574  d->realNumberPrecision = 6;
1575  return;
1576  }
1577  d->realNumberPrecision = precision;
1578 }
1579 
1587 {
1588  Q_D(const QTextStream);
1589  return d->realNumberPrecision;
1590 }
1591 
1599 {
1600  Q_D(const QTextStream);
1601  return d->status;
1602 }
1603 
1615 {
1616  Q_D(QTextStream);
1617  d->status = Ok;
1618 }
1619 
1634 {
1635  Q_D(QTextStream);
1636  if (d->status == Ok)
1637  d->status = status;
1638 }
1639 
1647 {
1648  Q_D(const QTextStream);
1649  CHECK_VALID_STREAM(true);
1650 
1651  if (d->string)
1652  return d->string->size() == d->stringOffset;
1653  return d->readBuffer.isEmpty() && d->device->atEnd();
1654 }
1655 
1667 {
1668  Q_D(QTextStream);
1670 
1671  return d->read(INT_MAX);
1672 }
1673 
1693 {
1694  Q_D(QTextStream);
1696 
1697  const QChar *readPtr;
1698  int length;
1699  if (!d->scan(&readPtr, &length, int(maxlen), QTextStreamPrivate::EndOfLine))
1700  return QString();
1701 
1702  QString tmp = QString(readPtr, length);
1703  d->consumeLastToken();
1704  return tmp;
1705 }
1706 
1719 {
1720  Q_D(QTextStream);
1722 
1723  if (maxlen <= 0)
1724  return QString::fromLatin1(""); // empty, not null
1725 
1726  return d->read(int(maxlen));
1727 }
1728 
1732 {
1733  scan(0, 0, 0, NotSpace);
1734  consumeLastToken();
1735 
1736  // detect int encoding
1737  int base = integerBase;
1738  if (base == 0) {
1739  QChar ch;
1740  if (!getChar(&ch))
1741  return npsInvalidPrefix;
1742  if (ch == QLatin1Char('0')) {
1743  QChar ch2;
1744  if (!getChar(&ch2)) {
1745  // Result is the number 0
1746  *ret = 0;
1747  return npsOk;
1748  }
1749  ch2 = ch2.toLower();
1750 
1751  if (ch2 == QLatin1Char('x')) {
1752  base = 16;
1753  } else if (ch2 == QLatin1Char('b')) {
1754  base = 2;
1755  } else if (ch2.isDigit() && ch2.digitValue() >= 0 && ch2.digitValue() <= 7) {
1756  base = 8;
1757  } else {
1758  base = 10;
1759  }
1760  ungetChar(ch2);
1761  } else if (ch == locale.negativeSign() || ch == locale.positiveSign() || ch.isDigit()) {
1762  base = 10;
1763  } else {
1764  ungetChar(ch);
1765  return npsInvalidPrefix;
1766  }
1767  ungetChar(ch);
1768  // State of the stream is now the same as on entry
1769  // (cursor is at prefix),
1770  // and local variable 'base' has been set appropriately.
1771  }
1772 
1773  qulonglong val=0;
1774  switch (base) {
1775  case 2: {
1776  QChar pf1, pf2, dig;
1777  // Parse prefix '0b'
1778  if (!getChar(&pf1) || pf1 != QLatin1Char('0'))
1779  return npsInvalidPrefix;
1780  if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('b'))
1781  return npsInvalidPrefix;
1782  // Parse digits
1783  int ndigits = 0;
1784  while (getChar(&dig)) {
1785  int n = dig.toLower().unicode();
1786  if (n == '0' || n == '1') {
1787  val <<= 1;
1788  val += n - '0';
1789  } else {
1790  ungetChar(dig);
1791  break;
1792  }
1793  ndigits++;
1794  }
1795  if (ndigits == 0) {
1796  // Unwind the prefix and abort
1797  ungetChar(pf2);
1798  ungetChar(pf1);
1799  return npsMissingDigit;
1800  }
1801  break;
1802  }
1803  case 8: {
1804  QChar pf, dig;
1805  // Parse prefix '0'
1806  if (!getChar(&pf) || pf != QLatin1Char('0'))
1807  return npsInvalidPrefix;
1808  // Parse digits
1809  int ndigits = 0;
1810  while (getChar(&dig)) {
1811  int n = dig.toLower().unicode();
1812  if (n >= '0' && n <= '7') {
1813  val *= 8;
1814  val += n - '0';
1815  } else {
1816  ungetChar(dig);
1817  break;
1818  }
1819  ndigits++;
1820  }
1821  if (ndigits == 0) {
1822  // Unwind the prefix and abort
1823  ungetChar(pf);
1824  return npsMissingDigit;
1825  }
1826  break;
1827  }
1828  case 10: {
1829  // Parse sign (or first digit)
1830  QChar sign;
1831  int ndigits = 0;
1832  if (!getChar(&sign))
1833  return npsMissingDigit;
1834  if (sign != locale.negativeSign() && sign != locale.positiveSign()) {
1835  if (!sign.isDigit()) {
1836  ungetChar(sign);
1837  return npsMissingDigit;
1838  }
1839  val += sign.digitValue();
1840  ndigits++;
1841  }
1842  // Parse digits
1843  QChar ch;
1844  while (getChar(&ch)) {
1845  if (ch.isDigit()) {
1846  val *= 10;
1847  val += ch.digitValue();
1848  } else if (locale != QLocale::c() && ch == locale.groupSeparator()) {
1849  continue;
1850  } else {
1851  ungetChar(ch);
1852  break;
1853  }
1854  ndigits++;
1855  }
1856  if (ndigits == 0)
1857  return npsMissingDigit;
1858  if (sign == locale.negativeSign()) {
1859  qlonglong ival = qlonglong(val);
1860  if (ival > 0)
1861  ival = -ival;
1862  val = qulonglong(ival);
1863  }
1864  break;
1865  }
1866  case 16: {
1867  QChar pf1, pf2, dig;
1868  // Parse prefix ' 0x'
1869  if (!getChar(&pf1) || pf1 != QLatin1Char('0'))
1870  return npsInvalidPrefix;
1871  if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('x'))
1872  return npsInvalidPrefix;
1873  // Parse digits
1874  int ndigits = 0;
1875  while (getChar(&dig)) {
1876  int n = dig.toLower().unicode();
1877  if (n >= '0' && n <= '9') {
1878  val <<= 4;
1879  val += n - '0';
1880  } else if (n >= 'a' && n <= 'f') {
1881  val <<= 4;
1882  val += 10 + (n - 'a');
1883  } else {
1884  ungetChar(dig);
1885  break;
1886  }
1887  ndigits++;
1888  }
1889  if (ndigits == 0) {
1890  return npsMissingDigit;
1891  }
1892  break;
1893  }
1894  default:
1895  // Unsupported integerBase
1896  return npsInvalidPrefix;
1897  }
1898 
1899  if (ret)
1900  *ret = val;
1901  return npsOk;
1902 }
1903 
1908 {
1909  // We use a table-driven FSM to parse floating point numbers
1910  // strtod() cannot be used directly since we may be reading from a
1911  // QIODevice.
1912  enum ParserState {
1913  Init = 0,
1914  Sign = 1,
1915  Mantissa = 2,
1916  Dot = 3,
1917  Abscissa = 4,
1918  ExpMark = 5,
1919  ExpSign = 6,
1920  Exponent = 7,
1921  Nan1 = 8,
1922  Nan2 = 9,
1923  Inf1 = 10,
1924  Inf2 = 11,
1925  NanInf = 12,
1926  Done = 13
1927  };
1928  enum InputToken {
1929  None = 0,
1930  InputSign = 1,
1931  InputDigit = 2,
1932  InputDot = 3,
1933  InputExp = 4,
1934  InputI = 5,
1935  InputN = 6,
1936  InputF = 7,
1937  InputA = 8,
1938  InputT = 9
1939  };
1940 
1941  static const uchar table[13][10] = {
1942  // None InputSign InputDigit InputDot InputExp InputI InputN InputF InputA InputT
1943  { 0, Sign, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 }, // 0 Init
1944  { 0, 0, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 }, // 1 Sign
1945  { Done, Done, Mantissa, Dot, ExpMark, 0, 0, 0, 0, 0 }, // 2 Mantissa
1946  { 0, 0, Abscissa, 0, 0, 0, 0, 0, 0, 0 }, // 3 Dot
1947  { Done, Done, Abscissa, Done, ExpMark, 0, 0, 0, 0, 0 }, // 4 Abscissa
1948  { 0, ExpSign, Exponent, 0, 0, 0, 0, 0, 0, 0 }, // 5 ExpMark
1949  { 0, 0, Exponent, 0, 0, 0, 0, 0, 0, 0 }, // 6 ExpSign
1950  { Done, Done, Exponent, Done, Done, 0, 0, 0, 0, 0 }, // 7 Exponent
1951  { 0, 0, 0, 0, 0, 0, 0, 0, Nan2, 0 }, // 8 Nan1
1952  { 0, 0, 0, 0, 0, 0, NanInf, 0, 0, 0 }, // 9 Nan2
1953  { 0, 0, 0, 0, 0, 0, Inf2, 0, 0, 0 }, // 10 Inf1
1954  { 0, 0, 0, 0, 0, 0, 0, NanInf, 0, 0 }, // 11 Inf2
1955  { Done, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 11 NanInf
1956  };
1957 
1958  ParserState state = Init;
1959  InputToken input = None;
1960 
1961  scan(0, 0, 0, NotSpace);
1962  consumeLastToken();
1963 
1964  const int BufferSize = 128;
1965  char buf[BufferSize];
1966  int i = 0;
1967 
1968  QChar c;
1969  while (getChar(&c)) {
1970  switch (c.unicode()) {
1971  case '0': case '1': case '2': case '3': case '4':
1972  case '5': case '6': case '7': case '8': case '9':
1973  input = InputDigit;
1974  break;
1975  case 'i': case 'I':
1976  input = InputI;
1977  break;
1978  case 'n': case 'N':
1979  input = InputN;
1980  break;
1981  case 'f': case 'F':
1982  input = InputF;
1983  break;
1984  case 'a': case 'A':
1985  input = InputA;
1986  break;
1987  case 't': case 'T':
1988  input = InputT;
1989  break;
1990  default: {
1991  QChar lc = c.toLower();
1992  if (lc == locale.decimalPoint().toLower())
1993  input = InputDot;
1994  else if (lc == locale.exponential().toLower())
1995  input = InputExp;
1996  else if (lc == locale.negativeSign().toLower()
1997  || lc == locale.positiveSign().toLower())
1998  input = InputSign;
1999  else if (locale != QLocale::c() // backward-compatibility
2000  && lc == locale.groupSeparator().toLower())
2001  input = InputDigit; // well, it isn't a digit, but no one cares.
2002  else
2003  input = None;
2004  }
2005  break;
2006  }
2007 
2008  state = ParserState(table[state][input]);
2009 
2010  if (state == Init || state == Done || i > (BufferSize - 5)) {
2011  ungetChar(c);
2012  if (i > (BufferSize - 5)) { // ignore rest of digits
2013  while (getChar(&c)) {
2014  if (!c.isDigit()) {
2015  ungetChar(c);
2016  break;
2017  }
2018  }
2019  }
2020  break;
2021  }
2022 
2023  buf[i++] = c.toLatin1();
2024  }
2025 
2026  if (i == 0)
2027  return false;
2028  if (!f)
2029  return true;
2030  buf[i] = '\0';
2031 
2032  // backward-compatibility. Old implementation supported +nan/-nan
2033  // for some reason. QLocale only checks for lower-case
2034  // nan/+inf/-inf, so here we also check for uppercase and mixed
2035  // case versions.
2036  if (!qstricmp(buf, "nan") || !qstricmp(buf, "+nan") || !qstricmp(buf, "-nan")) {
2037  *f = qSNaN();
2038  return true;
2039  } else if (!qstricmp(buf, "+inf") || !qstricmp(buf, "inf")) {
2040  *f = qInf();
2041  return true;
2042  } else if (!qstricmp(buf, "-inf")) {
2043  *f = -qInf();
2044  return true;
2045  }
2046  bool ok;
2047  *f = locale.toDouble(QString::fromLatin1(buf), &ok);
2048  return ok;
2049 }
2050 
2062 {
2063  Q_D(QTextStream);
2064  CHECK_VALID_STREAM(*this);
2065  d->scan(0, 0, 0, QTextStreamPrivate::NotSpace);
2066  if (!d->getChar(&c))
2068  return *this;
2069 }
2070 
2084 {
2085  QChar ch;
2086  *this >> ch;
2087  c = ch.toLatin1();
2088  return *this;
2089 }
2090 
2116 {
2118 }
2119 
2129 {
2130  IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned short);
2131 }
2132 
2142 {
2144 }
2145 
2155 {
2157 }
2158 
2168 {
2170 }
2171 
2181 {
2182  IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(unsigned long);
2183 }
2184 
2194 {
2196 }
2197 
2207 {
2209 }
2210 
2223 {
2225 }
2226 
2236 {
2238 }
2239 
2248 {
2249  Q_D(QTextStream);
2250  CHECK_VALID_STREAM(*this);
2251 
2252  str.clear();
2253  d->scan(0, 0, 0, QTextStreamPrivate::NotSpace);
2254  d->consumeLastToken();
2255 
2256  const QChar *ptr;
2257  int length;
2258  if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) {
2260  return *this;
2261  }
2262 
2263  str = QString(ptr, length);
2264  d->consumeLastToken();
2265  return *this;
2266 }
2267 
2279 {
2280  Q_D(QTextStream);
2281  CHECK_VALID_STREAM(*this);
2282 
2283  array.clear();
2284  d->scan(0, 0, 0, QTextStreamPrivate::NotSpace);
2285  d->consumeLastToken();
2286 
2287  const QChar *ptr;
2288  int length;
2289  if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) {
2291  return *this;
2292  }
2293 
2294  for (int i = 0; i < length; ++i)
2295  array += ptr[i].toLatin1();
2296 
2297  d->consumeLastToken();
2298  return *this;
2299 }
2300 
2318 {
2319  Q_D(QTextStream);
2320  *c = 0;
2321  CHECK_VALID_STREAM(*this);
2322  d->scan(0, 0, 0, QTextStreamPrivate::NotSpace);
2323  d->consumeLastToken();
2324 
2325  const QChar *ptr;
2326  int length;
2327  if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) {
2329  return *this;
2330  }
2331 
2332  for (int i = 0; i < length; ++i)
2333  *c++ = ptr[i].toLatin1();
2334  *c = '\0';
2335  d->consumeLastToken();
2336  return *this;
2337 }
2338 
2341 void QTextStreamPrivate::putNumber(qulonglong number, bool negative)
2342 {
2343  QString result;
2344 
2345  unsigned flags = 0;
2347  flags |= QLocalePrivate::ShowBase;
2353  flags |= QLocalePrivate::CapitalEorX;
2354 
2355  // add thousands group separators. For backward compatibility we
2356  // don't add a group separator for C locale.
2357  if (locale != QLocale::c())
2359 
2360  const QLocalePrivate *dd = locale.d();
2361  int base = integerBase ? integerBase : 10;
2362  if (negative && base == 10) {
2363  result = dd->longLongToString(-static_cast<qlonglong>(number), -1,
2364  base, -1, flags);
2365  } else if (negative) {
2366  // Workaround for backward compatibility for writing negative
2367  // numbers in octal and hex:
2368  // QTextStream(result) << showbase << hex << -1 << oct << -1
2369  // should output: -0x1 -0b1
2370  result = dd->unsLongLongToString(number, -1, base, -1, flags);
2371  result.prepend(locale.negativeSign());
2372  } else {
2373  result = dd->unsLongLongToString(number, -1, base, -1, flags);
2374  // workaround for backward compatibility - in octal form with
2375  // ShowBase flag set zero should be written as '00'
2376  if (number == 0 && base == 8 && numberFlags & QTextStream::ShowBase
2377  && result == QLatin1String("0")) {
2378  result.prepend(QLatin1Char('0'));
2379  }
2380  }
2381  putString(result, true);
2382 }
2383 
2389 {
2390  return *this << bool(b);
2391 }
2392 
2400 {
2401  Q_D(QTextStream);
2402  CHECK_VALID_STREAM(*this);
2403  d->putString(QString(c));
2404  return *this;
2405 }
2406 
2416 {
2417  Q_D(QTextStream);
2418  CHECK_VALID_STREAM(*this);
2419  d->putString(QString(QChar::fromAscii(c)));
2420  return *this;
2421 }
2422 
2432 {
2433  Q_D(QTextStream);
2434  CHECK_VALID_STREAM(*this);
2435  d->putNumber((qulonglong)qAbs(qlonglong(i)), i < 0);
2436  return *this;
2437 }
2438 
2448 {
2449  Q_D(QTextStream);
2450  CHECK_VALID_STREAM(*this);
2451  d->putNumber((qulonglong)i, false);
2452  return *this;
2453 }
2454 
2464 {
2465  Q_D(QTextStream);
2466  CHECK_VALID_STREAM(*this);
2467  d->putNumber((qulonglong)qAbs(qlonglong(i)), i < 0);
2468  return *this;
2469 }
2470 
2480 {
2481  Q_D(QTextStream);
2482  CHECK_VALID_STREAM(*this);
2483  d->putNumber((qulonglong)i, false);
2484  return *this;
2485 }
2486 
2496 {
2497  Q_D(QTextStream);
2498  CHECK_VALID_STREAM(*this);
2499  d->putNumber((qulonglong)qAbs(qlonglong(i)), i < 0);
2500  return *this;
2501 }
2502 
2512 {
2513  Q_D(QTextStream);
2514  CHECK_VALID_STREAM(*this);
2515  d->putNumber((qulonglong)i, false);
2516  return *this;
2517 }
2518 
2528 {
2529  Q_D(QTextStream);
2530  CHECK_VALID_STREAM(*this);
2531  d->putNumber((qulonglong)qAbs(i), i < 0);
2532  return *this;
2533 }
2534 
2544 {
2545  Q_D(QTextStream);
2546  CHECK_VALID_STREAM(*this);
2547  d->putNumber(i, false);
2548  return *this;
2549 }
2550 
2563 {
2564  return *this << double(f);
2565 }
2566 
2576 {
2577  Q_D(QTextStream);
2578  CHECK_VALID_STREAM(*this);
2579 
2581  switch (realNumberNotation()) {
2582  case FixedNotation:
2584  break;
2585  case ScientificNotation:
2587  break;
2588  case SmartNotation:
2590  break;
2591  }
2592 
2593  uint flags = 0;
2594  if (numberFlags() & ShowBase)
2595  flags |= QLocalePrivate::ShowBase;
2596  if (numberFlags() & ForceSign)
2598  if (numberFlags() & UppercaseBase)
2600  if (numberFlags() & UppercaseDigits)
2601  flags |= QLocalePrivate::CapitalEorX;
2602  if (numberFlags() & ForcePoint)
2603  flags |= QLocalePrivate::Alternate;
2604 
2605  const QLocalePrivate *dd = d->locale.d();
2606  QString num = dd->doubleToString(f, d->realNumberPrecision, form, -1, flags);
2607  d->putString(num, true);
2608  return *this;
2609 }
2610 
2620 {
2621  Q_D(QTextStream);
2622  CHECK_VALID_STREAM(*this);
2623  d->putString(string);
2624  return *this;
2625 }
2626 
2637 {
2638  Q_D(QTextStream);
2639  CHECK_VALID_STREAM(*this);
2640  d->putString(QString::fromAscii(array.constData(), array.length()));
2641  return *this;
2642 }
2643 
2661 {
2662  Q_D(QTextStream);
2663  CHECK_VALID_STREAM(*this);
2664  d->putString(QLatin1String(string));
2665  return *this;
2666 }
2667 
2678 {
2679  Q_D(QTextStream);
2680  CHECK_VALID_STREAM(*this);
2681  int oldBase = d->integerBase;
2682  NumberFlags oldFlags = d->numberFlags;
2683  d->integerBase = 16;
2684  d->numberFlags |= ShowBase;
2685  d->putNumber(reinterpret_cast<quintptr>(ptr), false);
2686  d->integerBase = oldBase;
2687  d->numberFlags = oldFlags;
2688  return *this;
2689 }
2690 
2703 {
2704  stream.setIntegerBase(2);
2705  return stream;
2706 }
2707 
2720 {
2721  stream.setIntegerBase(8);
2722  return stream;
2723 }
2724 
2737 {
2738  stream.setIntegerBase(10);
2739  return stream;
2740 }
2741 
2755 {
2756  stream.setIntegerBase(16);
2757  return stream;
2758 }
2759 
2772 {
2774  return stream;
2775 }
2776 
2789 {
2791  return stream;
2792 }
2793 
2806 {
2808  return stream;
2809 }
2810 
2823 {
2825  return stream;
2826 }
2827 
2840 {
2842  return stream;
2843 }
2844 
2857 {
2859  return stream;
2860 }
2861 
2874 {
2876  return stream;
2877 }
2878 
2891 {
2893  return stream;
2894 }
2895 
2908 {
2910  return stream;
2911 }
2912 
2925 {
2927  return stream;
2928 }
2929 
2942 {
2944  return stream;
2945 }
2946 
2959 {
2961  return stream;
2962 }
2963 
2976 {
2978  return stream;
2979 }
2980 
2993 {
2995  return stream;
2996 }
2997 
3010 {
3012  return stream;
3013 }
3014 
3033 {
3034  return stream << QLatin1Char('\n') << flush;
3035 }
3036 
3048 {
3049  stream.flush();
3050  return stream;
3051 }
3052 
3064 {
3065  stream.reset();
3066  return stream;
3067 }
3068 
3080 {
3081  stream.skipWhiteSpace();
3082  return stream;
3083 }
3084 
3115 #ifndef QT_NO_TEXTCODEC
3116 
3128 {
3129  stream.setGenerateByteOrderMark(true);
3130  return stream;
3131 }
3132 
3149 {
3150  Q_D(QTextStream);
3151  qint64 seekPos = -1;
3152  if (!d->readBuffer.isEmpty()) {
3153  if (!d->device->isSequential()) {
3154  seekPos = pos();
3155  }
3156  }
3157  d->codec = codec;
3158  if (seekPos >=0 && !d->readBuffer.isEmpty())
3159  seek(seekPos);
3160 }
3161 
3174 void QTextStream::setCodec(const char *codecName)
3175 {
3177  if (codec)
3178  setCodec(codec);
3179 }
3180 
3187 {
3188  Q_D(const QTextStream);
3189  return d->codec;
3190 }
3191 
3204 {
3205  Q_D(QTextStream);
3206  d->autoDetectUnicode = enabled;
3207 }
3208 
3216 {
3217  Q_D(const QTextStream);
3218  return d->autoDetectUnicode;
3219 }
3220 
3230 {
3231  Q_D(QTextStream);
3232  if (d->writeBuffer.isEmpty()) {
3233  if (generate)
3234  d->writeConverterState.flags &= ~QTextCodec::IgnoreHeader;
3235  else
3236  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
3237  }
3238 }
3239 
3248 {
3249  Q_D(const QTextStream);
3250  return (d->writeConverterState.flags & QTextCodec::IgnoreHeader) == 0;
3251 }
3252 
3253 #endif
3254 
3270 {
3271  Q_D(QTextStream);
3272  d->locale = locale;
3273 }
3274 
3286 {
3287  Q_D(const QTextStream);
3288  return d->locale;
3289 }
3290 
3291 #ifdef QT3_SUPPORT
3292 
3352 int QTextStream::flagsInternal() const
3353 {
3354  Q_D(const QTextStream);
3355 
3356  int f = 0;
3357  switch (d->fieldAlignment) {
3358  case AlignLeft: f |= left; break;
3359  case AlignRight: f |= right; break;
3360  case AlignCenter: f |= internal; break;
3361  default:
3362  break;
3363  }
3364  switch (d->integerBase) {
3365  case 2: f |= bin; break;
3366  case 8: f |= oct; break;
3367  case 10: f |= dec; break;
3368  case 16: f |= hex; break;
3369  default:
3370  break;
3371  }
3372  switch (d->realNumberNotation) {
3373  case FixedNotation: f |= fixed; break;
3374  case ScientificNotation: f |= scientific; break;
3375  default:
3376  break;
3377  }
3378  if (d->numberFlags & ShowBase)
3379  f |= showbase;
3380  if (d->numberFlags & ForcePoint)
3381  f |= showpoint;
3382  if (d->numberFlags & ForceSign)
3383  f |= showpos;
3384  if (d->numberFlags & UppercaseBase)
3385  f |= uppercase;
3386  return f;
3387 }
3388 
3391 int QTextStream::flagsInternal(int newFlags)
3392 {
3393  int oldFlags = flagsInternal();
3394 
3395  if (newFlags & left)
3397  else if (newFlags & right)
3399  else if (newFlags & internal)
3401 
3402  if (newFlags & bin)
3403  setIntegerBase(2);
3404  else if (newFlags & oct)
3405  setIntegerBase(8);
3406  else if (newFlags & dec)
3407  setIntegerBase(10);
3408  else if (newFlags & hex)
3409  setIntegerBase(16);
3410 
3411  if (newFlags & showbase)
3413  if (newFlags & showpos)
3415  if (newFlags & showpoint)
3417  if (newFlags & uppercase)
3419 
3420  if (newFlags & fixed)
3422  else if (newFlags & scientific)
3424 
3425  return oldFlags;
3426 }
3427 
3428 #ifndef QT_NO_TEXTCODEC
3429 
3432 void QTextStream::setEncoding(Encoding encoding)
3433 {
3434  Q_D(QTextStream);
3435  resetCodecConverterStateHelper(&d->readConverterState);
3436  resetCodecConverterStateHelper(&d->writeConverterState);
3437 
3438  switch (encoding) {
3439  case Locale:
3440  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
3442  d->autoDetectUnicode = true;
3443  break;
3444  case Latin1:
3445  d->readConverterState.flags |= QTextCodec::IgnoreHeader;
3446  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
3447  setCodec(QTextCodec::codecForName("ISO-8859-1"));
3448  d->autoDetectUnicode = false;
3449  break;
3450  case Unicode:
3452  d->autoDetectUnicode = false;
3453  break;
3454  case RawUnicode:
3455  d->readConverterState.flags |= QTextCodec::IgnoreHeader;
3456  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
3458  d->autoDetectUnicode = false;
3459  break;
3460  case UnicodeNetworkOrder:
3461  d->readConverterState.flags |= QTextCodec::IgnoreHeader;
3462  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
3463  setCodec(QTextCodec::codecForName("UTF-16BE"));
3464  d->autoDetectUnicode = false;
3465  break;
3466  case UnicodeReverse:
3467  d->readConverterState.flags |= QTextCodec::IgnoreHeader;
3468  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
3469  setCodec(QTextCodec::codecForName("UTF-16LE"));
3470  d->autoDetectUnicode = false;
3471  break;
3472  case UnicodeUTF8:
3473  d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
3475  d->autoDetectUnicode = true;
3476  break;
3477  }
3478 }
3479 #endif
3480 
3629 #endif
3630 
3632 
3633 #ifndef QT_NO_QOBJECT
3634 #include "qtextstream.moc"
3635 #endif
3636 
QString * string() const
Returns the current string assigned to the QTextStream, or 0 if no string has been assigned...
QTextStream * stream
static QTextCodec * codecForLocale()
Returns a pointer to the codec most suitable for this locale.
T qobject_cast(QObject *object)
Definition: qobject.h:375
double d
Definition: qnumeric_p.h:62
const QChar * readPtr() const
void ungetChar(const QChar &ch)
Status status() const
Returns the status of the text stream.
QChar positiveSign() const
Returns the positive sign character of this locale.
Definition: qlocale.cpp:1830
int integerBase() const
Returns the current base of integers.
bool fillReadBuffer(qint64 maxBytes=-1)
QTextStream & lowercasedigits(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & ~QTextStream::UppercaseDigits) on stre...
#define CHECK_VALID_STREAM(x)
QTextStream & endl(QTextStream &stream)
Writes &#39; &#39; to the stream and flushes the stream.
QTextStream()
Constructs a QTextStream.
bool blockSignals(bool b)
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke...
Definition: qobject.cpp:1406
QString & sprintf(const char *format,...)
Safely builds a formatted string from the format string cformat and an arbitrary list of arguments...
Definition: qstring.cpp:5567
void setDevice(QIODevice *device)
Sets the current device to device.
void putString(const QString &ch, bool number=false)
bool seek(qint64 pos)
Seeks to the position pos in the device.
unsigned char c[8]
Definition: qnumeric_p.h:62
QTextStream & noforcepoint(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & ~QTextStream::ForcePoint) on stream an...
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static void copyConverterStateHelper(QTextCodec::ConverterState *dest, const QTextCodec::ConverterState *src)
QString readLine(qint64 maxlen=0)
Reads one line of text from the stream, and returns it as a QString.
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QLocale locale() const
Returns the locale for this stream.
int digitValue() const
Returns the numeric value of the digit, or -1 if the character is not a digit.
Definition: qchar.cpp:817
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QTextStream & forcesign(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::ForceSign) on stream and ...
void write(const QString &data)
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
void reset()
Resets QTextStream&#39;s formatting options, bringing it back to its original constructed state...
void setStatus(Status status)
Sets the status of the text stream to the status given.
void putNumber(qulonglong number, bool negative)
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
Definition: qbuffer.cpp:315
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
void setString(QString *string, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
Sets the current string to string, using the given openMode.
static const int QTEXTSTREAM_BUFFERSIZE
Definition: qtextstream.cpp:43
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QChar exponential() const
Returns the exponential character of this locale.
Definition: qlocale.cpp:1843
QTextCodec::ConverterState writeConverterState
Q_CORE_EXPORT QTextStream & reset(QTextStream &s)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
void setCodec(QTextCodec *codec)
Sets the codec for this stream to codec.
QString & prepend(QChar c)
Definition: qstring.h:261
#define SLOT(a)
Definition: qobjectdefs.h:226
QScopedPointer< QTextStreamPrivate > d_ptr
Definition: qtextstream.h:261
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Definition: qiodevice.cpp:624
bool scan(const QChar **ptr, int *tokenLength, int maxlen, TokenDelimiter delimiter)
Scans no more than maxlen QChars in the current buffer for the first delimiter.
void setRealNumberNotation(RealNumberNotation notation)
Sets the real number notation to notation (SmartNotation, FixedNotation, ScientificNotation).
QTextStream::RealNumberNotation realNumberNotation
void setAutoDetectUnicode(bool enabled)
If enabled is true, QTextStream will attempt to detect Unicode encoding by peeking into the stream da...
FieldAlignment
This enum specifies how to align text in fields when the field is wider than the text that occupies i...
Definition: qtextstream.h:83
void setupDevice(QTextStream *stream, QIODevice *device)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setFieldWidth(int width)
Sets the current field width to width.
long ASN1_INTEGER_get ASN1_INTEGER * a
~ConverterState()
Destroys the ConverterState object.
Definition: qtextcodec.cpp:835
#define Q_SLOTS
Definition: qobjectdefs.h:71
int fieldWidth() const
Returns the current field width.
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
QString read(int maxlen)
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QTextStream & bom(QTextStream &stream)
Toggles insertion of the Byte Order Mark on stream when QTextStream is used with a UTF codec...
#define Q_D(Class)
Definition: qglobal.h:2482
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
Definition: qiodevice.cpp:711
static const uint base
Definition: qurl.cpp:268
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
void setTextModeEnabled(bool enabled)
If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is remove...
Definition: qiodevice.cpp:499
void saveConverterState(qint64 newPos)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
bool isSpace() const
Returns true if the character is a separator character (Separator_* categories); otherwise returns fa...
Definition: qchar.cpp:609
QTextCodec * codec() const
Returns the codec that is current assigned to the stream.
QTextStream::FieldAlignment fieldAlignment
static int sign(int x)
static QString doubleToString(const QChar zero, const QChar plus, const QChar minus, const QChar exponent, const QChar group, const QChar decimal, double d, int precision, DoubleForm form, int width, unsigned flags)
Definition: qlocale.cpp:2613
QDeviceClosedNotifier deviceClosedNotifier
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
QTextCodec::ConverterState * readConverterSavedState
Q_CORE_EXPORT void qDebug(const char *,...)
QTextStream & center(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignCenter) on stream and returns stream...
#define SIGNAL(a)
Definition: qobjectdefs.h:227
QIODevice * device
unsigned char uchar
Definition: qglobal.h:994
static QString unsLongLongToString(const QChar zero, const QChar group, const QChar plus, quint64 l, int precision, int base, int width, unsigned flags)
Definition: qlocale.cpp:2870
virtual ~QTextStream()
Destroys the QTextStream.
void setLocale(const QLocale &locale)
Sets the locale for this stream to locale.
QTextStream & right(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignRight) on stream and returns stream...
static QString longLongToString(const QChar zero, const QChar group, const QChar plus, const QChar minus, qint64 l, int precision, int base, int width, unsigned flags)
Definition: qlocale.cpp:2775
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QByteArray fromUnicode(const QString &uc) const
Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray...
QTextStream * q_ptr
static void resetCodecConverterStateHelper(QTextCodec::ConverterState *state)
QTextStream & reset(QTextStream &stream)
Calls QTextStream::reset() on stream and returns stream.
QTextStream & lowercasebase(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & ~QTextStream::UppercaseBase) on stream...
QTextStream & forcepoint(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::ForcePoint) on stream and...
static FILE * stream
QChar decimalPoint() const
Returns the decimal point character of this locale.
Definition: qlocale.cpp:1765
void setIntegerBase(int base)
Sets the base of integers to base, both for reading and for generating numbers.
QChar groupSeparator() const
Returns the group separator character of this locale.
Definition: qlocale.cpp:1778
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QTextStream & dec(QTextStream &stream)
Calls QTextStream::setIntegerBase(10) on stream and returns stream.
QTextStream & fixed(QTextStream &stream)
Calls QTextStream::setRealNumberNotation(QTextStream::FixedNotation) on stream and returns stream...
void resetStatus()
Resets the status of the text stream.
static unsigned int getChar(const QChar *str, int &i, const int len)
static QByteArray fromRawData(const char *, int size)
Constructs a QByteArray that uses the first size bytes of the data array.
bool flush()
Flushes any buffered data to the file.
Definition: qfile.cpp:1645
QTextStreamPrivate(QTextStream *q_ptr)
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
void setRealNumberPrecision(int precision)
Sets the precision of real numbers to precision.
#define IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(type)
QTextStream::Status status
bool autoDetectUnicode() const
Returns true if automatic Unicode detection is enabled, otherwise returns false.
const T * ptr(const T &t)
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
__int64 qint64
Definition: qglobal.h:942
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
QChar negativeSign() const
Returns the negative sign character of this locale.
Definition: qlocale.cpp:1817
int handle() const
Returns the file handle of the file.
Definition: qfile.cpp:1419
QIODevice * device() const
Returns the current device associated with the QTextStream, or 0 if no device has been assigned...
#define Q_OBJECT
Definition: qobjectdefs.h:157
QTextStream & scientific(QTextStream &stream)
Calls QTextStream::setRealNumberNotation(QTextStream::ScientificNotation) on stream and returns strea...
#define None
QTextStream & operator>>(QChar &ch)
Reads a character from the stream and stores it in c.
void setGenerateByteOrderMark(bool generate)
If generate is true and a UTF codec is used, QTextStream will insert the BOM (Byte Order Mark) before...
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
Definition: qiodevice.cpp:454
QTextStream & ws(QTextStream &stream)
Calls skipWhiteSpace() on stream and returns stream.
RealNumberNotation
This enum specifies which notations to use for expressing float and double as strings.
Definition: qtextstream.h:78
Q_CORE_EXPORT double qSNaN()
Returns the bit pattern of a signalling NaN as a double.
Definition: qnumeric.cpp:80
int length() const
Same as size().
Definition: qbytearray.h:356
ConversionFlags flags
Definition: qtextcodec.h:106
void resize(int size)
Sets the size of the string to size characters.
Definition: qstring.cpp:1353
virtual QByteArray name() const =0
QTextCodec subclasses must reimplement this function.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
bool getReal(double *f)
#define IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(type)
const QLocalePrivate * d() const
Definition: qlocale.cpp:761
NumberParsingStatus getNumber(qulonglong *l)
static QLocale c()
Returns a QLocale object initialized to the "C" locale.
Definition: qlocale.h:773
QTextStream & showbase(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::ShowBase) on stream and r...
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QTextCodec * codec
QString & append(QChar c)
Definition: qstring.cpp:1777
void setNumberFlags(NumberFlags flags)
Sets the current number flags to flags.
QTextCodec::ConverterState readConverterState
#define Q_VOID
FieldAlignment fieldAlignment() const
Returns the current field alignment.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
Status
This enum describes the current status of the text stream.
Definition: qtextstream.h:89
double toDouble(const QString &s, bool *ok=0) const
Returns the double represented by the localized string s, or 0.0 if the conversion failed...
Definition: qlocale.cpp:1279
qint64 readBufferStartDevicePos
void consume(int nchars)
QTextStream & bin(QTextStream &stream)
Calls QTextStream::setIntegerBase(2) on stream and returns stream.
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
QTextStream & oct(QTextStream &stream)
Calls QTextStream::setIntegerBase(8) on stream and returns stream.
qint64 readLine(char *data, qint64 maxlen)
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes...
Definition: qiodevice.cpp:1110
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QString read(qint64 maxlen)
Reads at most maxlen characters from the stream, and returns the data read as a QString.
bool generateByteOrderMark() const
Returns true if QTextStream is set to generate the UTF BOM (Byte Order Mark) when using a UTF codec; ...
Q_CORE_EXPORT double qInf()
Returns the bit pattern for an infinite number as a double.
Definition: qnumeric.cpp:90
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QTextStream & uppercasebase(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::UppercaseBase) on stream ...
QTextStream & operator<<(QBool b)
#define QT_NO_TEXTCODEC
QTextStream & noforcesign(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & ~QTextStream::ForceSign) on stream and...
NumberFlags numberFlags() const
Returns the current number flags.
QFactoryLoader * l
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
void restoreToSavedConverterState()
bool isTextModeEnabled() const
Returns true if the Text flag is enabled; otherwise returns false.
Definition: qiodevice.cpp:517
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
char toLatin1() const
Returns the Latin-1 character equivalent to the QChar, or 0.
Definition: qchar.h:376
QTextStream::NumberFlags numberFlags
QTextStream & hex(QTextStream &stream)
Calls QTextStream::setIntegerBase(16) on stream and returns stream.
RealNumberNotation realNumberNotation() const
Returns the current real number notation.
void flush()
Flushes any buffered data waiting to be written to the device.
void setFieldAlignment(FieldAlignment alignment)
Sets the field alignment to mode.
static QTextCodec * codecForUtfText(const QByteArray &ba)
Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and return...
QString readAll()
Reads the entire content of the stream, and returns it as a QString.
QChar padChar() const
Returns the current pad character.
QTextStream & noshowbase(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() & ~QTextStream::ShowBase) on stream and ...
qint64 pos() const
Returns the device position corresponding to the current position of the stream, or -1 if an error oc...
quint64 qulonglong
Definition: qglobal.h:952
bool getChar(QChar *ch)
bool atEnd() const
Returns true if there is no more data to be read from the QTextStream; otherwise returns false...
int realNumberPrecision() const
Returns the current real number precision, or the number of fraction digits QTextStream will write wh...
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
Q_CORE_EXPORT int qstricmp(const char *, const char *)
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
static QChar fromAscii(char c)
Converts the ASCII character c to its equivalent QChar.
Definition: qchar.cpp:1521
qint64 qlonglong
Definition: qglobal.h:951
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QChar toLower() const
Returns the lowercase equivalent if the character is uppercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1239
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
#define INT_MAX
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
bool isDigit() const
Returns true if the character is a decimal digit (Number_DecimalDigit); otherwise returns false...
Definition: qchar.cpp:699
void setPadChar(QChar ch)
Sets the pad character to ch.
#define enabled
void clear()
Clears the contents of the byte array and makes it empty.
QTextStream & flush(QTextStream &stream)
Calls QTextStream::flush() on stream and returns stream.
QIODevice::OpenMode stringOpenMode
QTextStream & uppercasedigits(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::UppercaseDigits) on strea...
void skipWhiteSpace()
Reads and discards whitespace from the stream until either a non-space character is detected...
QTextStream & left(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignLeft) on stream and returns stream...