2008-08-31 13:58:17 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/archive.h"
|
|
|
|
#include "common/fs.h"
|
|
|
|
#include "common/util.h"
|
2008-09-27 18:32:01 +00:00
|
|
|
#include "common/system.h"
|
2008-08-31 13:58:17 +00:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2008-10-03 16:57:40 +00:00
|
|
|
GenericArchiveMember::GenericArchiveMember(String name, Archive *parent)
|
|
|
|
: _parent(parent), _name(name) {
|
|
|
|
}
|
|
|
|
|
|
|
|
String GenericArchiveMember::getName() const {
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
2009-01-23 04:36:18 +00:00
|
|
|
SeekableReadStream *GenericArchiveMember::createReadStream() const {
|
2009-01-23 04:45:44 +00:00
|
|
|
return _parent->createReadStreamForMember(_name);
|
2008-10-03 16:57:40 +00:00
|
|
|
}
|
|
|
|
|
2008-08-31 13:58:17 +00:00
|
|
|
|
2008-10-03 16:57:40 +00:00
|
|
|
int Archive::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
|
2008-09-05 18:24:41 +00:00
|
|
|
// Get all "names" (TODO: "files" ?)
|
2008-10-03 16:57:40 +00:00
|
|
|
ArchiveMemberList allNames;
|
|
|
|
listMembers(allNames);
|
2008-09-05 18:24:41 +00:00
|
|
|
|
|
|
|
int matches = 0;
|
|
|
|
|
|
|
|
// need to match lowercase key
|
|
|
|
String lowercasePattern = pattern;
|
|
|
|
lowercasePattern.toLowercase();
|
|
|
|
|
2008-10-03 16:57:40 +00:00
|
|
|
ArchiveMemberList::iterator it = allNames.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != allNames.end(); ++it) {
|
2009-02-15 18:45:53 +00:00
|
|
|
if ((*it)->getName().matchString(lowercasePattern, true)) {
|
2008-09-05 18:24:41 +00:00
|
|
|
list.push_back(*it);
|
|
|
|
matches++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:58:17 +00:00
|
|
|
|
|
|
|
|
2009-03-09 22:26:02 +00:00
|
|
|
SearchSet::ArchiveNodeList::iterator SearchSet::find(const String &name) {
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = _list.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != _list.end(); ++it) {
|
2008-11-21 17:49:19 +00:00
|
|
|
if (it->_name == name)
|
2008-08-31 13:58:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2009-03-09 22:26:02 +00:00
|
|
|
SearchSet::ArchiveNodeList::const_iterator SearchSet::find(const String &name) const {
|
|
|
|
ArchiveNodeList::const_iterator it = _list.begin();
|
|
|
|
for ( ; it != _list.end(); ++it) {
|
|
|
|
if (it->_name == name)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:58:17 +00:00
|
|
|
/*
|
|
|
|
Keep the nodes sorted according to descending priorities.
|
|
|
|
In case two or node nodes have the same priority, insertion
|
|
|
|
order prevails.
|
|
|
|
*/
|
|
|
|
void SearchSet::insert(const Node &node) {
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = _list.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != _list.end(); ++it) {
|
2008-11-21 17:49:19 +00:00
|
|
|
if (it->_priority < node._priority)
|
2008-08-31 13:58:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_list.insert(it, node);
|
|
|
|
}
|
|
|
|
|
2008-11-07 13:42:05 +00:00
|
|
|
void SearchSet::add(const String &name, Archive *archive, int priority, bool autoFree) {
|
2008-08-31 13:58:17 +00:00
|
|
|
if (find(name) == _list.end()) {
|
2008-10-22 17:44:12 +00:00
|
|
|
Node node(priority, name, archive, autoFree);
|
2008-08-31 13:58:17 +00:00
|
|
|
insert(node);
|
|
|
|
} else {
|
2008-11-07 13:42:05 +00:00
|
|
|
if (autoFree)
|
|
|
|
delete archive;
|
2008-08-31 13:58:17 +00:00
|
|
|
warning("SearchSet::add: archive '%s' already present", name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-12-27 17:16:48 +00:00
|
|
|
void SearchSet::addDirectory(const String &name, const String &directory, int priority, int depth) {
|
|
|
|
FSNode dir(directory);
|
|
|
|
addDirectory(name, dir, priority, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchSet::addDirectory(const String &name, const FSNode &dir, int priority, int depth) {
|
|
|
|
if (!dir.exists() || !dir.isDirectory())
|
|
|
|
return;
|
|
|
|
|
|
|
|
add(name, new FSDirectory(dir, depth), priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 13:42:05 +00:00
|
|
|
void SearchSet::remove(const String &name) {
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = find(name);
|
2008-08-31 13:58:17 +00:00
|
|
|
if (it != _list.end()) {
|
2008-10-22 17:44:12 +00:00
|
|
|
if (it->_autoFree)
|
|
|
|
delete it->_arc;
|
2008-08-31 13:58:17 +00:00
|
|
|
_list.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-06 22:09:34 +00:00
|
|
|
bool SearchSet::hasArchive(const String &name) const {
|
|
|
|
return (find(name) != _list.end());
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:58:17 +00:00
|
|
|
void SearchSet::clear() {
|
2008-12-15 12:55:13 +00:00
|
|
|
for (ArchiveNodeList::iterator i = _list.begin(); i != _list.end(); ++i) {
|
2008-10-22 17:44:12 +00:00
|
|
|
if (i->_autoFree)
|
|
|
|
delete i->_arc;
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:58:17 +00:00
|
|
|
_list.clear();
|
|
|
|
}
|
|
|
|
|
2008-11-07 13:42:05 +00:00
|
|
|
void SearchSet::setPriority(const String &name, int priority) {
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = find(name);
|
2008-08-31 13:58:17 +00:00
|
|
|
if (it == _list.end()) {
|
|
|
|
warning("SearchSet::setPriority: archive '%s' is not present", name.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-21 17:49:19 +00:00
|
|
|
if (priority == it->_priority)
|
2008-08-31 13:58:17 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
Node node(*it);
|
|
|
|
_list.erase(it);
|
|
|
|
node._priority = priority;
|
|
|
|
insert(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchSet::hasFile(const String &name) {
|
2008-11-21 17:49:19 +00:00
|
|
|
if (name.empty())
|
2008-08-31 13:58:17 +00:00
|
|
|
return false;
|
|
|
|
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = _list.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != _list.end(); ++it) {
|
2008-11-21 17:49:19 +00:00
|
|
|
if (it->_arc->hasFile(name))
|
2008-08-31 13:58:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-03 16:57:40 +00:00
|
|
|
int SearchSet::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
|
2008-08-31 13:58:17 +00:00
|
|
|
int matches = 0;
|
|
|
|
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = _list.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != _list.end(); ++it)
|
2008-11-21 17:49:19 +00:00
|
|
|
matches += it->_arc->listMatchingMembers(list, pattern);
|
2008-08-31 13:58:17 +00:00
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
2008-10-03 16:57:40 +00:00
|
|
|
int SearchSet::listMembers(ArchiveMemberList &list) {
|
2008-09-18 08:19:00 +00:00
|
|
|
int matches = 0;
|
|
|
|
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = _list.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != _list.end(); ++it)
|
2008-11-21 17:49:19 +00:00
|
|
|
matches += it->_arc->listMembers(list);
|
2008-09-18 08:19:00 +00:00
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
2008-11-01 12:49:29 +00:00
|
|
|
ArchiveMemberPtr SearchSet::getMember(const String &name) {
|
2008-11-21 17:49:19 +00:00
|
|
|
if (name.empty())
|
2008-11-01 12:49:29 +00:00
|
|
|
return ArchiveMemberPtr();
|
|
|
|
|
2008-12-15 12:55:13 +00:00
|
|
|
ArchiveNodeList::iterator it = _list.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != _list.end(); ++it) {
|
2008-11-21 17:49:19 +00:00
|
|
|
if (it->_arc->hasFile(name))
|
|
|
|
return it->_arc->getMember(name);
|
2008-11-01 12:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ArchiveMemberPtr();
|
|
|
|
}
|
|
|
|
|
2009-01-23 04:45:44 +00:00
|
|
|
SeekableReadStream *SearchSet::createReadStreamForMember(const String &name) const {
|
2008-11-21 17:49:19 +00:00
|
|
|
if (name.empty())
|
2008-09-03 17:07:13 +00:00
|
|
|
return 0;
|
2008-08-31 13:58:17 +00:00
|
|
|
|
2009-03-09 22:26:02 +00:00
|
|
|
ArchiveNodeList::const_iterator it = _list.begin();
|
2008-12-27 17:16:48 +00:00
|
|
|
for ( ; it != _list.end(); ++it) {
|
2009-02-22 16:48:02 +00:00
|
|
|
SeekableReadStream *stream = it->_arc->createReadStreamForMember(name);
|
|
|
|
if (stream)
|
|
|
|
return stream;
|
2008-08-31 13:58:17 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 17:07:13 +00:00
|
|
|
return 0;
|
2008-08-31 13:58:17 +00:00
|
|
|
}
|
|
|
|
|
2008-09-11 13:24:01 +00:00
|
|
|
|
|
|
|
DECLARE_SINGLETON(SearchManager);
|
|
|
|
|
2008-09-27 18:32:01 +00:00
|
|
|
SearchManager::SearchManager() {
|
|
|
|
clear(); // Force a reset
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchManager::clear() {
|
|
|
|
SearchSet::clear();
|
|
|
|
|
|
|
|
// Always keep system specific archives in the SearchManager.
|
|
|
|
// But we give them a lower priority than the default priority (which is 0),
|
|
|
|
// so that archives added by client code are searched first.
|
2009-01-29 22:09:06 +00:00
|
|
|
if (g_system)
|
|
|
|
g_system->addSysArchivesToSearchSet(*this, -1);
|
2008-10-02 17:52:29 +00:00
|
|
|
|
|
|
|
// Add the current dir as a very last resort.
|
|
|
|
// See also bug #2137680.
|
2008-10-22 17:08:17 +00:00
|
|
|
addDirectory(".", ".", -2);
|
2008-09-27 18:32:01 +00:00
|
|
|
}
|
2008-09-11 13:24:01 +00:00
|
|
|
|
2008-08-31 13:58:17 +00:00
|
|
|
} // namespace Common
|