⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qfsfileengine.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    d->tried_stat = 0;    if (d->fh) {        bool closed = true;        if (d->closeFileHandle)            closed = fclose(d->fh) == 0;        d->fh = 0;        d->fd = -1;        return flushed && closed;    }    if (d->fd == -1)        return false;    int ret = d->closeFileHandle ? QT_CLOSE(d->fd) : 0;    d->fd = -1;    if(ret == -1) {        setError(QFile::UnspecifiedError, qt_error_string(errno));        return false;    }    return true;}/*!    \reimp*/bool QFSFileEngine::flush(){    Q_D(QFSFileEngine);#ifdef Q_OS_UNIX    if (d->is_readonly)        return true;#endif    d->ungetchBuffer.clear();    if (d->lastFlushFailed)        return false;    if (!d->fh) {        // There's no write buffer when using an fd.        return d->fd != -1;    }#ifdef Q_OS_WIN    QT_FPOS_T pos;    int gotPos = QT_FGETPOS(d->fh, &pos);#endif    int ret = fflush(d->fh);    d->lastFlushFailed = (ret != 0);#ifdef Q_OS_WIN    if (gotPos == 0)        QT_FSETPOS(d->fh, &pos);#endif    d->lastIOCommand = QFSFileEnginePrivate::IOFlushCommand;    if (ret != 0) {        setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError,                 qt_error_string(errno));        return false;    }    return true;}/*!    \reimp*/qint64 QFSFileEngine::read(char *data, qint64 len){    Q_D(QFSFileEngine);    if (d->fh) {        if (d->lastIOCommand != QFSFileEnginePrivate::IOReadCommand) {            flush();            d->lastIOCommand = QFSFileEnginePrivate::IOReadCommand;        }        if (feof(d->fh))            return 0;        size_t readBytes = 0;#ifdef Q_OS_UNIX        if (d->sequential) {            int oldFlags = fcntl(fileno(d->fh), F_GETFL);            for (int i = 0; i < 2; ++i) {                // Make the underlying file descriptor non-blocking                int v = 1;                if ((oldFlags & O_NONBLOCK) == 0)                    fcntl(fileno(d->fh), F_SETFL, oldFlags | O_NONBLOCK, &v, sizeof(v));                size_t read = fread(data + readBytes, 1, size_t(len - readBytes), d->fh);                if (read > 0) {                    readBytes += read;                    break;                } else {                    if (readBytes)                        break;                    readBytes = read;                }                // Restore the blocking state of the underlying socket                if ((oldFlags & O_NONBLOCK) == 0) {                    int v = 1;                    fcntl(fileno(d->fh), F_SETFL, oldFlags, &v, sizeof(v));                    if (readBytes == 0) {                        int readByte = fgetc(d->fh);                        if (readByte != -1) {                            *data = uchar(readByte);                            readBytes += 1;                        }                    }                }            }            if ((oldFlags & O_NONBLOCK) == 0) {                int v = 1;                fcntl(fileno(d->fh), F_SETFL, oldFlags, &v, sizeof(v));            }        } else#endif        {            readBytes = fread(data, 1, size_t(len), d->fh);        }        qint64 ret = qint64(readBytes);        if (ret == 0) {            setError(QFile::ReadError, qt_error_string(int(errno)));            if (!feof(d->fh))                ret = -1;        }        return ret;    }    qint64 ret = 0;    if (!d->ungetchBuffer.isEmpty()) {        qint64 l = d->ungetchBuffer.size();        while(ret < l) {            *data = d->ungetchBuffer.at(l - ret - 1);            data++;            ret++;        }        d->ungetchBuffer.resize(l - ret);        len -= ret;    }    if(len && ret != len) {        int result;        qint64 read = 0;        do {            qint64 bytesToRead = len - read;#ifdef Q_OS_WIN            // Reading on Windows fails with ERROR_NO_SYSTEM_RESOURCES            // when the chunks are too large, so we limit the block            // size to 32MB.            const qint64 MaxBlockSize = 32 * 1024 * 1024;            bytesToRead = qMin(bytesToRead, MaxBlockSize);#endif            result = QT_READ(d->fd, data + read, int(bytesToRead));            if (result > 0)                read += result;        } while (result > 0 && read < len);        if (read > 0) {            ret += read;        } else {            if (!ret)                ret = -1;            setError(QFile::ReadError, qt_error_string(errno));        }    }    return ret;}/*!    \reimp*/qint64 QFSFileEngine::readLine(char *data, qint64 maxlen){    Q_D(QFSFileEngine);    if (!d->fh)        return QAbstractFileEngine::readLine(data, maxlen);    if (d->lastIOCommand != QFSFileEnginePrivate::IOReadCommand) {        flush();        d->lastIOCommand = QFSFileEnginePrivate::IOReadCommand;    }    if (feof(d->fh))        return 0;    // QIODevice::readLine() passes maxlen - 1 to QFile::readLineData()    // because it has made space for the '\0' at the end of data.  But fgets    // does the same, so we'd get two '\0' at the end - passing maxlen + 1    // solves this.    if (!fgets(data, int(maxlen + 1), d->fh)) {        setError(QFile::ReadError, qt_error_string(int(errno)));        return 0;    }    return qstrlen(data);}/*!    \reimp*/qint64 QFSFileEngine::write(const char *data, qint64 len){    Q_D(QFSFileEngine);    if (d->fh) {        if (d->lastIOCommand != QFSFileEnginePrivate::IOWriteCommand) {            flush();            d->lastIOCommand = QFSFileEnginePrivate::IOWriteCommand;        }    }    qint64 result;    qint64 written = 0;    do {        qint64 bytesToWrite = len - written;#ifdef Q_OS_WIN        // Writing on Windows fails with ERROR_NO_SYSTEM_RESOURCES        // when the chunks are too large, so we limit the block size        // to 32MB.        const qint64 MaxChunkSize = 32 * 1024 * 1024;        bytesToWrite = qMin<qint64>(bytesToWrite, MaxChunkSize);#endif        if (d->fh) {            result = qint64(fwrite(data + written, 1, size_t(bytesToWrite), d->fh));            if (bytesToWrite > 0 && result == 0)                result = -1;        } else {            result = QT_WRITE(d->fd, data + written, bytesToWrite);        }        if (result > 0)            written += qint64(result);    } while (written < len && ((result > 0 || (result == 0 && errno == EINTR))));    if (result > 0)        return written;    setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, qt_error_string(errno));    return qint64(result);}/*!    \reimp*/qint64 QFSFileEngine::pos() const{    Q_D(const QFSFileEngine);    if (d->fh)        return qint64(QT_FTELL(d->fh));    return QT_LSEEK(d->fd, 0, SEEK_CUR);}/*!    \reimp*/int QFSFileEngine::handle() const{    Q_D(const QFSFileEngine);    return d->fd;}/*!    \internal*/QAbstractFileEngine::Iterator *QFSFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames){    Q_UNUSED(filters);    Q_UNUSED(filterNames);    return 0;}/*!    \internal*/QAbstractFileEngine::Iterator *QFSFileEngine::endEntryList(){    return 0;}/*!    \reimp*/bool QFSFileEngine::seek(qint64 pos){    Q_D(QFSFileEngine);    if (d->fh) {        if (QT_FSEEK(d->fh, QT_OFF_T(pos), SEEK_SET) == -1) {            setError(QFile::ReadError, qt_error_string(int(errno)));            return false;        }        return true;    }    if(QT_LSEEK(d->fd, pos, SEEK_SET) == -1) {        qWarning("QFile::at: Cannot set file position %lld", pos);        setError(QFile::PositionError, qt_error_string(errno));        return false;    }    d->ungetchBuffer.clear();    return true;}/*!    \reimp*/bool QFSFileEngine::isSequential() const{    Q_D(const QFSFileEngine);    return d->sequential;}/*!    \reimp*/bool QFSFileEngine::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output){    Q_UNUSED(extension);    Q_UNUSED(option);    Q_UNUSED(output);    return false;}/*!    \reimp*/bool QFSFileEngine::supportsExtension(Extension extension) const{    Q_UNUSED(extension);    return false;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -