📄 qfileinfo.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 "qglobal.h"#include "qatomic.h"#include "qhash.h"#include "qdir.h"class QFileInfoPrivate{public: QFileInfoPrivate(const QFileInfo *copy=0); ~QFileInfoPrivate(); void initFileEngine(const QString &); uint getFileFlags(QAbstractFileEngine::FileFlags) const; QDateTime &getFileTime(QAbstractFileEngine::FileTime) const; QString getFileName(QAbstractFileEngine::FileName) const; enum { CachedFileFlags=0x01, CachedLinkTypeFlag=0x02, CachedMTime=0x10, CachedCTime=0x20, CachedATime=0x40, CachedSize =0x08 }; struct Data { inline Data() : ref(1), fileEngine(0), cache_enabled(1) { clear(); } inline Data(const Data ©) : 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;}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 for LinkType and the rest because, 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. QAbstractFileEngine::FileFlags flags; if (!data->getCachedFlag(CachedFileFlags)) { QAbstractFileEngine::FileFlags req = QAbstractFileEngine::FileInfoAll; req &= (~QAbstractFileEngine::LinkType); 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; } } // 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 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. 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). The file's type is obtained with isFile(), isDir() and isSymLink(). The readLink() 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.readLink(); // returns "/opt/pretty++/bin/untabify" QFileInfo info2(info1.readLink()); 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.readLink(); // returns "C:\\Pretty++\\untabify" QFileInfo info2(info1.readLink()); 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(). 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. \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); 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++) { if(file1.at(i).toLower() != file2.at(i).toLower()) return false; } return true; } return (file1 == file2); } } return false;}/*! Returns true if this QFileInfo object refers to a file in the same location as \a fileinfo; otherwise returns false. Note that the result of comparing two empty QFileInfo objects, containing no file references, is undefined. \warning This will not compare two different symbolic links pointing to the same file. \sa operator!=()*/bool QFileInfo::operator==(const QFileInfo &fileinfo){ return const_cast<const QFileInfo *>(this)->operator==(fileinfo);}/*! Makes a copy of the given \a fileinfo and assigns it to this QFileInfo.*/QFileInfo &QFileInfo::operator=(const QFileInfo &fileinfo){ Q_D(QFileInfo); qAtomicAssign(d->data, fileinfo.d_func()->data); return *this;}/*! Sets the file that the QFileInfo provides information about to \a file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -