?? qresource.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 "qresource.h"#include "qresource_p.h"#include "qset.h"#include "qhash.h"#include "qmutex.h"#include "qdebug.h"#include "qlocale.h"#include "qglobal.h"#include "qvector.h"#include "qdatetime.h"#include "qbytearray.h"#include "qstringlist.h"#include <qshareddata.h>#include <qplatformdefs.h>#include "private/qabstractfileengine_p.h"//resource glueclass QResourceRoot{ enum Flags { Compressed = 0x01, Directory = 0x02 }; const uchar *tree, *names, *payloads; inline int findOffset(int node) const { return node * 14; } //sizeof each tree element int hash(int node) const; QString name(int node) const; short flags(int node) const;public: mutable QAtomic ref; inline QResourceRoot(): tree(0), names(0), payloads(0) {} inline QResourceRoot(const uchar *t, const uchar *n, const uchar *d) { setSource(t, n, d); } virtual ~QResourceRoot() { } int findNode(const QString &path, const QLocale &locale=QLocale()) const; inline bool isContainer(int node) const { return flags(node) & Directory; } inline bool isCompressed(int node) const { return flags(node) & Compressed; } const uchar *data(int node, qint64 *size) const; QStringList children(int node) const; virtual QString mappingRoot() const { return QString(); } bool mappingRootSubdir(const QString &path, QString *match=0) const; inline bool operator==(const QResourceRoot &other) const { return tree == other.tree && names == other.names && payloads == other.payloads; } inline bool operator!=(const QResourceRoot &other) const { return !operator==(other); } virtual bool isDynamicRoot() const { return false; }protected: inline void setSource(const uchar *t, const uchar *n, const uchar *d) { tree = t; names = n; payloads = d; }};Q_DECLARE_TYPEINFO(QResourceRoot, Q_MOVABLE_TYPE);Q_GLOBAL_STATIC_WITH_ARGS(QMutex, resourceMutex, (QMutex::Recursive))typedef QList<QResourceRoot*> ResourceList;Q_GLOBAL_STATIC(ResourceList, resourceList)Q_GLOBAL_STATIC(QStringList, resourceSearchPaths)/*! \class QResource \brief The QResource class provides an interface for reading directly from resources. \ingroup io \mainclass \reentrant \since 4.2 QResource is an object that represents a set of data (and possibly children) relating to a single resource entity. QResource gives direct access to the bytes in their raw format. In this way direct access allows reading data without buffer copying or indirection. Indirection is often useful when interacting with the resource entity as if it is a file, this can be achieved with QFile. The data and children behind a QResource are normally compiled into an application/library, but it is also possible to load a resource at runtime. When loaded at run time the resource file will be loaded as one big set of data and then given out in pieces via references into the resource tree. A QResource can either be loaded with an absolute path, either treated as a file system rooted with a \c{/} character, or in resource notation rooted with a \c{:} character. A relative resource can also be opened which will be found through the searchPaths(). A QResource that is representing a file will have data backing it, this data can possibly be compressed, in which case qUncompress() must be used to access the real data; this happens implicitly when accessed through a QFile. A QResource that is representing a directory will have only children and no data. \section1 Dynamic Resource Loading A resource can be left out of an application's binary and loaded when it is needed at run-time by using the registerResource() function. The resource file passed into registerResource() must be a binary resource as created by rcc. Further information about binary resources can be found in {The Qt Resource System} documentation. This can often be useful when loading a large set of application icons that may change based on a setting, or that can be edited by a user and later recreated. The resource is immediately loaded into memory, either as a result of a single file read operation, or as a memory mapped file. This approach can prove to be a significant performance gain as only a single file will be loaded, and pieces of data will be given out via the path requested in setFileName(). The unregisterResource() function removes a reference to a particular file. If there are QResources that currently reference resources related to the unregistered file, they will continue to be valid but the resource file itself will be removed from the resource roots, and thus no further QResource can be created pointing into this resource data. The resource itself will be unmapped from memory when the last QResource points into it. \sa {The Qt Resource System}, QFile, QDir, QFileInfo*/class QResourcePrivate {public: inline QResourcePrivate(QResource *_q) : q_ptr(_q) { clear(); } inline ~QResourcePrivate() { clear(); } void ensureInitialized() const; void ensureChildren() const; bool load(const QString &file); void clear(); QLocale locale; QString fileName, absoluteFilePath; QList<QResourceRoot*> related; uint container : 1; mutable uint compressed : 1; mutable qint64 size; mutable const uchar *data; mutable QStringList children; QResource *q_ptr; Q_DECLARE_PUBLIC(QResource)};voidQResourcePrivate::clear(){ absoluteFilePath.clear(); compressed = 0; data = 0; size = 0; children.clear(); container = 0; for(int i = 0; i < related.size(); ++i) { QResourceRoot *root = related.at(i); if(!root->ref.deref()) delete root; } related.clear();}boolQResourcePrivate::load(const QString &file){ related.clear(); QMutexLocker lock(resourceMutex()); const ResourceList *list = resourceList(); for(int i = 0; i < list->size(); ++i) { QResourceRoot *res = list->at(i); const int node = res->findNode(file); if(node != -1) { if(related.isEmpty()) { container = res->isContainer(node); if(!container) { data = res->data(node, &size); compressed = res->isCompressed(node); } else { data = 0; size = 0; compressed = 0; } } else if(res->isContainer(node) != container) { qWarning("QResourceInfo: Resource [%s] has both data and children!", file.toLatin1().constData()); } res->ref.ref(); related.append(res); } else if(res->mappingRootSubdir(file)) { container = true; data = 0; size = 0; compressed = 0; res->ref.ref(); related.append(res); } } return !related.isEmpty();}voidQResourcePrivate::ensureInitialized() const{ if(!related.isEmpty()) return; QResourcePrivate *that = const_cast<QResourcePrivate *>(this); if(fileName == QLatin1String(":")) that->fileName += QLatin1Char('/'); that->absoluteFilePath = fileName; if(!that->absoluteFilePath.startsWith(QLatin1Char(':'))) that->absoluteFilePath.prepend(QLatin1Char(':')); QString path = fileName; if(path.startsWith(QLatin1Char(':'))) path = path.mid(1); bool found = false; if(path.startsWith(QLatin1Char('/'))) { found = that->load(path); } else { QMutexLocker lock(resourceMutex()); QStringList searchPaths = *resourceSearchPaths(); searchPaths << QLatin1String(""); for(int i = 0; i < searchPaths.size(); ++i) { const QString searchPath(searchPaths.at(i) + QLatin1Char('/') + path); if(that->load(searchPath)) { found = true; that->absoluteFilePath = QLatin1Char(':') + searchPath; break; } } }}voidQResourcePrivate::ensureChildren() const{ ensureInitialized(); if(!children.isEmpty() || !container || related.isEmpty()) return; QString path = absoluteFilePath, k; if(path.startsWith(QLatin1Char(':'))) path = path.mid(1); QSet<QString> kids; for(int i = 0; i < related.size(); ++i) { QResourceRoot *res = related.at(i); if(res->mappingRootSubdir(path, &k) && !k.isEmpty()) { if(!kids.contains(k)) { children += k; kids.insert(k); } } else { const int node = res->findNode(path); if(node != -1) { QStringList related_children = res->children(node); for(int kid = 0; kid < related_children.size(); ++kid) { k = related_children.at(kid); if(!kids.contains(k)) { children += k; kids.insert(k); } } } } }}/*! Constructs a QResource pointing to \a file. \a locale is used to load a specific localization of a resource data. \sa QFileInfo, searchPaths(), setFileName(), setLocale()*/QResource::QResource(const QString &file, const QLocale &locale) : d_ptr(new QResourcePrivate(this)){ Q_D(QResource); d->fileName = file; d->locale = locale;}/*! Releases the resources of the QResource object.*/QResource::~QResource(){ delete d_ptr;}/*! Sets a QResource to only load the localization of resource to for \a locale. If a resource for the specific locale is not found then the C locale is used. \sa setFileName()*/void QResource::setLocale(const QLocale &locale){ Q_D(QResource); d->clear(); d->locale = locale;}/*! Returns the locale used to locate the data for the QResource.*/QLocale QResource::locale() const{ Q_D(const QResource); return d->locale;}/*! Sets a QResource to point to \a file. \a file can either be absolute, in which case it is opened directly, if relative then the file will be tried to be found in searchPaths(). \sa absoluteFilePath()*/void QResource::setFileName(const QString &file){ Q_D(QResource); d->clear(); d->fileName = file;}/*! Returns the full path to the file that this QResource represents as it was passed. \sa absoluteFilePath()*/QString QResource::fileName() const{ Q_D(const QResource); d->ensureInitialized(); return d->fileName;}/*! Returns the real path that this QResource represents, if the resource was found via the searchPaths() it will be indicated in the path. \sa fileName()*/QString QResource::absoluteFilePath() const{ Q_D(const QResource); d->ensureInitialized(); return d->absoluteFilePath;}/*! Returns true if the resource really exists in the resource heirarchy, false otherwise.*/bool QResource::isValid() const{ Q_D(const QResource); d->ensureInitialized(); return !d->related.isEmpty();}/*! \fn bool QResource::isFile() const Returns true if the resource represents a file and thus has data backing it, false if it represents a directory. \sa isDir()*//*! Returns true if the resource represents a file and the data backing it is in a compressed format, false otherwise. \sa data(), isFile()*/bool QResource::isCompressed() const{ Q_D(const QResource); d->ensureInitialized(); return d->compressed;}/*! Returns the size of the data backing the resource. \sa data(), isFile()*/qint64 QResource::size() const{ Q_D(const QResource); d->ensureInitialized(); return d->size;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -