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

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

Writes at most maxSize bytes of data from data to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.

See also:
read() writeData()

Definition at line 1193 of file qiodevice.cpp.

References QIODevice::seek(), and QIODevice::writeData().

Referenced by QTemporaryFile::createLocalFile(), QDataStream::operator<<(), QFile::rename(), QPicture::save(), QAbstractFormBuilder::save(), Q3SocketDevice::writeBlock(), Q3SocketDevice::writeData(), and QDataStream::writeRawData().

{
    Q_D(QIODevice);
    CHECK_WRITABLE(write, qint64(-1));
    CHECK_MAXLEN(write, qint64(-1));

    const bool sequential = d->isSequential();
    // Make sure the device is positioned correctly.
    if (d->pos != d->devicePos && !sequential && !seek(d->pos))
        return qint64(-1);

#ifdef Q_OS_WIN
    if (d->openMode & Text) {
        const char *endOfData = data + maxSize;
        const char *startOfBlock = data;

        qint64 writtenSoFar = 0;

        forever {
            const char *endOfBlock = startOfBlock;
            while (endOfBlock < endOfData && *endOfBlock != '\n')
                ++endOfBlock;

            qint64 blockSize = endOfBlock - startOfBlock;
            if (blockSize > 0) {
                qint64 ret = writeData(startOfBlock, blockSize);
                if (ret <= 0) {
                    if (writtenSoFar && !sequential)
                        d->buffer.skip(writtenSoFar);
                    return writtenSoFar ? writtenSoFar : ret;
                }
                if (!sequential) {
                    d->pos += ret;
                    d->devicePos += ret;
                }
                writtenSoFar += ret;
            }

            if (endOfBlock == endOfData)
                break;

            qint64 ret = writeData("\r\n", 2);
            if (ret <= 0) {
                if (writtenSoFar && !sequential)
                    d->buffer.skip(writtenSoFar);
                return writtenSoFar ? writtenSoFar : ret;
            }
            if (!sequential) {
                d->pos += ret;
                d->devicePos += ret;
            }
            ++writtenSoFar;

            startOfBlock = endOfBlock + 1;
        }

        if (writtenSoFar && !sequential)
            d->buffer.skip(writtenSoFar);
        return writtenSoFar;
    }
#endif

    qint64 written = writeData(data, maxSize);
    if (written > 0) {
        if (!sequential) {
            d->pos += written;
            d->devicePos += written;
        }
        if (!d->buffer.isEmpty() && !sequential)
            d->buffer.skip(written);
    }
    return written;
}


Generated by  Doxygen 1.6.0   Back to index