📄 qfsfileengine.cpp
字号:
q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, qt_error_string(errno)); return false; } return true;}/*! \reimp*/qint64 QFSFileEngine::size() const{ Q_D(const QFSFileEngine); return d->nativeSize();}/*! \internal*/qint64 QFSFileEnginePrivate::sizeFdFh() const{ Q_Q(const QFSFileEngine); // ### Fix this function, it should not stat unless the file is closed. QT_STATBUF st; int ret = 0; const_cast<QFSFileEngine *>(q)->flush(); if (fh && nativeFilePath.isEmpty()) { // Buffered stdlib mode. // ### This should really be an ftell ret = QT_FSTAT(QT_FILENO(fh), &st); } else if (fd == -1) { // Stateless stat. ret = QT_STAT(nativeFilePath.constData(), &st); } else { // Unbuffered stdio mode. ret = QT_FSTAT(fd, &st); } if (ret == -1) return 0; return st.st_size;}/*! \reimp*/qint64 QFSFileEngine::pos() const{ Q_D(const QFSFileEngine); return d->nativePos();}/*! \internal*/qint64 QFSFileEnginePrivate::posFdFh() const{ if (fh) return qint64(QT_FTELL(fh)); return QT_LSEEK(fd, 0, SEEK_CUR);}/*! \reimp*/bool QFSFileEngine::seek(qint64 pos){ Q_D(QFSFileEngine); return d->nativeSeek(pos);}/*! \internal*/bool QFSFileEnginePrivate::seekFdFh(qint64 pos){ Q_Q(QFSFileEngine); // On Windows' stdlib implementation, the results of calling fread and // fwrite are undefined if not called either in sequence, or if preceded // with a call to fflush(). if (lastIOCommand != QFSFileEnginePrivate::IOFlushCommand && !q->flush()) return false; if (fh) { // Buffered stdlib mode. int ret; do { ret = QT_FSEEK(fh, QT_OFF_T(pos), SEEK_SET); } while (ret == -1 && errno == EINTR); if (ret == -1) { q->setError(QFile::ReadError, qt_error_string(int(errno))); return false; } } else { // Unbuffered stdio mode. if (QT_LSEEK(fd, pos, SEEK_SET) == -1) { qWarning("QFile::at: Cannot set file position %lld", pos); q->setError(QFile::PositionError, qt_error_string(errno)); return false; } } return true;}/*! \reimp*/int QFSFileEngine::handle() const{ Q_D(const QFSFileEngine); return d->nativeHandle();}/*! \reimp*/qint64 QFSFileEngine::read(char *data, qint64 maxlen){ Q_D(QFSFileEngine); // On Windows' stdlib implementation, the results of calling fread and // fwrite are undefined if not called either in sequence, or if preceded // with a call to fflush(). if (d->lastIOCommand != QFSFileEnginePrivate::IOReadCommand) { flush(); d->lastIOCommand = QFSFileEnginePrivate::IOReadCommand; } return d->nativeRead(data, maxlen);}/*! \internal*/qint64 QFSFileEnginePrivate::readFdFh(char *data, qint64 len){ Q_Q(QFSFileEngine); // Buffered stdlib mode. if (fh) { qint64 readBytes = 0; qint64 read = 0; int retry = 0; // Read in blocks of 4k to avoid platform limitations (Windows // commonly bails out if you read or write too large blocks at once). qint64 bytesToRead; do { if (retry == 1) retry = 2; bytesToRead = qMin<qint64>(4096, len - read); do { readBytes = fread(data + read, 1, size_t(bytesToRead), fh); } while (readBytes == 0 && !feof(fh) && errno == EINTR); if (readBytes > 0) { read += readBytes; } else if (!retry && feof(fh)) { // Synchronize and try again (just once though). if (++retry == 1) QT_FSEEK(fh, QT_FTELL(fh), SEEK_SET); } } while (retry == 1 || (readBytes == bytesToRead && read < len)); // Return the number of bytes read, or if nothing was read, return -1 // if an error occurred, or 0 if we detected EOF. if (read == 0) { q->setError(QFile::ReadError, qt_error_string(int(errno))); if (!feof(fh)) read = -1; } return read; } // Unbuffered stdio mode. qint64 ret = 0; if (len) { int result; qint64 read = 0; // Read in blocks of 4k to avoid platform limitations (Windows // commonly bails out if you read or write too large blocks at once). do { qint64 bytesToRead = qMin<qint64>(4096, len - read); do { result = QT_READ(fd, data + read, int(bytesToRead)); } while (result == -1 && errno == EINTR); if (result > 0) read += result; } while (result > 0 && read < len); // Return the number of bytes read, or if nothing was read, return -1 // if an error occurred. if (read > 0) { ret += read; } else { ret = -1; q->setError(QFile::ReadError, qt_error_string(errno)); } } return ret;}/*! \reimp*/qint64 QFSFileEngine::readLine(char *data, qint64 maxlen){ Q_D(QFSFileEngine); // On Windows' stdlib implementation, the results of calling fread and // fwrite are undefined if not called either in sequence, or if preceded // with a call to fflush(). if (d->lastIOCommand != QFSFileEnginePrivate::IOReadCommand) { flush(); d->lastIOCommand = QFSFileEnginePrivate::IOReadCommand; } return d->nativeReadLine(data, maxlen);}/*! \internal*/qint64 QFSFileEnginePrivate::readLineFdFh(char *data, qint64 maxlen){ Q_Q(QFSFileEngine); if (!fh) return q->QAbstractFileEngine::readLine(data, maxlen); QT_OFF_T oldPos = 0;#ifdef Q_OS_WIN bool seq = q->isSequential(); if (!seq)#endif oldPos = QT_FTELL(fh); // 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), fh)) { q->setError(QFile::ReadError, qt_error_string(int(errno))); return 0; }#ifdef Q_OS_WIN if (seq) return qstrlen(data);#endif qint64 lineLength = QT_FTELL(fh) - oldPos; return lineLength > 0 ? lineLength : qstrlen(data);}/*! \reimp*/qint64 QFSFileEngine::write(const char *data, qint64 len){ Q_D(QFSFileEngine); // On Windows' stdlib implementation, the results of calling fread and // fwrite are undefined if not called either in sequence, or if preceded // with a call to fflush(). if (d->lastIOCommand != QFSFileEnginePrivate::IOWriteCommand) { flush(); d->lastIOCommand = QFSFileEnginePrivate::IOWriteCommand; } return d->nativeWrite(data, len);}/*! \internal*/qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len){ Q_Q(QFSFileEngine); qint64 result; qint64 written = 0; do { // Write blocks of 4k to avoid platform limitations (Windows commonly // bails out if you read or write too large blocks at once). qint64 bytesToWrite = qMin<qint64>(4096, len - written); if (fh) { do { // Buffered stdlib mode. result = qint64(fwrite(data + written, 1, size_t(bytesToWrite), fh)); } while (result == 0 && errno == EINTR); if (bytesToWrite > 0 && result == 0) result = -1; } else { do { // Unbuffered stdio mode. result = QT_WRITE(fd, data + written, bytesToWrite); } while (result == -1 && errno == EINTR); } if (result > 0) written += qint64(result); } while (written < len && result > 0); // If we read anything, return that with success. Otherwise, set an error, // and return the last return value. if (result > 0) return written; q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, qt_error_string(errno)); return result;}/*! \internal*/QAbstractFileEngine::Iterator *QFSFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames){ return new QFSFileEngineIterator(filters, filterNames);}/*! \internal*/QAbstractFileEngine::Iterator *QFSFileEngine::endEntryList(){ return 0;}/*! \internal*/QStringList QFSFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const{ return QAbstractFileEngine::entryList(filters, filterNames);}/*! \reimp*/bool QFSFileEngine::isSequential() const{ Q_D(const QFSFileEngine); if (d->is_sequential == 0) d->is_sequential = d->nativeIsSequential() ? 1 : 2; return d->is_sequential == 1;}/*! \internal*/bool QFSFileEnginePrivate::isSequentialFdFh() const{ if (!tried_stat) doStat(); if (could_stat) {#ifdef Q_OS_UNIX return (st.st_mode & S_IFMT) != S_IFREG; // ### WINDOWS!#endif } return true;}/*! \reimp*/bool QFSFileEngine::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output){ Q_D(QFSFileEngine); if (extension == AtEndExtension && d->fh && isSequential()) return feof(d->fh); Q_UNUSED(option); Q_UNUSED(output); return false;}/*! \reimp*/bool QFSFileEngine::supportsExtension(Extension extension) const{ Q_D(const QFSFileEngine); if (extension == AtEndExtension && d->fh && isSequential()) return true; if (extension == FastReadLineExtension && d->fh) return true; if (extension == FastReadLineExtension && d->fd != -1 && isSequential()) return true; return false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -