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"
41 class DirectoryArchive : public Archive
45 DirectoryArchive( const char* root ) : m_root( root ){
51 virtual ArchiveFile* openFile( const char* name ){
52 UnixPath path( m_root.c_str() );
53 path.push_filename( name );
54 DirectoryArchiveFile* file = new DirectoryArchiveFile( name, path.c_str() );
55 if ( !file->failed() ) {
61 virtual ArchiveTextFile* openTextFile( const char* name ){
62 UnixPath path( m_root.c_str() );
63 path.push_filename( name );
64 DirectoryArchiveTextFile* file = new DirectoryArchiveTextFile( name, path.c_str() );
65 if ( !file->failed() ) {
71 virtual bool containsFile( const char* name ){
72 UnixPath path( m_root.c_str() );
73 path.push_filename( name );
74 return file_readable( path.c_str() );
76 virtual void forEachFile( VisitorFunc visitor, const char* root ){
77 std::vector<Directory*> dirs;
78 UnixPath path( m_root.c_str() );
80 dirs.push_back( directory_open( path.c_str() ) );
82 while ( !dirs.empty() && directory_good( dirs.back() ) )
84 const char* name = directory_read_and_increment( dirs.back() );
87 directory_close( dirs.back() );
91 else if ( !string_equal( name, "." ) && !string_equal( name, ".." ) ) {
92 path.push_filename( name );
94 bool is_directory = file_is_directory( path.c_str() );
96 if ( !is_directory ) {
97 visitor.file( path_make_relative( path.c_str(), m_root.c_str() ) );
102 if ( is_directory ) {
105 if ( !visitor.directory( path_make_relative( path.c_str(), m_root.c_str() ), dirs.size() ) ) {
106 dirs.push_back( directory_open( path.c_str() ) );
117 Archive* OpenArchive( const char* name ){
118 return new DirectoryArchive( name );
125 class TestVisitor : public Archive::IVisitor
128 virtual void visit( const char* name ){
134 Archive* archive = OpenArchive( "d:/quake/id1/" );
135 ArchiveFile* file = archive->openFile( "quake101.wad" );
138 file->getInputStream().read( buffer, 1024 );
142 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 0 ), "" );
146 Archive* archive = OpenArchive( "d:/gtkradiant_root/baseq3/" );
148 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 2 ), "" );
149 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFiles, 1 ), "textures" );
150 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eDirectories, 1 ), "textures" );
151 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 1 ), "textures" );