The QTextStream class provides basic functions for reading and writing text using a QIODevice. More...
#include <qtextstream.h>
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 } |
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 () |
QTextStream & | output_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 () |
QTextStream & | writeBlock (const char *p, uint len) |
QTextStream & | writeBlock (const QChar *p, uint len) |
QTextStream (const QTextStream &) | |
QTextStream & | operator= (const QTextStream &) |
Private Attributes | |
QIODevice * | dev |
int | fflags |
int | fwidth |
int | fillchar |
int | fprec |
bool | fstrm |
bool | owndev |
QTextCodec * | mapper |
QTextStreamPrivate * | d |
QChar | ungetcBuf |
bool | latin1 |
bool | internalOrder |
bool | doUnicodeHeader |
void * | reserved_ptr |
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:
bin
sets the QTextStream to read/write binary numbers oct
sets the QTextStream to read/write octal numbers dec
sets the QTextStream to read/write decimal numbers hex
sets the QTextStream to read/write hexadecimal numbers endl
forces a line break flush
forces the QIODevice to flush any buffered data ws
eats any available white space (on input) reset
resets the QTextStream to its default mode (see reset()). 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.
Definition at line 53 of file qtextstream.h.
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.
Enumerator | |
---|---|
Locale | |
Latin1 | |
Unicode | |
UnicodeNetworkOrder | |
UnicodeReverse | |
RawUnicode | |
UnicodeUTF8 |
Definition at line 56 of file qtextstream.h.
QTextStream::QTextStream | ( | ) |
Constructs a data stream that has no IO device.
Definition at line 235 of file qtextstream.cpp.
QTextStream::QTextStream | ( | QIODevice * | iod | ) |
Constructs a text stream that uses the IO device iod.
Definition at line 247 of file qtextstream.cpp.
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.
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:
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.
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.
QTextStream::QTextStream | ( | QByteArray | a, |
int | mode | ||
) |
Constructs a text stream that operates on a byte array through an internal QBuffer device.
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:
Definition at line 550 of file qtextstream.cpp.
QTextStream::QTextStream | ( | FILE * | fh, |
int | mode | ||
) |
Constructs a text stream that operates on an existing file handle fh through an internal QFile device.
Definition at line 573 of file qtextstream.cpp.
|
virtual |
Destructs the text stream.
The destructor does not affect the current IO device.
Definition at line 590 of file qtextstream.cpp.
|
private |
|
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.
Definition at line 226 of file qtextstream.h.
|
inline |
Returns the IO device currently set.
Definition at line 223 of file qtextstream.h.
|
private |
Definition at line 212 of file qtextstream.cpp.
|
inline |
This function has been renamed to atEnd().
Definition at line 229 of file qtextstream.h.
|
inline |
Returns the fill character. The default value is ' ' (space).
Definition at line 253 of file qtextstream.h.
|
inline |
Sets the fill character to f. Returns the previous fill character.
Definition at line 256 of file qtextstream.h.
|
inline |
Returns the current stream flags. The default value is 0.
The meaning of the flags are:
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.
Definition at line 232 of file qtextstream.h.
|
inline |
Sets the stream flags to f. Returns the previous stream flags.
Definition at line 235 of file qtextstream.h.
|
private |
Definition at line 219 of file qtextstream.cpp.
|
private |
Definition at line 1049 of file qtextstream.cpp.
|
private |
Definition at line 1083 of file qtextstream.cpp.
|
private |
Definition at line 1193 of file qtextstream.cpp.
|
private |
Definition at line 1098 of file qtextstream.cpp.
|
private |
Definition at line 1116 of file qtextstream.cpp.
|
private |
Definition at line 1064 of file qtextstream.cpp.
|
inlineprivate |
Definition at line 158 of file qtextstream.h.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
|
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
|
private |
Definition at line 1593 of file qtextstream.cpp.
|
inline |
|
inline |
Sets the precision to p. Returns the previous precision setting.
Definition at line 262 of file qtextstream.h.
QString QTextStream::read | ( | ) |
Reads the entire stream and returns a string containing the text.
Definition at line 1516 of file qtextstream.cpp.
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.
Definition at line 1475 of file qtextstream.cpp.
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.
Definition at line 883 of file qtextstream.cpp.
void QTextStream::reset | ( | ) |
Resets the text stream.
Definition at line 958 of file qtextstream.cpp.
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.
Definition at line 2228 of file qtextstream.cpp.
void QTextStream::setDevice | ( | QIODevice * | iod | ) |
Sets the IO device to iod.
Definition at line 978 of file qtextstream.cpp.
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.
Definition at line 2155 of file qtextstream.cpp.
|
inline |
Sets the stream flag bits bits. Returns the previous stream flags.
Equivalent to flags( flags() | bits )
.
Definition at line 238 of file qtextstream.h.
|
inline |
Sets the stream flag bits bits with a bit mask mask. Returns the previous stream flags.
Equivalent to flags( (flags() & ~mask) | (bits & mask) )
.
Definition at line 241 of file qtextstream.h.
void QTextStream::skipWhiteSpace | ( | ) |
Positions the read pointer at the first non-whitespace character.
Definition at line 600 of file qtextstream.cpp.
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.
|
inlineprivate |
Definition at line 849 of file qtextstream.cpp.
Definition at line 854 of file qtextstream.cpp.
|
private |
|
private |
Puts one character to the stream.
Definition at line 809 of file qtextstream.cpp.
|
private |
Definition at line 859 of file qtextstream.cpp.
void QTextStream::unsetDevice | ( | ) |
Unsets the IO device. Equivalent to setDevice( 0 ).
Definition at line 993 of file qtextstream.cpp.
|
inline |
Clears the stream flag bits bits. Returns the previous stream flags.
Equivalent to flags( flags() & ~mask )
.
Definition at line 244 of file qtextstream.h.
|
inline |
|
inline |
Sets the field width to w. Returns the previous field width.
Definition at line 250 of file qtextstream.h.
|
private |
Definition at line 905 of file qtextstream.cpp.
|
private |
Definition at line 928 of file qtextstream.cpp.
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.
Definition at line 898 of file qtextstream.cpp.
|
static |
Definition at line 135 of file qtextstream.h.
|
static |
Definition at line 134 of file qtextstream.h.
|
private |
Definition at line 167 of file qtextstream.h.
|
private |
Definition at line 157 of file qtextstream.h.
|
private |
Definition at line 171 of file qtextstream.h.
|
private |
Definition at line 160 of file qtextstream.h.
|
private |
Definition at line 162 of file qtextstream.h.
|
static |
Definition at line 136 of file qtextstream.h.
|
private |
Definition at line 163 of file qtextstream.h.
|
private |
Definition at line 164 of file qtextstream.h.
|
private |
Definition at line 161 of file qtextstream.h.
|
private |
Definition at line 170 of file qtextstream.h.
|
private |
Definition at line 169 of file qtextstream.h.
|
private |
Definition at line 166 of file qtextstream.h.
|
private |
Definition at line 165 of file qtextstream.h.
|
private |
Definition at line 172 of file qtextstream.h.
|
private |
Definition at line 168 of file qtextstream.h.