Public Types | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
QTextStream Class Reference

The QTextStream class provides basic functions for reading and writing text using a QIODevice. More...

#include <qtextstream.h>

Inheritance diagram for QTextStream:
QTextIStream QTextOStream

Public Types

enum  Encoding {
  Locale, Latin1, Unicode, UnicodeNetworkOrder,
  UnicodeReverse, RawUnicode, UnicodeUTF8
}
 
enum  {
  skipws = 0x0001, left = 0x0002, right = 0x0004, internal = 0x0008,
  bin = 0x0010, oct = 0x0020, dec = 0x0040, hex = 0x0080,
  showbase = 0x0100, showpoint = 0x0200, uppercase = 0x0400, showpos = 0x0800,
  scientific = 0x1000, fixed = 0x2000
}
 

Public Member Functions

void setEncoding (Encoding)
 
void setCodec (QTextCodec *)
 
 QTextStream ()
 
 QTextStream (QIODevice *)
 
 QTextStream (QString *, int mode)
 
 QTextStream (QString &, int mode)
 
 QTextStream (QByteArray, int mode)
 
 QTextStream (FILE *, int mode)
 
virtual ~QTextStream ()
 
QIODevicedevice () const
 
void setDevice (QIODevice *)
 
void unsetDevice ()
 
bool atEnd () const
 
bool eof () const
 
QTextStreamoperator>> (QChar &)
 
QTextStreamoperator>> (char &)
 
QTextStreamoperator>> (signed short &)
 
QTextStreamoperator>> (unsigned short &)
 
QTextStreamoperator>> (signed int &)
 
QTextStreamoperator>> (unsigned int &)
 
QTextStreamoperator>> (signed long &)
 
QTextStreamoperator>> (unsigned long &)
 
QTextStreamoperator>> (float &)
 
QTextStreamoperator>> (double &)
 
QTextStreamoperator>> (char *)
 
QTextStreamoperator>> (QString &)
 
QTextStreamoperator>> (QCString &)
 
QTextStreamoperator<< (QChar)
 
QTextStreamoperator<< (char)
 
QTextStreamoperator<< (signed short)
 
QTextStreamoperator<< (unsigned short)
 
QTextStreamoperator<< (signed int)
 
QTextStreamoperator<< (unsigned int)
 
QTextStreamoperator<< (signed long)
 
QTextStreamoperator<< (unsigned long)
 
QTextStreamoperator<< (float)
 
QTextStreamoperator<< (double)
 
QTextStreamoperator<< (const char *)
 
QTextStreamoperator<< (const QString &)
 
QTextStreamoperator<< (const QCString &)
 
QTextStreamoperator<< (void *)
 
QTextStreamreadRawBytes (char *, uint len)
 
QTextStreamwriteRawBytes (const char *, uint len)
 
QString readLine ()
 
QString read ()
 
void skipWhiteSpace ()
 
int flags () const
 
int flags (int f)
 
int setf (int bits)
 
int setf (int bits, int mask)
 
int unsetf (int bits)
 
void reset ()
 
int width () const
 
int width (int)
 
int fill () const
 
int fill (int)
 
int precision () const
 
int precision (int)
 

Static Public Attributes

static const int basefield = I_BASE_MASK
 
static const int adjustfield
 
static const int floatfield
 

Private Member Functions

long input_int ()
 
void init ()
 
QTextStreamoutput_int (int, ulong, bool)
 
bool isNetworkOrder ()
 
QChar eat_ws ()
 
void ts_ungetc (QChar)
 
QChar ts_getc ()
 
uint ts_getbuf (QChar *, uint)
 
void ts_putc (int)
 
void ts_putc (QChar)
 
bool ts_isspace (QChar)
 
bool ts_isdigit (QChar)
 
ulong input_bin ()
 
ulong input_oct ()
 
ulong input_dec ()
 
ulong input_hex ()
 
double input_double ()
 
QTextStreamwriteBlock (const char *p, uint len)
 
QTextStreamwriteBlock (const QChar *p, uint len)
 
 QTextStream (const QTextStream &)
 
QTextStreamoperator= (const QTextStream &)
 

Private Attributes

QIODevicedev
 
int fflags
 
int fwidth
 
int fillchar
 
int fprec
 
bool fstrm
 
bool owndev
 
QTextCodecmapper
 
QTextStreamPrivated
 
QChar ungetcBuf
 
bool latin1
 
bool internalOrder
 
bool doUnicodeHeader
 
void * reserved_ptr
 

Detailed Description

The QTextStream class provides basic functions for reading and writing text using a QIODevice.

endl bin oct dec hex flush ws

The text stream class has a functional interface that is very similar to that of the standard C++ iostream class. The difference between iostream and QTextStream is that our stream operates on a QIODevice, which is easily subclassed, while iostream operates on FILE * pointers, which can not be subclassed.

Qt provides several global functions similar to the ones in iostream:

Warning
By default, QTextStream will automatically detect whether integers in the stream are in decimal, octal, hexadecimal or binary format when reading from the stream. In particular, a leading '0' signifies octal, ie. the sequence "0100" will be interpreted as 64.

The QTextStream class reads and writes text and it is not appropriate for dealing with binary data (but QDataStream is).

By default output of Unicode text (ie. QString) is done using the local 8-bit encoding. This can be changed using the setEncoding() method. For input, the QTextStream will auto-detect standard Unicode "byte order marked" text files, but otherwise the local 8-bit encoding is used.

See also
QDataStream

Definition at line 53 of file qtextstream.h.

Member Enumeration Documentation

anonymous enum
Enumerator
skipws 
left 
right 
internal 
bin 
oct 
dec 
hex 
showbase 
showpoint 
uppercase 
showpos 
scientific 
fixed 

Definition at line 117 of file qtextstream.h.

117  {
118  skipws = 0x0001, // skip whitespace on input
119  left = 0x0002, // left-adjust output
120  right = 0x0004, // right-adjust output
121  internal = 0x0008, // pad after sign
122  bin = 0x0010, // binary format integer
123  oct = 0x0020, // octal format integer
124  dec = 0x0040, // decimal format integer
125  hex = 0x0080, // hex format integer
126  showbase = 0x0100, // show base indicator
127  showpoint = 0x0200, // force decimal point (float)
128  uppercase = 0x0400, // upper-case hex output
129  showpos = 0x0800, // add '+' to positive integers
130  scientific= 0x1000, // scientific float output
131  fixed = 0x2000 // fixed float output
132  };
Enumerator
Locale 
Latin1 
Unicode 
UnicodeNetworkOrder 
UnicodeReverse 
RawUnicode 
UnicodeUTF8 

Definition at line 56 of file qtextstream.h.

Constructor & Destructor Documentation

QTextStream::QTextStream ( )

Constructs a data stream that has no IO device.

Definition at line 235 of file qtextstream.cpp.

236 {
237  init();
238  setEncoding( Locale ); //###
239  reset();
241 }
SourceType sourceType
void setEncoding(Encoding)
QTextStreamPrivate * d
Definition: qtextstream.h:167
QTextStream::QTextStream ( QIODevice iod)

Constructs a text stream that uses the IO device iod.

Definition at line 247 of file qtextstream.cpp.

248 {
249  init();
250  setEncoding( Locale ); //###
251  dev = iod; // set device
252  reset();
254 }
SourceType sourceType
void setEncoding(Encoding)
QIODevice * dev
Definition: qtextstream.h:157
QTextStreamPrivate * d
Definition: qtextstream.h:167
QTextStream::QTextStream ( QString str,
int  filemode 
)

Constructs a text stream that operates on a Unicode QString through an internal device.

If you set an encoding or codec with setEncoding() or setCodec(), this setting is ignored for text streams that operate on QString.

Example:

ts << "pi = " << 3.14; // str == "pi = 3.14"

Writing data to the text stream will modify the contents of the string. The string will be expanded when data is written beyond the end of the string. Note that the string will not be truncated:

QString str = "pi = 3.14";
ts << "2+2 = " << 2+2; // str == "2+2 = 414"

Note that since QString is Unicode, you should not use readRawBytes() or writeRawBytes() on such a stream.

Definition at line 494 of file qtextstream.cpp.

495 {
496  // TODO: optimize for this case as it becomes more common
497  // (see QStringBuffer above)
498  init();
499  dev = new QStringBuffer( str );
500  ((QStringBuffer *)dev)->open( filemode );
501  owndev = TRUE;
503  reset();
505 }
SourceType sourceType
void setEncoding(Encoding)
QIODevice * dev
Definition: qtextstream.h:157
QTextStreamPrivate * d
Definition: qtextstream.h:167
const bool TRUE
Definition: qglobal.h:371
QTextStream::QTextStream ( QString str,
int  filemode 
)

This constructor is equivalent to the constructor taking a QString* parameter.

Definition at line 513 of file qtextstream.cpp.

514 {
515  init();
516  dev = new QStringBuffer( &str );
517  ((QStringBuffer *)dev)->open( filemode );
518  owndev = TRUE;
520  reset();
522 }
SourceType sourceType
void setEncoding(Encoding)
QIODevice * dev
Definition: qtextstream.h:157
QTextStreamPrivate * d
Definition: qtextstream.h:167
const bool TRUE
Definition: qglobal.h:371
QTextStream::QTextStream ( QByteArray  a,
int  mode 
)

Constructs a text stream that operates on a byte array through an internal QBuffer device.

Example:

ts << "pi = " << 3.14 << '\0'; // array == "pi = 3.14"

Writing data to the text stream will modify the contents of the array. The array will be expanded when data is written beyond the end of the string.

Same example, using a QBuffer:

QBuffer buf( array );
buf.open( IO_WriteOnly );
QTextStream ts( &buf );
ts << "pi = " << 3.14 << '\0'; // array == "pi = 3.14"
buf.close();

Definition at line 550 of file qtextstream.cpp.

551 {
552  init();
553  dev = new QBuffer( a );
554  ((QBuffer *)dev)->open( mode );
555  owndev = TRUE;
556  setEncoding( Locale ); //### Locale???
557  reset();
559 }
SourceType sourceType
void setEncoding(Encoding)
The QBuffer class is an I/O device that operates on a QByteArray.
Definition: qbuffer.h:47
QIODevice * dev
Definition: qtextstream.h:157
QTextStreamPrivate * d
Definition: qtextstream.h:167
const bool TRUE
Definition: qglobal.h:371
QTextStream::QTextStream ( FILE *  fh,
int  mode 
)

Constructs a text stream that operates on an existing file handle fh through an internal QFile device.

Example:

Definition at line 573 of file qtextstream.cpp.

574 {
575  init();
576  setEncoding( Locale ); //###
577  dev = new QFile;
578  ((QFile *)dev)->open( mode, fh );
579  fstrm = owndev = TRUE;
580  reset();
582 }
SourceType sourceType
void setEncoding(Encoding)
QIODevice * dev
Definition: qtextstream.h:157
bool open(int)
Definition: qfile_unix.cpp:134
The QFile class is an I/O device that operates on files.
Definition: qfile.h:50
QTextStreamPrivate * d
Definition: qtextstream.h:167
const bool TRUE
Definition: qglobal.h:371
QTextStream::~QTextStream ( )
virtual

Destructs the text stream.

The destructor does not affect the current IO device.

Definition at line 590 of file qtextstream.cpp.

591 {
592  if ( owndev )
593  delete dev;
594  delete d;
595 }
QIODevice * dev
Definition: qtextstream.h:157
QTextStreamPrivate * d
Definition: qtextstream.h:167
QTextStream::QTextStream ( const QTextStream )
private

Member Function Documentation

bool QTextStream::atEnd ( ) const
inline

Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set.

Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.

See also
QIODevice::atEnd()

Definition at line 226 of file qtextstream.h.

227 { return dev ? dev->atEnd() : FALSE; }
virtual bool atEnd() const
Definition: qiodevice.cpp:498
const bool FALSE
Definition: qglobal.h:370
QIODevice * dev
Definition: qtextstream.h:157
QIODevice * QTextStream::device ( ) const
inline

Returns the IO device currently set.

See also
setDevice(), unsetDevice()

Definition at line 223 of file qtextstream.h.

224 { return dev; }
QIODevice * dev
Definition: qtextstream.h:157
QChar QTextStream::eat_ws ( )
private

Definition at line 212 of file qtextstream.cpp.

213 {
214  QChar c;
215  do { c = ts_getc(); } while ( c != QEOF && ts_isspace(c) );
216  return c;
217 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
bool ts_isspace(QChar)
bool QTextStream::eof ( ) const
inline

This function has been renamed to atEnd().

See also
QIODevice::atEnd()

Definition at line 229 of file qtextstream.h.

230 { return atEnd(); }
bool atEnd() const
Definition: qtextstream.h:226
int QTextStream::fill ( ) const
inline

Returns the fill character. The default value is ' ' (space).

Definition at line 253 of file qtextstream.h.

254 { return fillchar; }
int QTextStream::fill ( int  f)
inline

Sets the fill character to f. Returns the previous fill character.

Definition at line 256 of file qtextstream.h.

257 { int oldc = fillchar; fillchar = f; return oldc; }
int QTextStream::flags ( ) const
inline

Returns the current stream flags. The default value is 0.

The meaning of the flags are:

  • skipws - Not currently used - whitespace always skipped
  • left - Numeric fields are left-aligned
  • right - Not currently used (by default numerics are right aligned)
  • internal - Put any padding spaces between +/- and value
  • bin - Output and input only in binary
  • oct - Output and input only in octal
  • dec - Output and input only in decimal
  • hex - Output and input only in hexadecimal
  • showbase - Annotate numeric outputs with 0b, 0, or 0x if in bin, oct, or hex format
  • showpoint - Not currently used
  • uppercase - Use 0B and 0X rather than 0b and 0x
  • showpos - Show + for positive numeric values
  • scientific - Use scientific notation for floating point values
  • fixed - Use fixed-point notation for floating point values

Note that unless bin, oct, dec, or hex is set, the input base is octal if the value starts with 0, hexadecimal if it starts with 0x, binary if the value starts with 0b, and decimal otherwise.

See also
setf(), unsetf()

Definition at line 232 of file qtextstream.h.

233 { return fflags; }
int QTextStream::flags ( int  f)
inline

Sets the stream flags to f. Returns the previous stream flags.

See also
setf(), unsetf(), flags()

Definition at line 235 of file qtextstream.h.

236 { int oldf = fflags; fflags = f; return oldf; }
void QTextStream::init ( )
private

Definition at line 219 of file qtextstream.cpp.

220 {
221  // ### ungetcBuf = QEOF;
222  dev = 0; // no device set
223  fstrm = owndev = FALSE;
224  mapper = 0;
225  d = new QTextStreamPrivate;
226  doUnicodeHeader = TRUE; //default to autodetect
227  latin1 = TRUE; // ### should use local?
228  internalOrder = QChar::networkOrdered(); //default to network order
229 }
bool doUnicodeHeader
Definition: qtextstream.h:171
const bool FALSE
Definition: qglobal.h:370
QIODevice * dev
Definition: qtextstream.h:157
static bool networkOrdered()
Definition: qstring.h:172
bool internalOrder
Definition: qtextstream.h:170
QTextStreamPrivate * d
Definition: qtextstream.h:167
const bool TRUE
Definition: qglobal.h:371
QTextCodec * mapper
Definition: qtextstream.h:166
ulong QTextStream::input_bin ( )
private

Definition at line 1049 of file qtextstream.cpp.

1050 {
1051  ulong val = 0;
1052  QChar ch = eat_ws();
1053  int dv = ch.digitValue();
1054  while ( dv == 0 || dv == 1 ) {
1055  val = ( val << 1 ) + dv;
1056  ch = ts_getc();
1057  dv = ch.digitValue();
1058  }
1059  if ( ch != QEOF )
1060  ts_ungetc( ch );
1061  return val;
1062 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
int digitValue() const
Definition: qstring.cpp:11075
unsigned long ulong
Definition: qglobal.h:352
void ts_ungetc(QChar)
ulong QTextStream::input_dec ( )
private

Definition at line 1083 of file qtextstream.cpp.

1084 {
1085  ulong val = 0;
1086  QChar ch = eat_ws();
1087  int dv = ch.digitValue();
1088  while ( ts_isdigit(ch) ) {
1089  val = val * 10 + dv;
1090  ch = ts_getc();
1091  dv = ch.digitValue();
1092  }
1093  if ( ch != QEOF )
1094  ts_ungetc( ch );
1095  return val;
1096 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isdigit(QChar)
int digitValue() const
Definition: qstring.cpp:11075
unsigned long ulong
Definition: qglobal.h:352
void ts_ungetc(QChar)
double QTextStream::input_double ( )
private

Definition at line 1193 of file qtextstream.cpp.

1194 {
1195  const int Init = 0; // states
1196  const int Sign = 1;
1197  const int Mantissa = 2;
1198  const int Dot = 3;
1199  const int Abscissa = 4;
1200  const int ExpMark = 5;
1201  const int ExpSign = 6;
1202  const int Exponent = 7;
1203  const int Done = 8;
1204 
1205  const int InputSign = 1; // input tokens
1206  const int InputDigit = 2;
1207  const int InputDot = 3;
1208  const int InputExp = 4;
1209 
1210  static uchar table[8][5] = {
1211  /* None InputSign InputDigit InputDot InputExp */
1212  { 0, Sign, Mantissa, Dot, 0, }, // Init
1213  { 0, 0, Mantissa, Dot, 0, }, // Sign
1214  { Done, Done, Mantissa, Dot, ExpMark,}, // Mantissa
1215  { 0, 0, Abscissa, 0, 0, }, // Dot
1216  { Done, Done, Abscissa, Done, ExpMark,}, // Abscissa
1217  { 0, ExpSign, Exponent, 0, 0, }, // ExpMark
1218  { 0, 0, Exponent, 0, 0, }, // ExpSign
1219  { Done, Done, Exponent, Done, Done } // Exponent
1220  };
1221 
1222  int state = Init; // parse state
1223  int input; // input token
1224 
1225  char buf[256];
1226  int i = 0;
1227  QChar c = eat_ws();
1228 
1229  while ( TRUE ) {
1230 
1231  switch ( c ) {
1232  case '+':
1233  case '-':
1234  input = InputSign;
1235  break;
1236  case '0': case '1': case '2': case '3': case '4':
1237  case '5': case '6': case '7': case '8': case '9':
1238  input = InputDigit;
1239  break;
1240  case '.':
1241  input = InputDot;
1242  break;
1243  case 'e':
1244  case 'E':
1245  input = InputExp;
1246  break;
1247  default:
1248  input = 0;
1249  break;
1250  }
1251 
1252  state = table[state][input];
1253 
1254  if ( state == 0 || state == Done || i > 250 ) {
1255  if ( i > 250 ) { // ignore rest of digits
1256  do { c = ts_getc(); } while ( c != QEOF && ts_isdigit(c) );
1257  }
1258  if ( c != QEOF )
1259  ts_ungetc( c );
1260  buf[i] = '\0';
1261  char *end;
1262  return strtod( buf, &end );
1263  }
1264 
1265  buf[i++] = c;
1266  c = ts_getc();
1267  }
1268 
1269 #if !defined(_CC_EGG_)
1270  return 0.0;
1271 #endif
1272 }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isdigit(QChar)
unsigned char uchar
Definition: nybbler.cc:11
static int input(void)
Definition: code.cpp:15695
void Init(void)
Definition: gXSecComp.cxx:138
void ts_ungetc(QChar)
const bool TRUE
Definition: qglobal.h:371
ulong QTextStream::input_hex ( )
private

Definition at line 1098 of file qtextstream.cpp.

1099 {
1100  ulong val = 0;
1101  QChar ch = eat_ws();
1102  char c = ch;
1103  while ( isxdigit(c) ) {
1104  val <<= 4;
1105  if ( ts_isdigit(c) )
1106  val += c - '0';
1107  else
1108  val += 10 + tolower(c) - 'a';
1109  c = ch = ts_getc();
1110  }
1111  if ( ch != QEOF )
1112  ts_ungetc( ch );
1113  return val;
1114 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isdigit(QChar)
unsigned long ulong
Definition: qglobal.h:352
void ts_ungetc(QChar)
long QTextStream::input_int ( )
private

Definition at line 1116 of file qtextstream.cpp.

1117 {
1118  long val;
1119  QChar ch;
1120  char c;
1121  switch ( flags() & basefield ) {
1122  case bin:
1123  val = (long)input_bin();
1124  break;
1125  case oct:
1126  val = (long)input_oct();
1127  break;
1128  case dec:
1129  c = ch = eat_ws();
1130  if ( ch == QEOF ) {
1131  val = 0;
1132  } else {
1133  if ( !(c == '-' || c == '+') )
1134  ts_ungetc( ch );
1135  if ( c == '-' ) {
1136  ulong v = input_dec();
1137  if ( v ) { // ensure that LONG_MIN can be read
1138  v--;
1139  val = -((long)v) - 1;
1140  } else {
1141  val = 0;
1142  }
1143  } else {
1144  val = (long)input_dec();
1145  }
1146  }
1147  break;
1148  case hex:
1149  val = (long)input_hex();
1150  break;
1151  default:
1152  val = 0;
1153  c = ch = eat_ws();
1154  if ( c == '0' ) { // bin, oct or hex
1155  c = ch = ts_getc();
1156  if ( tolower(c) == 'x' )
1157  val = (long)input_hex();
1158  else if ( tolower(c) == 'b' )
1159  val = (long)input_bin();
1160  else { // octal
1161  ts_ungetc( ch );
1162  if ( c >= '0' && c <= '7' ) {
1163  val = (long)input_oct();
1164  } else {
1165  val = 0;
1166  }
1167  }
1168  } else if ( ts_isdigit(ch) ) {
1169  ts_ungetc( ch );
1170  val = (long)input_dec();
1171  } else if ( c == '-' || c == '+' ) {
1172  ulong v = input_dec();
1173  if ( c == '-' ) {
1174  if ( v ) { // ensure that LONG_MIN can be read
1175  v--;
1176  val = -((long)v) - 1;
1177  } else {
1178  val = 0;
1179  }
1180  } else {
1181  val = (long)v;
1182  }
1183  }
1184  }
1185  return val;
1186 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
ulong input_hex()
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isdigit(QChar)
unsigned long ulong
Definition: qglobal.h:352
ulong input_bin()
ulong input_oct()
ulong input_dec()
void ts_ungetc(QChar)
static const int basefield
Definition: qtextstream.h:134
int flags() const
Definition: qtextstream.h:232
ulong QTextStream::input_oct ( )
private

Definition at line 1064 of file qtextstream.cpp.

1065 {
1066  ulong val = 0;
1067  QChar ch = eat_ws();
1068  int dv = ch.digitValue();
1069  while ( dv >= 0 && dv <= 7 ) {
1070  val = ( val << 3 ) + dv;
1071  ch = ts_getc();
1072  dv = ch.digitValue();
1073  }
1074  if ( dv == 8 || dv == 9 ) {
1075  while ( ts_isdigit(ch) )
1076  ch = ts_getc();
1077  }
1078  if ( ch != QEOF )
1079  ts_ungetc( ch );
1080  return val;
1081 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isdigit(QChar)
int digitValue() const
Definition: qstring.cpp:11075
unsigned long ulong
Definition: qglobal.h:352
void ts_ungetc(QChar)
bool QTextStream::isNetworkOrder ( )
inlineprivate

Definition at line 158 of file qtextstream.h.

158 { return internalOrder == QChar::networkOrdered(); }
static bool networkOrdered()
Definition: qstring.h:172
bool internalOrder
Definition: qtextstream.h:170
QTextStream & QTextStream::operator<< ( QChar  c)

Writes a char to the stream and returns a reference to the stream.

The character c is assumed to be Latin1 encoded independent of the Encoding set for the QTextStream.

Definition at line 1575 of file qtextstream.cpp.

1576 {
1578  ts_putc( c );
1579  return *this;
1580 }
#define CHECK_STREAM_PRECOND
void ts_putc(int)
QTextStream & QTextStream::operator<< ( char  c)

Writes a char to the stream and returns a reference to the stream.

Definition at line 1585 of file qtextstream.cpp.

1586 {
1588  unsigned char uc = (unsigned char) c;
1589  ts_putc( uc );
1590  return *this;
1591 }
#define CHECK_STREAM_PRECOND
void ts_putc(int)
QTextStream & QTextStream::operator<< ( signed short  i)

Writes a short integer to the stream and returns a reference to the stream.

Definition at line 1700 of file qtextstream.cpp.

1701 {
1702  return output_int( I_SHORT | I_SIGNED, i, i < 0 );
1703 }
#define I_SIGNED
QTextStream & output_int(int, ulong, bool)
#define I_SHORT
QTextStream & QTextStream::operator<< ( unsigned short  i)

Writes an unsigned short integer to the stream and returns a reference to the stream.

Definition at line 1711 of file qtextstream.cpp.

1712 {
1713  return output_int( I_SHORT | I_UNSIGNED, i, FALSE );
1714 }
const bool FALSE
Definition: qglobal.h:370
#define I_UNSIGNED
QTextStream & output_int(int, ulong, bool)
#define I_SHORT
QTextStream & QTextStream::operator<< ( signed int  i)

Writes an int to the stream and returns a reference to the stream.

Definition at line 1722 of file qtextstream.cpp.

1723 {
1724  return output_int( I_INT | I_SIGNED, i, i < 0 );
1725 }
#define I_SIGNED
#define I_INT
QTextStream & output_int(int, ulong, bool)
QTextStream & QTextStream::operator<< ( unsigned int  i)

Writes an unsigned int to the stream and returns a reference to the stream.

Definition at line 1733 of file qtextstream.cpp.

1734 {
1735  return output_int( I_INT | I_UNSIGNED, i, FALSE );
1736 }
const bool FALSE
Definition: qglobal.h:370
#define I_UNSIGNED
#define I_INT
QTextStream & output_int(int, ulong, bool)
QTextStream & QTextStream::operator<< ( signed long  i)

Writes a long int to the stream and returns a reference to the stream.

Definition at line 1744 of file qtextstream.cpp.

1745 {
1746  return output_int( I_LONG | I_SIGNED, i, i < 0 );
1747 }
#define I_SIGNED
QTextStream & output_int(int, ulong, bool)
#define I_LONG
QTextStream & QTextStream::operator<< ( unsigned long  i)

Writes an unsigned long int to the stream and returns a reference to the stream.

Definition at line 1755 of file qtextstream.cpp.

1756 {
1757  return output_int( I_LONG | I_UNSIGNED, i, FALSE );
1758 }
const bool FALSE
Definition: qglobal.h:370
#define I_UNSIGNED
QTextStream & output_int(int, ulong, bool)
#define I_LONG
QTextStream & QTextStream::operator<< ( float  f)

Writes a float to the stream and returns a reference to the stream.

Definition at line 1765 of file qtextstream.cpp.

1766 {
1767  return *this << (double)f;
1768 }
QTextStream & QTextStream::operator<< ( double  f)

Writes a double to the stream and returns a reference to the stream.

Definition at line 1775 of file qtextstream.cpp.

1776 {
1778  char buf[64];
1779  char f_char;
1780  char format[16];
1781  if ( (flags()&floatfield) == fixed )
1782  f_char = 'f';
1783  else if ( (flags()&floatfield) == scientific )
1784  f_char = (flags() & uppercase) ? 'E' : 'e';
1785  else
1786  f_char = (flags() & uppercase) ? 'G' : 'g';
1787  register char *fs = format; // generate format string
1788  *fs++ = '%'; // "%.<prec>l<f_char>"
1789  *fs++ = '.';
1790  int prec = precision();
1791  if ( prec > 99 )
1792  prec = 99;
1793  if ( prec >= 10 ) {
1794  *fs++ = prec / 10 + '0';
1795  *fs++ = prec % 10 + '0';
1796  } else {
1797  *fs++ = prec + '0';
1798  }
1799  *fs++ = 'l';
1800  *fs++ = f_char;
1801  *fs = '\0';
1802  sprintf( buf, format, f ); // convert to text
1803  if ( fwidth ) // padding
1804  *this << (const char*)buf;
1805  else // just write it
1806  writeBlock( buf, qstrlen(buf) );
1807  return *this;
1808 }
QTextStream & writeBlock(const char *p, uint len)
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
#define CHECK_STREAM_PRECOND
static constexpr double fs
Definition: Units.h:100
int precision() const
Definition: qtextstream.h:259
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
static const int floatfield
Definition: qtextstream.h:136
int flags() const
Definition: qtextstream.h:232
QTextStream & QTextStream::operator<< ( const char *  s)

Writes a string to the stream and returns a reference to the stream.

The string s is assumed to be Latin1 encoded independent of the Encoding set for the QTextStream.

Definition at line 1818 of file qtextstream.cpp.

1819 {
1821  char padbuf[48];
1822  uint len = qstrlen( s ); // don't write null terminator
1823  if ( fwidth ) { // field width set
1824  int padlen = fwidth - len;
1825  fwidth = 0; // reset width
1826  if ( padlen > 0 ) {
1827  char *ppad;
1828  if ( padlen > 46 ) { // create extra big fill buffer
1829  ppad = new char[padlen];
1830  CHECK_PTR( ppad );
1831  } else {
1832  ppad = padbuf;
1833  }
1834  memset( ppad, (char)fillchar, padlen ); // fill with fillchar
1835  if ( !(flags() & left) ) {
1836  writeBlock( ppad, padlen );
1837  padlen = 0;
1838  }
1839  writeBlock( s, len );
1840  if ( padlen )
1841  writeBlock( ppad, padlen );
1842  if ( ppad != padbuf ) // delete extra big fill buf
1843  delete[] ppad;
1844  return *this;
1845  }
1846  }
1847  writeBlock( s, len );
1848  return *this;
1849 }
QTextStream & writeBlock(const char *p, uint len)
#define CHECK_STREAM_PRECOND
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
#define CHECK_PTR(p)
Definition: qglobal.h:601
int flags() const
Definition: qtextstream.h:232
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
QTextStream & QTextStream::operator<< ( const QString s)

Writes s to the stream and returns a reference to the stream.

Definition at line 1867 of file qtextstream.cpp.

1868 {
1870  uint len = s.length();
1871  QString s1 = s;
1872  if ( fwidth ) { // field width set
1873  if ( !(flags() & left) ) {
1874  s1 = s.rightJustify(fwidth, (char)fillchar);
1875  } else {
1876  s1 = s.leftJustify(fwidth, (char)fillchar);
1877  }
1878  fwidth = 0; // reset width
1879  }
1880  writeBlock( s1.unicode(), len );
1881  return *this;
1882 }
const QChar * unicode() const
Definition: qstring.h:508
QString leftJustify(uint width, QChar fill=' ', bool trunc=FALSE) const
Definition: qstring.cpp:13303
QString rightJustify(uint width, QChar fill=' ', bool trunc=FALSE) const
Definition: qstring.cpp:13342
QTextStream & writeBlock(const char *p, uint len)
#define CHECK_STREAM_PRECOND
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
uint length() const
Definition: qstring.h:679
int flags() const
Definition: qtextstream.h:232
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
QTextStream & QTextStream::operator<< ( const QCString s)

Writes s to the stream and returns a reference to the stream.

The string s is assumed to be Latin1 encoded independent of the Encoding set for the QTextStream.

Definition at line 1858 of file qtextstream.cpp.

1859 {
1860  return operator<<(s.data());
1861 }
const char * data() const
Definition: qcstring.h:207
QTextStream & operator<<(QChar)
QTextStream & QTextStream::operator<< ( void *  ptr)

Writes a pointer to the stream and returns a reference to the stream.

The ptr is output as an unsigned long hexadecimal integer.

Definition at line 1891 of file qtextstream.cpp.

1892 {
1893  int f = flags();
1894  setf( hex, basefield );
1895  setf( showbase );
1896  unsetf( uppercase );
1897  output_int( I_LONG | I_UNSIGNED, (uintptr_t)ptr, FALSE );
1898  flags( f );
1899  return *this;
1900 }
int setf(int bits)
Definition: qtextstream.h:238
const bool FALSE
Definition: qglobal.h:370
int unsetf(int bits)
Definition: qtextstream.h:244
#define I_UNSIGNED
static const int basefield
Definition: qtextstream.h:134
int flags() const
Definition: qtextstream.h:232
QTextStream & output_int(int, ulong, bool)
#define I_LONG
QTextStream& QTextStream::operator= ( const QTextStream )
private
QTextStream & QTextStream::operator>> ( QChar c)

Reads a char from the stream and returns a reference to the stream. Note that whitespace is not skipped.

Definition at line 1041 of file qtextstream.cpp.

1042 {
1044  c = ts_getc();
1045  return *this;
1046 }
QChar ts_getc()
Definition: qtextstream.h:268
#define CHECK_STREAM_PRECOND
QTextStream & QTextStream::operator>> ( char &  c)

Reads a char from the stream and returns a reference to the stream. Note that whitespace is skipped.

Definition at line 1029 of file qtextstream.cpp.

1030 {
1032  c = eat_ws();
1033  return *this;
1034 }
#define CHECK_STREAM_PRECOND
QChar eat_ws()
QTextStream & QTextStream::operator>> ( signed short &  i)

Reads a signed short integer from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1280 of file qtextstream.cpp.

1281 {
1283  i = (signed short)input_int();
1284  return *this;
1285 }
#define CHECK_STREAM_PRECOND
long input_int()
QTextStream & QTextStream::operator>> ( unsigned short &  i)

Reads an unsigned short integer from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1293 of file qtextstream.cpp.

1294 {
1296  i = (unsigned short)input_int();
1297  return *this;
1298 }
#define CHECK_STREAM_PRECOND
long input_int()
QTextStream & QTextStream::operator>> ( signed int &  i)

Reads a signed int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1306 of file qtextstream.cpp.

1307 {
1309  i = (signed int)input_int();
1310  return *this;
1311 }
#define CHECK_STREAM_PRECOND
long input_int()
QTextStream & QTextStream::operator>> ( unsigned int &  i)

Reads an unsigned int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1319 of file qtextstream.cpp.

1320 {
1322  i = (unsigned int)input_int();
1323  return *this;
1324 }
#define CHECK_STREAM_PRECOND
long input_int()
QTextStream & QTextStream::operator>> ( signed long &  i)

Reads a signed long int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1332 of file qtextstream.cpp.

1333 {
1335  i = (signed long)input_int();
1336  return *this;
1337 }
#define CHECK_STREAM_PRECOND
long input_int()
QTextStream & QTextStream::operator>> ( unsigned long &  i)

Reads an unsigned long int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1345 of file qtextstream.cpp.

1346 {
1348  i = (unsigned long)input_int();
1349  return *this;
1350 }
#define CHECK_STREAM_PRECOND
long input_int()
QTextStream & QTextStream::operator>> ( float &  f)

Reads a float from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1358 of file qtextstream.cpp.

1359 {
1361  f = (float)input_double();
1362  return *this;
1363 }
double input_double()
#define CHECK_STREAM_PRECOND
QTextStream & QTextStream::operator>> ( double &  f)

Reads a double from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

Definition at line 1371 of file qtextstream.cpp.

1372 {
1374  f = input_double();
1375  return *this;
1376 }
double input_double()
#define CHECK_STREAM_PRECOND
QTextStream & QTextStream::operator>> ( char *  s)

Reads a word from the stream and returns a reference to the stream.

Definition at line 1383 of file qtextstream.cpp.

1384 {
1386  int maxlen = width( 0 );
1387  QChar c = eat_ws();
1388  if ( !maxlen )
1389  maxlen = -1;
1390  while ( c != QEOF ) {
1391  if ( ts_isspace(c) || maxlen-- == 0 ) {
1392  ts_ungetc( c );
1393  break;
1394  }
1395  *s++ = c;
1396  c = ts_getc();
1397  }
1398 
1399  *s = '\0';
1400  return *this;
1401 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
static const int maxlen
Definition: qregexp.cpp:904
#define CHECK_STREAM_PRECOND
int width() const
Definition: qtextstream.h:247
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isspace(QChar)
void ts_ungetc(QChar)
static QCString * s
Definition: config.cpp:1042
QTextStream & QTextStream::operator>> ( QString str)

Reads a word from the stream and returns a reference to the stream.

Definition at line 1407 of file qtextstream.cpp.

1408 {
1410  str=QString::fromLatin1("");
1411  QChar c = eat_ws();
1412 
1413  while ( c != QEOF ) {
1414  if ( ts_isspace(c) ) {
1415  ts_ungetc( c );
1416  break;
1417  }
1418  str += c;
1419  c = ts_getc();
1420  }
1421  return *this;
1422 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
#define CHECK_STREAM_PRECOND
static QString fromLatin1(const char *, int len=-1)
Definition: qstring.cpp:14539
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isspace(QChar)
void ts_ungetc(QChar)
QTextStream & QTextStream::operator>> ( QCString str)

Reads a word from the stream and returns a reference to the stream.

Definition at line 1428 of file qtextstream.cpp.

1429 {
1431  QCString *dynbuf = 0;
1432  const int buflen = 256;
1433  char buffer[buflen];
1434  char *s = buffer;
1435  int i = 0;
1436  QChar c = eat_ws();
1437 
1438  while ( c != QEOF ) {
1439  if ( ts_isspace(c) ) {
1440  ts_ungetc( c );
1441  break;
1442  }
1443  if ( i >= buflen-1 ) {
1444  if ( !dynbuf ) { // create dynamic buffer
1445  dynbuf = new QCString(buflen*2);
1446  memcpy( dynbuf->rawData(), s, i ); // copy old data
1447  } else if ( i >= (int)dynbuf->size()-1 ) {
1448  dynbuf->resize( dynbuf->size()*2 );
1449  }
1450  s = dynbuf->rawData();
1451  }
1452  s[i++] = c;
1453  c = ts_getc();
1454  }
1455  str.resize( i+1 );
1456  memcpy( str.rawData(), s, i );
1457  delete dynbuf;
1458  return *this;
1459 }
static const QChar QEOF
bool resize(uint newlen)
Definition: qcstring.h:225
char * rawData() const
Definition: qcstring.h:216
QChar ts_getc()
Definition: qtextstream.h:268
#define CHECK_STREAM_PRECOND
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar eat_ws()
bool ts_isspace(QChar)
void ts_ungetc(QChar)
static QCString * s
Definition: config.cpp:1042
QTextStream & QTextStream::output_int ( int  format,
ulong  n,
bool  neg 
)
private

Definition at line 1593 of file qtextstream.cpp.

1594 {
1595  static char hexdigits_lower[] = "0123456789abcdef";
1596  static char hexdigits_upper[] = "0123456789ABCDEF";
1598  char buf[76];
1599  register char *p;
1600  int len;
1601  char *hexdigits;
1602 
1603  switch ( flags() & I_BASE_MASK ) {
1604 
1605  case I_BASE_2: // output binary number
1606  switch ( format & I_TYPE_MASK ) {
1607  case I_SHORT: len=16; break;
1608  case I_INT: len=sizeof(int)*8; break;
1609  case I_LONG: len=32; break;
1610  default: len = 0;
1611  }
1612  p = &buf[74]; // go reverse order
1613  *p = '\0';
1614  while ( len-- ) {
1615  *--p = (char)(n&1) + '0';
1616  n >>= 1;
1617  if ( !n )
1618  break;
1619  }
1620  if ( flags() & showbase ) { // show base
1621  *--p = (flags() & uppercase) ? 'B' : 'b';
1622  *--p = '0';
1623  }
1624  break;
1625 
1626  case I_BASE_8: // output octal number
1627  p = &buf[74];
1628  *p = '\0';
1629  do {
1630  *--p = (char)(n&7) + '0';
1631  n >>= 3;
1632  } while ( n );
1633  if ( flags() & showbase )
1634  *--p = '0';
1635  break;
1636 
1637  case I_BASE_16: // output hexadecimal number
1638  p = &buf[74];
1639  *p = '\0';
1640  hexdigits = (flags() & uppercase) ?
1641  hexdigits_upper : hexdigits_lower;
1642  do {
1643  *--p = hexdigits[(int)n&0xf];
1644  n >>= 4;
1645  } while ( n );
1646  if ( flags() & showbase ) {
1647  *--p = (flags() & uppercase) ? 'X' : 'x';
1648  *--p = '0';
1649  }
1650  break;
1651 
1652  default: // decimal base is default
1653  p = &buf[74];
1654  *p = '\0';
1655  if ( neg )
1656  n = (ulong)(-(long)n);
1657  do {
1658  *--p = ((int)(n%10)) + '0';
1659  n /= 10;
1660  } while ( n );
1661  if ( neg )
1662  *--p = '-';
1663  else if ( flags() & showpos )
1664  *--p = '+';
1665  if ( (flags() & internal) && fwidth && !ts_isdigit(*p) ) {
1666  ts_putc( *p ); // special case for internal
1667  ++p; // padding
1668  fwidth--;
1669  return *this << (const char*)p;
1670  }
1671  }
1672  if ( fwidth ) { // adjustment required
1673  if ( !(flags() & left) ) { // but NOT left adjustment
1674  len = qstrlen(p);
1675  int padlen = fwidth - len;
1676  if ( padlen <= 0 ) { // no padding required
1677  writeBlock( p, len );
1678  } else if ( padlen < (int)(p-buf) ) { // speeds up padding
1679  memset( p-padlen, (char)fillchar, padlen );
1680  writeBlock( p-padlen, padlen+len );
1681  }
1682  else // standard padding
1683  *this << (const char*)p;
1684  }
1685  else
1686  *this << (const char*)p;
1687  fwidth = 0; // reset field width
1688  }
1689  else
1690  writeBlock( p, qstrlen(p) );
1691  return *this;
1692 }
QTextStream & writeBlock(const char *p, uint len)
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
#define CHECK_STREAM_PRECOND
bool ts_isdigit(QChar)
#define I_TYPE_MASK
#define I_BASE_16
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
std::void_t< T > n
unsigned long ulong
Definition: qglobal.h:352
p
Definition: test.py:223
#define I_BASE_8
void ts_putc(int)
#define I_BASE_MASK
#define I_INT
#define I_BASE_2
int flags() const
Definition: qtextstream.h:232
#define I_LONG
#define I_SHORT
int QTextStream::precision ( ) const
inline

Returns the precision. The default value is 6.

Definition at line 259 of file qtextstream.h.

260 { return fprec; }
int QTextStream::precision ( int  p)
inline

Sets the precision to p. Returns the previous precision setting.

Definition at line 262 of file qtextstream.h.

263 { int oldp = fprec; fprec = p; return oldp; }
p
Definition: test.py:223
QString QTextStream::read ( )

Reads the entire stream and returns a string containing the text.

See also
QIODevice::readLine()

Definition at line 1516 of file qtextstream.cpp.

1517 {
1518 #if defined(CHECK_STATE)
1519  if ( !dev ) {
1520  qWarning( "QTextStream::read: No device" );
1521  return QString::null;
1522  }
1523 #endif
1524  QString result;
1525  const uint bufsize = 512;
1526  QChar buf[bufsize];
1527  uint i, num, start;
1528  bool skipped_cr = FALSE;
1529 
1530  while ( 1 ) {
1531  num = ts_getbuf(buf,bufsize);
1532  // do a s/\r\n/\n
1533  start = 0;
1534  for ( i=0; i<num; i++ ) {
1535  if ( buf[i] == '\r' ) {
1536  // Only skip single cr's preceding lf's
1537  if ( skipped_cr ) {
1538  result += buf[i];
1539  start++;
1540  } else {
1541  result += QString( &buf[start], i-start );
1542  start = i+1;
1543  skipped_cr = TRUE;
1544  }
1545  } else {
1546  if ( skipped_cr ) {
1547  if ( buf[i] != '\n' ) {
1548  // Should not have skipped it
1549  result += '\r';
1550  }
1551  skipped_cr = FALSE;
1552  }
1553  }
1554  }
1555  if ( start < num )
1556  result += QString( &buf[start], i-start );
1557  if ( num != bufsize ) // if ( EOF )
1558  break;
1559  }
1560  return result;
1561 }
static QCString result
uint ts_getbuf(QChar *, uint)
const bool FALSE
Definition: qglobal.h:370
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QIODevice * dev
Definition: qtextstream.h:157
static const Null null
Definition: qstring.h:376
unsigned uint
Definition: qglobal.h:351
const bool TRUE
Definition: qglobal.h:371
QString QTextStream::readLine ( )

Reads a line from the stream and returns a string containing the text.

The returned string does not contain any trailing newline or carriage return. Note that this is different from QIODevice::readLine(), which does not strip the newline at the end of the line.

On EOF you will get a QString that is null. On reading an empty line the returned QString is empty but not null.

See also
QIODevice::readLine()

Definition at line 1475 of file qtextstream.cpp.

1476 {
1477 #if defined(CHECK_STATE)
1478  if ( !dev ) {
1479  qWarning( "QTextStream::readLine: No device" );
1480  return QString::null;
1481  }
1482 #endif
1483  QString result( "" );
1484  const int buf_size = 256;
1485  QChar c[buf_size];
1486  int pos = 0;
1487 
1488  c[pos] = ts_getc();
1489  if ( c[pos] == QEOF )
1490  return QString::null;
1491 
1492  while ( c[pos] != QEOF && c[pos] != '\n' ) {
1493  pos++;
1494  if ( pos >= buf_size ) {
1495  result += QString( c, pos );
1496  pos = 0;
1497  }
1498  c[pos] = ts_getc();
1499  }
1500  result += QString( c, pos );
1501 
1502  int len = (int)result.length();
1503  if ( len && result[len-1] == '\r' )
1504  result.truncate(len-1); // (if there are two \r, let one stay)
1505 
1506  return result;
1507 }
static const QChar QEOF
QChar ts_getc()
Definition: qtextstream.h:268
static QCString result
uint length() const
Definition: qcstring.h:195
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QIODevice * dev
Definition: qtextstream.h:157
bool truncate(uint pos)
Definition: qcstring.h:232
static const Null null
Definition: qstring.h:376
QTextStream & QTextStream::readRawBytes ( char *  s,
uint  len 
)

Reads len bytes from the stream into e s and returns a reference to the stream.

The buffer s must be preallocated.

Note that no encoding is done by this function.

Warning
The behaviour of this function is undefined unless the stream's encoding is set to Unicode or Latin1.
See also
QIODevice::readBlock()

Definition at line 883 of file qtextstream.cpp.

884 {
885  dev->readBlock( s, len );
886  return *this;
887 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qtextstream.h:157
static QCString * s
Definition: config.cpp:1042
void QTextStream::reset ( )

Resets the text stream.

  • All flags are set to 0.
  • The field width is set to 0.
  • The fill character is set to ' ' (space).
  • The precision is set to 6.
See also
setf(), width(), fill(), precision()

Definition at line 958 of file qtextstream.cpp.

959 {
960  fflags = 0;
961  fwidth = 0;
962  fillchar = ' ';
963  fprec = 6;
964 }
void QTextStream::setCodec ( QTextCodec codec)

Sets the codec for this stream to codec. Will not try to autodetect Unicode.

Note that this function should be called before any data is read to/written from the stream.

See also
setEncoding()

Definition at line 2228 of file qtextstream.cpp.

2229 {
2231  return; // QString does not need any codec
2232  mapper = codec;
2234 }
SourceType sourceType
bool doUnicodeHeader
Definition: qtextstream.h:171
const bool FALSE
Definition: qglobal.h:370
QTextStreamPrivate * d
Definition: qtextstream.h:167
QTextCodec * mapper
Definition: qtextstream.h:166
void QTextStream::setDevice ( QIODevice iod)

Sets the IO device to iod.

See also
device(), unsetDevice()

Definition at line 978 of file qtextstream.cpp.

979 {
980  if ( owndev ) {
981  delete dev;
982  owndev = FALSE;
983  }
984  dev = iod;
986 }
SourceType sourceType
const bool FALSE
Definition: qglobal.h:370
QIODevice * dev
Definition: qtextstream.h:157
QTextStreamPrivate * d
Definition: qtextstream.h:167
void QTextStream::setEncoding ( Encoding  e)

Sets the encoding of this stream to e, where e is one of:

  • Locale Using local file format (Latin1 if locale is not set), but autodetecting Unicode(utf16) on input.
  • Unicode Using Unicode(utf16) for input and output. Output will be written in the order most efficient for the current platform (i.e. the order used internally in QString).
  • UnicodeUTF8 Using Unicode(utf8) for input and output. If you use it for input it will autodetect utf16 and use it instead of utf8.
  • Latin1 ISO-8859-1. Will not autodetect utf16.
  • UnicodeNetworkOrder Using network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker.
  • UnicodeReverse Using reverse network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker, or writing data that should be read by buggy Windows applications.
  • RawUnicode Like Unicode, but does not write the byte order marker, nor does it autodetect the byte order. Only useful when writing to non-persistent storage used by a single process.

Locale and all Unicode encodings, except RawUnicode, will look at the first two bytes in a input stream to determine the byte order. The initial byte order marker will be stripped off before data is read.

Note that this function should be called before any data is read to/written from the stream.

See also
setCodec()

Definition at line 2155 of file qtextstream.cpp.

2156 {
2158  return; // QString does not need any encoding
2159  switch ( e ) {
2160  case Unicode:
2161  mapper = 0;
2162  latin1 = FALSE;
2164  internalOrder = TRUE;
2165  break;
2166  case UnicodeUTF8:
2167 #ifndef QT_NO_CODECS
2169  latin1 = FALSE;
2171  internalOrder = TRUE;
2172 #else
2173  mapper = 0;
2174  latin1 = TRUE;
2176 #endif
2177  break;
2178  case UnicodeNetworkOrder:
2179  mapper = 0;
2180  latin1 = FALSE;
2183  break;
2184  case UnicodeReverse:
2185  mapper = 0;
2186  latin1 = FALSE;
2188  internalOrder = !QChar::networkOrdered(); //reverse network ordered
2189  break;
2190  case RawUnicode:
2191  mapper = 0;
2192  latin1 = FALSE;
2194  internalOrder = TRUE;
2195  break;
2196  case Locale:
2197  latin1 = TRUE; // fallback to Latin 1
2198 #ifndef QT_NO_TEXTCODEC
2200 #if defined(_OS_WIN32_)
2201  if ( GetACP() == 1252 )
2202  mapper = 0; // Optimized latin1 processing
2203 #endif
2204  if ( mapper && mapper->mibEnum() == 4 )
2205 #endif
2206  mapper = 0; // Optimized latin1 processing
2207  doUnicodeHeader = TRUE; // If it reads as Unicode, accept it
2208  break;
2209  case Latin1:
2210  mapper = 0;
2212  latin1 = TRUE;
2213  break;
2214  }
2215 }
static QTextCodec * codecForLocale()
Definition: qtextcodec.cpp:542
SourceType sourceType
bool doUnicodeHeader
Definition: qtextstream.h:171
const bool FALSE
Definition: qglobal.h:370
const double e
static bool networkOrdered()
Definition: qstring.h:172
bool internalOrder
Definition: qtextstream.h:170
static QTextCodec * codecForMib(int mib)
Definition: qtextcodec.cpp:354
virtual int mibEnum() const =0
QTextStreamPrivate * d
Definition: qtextstream.h:167
const bool TRUE
Definition: qglobal.h:371
QTextCodec * mapper
Definition: qtextstream.h:166
int QTextStream::setf ( int  bits)
inline

Sets the stream flag bits bits. Returns the previous stream flags.

Equivalent to flags( flags() | bits ).

See also
setf(), unsetf()

Definition at line 238 of file qtextstream.h.

239 { int oldf = fflags; fflags |= bits; return oldf; }
int QTextStream::setf ( int  bits,
int  mask 
)
inline

Sets the stream flag bits bits with a bit mask mask. Returns the previous stream flags.

Equivalent to flags( (flags() & ~mask) | (bits & mask) ).

See also
setf(), unsetf()

Definition at line 241 of file qtextstream.h.

242 { int oldf = fflags; fflags = (fflags & ~mask) | (bits & mask); return oldf; }
void QTextStream::skipWhiteSpace ( )

Positions the read pointer at the first non-whitespace character.

Definition at line 600 of file qtextstream.cpp.

601 {
602  ts_ungetc( eat_ws() );
603 }
QChar eat_ws()
void ts_ungetc(QChar)
uint QTextStream::ts_getbuf ( QChar buf,
uint  len 
)
private

Tries to read len characters from the stream and stores them in buf. Returns the number of characters really read. Attention: There will no QEOF appended if the read reaches the end of the file. EOF is reached when the return value does not equal len.

Definition at line 620 of file qtextstream.cpp.

621 {
622  if( len<1 )
623  return 0;
624 
625  uint rnum=0; // the number of QChars really read
626 
627  if ( d && d->ungetcBuf.length() ) {
628  while( rnum < len && rnum < d->ungetcBuf.length() ) {
629  buf[rnum] = d->ungetcBuf.constref(rnum);
630  rnum++;
631  }
632  d->ungetcBuf = d->ungetcBuf.mid( rnum );
633  if ( rnum >= len )
634  return rnum;
635  }
636 
637  // we use dev->ungetch() for one of the bytes of the unicode
638  // byte-order mark, but a local unget hack for the other byte:
639  int ungetHack = EOF;
640 
641  if ( doUnicodeHeader ) {
642  doUnicodeHeader = FALSE; //only at the top
643  int c1 = dev->getch();
644  if ( c1 == EOF )
645  return rnum;
646  int c2 = dev->getch();
647  if ( c1 == 0xfe && c2 == 0xff ) {
648  mapper = 0;
649  latin1 = FALSE;
650  internalOrder = QChar::networkOrdered(); //network order
651  } else if ( c1 == 0xff && c2 == 0xfe ) {
652  mapper = 0;
653  latin1 = FALSE;
654  internalOrder = !QChar::networkOrdered(); //reverse network order
655  } else {
656  if ( c2 != EOF ) {
657  dev->ungetch( c2 );
658  ungetHack = c1;
659  } else {
660  dev->ungetch( c1 );
661  // note that a small possible bug might hide here
662  // here, if only the first byte of a file has made it
663  // so far, and that first byte is half of the
664  // byte-order mark, then the utfness will not be
665  // detected. whether or not this is a bug depends on
666  // taste. I can't really decide.
667  }
668  }
669  }
670 
671 #ifndef QT_NO_TEXTCODEC
672  if ( mapper ) {
673  bool shortRead = FALSE;
674  if ( !d->decoder )
675  d->decoder = mapper->makeDecoder();
676  while( rnum < len ) {
677  QString s;
678  bool readBlock = !( len == 1+rnum );
679  while ( TRUE ) {
680  // for efficiency: normally read a whole block
681  if ( readBlock ) {
682  // guess buffersize; this may be wrong (too small or too
683  // big). But we can handle this (either iterate reading
684  // or use ungetcBuf).
685  // Note that this might cause problems for codecs where
686  // one byte can result in >1 Unicode Characters if bytes
687  // are written to the stream in the meantime (loss of
688  // synchronicity).
689  uint rlen = len - rnum;
690  char *cbuf = new char[ rlen ];
691  if ( ungetHack != EOF ) {
692  rlen = 1+dev->readBlock( cbuf+1, rlen-1 );
693  cbuf[0] = (char)ungetHack;
694  ungetHack = EOF;
695  } else {
696  rlen = dev->readBlock( cbuf, rlen );
697  }
698  s += d->decoder->toUnicode( cbuf, rlen );
699  delete[] cbuf;
700  // use buffered reading only for the first time, because we
701  // have to get the stream synchronous again (this is easier
702  // with single character reading)
703  readBlock = FALSE;
704  }
705  // get stream (and codec) in sync
706  int c;
707  if ( ungetHack == EOF ) {
708  c = dev->getch();
709  } else {
710  c = ungetHack;
711  ungetHack = EOF;
712  }
713  if ( c == EOF ) {
714  shortRead = TRUE;
715  break;
716  }
717  char b = c;
718  uint lengthBefore = s.length();
719  s += d->decoder->toUnicode( &b, 1 );
720  if ( s.length() > lengthBefore )
721  break; // it seems we are in sync now
722  }
723  uint i = 0;
724  while( rnum < len && i < s.length() )
725  buf[rnum++] = s.constref(i++);
726  if ( s.length() > i )
727  // could be = but append is clearer
728  d->ungetcBuf.append( s.mid( i ) );
729  if ( shortRead )
730  return rnum;
731  }
732  } else
733 #endif
734  if ( latin1 ) {
735  if ( len == 1+rnum ) {
736  // use this method for one character because it is more efficient
737  // (arnt doubts whether it makes a difference, but lets it stand)
738  int c = (ungetHack == EOF) ? dev->getch() : ungetHack;
739  if ( c != EOF )
740  buf[rnum++] = (char)c;
741  } else {
742  if ( ungetHack != EOF ) {
743  buf[rnum++] = (char)ungetHack;
744  ungetHack = EOF;
745  }
746  char *cbuf = new char[len - rnum];
747  while ( !dev->atEnd() && rnum < len ) {
748  uint rlen = len - rnum;
749  rlen = dev->readBlock( cbuf, rlen );
750  uint i = 0;
751  while( i < rlen )
752  buf[rnum++] = cbuf[i++];
753  }
754  delete[] cbuf;
755  }
756  } else { // UCS-2 or UTF-16
757  if ( len == 1+rnum ) {
758  int c1 = (ungetHack == EOF) ? dev->getch() : ungetHack;
759  if ( c1 == EOF )
760  return rnum;
761  int c2 = dev->getch();
762  if ( c2 == EOF )
763  return rnum;
764  if ( isNetworkOrder() )
765  buf[rnum++] = QChar( c2, c1 );
766  else
767  buf[rnum++] = QChar( c1, c2 );
768  } else {
769  char *cbuf = new char[ 2*( len - rnum ) ]; // for paranoids: overflow possible
770  while ( !dev->atEnd() && rnum < len ) {
771  uint rlen = 2 * ( len-rnum );
772  if ( ungetHack != EOF ) {
773  rlen = 1+dev->readBlock( cbuf+1, rlen-1 );
774  cbuf[0] = (char)ungetHack;
775  ungetHack = EOF;
776  } else {
777  rlen = dev->readBlock( cbuf, rlen );
778  }
779  // We can't use an odd number of bytes, so put it back. But
780  // do it only if we are capable of reading more -- normally
781  // there should not be an odd number, but the file might be
782  // truncated or not in UTF-16...
783  if ( (rlen & 1) == 1 )
784  if ( !dev->atEnd() )
785  dev->ungetch( cbuf[--rlen] );
786  uint i = 0;
787  if ( isNetworkOrder() ) {
788  while( i < rlen ) {
789  buf[rnum++] = QChar( cbuf[i+1], cbuf[i] );
790  i+=2;
791  }
792  } else {
793  while( i < rlen ) {
794  buf[rnum++] = QChar( cbuf[i], cbuf[i+1] );
795  i+=2;
796  }
797  }
798  }
799  delete[] cbuf;
800  }
801  }
802  return rnum;
803 }
virtual QTextDecoder * makeDecoder() const
Definition: qtextcodec.cpp:726
virtual int ungetch(int)=0
bool isNetworkOrder()
Definition: qtextstream.h:158
QTextDecoder * decoder
bool doUnicodeHeader
Definition: qtextstream.h:171
QString mid(uint index, uint len=0xffffffff) const
Definition: qstring.cpp:13265
virtual bool atEnd() const
Definition: qiodevice.cpp:498
virtual int readBlock(char *data, uint maxlen)=0
const bool FALSE
Definition: qglobal.h:370
QString & append(char)
Definition: qstring.h:703
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
QChar ungetcBuf
Definition: qtextstream.h:168
QChar constref(uint i) const
Definition: qstring.h:498
virtual int getch()=0
QIODevice * dev
Definition: qtextstream.h:157
static bool networkOrdered()
Definition: qstring.h:172
uint length() const
Definition: qstring.h:679
bool internalOrder
Definition: qtextstream.h:170
QTextStreamPrivate * d
Definition: qtextstream.h:167
static bool * b
Definition: config.cpp:1043
virtual QString toUnicode(const char *chars, int len)=0
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
const bool TRUE
Definition: qglobal.h:371
QTextCodec * mapper
Definition: qtextstream.h:166
QChar QTextStream::ts_getc ( )
inlineprivate

Returns one character from the stream, or EOF.

Definition at line 268 of file qtextstream.h.

269 { QChar r; return ( ts_getbuf( &r,1 ) == 1 ? r : QChar((ushort)0xffff) ); }
uint ts_getbuf(QChar *, uint)
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
unsigned short ushort
Definition: qglobal.h:350
bool QTextStream::ts_isdigit ( QChar  c)
private

Definition at line 849 of file qtextstream.cpp.

850 {
851  return c.isDigit();
852 }
bool isDigit() const
Definition: qstring.cpp:11066
bool QTextStream::ts_isspace ( QChar  c)
private

Definition at line 854 of file qtextstream.cpp.

855 {
856  return c.isSpace();
857 }
bool isSpace() const
Definition: qstring.cpp:11006
void QTextStream::ts_putc ( int  ch)
private

Puts one character to the stream.

Definition at line 844 of file qtextstream.cpp.

845 {
846  ts_putc(QChar((ushort)ch));
847 }
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
unsigned short ushort
Definition: qglobal.h:350
void ts_putc(int)
void QTextStream::ts_putc ( QChar  c)
private

Puts one character to the stream.

Definition at line 809 of file qtextstream.cpp.

810 {
811 #ifndef QT_NO_TEXTCODEC
812  if ( mapper ) {
813  int len = 1;
814  QString s = c;
815  QCString block = mapper->fromUnicode( s, len );
816  dev->writeBlock( block, len );
817  } else
818 #endif
819  if ( latin1 ) {
820  if( c.row() )
821  dev->putch( '?' ); //######unknown character???
822  else
823  dev->putch( c.cell() );
824  } else {
825  if ( doUnicodeHeader ) {
828  }
829  if ( internalOrder ) {
830  dev->writeBlock( (char*)&c, sizeof(QChar) );
831  } else if ( isNetworkOrder() ) {
832  dev->putch(c.row());
833  dev->putch(c.cell());
834  } else {
835  dev->putch(c.cell());
836  dev->putch(c.row());
837  }
838  }
839 }
bool isNetworkOrder()
Definition: qtextstream.h:158
bool doUnicodeHeader
Definition: qtextstream.h:171
virtual QCString fromUnicode(const QString &uc, int &lenInOut) const
Definition: qtextcodec.cpp:783
const bool FALSE
Definition: qglobal.h:370
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
virtual int writeBlock(const char *data, uint len)=0
uchar & cell()
Definition: qstring.h:167
QIODevice * dev
Definition: qtextstream.h:157
bool internalOrder
Definition: qtextstream.h:170
QT_STATIC_CONST QChar byteOrderMark
Definition: qstring.h:70
void ts_putc(int)
uchar & row()
Definition: qstring.h:168
virtual int putch(int)=0
static QCString * s
Definition: config.cpp:1042
QTextCodec * mapper
Definition: qtextstream.h:166
void QTextStream::ts_ungetc ( QChar  c)
private

Definition at line 859 of file qtextstream.cpp.

860 {
861  if ( c.unicode() == 0xffff )
862  return;
863 
864  d->ungetcBuf.prepend( c );
865 }
ushort unicode() const
Definition: qstring.h:151
QString & prepend(char)
Definition: qstring.h:694
QTextStreamPrivate * d
Definition: qtextstream.h:167
void QTextStream::unsetDevice ( )

Unsets the IO device. Equivalent to setDevice( 0 ).

See also
device(), setDevice()

Definition at line 993 of file qtextstream.cpp.

994 {
995  setDevice( 0 );
997 }
SourceType sourceType
void setDevice(QIODevice *)
QTextStreamPrivate * d
Definition: qtextstream.h:167
int QTextStream::unsetf ( int  bits)
inline

Clears the stream flag bits bits. Returns the previous stream flags.

Equivalent to flags( flags() & ~mask ).

See also
setf()

Definition at line 244 of file qtextstream.h.

245 { int oldf = fflags; fflags &= ~bits; return oldf; }
int QTextStream::width ( ) const
inline

Returns the field width. The default value is 0.

Definition at line 247 of file qtextstream.h.

248 { return fwidth; }
int QTextStream::width ( int  w)
inline

Sets the field width to w. Returns the previous field width.

Definition at line 250 of file qtextstream.h.

251 { int oldw = fwidth; fwidth = w; return oldw; }
QTextStream & QTextStream::writeBlock ( const char *  p,
uint  len 
)
private

Definition at line 905 of file qtextstream.cpp.

906 {
907  if ( doUnicodeHeader ) {
909  if ( !mapper && !latin1 )
911  }
912  //All QCStrings and const char* are defined to be in Latin1
913  if ( !mapper && latin1 ) {
914  dev->writeBlock( p, len );
915  } else if ( !mapper && internalOrder ) {
916  QChar *u = new QChar[len];
917  for (uint i=0; i<len; i++)
918  u[i] = p[i];
919  dev->writeBlock( (char*)u, len*(int)sizeof(QChar) );
920  delete [] u;
921  } else {
922  for (uint i=0; i<len; i++)
923  ts_putc( (uchar)p[i] );
924  }
925  return *this;
926 }
bool doUnicodeHeader
Definition: qtextstream.h:171
const bool FALSE
Definition: qglobal.h:370
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
virtual int writeBlock(const char *data, uint len)=0
unsigned char uchar
Definition: nybbler.cc:11
QIODevice * dev
Definition: qtextstream.h:157
bool internalOrder
Definition: qtextstream.h:170
p
Definition: test.py:223
QT_STATIC_CONST QChar byteOrderMark
Definition: qstring.h:70
void ts_putc(int)
unsigned uint
Definition: qglobal.h:351
QTextCodec * mapper
Definition: qtextstream.h:166
QTextStream & QTextStream::writeBlock ( const QChar p,
uint  len 
)
private

Definition at line 928 of file qtextstream.cpp.

929 {
930  if ( !mapper && !latin1 && internalOrder ) {
931  if ( doUnicodeHeader ) {
934  }
935  dev->writeBlock( (char*)p, (int)sizeof(QChar)*len );
936  } else {
937  for (uint i=0; i<len; i++)
938  ts_putc( p[i] );
939  }
940  return *this;
941 }
bool doUnicodeHeader
Definition: qtextstream.h:171
const bool FALSE
Definition: qglobal.h:370
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
virtual int writeBlock(const char *data, uint len)=0
QIODevice * dev
Definition: qtextstream.h:157
bool internalOrder
Definition: qtextstream.h:170
QT_STATIC_CONST QChar byteOrderMark
Definition: qstring.h:70
void ts_putc(int)
unsigned uint
Definition: qglobal.h:351
QTextCodec * mapper
Definition: qtextstream.h:166
QTextStream & QTextStream::writeRawBytes ( const char *  s,
uint  len 
)

Writes the len bytes from s to the stream and returns a reference to the stream.

Note that no encoding is done by this function.

See also
QIODevice::writeBlock()

Definition at line 898 of file qtextstream.cpp.

899 {
900  dev->writeBlock( s, len );
901  return *this;
902 }
virtual int writeBlock(const char *data, uint len)=0
QIODevice * dev
Definition: qtextstream.h:157
static QCString * s
Definition: config.cpp:1042

Member Data Documentation

const int QTextStream::adjustfield
static
Initial value:

Definition at line 135 of file qtextstream.h.

const int QTextStream::basefield = I_BASE_MASK
static

Definition at line 134 of file qtextstream.h.

QTextStreamPrivate* QTextStream::d
private

Definition at line 167 of file qtextstream.h.

QIODevice* QTextStream::dev
private

Definition at line 157 of file qtextstream.h.

bool QTextStream::doUnicodeHeader
private

Definition at line 171 of file qtextstream.h.

int QTextStream::fflags
private

Definition at line 160 of file qtextstream.h.

int QTextStream::fillchar
private

Definition at line 162 of file qtextstream.h.

const int QTextStream::floatfield
static
Initial value:

Definition at line 136 of file qtextstream.h.

int QTextStream::fprec
private

Definition at line 163 of file qtextstream.h.

bool QTextStream::fstrm
private

Definition at line 164 of file qtextstream.h.

int QTextStream::fwidth
private

Definition at line 161 of file qtextstream.h.

bool QTextStream::internalOrder
private

Definition at line 170 of file qtextstream.h.

bool QTextStream::latin1
private

Definition at line 169 of file qtextstream.h.

QTextCodec* QTextStream::mapper
private

Definition at line 166 of file qtextstream.h.

bool QTextStream::owndev
private

Definition at line 165 of file qtextstream.h.

void* QTextStream::reserved_ptr
private

Definition at line 172 of file qtextstream.h.

QChar QTextStream::ungetcBuf
private

Definition at line 168 of file qtextstream.h.


The documentation for this class was generated from the following files: