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

📄 qfsfileengine.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qfsfileengine_p.h"#include "qfsfileengine_iterator_p.h"#include "qdatetime.h"#include "qdiriterator.h"#include <errno.h>#include <stdio.h>#ifdef Q_OS_WIN#  ifndef S_ISREG#    define S_ISREG(x)   (((x) & S_IFMT) == S_IFREG)#  endif#  ifndef S_ISCHR#    define S_ISCHR(x)   (((x) & S_IFMT) == S_IFCHR)#  endif#  ifndef S_ISFIFO#    define S_ISFIFO(x) false#  endif#  ifndef S_ISSOCK#    define S_ISSOCK(x) false#  endif#  ifndef INVALID_FILE_ATTRIBUTES#    define INVALID_FILE_ATTRIBUTES (DWORD (-1))#  endif#endif/*! \class QFSFileEngine    \brief The QFSFileEngine class implements Qt's default file engine.    \since 4.1    This class is part of the file engine framework in Qt. If you only want to    access files or directories, use QFile, QFileInfo or QDir instead.    QFSFileEngine is the default file engine for accessing regular files. It    is provided for convenience; by subclassing this class, you can alter its    behavior slightly, without having to write a complete QAbstractFileEngine    subclass. To install your custom file engine, you must also subclass    QAbstractFileEngineHandler and create an instance of your handler.    It can also be useful to create a QFSFileEngine object directly if you    need to use the local file system inside QAbstractFileEngine::create(), in    order to avoid recursion (as higher-level classes tend to call    QAbstractFileEngine::create()).*///**************** QFSFileEnginePrivateQFSFileEnginePrivate::QFSFileEnginePrivate() : QAbstractFileEnginePrivate(){    init();}/*!    \internal*/void QFSFileEnginePrivate::init(){    is_sequential = 0;    tried_stat = 0;#ifdef Q_OS_UNIX    need_lstat = 1;    is_link = 0;#endif    openMode = QIODevice::NotOpen;    fd = -1;    fh = 0;    lastIOCommand = IOFlushCommand;    lastFlushFailed = false;    closeFileHandle = false;#ifdef Q_OS_WIN    fileAttrib = INVALID_FILE_ATTRIBUTES;    fileHandle = INVALID_HANDLE_VALUE;#endif}/*!    Constructs a QFSFileEngine for the file name \a file.*/QFSFileEngine::QFSFileEngine(const QString &file) : QAbstractFileEngine(*new QFSFileEnginePrivate){    Q_D(QFSFileEngine);    d->filePath = QDir::fromNativeSeparators(file);    d->nativeInitFileName();}/*!    Constructs a QFSFileEngine.*/QFSFileEngine::QFSFileEngine() : QAbstractFileEngine(*new QFSFileEnginePrivate){}/*!    \internal*/QFSFileEngine::QFSFileEngine(QFSFileEnginePrivate &dd)    : QAbstractFileEngine(dd){}/*!    Destructs the QFSFileEngine.*/QFSFileEngine::~QFSFileEngine(){    Q_D(QFSFileEngine);    if (d->closeFileHandle) {        if (d->fh) {            int ret;            do {                ret = fclose(d->fh);            } while (ret == EOF && errno == EINTR);        } else if (d->fd != -1) {            int ret;            do {                ret = QT_CLOSE(d->fd);            } while (ret == -1 && errno == EINTR);        }    }}/*!    \reimp*/void QFSFileEngine::setFileName(const QString &file){    Q_D(QFSFileEngine);    d->init();    d->filePath = QDir::fromNativeSeparators(file);    d->nativeInitFileName();}/*!    \reimp*/bool QFSFileEngine::open(QIODevice::OpenMode openMode){    Q_D(QFSFileEngine);    if (d->filePath.isEmpty()) {        qWarning("QFSFileEngine::open: No file name specified");        setError(QFile::OpenError, QLatin1String("No file name specified"));        return false;    }    // Append implies WriteOnly.    if (openMode & QFile::Append)        openMode |= QFile::WriteOnly;    // WriteOnly implies Truncate if neither ReadOnly nor Append are sent.    if ((openMode & QFile::WriteOnly) && !(openMode & (QFile::ReadOnly | QFile::Append)))        openMode |= QFile::Truncate;    d->openMode = openMode;    d->lastFlushFailed = false;    d->tried_stat = 0;    d->fh = 0;    d->fd = -1;    return d->nativeOpen(openMode);}/*!    Opens the file handle \a fh in \a openMode mode. Returns true on    success; otherwise returns false.*/bool QFSFileEngine::open(QIODevice::OpenMode openMode, FILE *fh){    Q_D(QFSFileEngine);    // Append implies WriteOnly.    if (openMode & QFile::Append)        openMode |= QFile::WriteOnly;    // WriteOnly implies Truncate if neither ReadOnly nor Append are sent.    if ((openMode & QFile::WriteOnly) && !(openMode & (QFile::ReadOnly | QFile::Append)))        openMode |= QFile::Truncate;    d->openMode = openMode;    d->lastFlushFailed = false;    d->closeFileHandle = false;    d->nativeFilePath.clear();    d->filePath.clear();    d->tried_stat = 0;    d->fd = -1;    return d->openFh(openMode, fh);}/*!    Opens the file handle \a fh using the open mode \a flags.*/bool QFSFileEnginePrivate::openFh(QIODevice::OpenMode openMode, FILE *fh){    Q_Q(QFSFileEngine);    this->fh = fh;    fd = -1;    // Seek to the end when in Append mode.    if (openMode & QIODevice::Append) {        int ret;        do {            ret = QT_FSEEK(fh, 0, SEEK_END);        } while (ret == -1 && errno == EINTR);        if (ret == -1) {            q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError,                        qt_error_string(int(errno)));            return false;        }    }    return true;}/*!    Opens the file descriptor \a fd in \a openMode mode. Returns true    on success; otherwise returns false.*/bool QFSFileEngine::open(QIODevice::OpenMode openMode, int fd){    Q_D(QFSFileEngine);    // Append implies WriteOnly.    if (openMode & QFile::Append)        openMode |= QFile::WriteOnly;    // WriteOnly implies Truncate if neither ReadOnly nor Append are sent.    if ((openMode & QFile::WriteOnly) && !(openMode & (QFile::ReadOnly | QFile::Append)))        openMode |= QFile::Truncate;    d->lastFlushFailed = false;    d->closeFileHandle = false;    d->nativeFilePath.clear();    d->filePath.clear();    d->fh = 0;    d->fd = -1;    d->tried_stat = 0;    return d->openFd(openMode, fd);}/*!    Opens the file descriptor \a fd to the file engine, using the open mode \a    flags.*/bool QFSFileEnginePrivate::openFd(QIODevice::OpenMode openMode, int fd){    Q_Q(QFSFileEngine);    this->fd = fd;    fh = 0;    // Seek to the end when in Append mode.    if (openMode & QFile::Append) {        int ret;        do {            ret = QT_LSEEK(fd, 0, SEEK_END);        } while (ret == -1 && errno == EINTR);        if (ret == -1) {            q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError,                        qt_error_string(int(errno)));            return false;        }    }    return true;}/*!    \reimp*/bool QFSFileEngine::close(){    Q_D(QFSFileEngine);    d->openMode = QIODevice::NotOpen;    return d->nativeClose();}/*!    \internal*/bool QFSFileEnginePrivate::closeFdFh(){    Q_Q(QFSFileEngine);    if (fd == -1 && !fh)        return false;    // Flush the file if it's buffered, and if the last flush didn't fail.    bool flushed = !fh || (!lastFlushFailed && q->flush());    bool closed = true;    tried_stat = 0;    // Close the file if we created the handle.    if (closeFileHandle) {        int ret;        do {            if (fh) {                // Close buffered file.                ret = fclose(fh) != 0 ? -1 : 0;            } else {                // Close unbuffered file.                ret = QT_CLOSE(fd);            }        } while (ret == -1 && errno == EINTR);        // We must reset these guys regardless; calling close again after a        // failed close causes crashes on some systems.        fh = 0;        fd = -1;        closed = (ret == 0);    }    // Report errors.    if (!flushed || !closed) {        if (flushed) {            // If not flushed, we want the flush error to fall through.            q->setError(QFile::UnspecifiedError, qt_error_string(errno));        }        return false;    }    return true;}/*!    \reimp*/bool QFSFileEngine::flush(){    Q_D(QFSFileEngine);    if ((d->openMode & QIODevice::WriteOnly) == 0) {        // Nothing in the write buffers, so flush succeeds in doing        // nothing.        return true;    }    return d->nativeFlush();}/*!    \internal*/bool QFSFileEnginePrivate::flushFh(){    Q_Q(QFSFileEngine);    // Never try to flush again if the last flush failed. Otherwise you can    // get crashes on some systems (AIX).    if (lastFlushFailed)        return false;    int ret = fflush(fh);    lastFlushFailed = (ret != 0);    lastIOCommand = QFSFileEnginePrivate::IOFlushCommand;    if (ret != 0) {

⌨️ 快捷键说明

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