2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant 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 GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "idatastream.h"
31 #include "stream/filestream.h"
32 #include "stream/textfilestream.h"
33 #include "string/string.h"
37 #include "archivelib.h"
43 class DirectoryArchive : public Archive
47 DirectoryArchive(const char* root) : m_root(root)
55 virtual ArchiveFile* openFile(const char* name)
57 UnixPath path(m_root.c_str());
58 path.push_filename(name);
59 DirectoryArchiveFile* file = new DirectoryArchiveFile(name, path.c_str());
67 virtual ArchiveTextFile* openTextFile(const char* name)
69 UnixPath path(m_root.c_str());
70 path.push_filename(name);
71 DirectoryArchiveTextFile* file = new DirectoryArchiveTextFile(name, path.c_str());
79 virtual bool containsFile(const char* name)
81 UnixPath path(m_root.c_str());
82 path.push_filename(name);
83 return file_readable(path.c_str());
85 virtual void forEachFile(VisitorFunc visitor, const char* root)
87 std::vector<Directory*> dirs;
88 UnixPath path(m_root.c_str());
90 dirs.push_back(directory_open(path.c_str()));
92 while(!dirs.empty() && directory_good(dirs.back()))
94 const char* name = directory_read_and_increment(dirs.back());
98 directory_close(dirs.back());
102 else if(!string_equal(name, ".") && !string_equal(name, ".."))
104 path.push_filename(name);
106 bool is_directory = file_is_directory(path.c_str());
109 visitor.file(path_make_relative(path.c_str(), m_root.c_str()));
117 if(!visitor.directory(path_make_relative(path.c_str(), m_root.c_str()), dirs.size()))
118 dirs.push_back(directory_open(path.c_str()));
127 Archive* OpenArchive(const char* name)
129 return new DirectoryArchive(name);
136 class TestVisitor : public Archive::IVisitor
139 virtual void visit(const char* name)
147 Archive* archive = OpenArchive("d:/quake/id1/");
148 ArchiveFile* file = archive->openFile("quake101.wad");
152 file->getInputStream().read(buffer, 1024);
156 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 0), "");
161 Archive* archive = OpenArchive("d:/gtkradiant_root/baseq3/");
163 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 2), "");
164 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFiles, 1), "textures");
165 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eDirectories, 1), "textures");
166 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "textures");