]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/vfspk3/archive.cpp
9fd1542894b2e3793c88453050f981efda80d715
[xonotic/netradiant.git] / plugins / vfspk3 / archive.cpp
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 class DirectoryArchive : public Archive
41 {
42 CopiedString m_root;
43 public:
44 DirectoryArchive( const char* root ) : m_root( root ){
45 }
46
47 void release(){
48         delete this;
49 }
50 virtual ArchiveFile* openFile( const char* name ){
51         UnixPath path( m_root.c_str() );
52         path.push_filename( name );
53         DirectoryArchiveFile* file = new DirectoryArchiveFile( name, path.c_str() );
54         if ( !file->failed() ) {
55                 return file;
56         }
57         file->release();
58         return 0;
59 }
60 virtual ArchiveTextFile* openTextFile( const char* name ){
61         UnixPath path( m_root.c_str() );
62         path.push_filename( name );
63         DirectoryArchiveTextFile* file = new DirectoryArchiveTextFile( name, path.c_str() );
64         if ( !file->failed() ) {
65                 return file;
66         }
67         file->release();
68         return 0;
69 }
70 virtual bool containsFile( const char* name ){
71         UnixPath path( m_root.c_str() );
72         path.push_filename( name );
73         return file_readable( path.c_str() );
74 }
75 virtual void forEachFile( VisitorFunc visitor, const char* root ){
76         std::vector<Directory*> dirs;
77         UnixPath path( m_root.c_str() );
78         path.push( root );
79         dirs.push_back( directory_open( path.c_str() ) );
80
81         while ( !dirs.empty() && directory_good( dirs.back() ) )
82         {
83                 const char* name = directory_read_and_increment( dirs.back() );
84
85                 if ( name == 0 ) {
86                         directory_close( dirs.back() );
87                         dirs.pop_back();
88                         path.pop();
89                 }
90                 else if ( !string_equal( name, "." ) && !string_equal( name, ".." ) ) {
91                         path.push_filename( name );
92
93                         bool is_directory = file_is_directory( path.c_str() );
94
95                         if ( !is_directory ) {
96                                 visitor.file( path_make_relative( path.c_str(), m_root.c_str() ) );
97                         }
98
99                         path.pop();
100
101                         if ( is_directory ) {
102                                 path.push( name );
103
104                                 if ( !visitor.directory( path_make_relative( path.c_str(), m_root.c_str() ), dirs.size() ) ) {
105                                         dirs.push_back( directory_open( path.c_str() ) );
106                                 }
107                                 else{
108                                         path.pop();
109                                 }
110                         }
111                 }
112         }
113 }
114 };
115
116 Archive* OpenArchive( const char* name ){
117         return new DirectoryArchive( name );
118 }
119
120 #if 0
121
122 class TestArchive
123 {
124 class TestVisitor : public Archive::IVisitor
125 {
126 public:
127 virtual void visit( const char* name ){
128         int bleh = 0;
129 }
130 };
131 public:
132 void test1(){
133         Archive* archive = OpenArchive( "d:/quake/id1/" );
134         ArchiveFile* file = archive->openFile( "quake101.wad" );
135         if ( file != 0 ) {
136                 char buffer[1024];
137                 file->getInputStream().read( buffer, 1024 );
138                 file->release();
139         }
140         TestVisitor visitor;
141         archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 0 ), "" );
142         archive->release();
143 }
144 void test2(){
145         Archive* archive = OpenArchive( "d:/gtkradiant_root/baseq3/" );
146         TestVisitor visitor;
147         archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 2 ), "" );
148         archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFiles, 1 ), "textures" );
149         archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eDirectories, 1 ), "textures" );
150         archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 1 ), "textures" );
151         archive->release();
152 }
153 TestArchive(){
154         test1();
155         test2();
156 }
157 };
158
159 TestArchive g_test;
160
161 #endif