Timings seem to be _really_ right this time.
[scummvm-innocent.git] / common / archive.cpp
blob86ae5b67951b5f8478efc95ee208fa1ebdfe835e
1 /* ScummVM - Graphic Adventure Engine
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $URL$
22 * $Id$
26 #include "common/archive.h"
27 #include "common/fs.h"
28 #include "common/util.h"
29 #include "common/system.h"
31 namespace Common {
33 GenericArchiveMember::GenericArchiveMember(String name, Archive *parent)
34 : _parent(parent), _name(name) {
37 String GenericArchiveMember::getName() const {
38 return _name;
41 SeekableReadStream *GenericArchiveMember::createReadStream() const {
42 return _parent->createReadStreamForMember(_name);
46 int Archive::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
47 // Get all "names" (TODO: "files" ?)
48 ArchiveMemberList allNames;
49 listMembers(allNames);
51 int matches = 0;
53 // need to match lowercase key
54 String lowercasePattern = pattern;
55 lowercasePattern.toLowercase();
57 ArchiveMemberList::iterator it = allNames.begin();
58 for ( ; it != allNames.end(); ++it) {
59 if ((*it)->getName().matchString(lowercasePattern, true)) {
60 list.push_back(*it);
61 matches++;
65 return matches;
70 SearchSet::ArchiveNodeList::iterator SearchSet::find(const String &name) {
71 ArchiveNodeList::iterator it = _list.begin();
72 for ( ; it != _list.end(); ++it) {
73 if (it->_name == name)
74 break;
76 return it;
79 SearchSet::ArchiveNodeList::const_iterator SearchSet::find(const String &name) const {
80 ArchiveNodeList::const_iterator it = _list.begin();
81 for ( ; it != _list.end(); ++it) {
82 if (it->_name == name)
83 break;
85 return it;
89 Keep the nodes sorted according to descending priorities.
90 In case two or node nodes have the same priority, insertion
91 order prevails.
93 void SearchSet::insert(const Node &node) {
94 ArchiveNodeList::iterator it = _list.begin();
95 for ( ; it != _list.end(); ++it) {
96 if (it->_priority < node._priority)
97 break;
99 _list.insert(it, node);
102 void SearchSet::add(const String &name, Archive *archive, int priority, bool autoFree) {
103 if (find(name) == _list.end()) {
104 Node node(priority, name, archive, autoFree);
105 insert(node);
106 } else {
107 if (autoFree)
108 delete archive;
109 warning("SearchSet::add: archive '%s' already present", name.c_str());
114 void SearchSet::addDirectory(const String &name, const String &directory, int priority, int depth, bool flat) {
115 FSNode dir(directory);
116 addDirectory(name, dir, priority, depth, flat);
119 void SearchSet::addDirectory(const String &name, const FSNode &dir, int priority, int depth, bool flat) {
120 if (!dir.exists() || !dir.isDirectory())
121 return;
123 add(name, new FSDirectory(dir, depth, flat), priority);
127 void SearchSet::remove(const String &name) {
128 ArchiveNodeList::iterator it = find(name);
129 if (it != _list.end()) {
130 if (it->_autoFree)
131 delete it->_arc;
132 _list.erase(it);
136 bool SearchSet::hasArchive(const String &name) const {
137 return (find(name) != _list.end());
140 void SearchSet::clear() {
141 for (ArchiveNodeList::iterator i = _list.begin(); i != _list.end(); ++i) {
142 if (i->_autoFree)
143 delete i->_arc;
146 _list.clear();
149 void SearchSet::setPriority(const String &name, int priority) {
150 ArchiveNodeList::iterator it = find(name);
151 if (it == _list.end()) {
152 warning("SearchSet::setPriority: archive '%s' is not present", name.c_str());
153 return;
156 if (priority == it->_priority)
157 return;
159 Node node(*it);
160 _list.erase(it);
161 node._priority = priority;
162 insert(node);
165 bool SearchSet::hasFile(const String &name) {
166 if (name.empty())
167 return false;
169 ArchiveNodeList::iterator it = _list.begin();
170 for ( ; it != _list.end(); ++it) {
171 if (it->_arc->hasFile(name))
172 return true;
175 return false;
178 int SearchSet::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
179 int matches = 0;
181 ArchiveNodeList::iterator it = _list.begin();
182 for ( ; it != _list.end(); ++it)
183 matches += it->_arc->listMatchingMembers(list, pattern);
185 return matches;
188 int SearchSet::listMembers(ArchiveMemberList &list) {
189 int matches = 0;
191 ArchiveNodeList::iterator it = _list.begin();
192 for ( ; it != _list.end(); ++it)
193 matches += it->_arc->listMembers(list);
195 return matches;
198 ArchiveMemberPtr SearchSet::getMember(const String &name) {
199 if (name.empty())
200 return ArchiveMemberPtr();
202 ArchiveNodeList::iterator it = _list.begin();
203 for ( ; it != _list.end(); ++it) {
204 if (it->_arc->hasFile(name))
205 return it->_arc->getMember(name);
208 return ArchiveMemberPtr();
211 SeekableReadStream *SearchSet::createReadStreamForMember(const String &name) const {
212 if (name.empty())
213 return 0;
215 ArchiveNodeList::const_iterator it = _list.begin();
216 for ( ; it != _list.end(); ++it) {
217 SeekableReadStream *stream = it->_arc->createReadStreamForMember(name);
218 if (stream)
219 return stream;
222 return 0;
226 DECLARE_SINGLETON(SearchManager);
228 SearchManager::SearchManager() {
229 clear(); // Force a reset
232 void SearchManager::clear() {
233 SearchSet::clear();
235 // Always keep system specific archives in the SearchManager.
236 // But we give them a lower priority than the default priority (which is 0),
237 // so that archives added by client code are searched first.
238 if (g_system)
239 g_system->addSysArchivesToSearchSet(*this, -1);
241 // Add the current dir as a very last resort.
242 // See also bug #2137680.
243 addDirectory(".", ".", -2);
246 } // namespace Common