Debian Codegraph @

plugins/vfspk3/archive.cpp

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