Logo Search packages:      
Sourcecode: qt4-x11 version File versions

qint64 QIODevice::readLine ( char *  data,
qint64  maxSize 
) [inherited]

This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. If a line could not be read but no error ocurred, this function returns 0. If an error occurs, this function returns what it could the length of what could be read, or -1 if nothing was read.

A terminating '' byte is always appended to data, so maxSize must be larger than 1.

Data is read until either of the following conditions are met:

The first '
' character is read. maxSize - 1 bytes are read. The end of the device data is detected.

For example, the following code reads a line of characters from a file:

doc/src/snippets/code/src.corelib.io.qiodevice.cpp 2

The newline character ('
') is included in the buffer. If a newline is not encountered before maxSize - 1 bytes are read, a newline will not be inserted into the buffer. On windows newline characters are replaced with '
'.

This function calls readLineData(), which is implemented using repeated calls to getChar(). You can provide a more efficient implementation by reimplementing readLineData() in your own subclass.

See also:
getChar(), read(), write()

Definition at line 1004 of file qiodevice.cpp.

References QIODevice::readLineData(), QIODevice::seek(), and QIODevice::size().

Referenced by MainWindow::modelFromFile(), QIODevice::readLine(), and Q3TextStream::ts_getline().

{
    Q_D(QIODevice);
    if (maxSize < 2) {
        qWarning("QIODevice::readLine: Called with maxSize < 2");
        return qint64(-1);
    }

#if defined QIODEVICE_DEBUG
    printf("%p QIODevice::readLine(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
           this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
#endif

    // Leave room for a '\0'
    --maxSize;

    const bool sequential = d->isSequential();

    qint64 readSoFar = 0;
    if (!d->buffer.isEmpty()) {
        readSoFar = d->buffer.readLine(data, maxSize);
        if (!sequential)
            d->pos += readSoFar;
#if defined QIODEVICE_DEBUG
        printf("%p \tread from buffer: %d bytes, last character read: %hhx\n", this,
               int(readSoFar), data[int(readSoFar) - 1]);
        if (readSoFar)
            debugBinaryString(data, int(readSoFar));
#endif
        if (readSoFar && data[readSoFar - 1] == '\n') {
            if (d->openMode & Text) {
                // QRingBuffer::readLine() isn't Text aware.
                if (readSoFar > 1 && data[readSoFar - 2] == '\r') {
                    --readSoFar;
                    data[readSoFar - 1] = '\n';
                }
            }
            data[readSoFar] = '\0';
            return readSoFar;
        }
    }

    if (d->pos != d->devicePos && !sequential && !seek(d->pos))
        return qint64(-1);
    d->baseReadLineDataCalled = false;
    qint64 readBytes = readLineData(data + readSoFar, maxSize - readSoFar);
#if defined QIODEVICE_DEBUG
    printf("%p \tread from readLineData: %d bytes, readSoFar = %d bytes\n", this,
           int(readBytes), int(readSoFar));
    if (readBytes > 0) {
        debugBinaryString(data, int(readSoFar + readBytes));
    }
#endif
    if (readBytes < 0) {
        data[readSoFar] = '\0';
        return readSoFar ? readSoFar : -1;
    }
    readSoFar += readBytes;
    if (!d->baseReadLineDataCalled && !sequential) {
        d->pos += readBytes;
        // If the base implementation was not called, then we must
        // assume the device position is invalid and force a seek.
        d->devicePos = qint64(-1);
    }
    data[readSoFar] = '\0';

    if (d->openMode & Text) {
        if (readSoFar > 1 && data[readSoFar - 1] == '\n' && data[readSoFar - 2] == '\r') {
            data[readSoFar - 2] = '\n';
            data[readSoFar - 1] = '\0';
            --readSoFar;
        }
    }

#if defined QIODEVICE_DEBUG
    printf("%p \treturning %d, d->pos = %d, d->buffer.size() = %d, size() = %d\n",
           this, int(readSoFar), int(d->pos), d->buffer.size(), int(size()));
    debugBinaryString(data, int(readSoFar));
#endif
    return readSoFar;
}


Generated by  Doxygen 1.6.0   Back to index