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

📄 qfileinfo.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************** 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 "qplatformdefs.h"#include "qfileinfo.h"#include "qdatetime.h"#include "qabstractfileengine.h"#include "qfsfileengine_p.h"#include "qglobal.h"#include "qatomic.h"#include "qhash.h"#include "qdir.h"class QFileInfoPrivate{public:    QFileInfoPrivate(const QFileInfo *copy=0);    ~QFileInfoPrivate();    void initFileEngine(const QString &);    enum Access {        ReadAccess,        WriteAccess,        ExecuteAccess    };    bool hasAccess(Access access) const;    uint getFileFlags(QAbstractFileEngine::FileFlags) const;    QDateTime &getFileTime(QAbstractFileEngine::FileTime) const;    QString getFileName(QAbstractFileEngine::FileName) const;    enum { CachedFileFlags=0x01, CachedLinkTypeFlag=0x02, CachedBundleTypeFlag=0x04,           CachedMTime=0x10, CachedCTime=0x20, CachedATime=0x40,           CachedSize =0x08 };    struct Data {        inline Data()            : ref(1), fileEngine(0), cache_enabled(1)        { clear(); }        inline Data(const Data &copy)            : ref(1), fileEngine(QAbstractFileEngine::create(copy.fileName)),              fileName(copy.fileName), cache_enabled(copy.cache_enabled)        { clear(); }        inline ~Data() { delete fileEngine; }        inline void clear() {            fileNames.clear();            fileFlags = 0;            cachedFlags = 0;        }        mutable QAtomic ref;        QAbstractFileEngine *fileEngine;        mutable QString fileName;        mutable QHash<int, QString> fileNames;        mutable uint cachedFlags : 31;        mutable uint cache_enabled : 1;        mutable uint fileFlags;        mutable qint64 fileSize;        mutable QDateTime fileTimes[3];        inline bool getCachedFlag(uint c) const        { return cache_enabled ? (cachedFlags & c) : 0; }        inline void setCachedFlag(uint c)        { if (cache_enabled) cachedFlags |= c; }    } *data;    inline void reset() {        detach();        data->clear();    }    void detach();};QFileInfoPrivate::QFileInfoPrivate(const QFileInfo *copy){    if(copy) {        copy->d_func()->data->ref.ref();        data = copy->d_func()->data;    } else {        data = new QFileInfoPrivate::Data;        data->clear();    }}QFileInfoPrivate::~QFileInfoPrivate(){    if (!data->ref.deref())        delete data;    data = 0;}voidQFileInfoPrivate::initFileEngine(const QString &file){    detach();    delete data->fileEngine;    data->fileEngine = 0;    data->clear();    data->fileEngine = QAbstractFileEngine::create(file);    data->fileName = file;}bool QFileInfoPrivate::hasAccess(Access access) const{    if (!(data->fileEngine->fileFlags() & QAbstractFileEngine::LocalDiskFlag)) {        switch (access) {        case ReadAccess:            return getFileFlags(QAbstractFileEngine::ReadUserPerm);        case WriteAccess:            return getFileFlags(QAbstractFileEngine::WriteUserPerm);        case ExecuteAccess:            return getFileFlags(QAbstractFileEngine::ExeUserPerm);        default:            return false;        }    }    int mode = 0;    switch (access) {    case ReadAccess:        mode = R_OK;        break;    case WriteAccess:        mode = W_OK;        break;    case ExecuteAccess:        mode = X_OK;        break;    };#ifdef Q_OS_UNIX    return QT_ACCESS(QFile::encodeName(data->fileName).data(), mode) == 0;#endif#ifdef Q_OS_WIN    if ((access == ReadAccess && !getFileFlags(QAbstractFileEngine::ReadUserPerm))        || (access == WriteAccess && !getFileFlags(QAbstractFileEngine::WriteUserPerm))) {        return false;    }    if (access == ExecuteAccess)        return getFileFlags(QAbstractFileEngine::ExeUserPerm);    QT_WA( {        return ::_waccess((TCHAR *)QFSFileEnginePrivate::longFileName(data->fileName).utf16(), mode) == 0;    } , {        return QT_ACCESS(QFSFileEnginePrivate::win95Name(data->fileName), mode) == 0;    } );#endif    return false;}void QFileInfoPrivate::detach(){ qAtomicDetach(data); }QStringQFileInfoPrivate::getFileName(QAbstractFileEngine::FileName name) const{    if(data->cache_enabled && data->fileNames.contains((int)name))        return data->fileNames.value(name);    QString ret = data->fileEngine->fileName(name);    if(data->cache_enabled)        data->fileNames.insert((int)name, ret);    return ret;}uintQFileInfoPrivate::getFileFlags(QAbstractFileEngine::FileFlags request) const{    // We split the testing into tests for for LinkType, BundleType and the rest.    // In order to determine if a file is a symlink or not, we have to lstat().     // If we're not interested in that information, we might as well avoid one     // extra syscall. Bundle detecton on Mac can be slow, expecially on network    // paths, so we separate out that as well.    QAbstractFileEngine::FileFlags flags;    if (!data->getCachedFlag(CachedFileFlags)) {        QAbstractFileEngine::FileFlags req = QAbstractFileEngine::FileInfoAll;        req &= (~QAbstractFileEngine::LinkType);        req &= (~QAbstractFileEngine::BundleType);        flags = data->fileEngine->fileFlags(req);        data->setCachedFlag(CachedFileFlags);        data->fileFlags |= uint(flags);    } else {        flags = QAbstractFileEngine::FileFlags(data->fileFlags & request);    }    if (request & QAbstractFileEngine::LinkType) {        if (!data->getCachedFlag(CachedLinkTypeFlag)) {            QAbstractFileEngine::FileFlags linkflag;            linkflag = data->fileEngine->fileFlags(QAbstractFileEngine::LinkType);            data->setCachedFlag(CachedLinkTypeFlag);            data->fileFlags |= uint(linkflag);            flags |= linkflag;        }    }    if (request & QAbstractFileEngine::BundleType) {        if (!data->getCachedFlag(CachedBundleTypeFlag)) {            QAbstractFileEngine::FileFlags bundleflag;            bundleflag = data->fileEngine->fileFlags(QAbstractFileEngine::BundleType);            data->setCachedFlag(CachedBundleTypeFlag);            data->fileFlags |= uint(bundleflag);            flags |= bundleflag;        }    }    // no else branch    // if we had it cached, it was caught in the previous else branch    return flags & request;}QDateTime&QFileInfoPrivate::getFileTime(QAbstractFileEngine::FileTime request) const{    if(request == QAbstractFileEngine::CreationTime) {        if(data->getCachedFlag(CachedCTime))            return data->fileTimes[request];        data->setCachedFlag(CachedCTime);        return (data->fileTimes[request] = data->fileEngine->fileTime(request));    }    if(request == QAbstractFileEngine::ModificationTime) {        if(data->getCachedFlag(CachedMTime))            return data->fileTimes[request];        data->setCachedFlag(CachedMTime);        return (data->fileTimes[request] = data->fileEngine->fileTime(request));    }    if(request == QAbstractFileEngine::AccessTime) {        if(data->getCachedFlag(CachedATime))            return data->fileTimes[request];        data->setCachedFlag(CachedATime);        return (data->fileTimes[request] = data->fileEngine->fileTime(request));    }    return data->fileTimes[0]; //cannot really happen}//************* QFileInfo/*!    \class QFileInfo    \reentrant    \brief The QFileInfo class provides system-independent file information.    \ingroup io    \ingroup shared    QFileInfo provides information about a file's name and position    (path) in the file system, its access rights and whether it is a    directory or symbolic link, etc. The file's size and last    modified/read times are also available. QFileInfo can also be    used to obtain information about a Qt \l{resource    system}{resource}.    A QFileInfo can point to a file with either a relative or an    absolute file path. Absolute file paths begin with the directory    separator "/" (or with a drive specification on Windows). Relative    file names begin with a directory name or a file name and specify    a path relative to the current working directory. An example of an    absolute path is the string "/tmp/quartz". A relative path might    look like "src/fatlib". You can use the function isRelative() to    check whether a QFileInfo is using a relative or an absolute file    path. You can call the function makeAbsolute() to convert a    relative QFileInfo's path to an absolute path.    The file that the QFileInfo works on is set in the constructor or    later with setFile(). Use exists() to see if the file exists and    size() to get its size.    The file's type is obtained with isFile(), isDir() and    isSymLink(). The symLinkTarget() function provides the name of the file    the symlink points to.    On Unix (including Mac OS X), the symlink has the same size() has    the file it points to, because Unix handles symlinks    transparently; similarly, opening a symlink using QFile    effectively opens the link's target. For example:    \code        #ifdef Q_OS_UNIX        QFileInfo info1("/home/bob/bin/untabify");        info1.isSymLink();          // returns true        info1.absoluteFilePath();   // returns "/home/bob/bin/untabify"        info1.size();               // returns 56201        info1.symLinkTarget();      // returns "/opt/pretty++/bin/untabify"        QFileInfo info2(info1.symLinkTarget());        info1.isSymLink();          // returns false        info1.absoluteFilePath();   // returns "/opt/pretty++/bin/untabify"        info1.size();               // returns 56201        #endif    \endcode    On Windows, symlinks (shortcuts) are \c .lnk files. The reported    size() is that of the symlink (not the link's target), and    opening a symlink using QFile opens the \c .lnk file. For    example:    \code        #ifdef Q_OS_WIN        QFileInfo info1("C:\\Documents and Settings\\Bob\\untabify.lnk");        info1.isSymLink();          // returns true        info1.absoluteFilePath();   // returns "C:/Documents and Settings/Bob/untabify.lnk"        info1.size();               // returns 743        info1.symLinkTarget();      // returns "C:/Pretty++/untabify"        QFileInfo info2(info1.symLinkTarget());        info1.isSymLink();          // returns false        info1.absoluteFilePath();   // returns "C:/Pretty++/untabify"        info1.size();               // returns 63942        #endif    \endcode    Elements of the file's name can be extracted with path() and    fileName(). The fileName()'s parts can be extracted with    baseName() and extension(). QFileInfo objects to directories    created by Qt classes will not have a trailing file separator. If    you wish to use trailing separators in your own file info objects,    just append one to the file name given to the constructors or    setFile().    The file's dates are returned by created(), lastModified() and    lastRead(). Information about the file's access permissions is    obtained with isReadable(), isWritable() and isExecutable(). The    file's ownership is available from owner(), ownerId(), group() and    groupId(). You can examine a file's permissions and ownership in a    single statement using the permission() function.    \section1 Performance Issues    Some of QFileInfo's functions query the file system, but for    performance reasons, some functions only operate on the    file name itself. For example: To return the absolute path of    a relative file name, absolutePath() has to query the file system.    The path() function, however, can work on the file name directly,    and so it is faster.    \note To speed up performance, QFileInfo caches information about    the file.    To speed up performance, QFileInfo caches information about the    file. Because files can be changed by other users or programs, or    even by other parts of the same program, there is a function that    refreshes the file information: refresh(). If you want to switch    off a QFileInfo's caching and force it to access the file system    every time you request information from it call setCaching(false).    \sa QDir, QFile*//*!    Constructs an empty QFileInfo object.    Note that an empty QFileInfo object contain no file reference.    \sa setFile()*/QFileInfo::QFileInfo() : d_ptr(new QFileInfoPrivate()){}/*!    Constructs a new QFileInfo that gives information about the given    file. The \a file can also include an absolute or relative path.    \sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath()*/QFileInfo::QFileInfo(const QString &file) : d_ptr(new QFileInfoPrivate()){    d_ptr->initFileEngine(file);}/*!    Constructs a new QFileInfo that gives information about file \a    file.    If the \a file has a relative path, the QFileInfo will also have a    relative path.    \sa isRelative()*/QFileInfo::QFileInfo(const QFile &file) : d_ptr(new QFileInfoPrivate()){    d_ptr->initFileEngine(file.fileName());}/*!    Constructs a new QFileInfo that gives information about the given    \a file in the directory \a dir.    If \a dir has a relative path, the QFileInfo will also have a    relative path.    \sa isRelative()*/QFileInfo::QFileInfo(const QDir &dir, const QString &file) : d_ptr(new QFileInfoPrivate()){    d_ptr->initFileEngine(dir.filePath(file));}/*!    Constructs a new QFileInfo that is a copy of the given \a fileinfo.*/QFileInfo::QFileInfo(const QFileInfo &fileinfo) : d_ptr(new QFileInfoPrivate(&fileinfo)){}/*!    Destroys the QFileInfo and frees its resources.*/QFileInfo::~QFileInfo(){    delete d_ptr;    d_ptr = 0;}/*!    \fn bool QFileInfo::operator!=(const QFileInfo &fileinfo)    Returns true if this QFileInfo object refers to a different file    than the one specified by \a fileinfo; otherwise returns false.    \sa operator==()*//*!    \overload    \fn bool QFileInfo::operator!=(const QFileInfo &fileinfo) const*//*!    \overload*/boolQFileInfo::operator==(const QFileInfo &fileinfo) const{    Q_D(const QFileInfo);    // ### Qt 5: understand long and short file names on Windows    // ### (GetFullPathName()).    if(fileinfo.d_func()->data == d->data)        return true;    if(!d->data->fileEngine || !fileinfo.d_func()->data->fileEngine)        return false;    if(d->data->fileEngine->caseSensitive() != fileinfo.d_func()->data->fileEngine->caseSensitive())        return false;    if(fileinfo.size() == size()) { //if the size isn't the same...        QString file1 = absoluteFilePath(),                file2 = fileinfo.absoluteFilePath();        if(file1.length() == file2.length()) {            if(!fileinfo.d_func()->data->fileEngine->caseSensitive()) {                for(int i = 0; i < file1.length(); i++) {

⌨️ 快捷键说明

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