1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #include "archive.h"
23
24 #include "idatastream.h"
25 #include "iarchive.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <vector>
30
31 #include "stream/filestream.h"
32 #include "stream/textfilestream.h"
33 #include "string/string.h"
34 #include "os/path.h"
35 #include "os/file.h"
36 #include "os/dir.h"
37 #include "archivelib.h"
38 #include "fs_path.h"
39
40 #include "vfspk3.h"
41
42
43 class DirectoryArchive : public Archive
44 {
45 CopiedString m_root;
46 public:
DirectoryArchive(const char * root)47 DirectoryArchive( const char* root ) : m_root( root ){
48 }
49
release()50 void release(){
51 delete this;
52 }
openFile(const char * name)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() ) {
58 return file;
59 }
60 file->release();
61 return 0;
62 }
openTextFile(const char * name)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() ) {
68 return file;
69 }
70 file->release();
71 return 0;
72 }
containsFile(const char * name)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() );
77 }
forEachFile(VisitorFunc visitor,const char * root)78 virtual void forEachFile( VisitorFunc visitor, const char* root ){
79 std::vector<Directory*> dirs;
80 UnixPath path( m_root.c_str() );
81 path.push( root );
82 dirs.push_back( directory_open( path.c_str() ) );
83
84 while ( !dirs.empty() && directory_good( dirs.back() ) )
85 {
86 const char* name = directory_read_and_increment( dirs.back() );
87
88 if ( name == 0 ) {
89 directory_close( dirs.back() );
90 dirs.pop_back();
91 path.pop();
92 }
93 else if ( !string_equal( name, "." ) && !string_equal( name, ".." ) ) {
94 path.push_filename( name );
95
96 bool is_directory = file_is_directory( path.c_str() );
97
98 if ( !is_directory ) {
99 visitor.file( path_make_relative( path.c_str(), m_root.c_str() ) );
100 }
101
102 path.pop();
103
104 if ( is_directory ) {
105 path.push( name );
106
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() ) );
109 }
110 else{
111 path.pop();
112 }
113 }
114 }
115 }
116 }
117 };
118
OpenArchive(const char * name)119 Archive* OpenArchive( const char* name ){
120 return new DirectoryArchive( name );
121 }
122
123 #if 0
124
125 class TestArchive
126 {
127 class TestVisitor : public Archive::IVisitor
128 {
129 public:
130 virtual void visit( const char* name ){
131 int bleh = 0;
132 }
133 };
134 public:
135 void test1(){
136 Archive* archive = OpenArchive( "d:/quake/id1/" );
137 ArchiveFile* file = archive->openFile( "quake101.wad" );
138 if ( file != 0 ) {
139 char buffer[1024];
140 file->getInputStream().read( buffer, 1024 );
141 file->release();
142 }
143 TestVisitor visitor;
144 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 0 ), "" );
145 archive->release();
146 }
147 void test2(){
148 Archive* archive = OpenArchive( "d:/gtkradiant_root/baseq3/" );
149 TestVisitor visitor;
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" );
154 archive->release();
155 }
156 TestArchive(){
157 test1();
158 test2();
159 }
160 };
161
162 TestArchive g_test;
163
164 #endif
165