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 ){
53 virtual ArchiveFile* openFile( const char* name ){
54 UnixPath path( m_root.c_str() );
55 path.push_filename( name );
56 DirectoryArchiveFile* file = new DirectoryArchiveFile( name, path.c_str() );
57 if ( !file->failed() ) {
63 virtual ArchiveTextFile* openTextFile( const char* name ){
64 UnixPath path( m_root.c_str() );
65 path.push_filename( name );
66 DirectoryArchiveTextFile* file = new DirectoryArchiveTextFile( name, path.c_str() );
67 if ( !file->failed() ) {
73 virtual bool containsFile( const char* name ){
74 UnixPath path( m_root.c_str() );
75 path.push_filename( name );
76 return file_readable( path.c_str() );
78 virtual void forEachFile( VisitorFunc visitor, const char* root ){
79 std::vector<Directory*> dirs;
80 UnixPath path( m_root.c_str() );
82 dirs.push_back( directory_open( path.c_str() ) );
84 while ( !dirs.empty() && directory_good( dirs.back() ) )
86 const char* name = directory_read_and_increment( dirs.back() );
89 directory_close( dirs.back() );
93 else if ( !string_equal( name, "." ) && !string_equal( name, ".." ) ) {
94 path.push_filename( name );
96 bool is_directory = file_is_directory( path.c_str() );
98 if ( !is_directory ) {
99 visitor.file( path_make_relative( path.c_str(), m_root.c_str() ) );
104 if ( is_directory ) {
107 if ( !visitor.directory( path_make_relative( path.c_str(), m_root.c_str() ), dirs.size() ) ) {
108 dirs.push_back( directory_open( path.c_str() ) );
119 Archive* OpenArchive( const char* name ){
120 return new DirectoryArchive( name );
127 class TestVisitor : public Archive::IVisitor
130 virtual void visit( const char* name ){
136 Archive* archive = OpenArchive( "d:/quake/id1/" );
137 ArchiveFile* file = archive->openFile( "quake101.wad" );
140 file->getInputStream().read( buffer, 1024 );
144 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 0 ), "" );
148 Archive* archive = OpenArchive( "d:/gtkradiant_root/baseq3/" );
150 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 2 ), "" );
151 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFiles, 1 ), "textures" );
152 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eDirectories, 1 ), "textures" );
153 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 1 ), "textures" );