]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/iarchive.h
Merge remote-tracking branch 'ttimo/master'
[xonotic/netradiant.git] / include / iarchive.h
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 #if !defined( INCLUDED_IARCHIVE_H )
23 #define INCLUDED_IARCHIVE_H
24
25 #include <cstddef>
26 #include "generic/constant.h"
27
28 class InputStream;
29
30 /// \brief A file opened in binary mode.
31 class ArchiveFile
32 {
33 public:
34 /// \brief Destroys the file object.
35 virtual void release() = 0;
36 /// \brief Returns the size of the file data in bytes.
37 virtual std::size_t size() const = 0;
38 /// \brief Returns the path to this file (relative to the filesystem root)
39 virtual const char* getName() const = 0;
40 /// \brief Returns the stream associated with this file.
41 /// Subsequent calls return the same stream.
42 /// The stream may be read forwards until it is exhausted.
43 /// The stream remains valid for the lifetime of the file.
44 virtual InputStream& getInputStream() = 0;
45 };
46
47 class TextInputStream;
48
49 /// \brief A file opened in text mode.
50 class ArchiveTextFile
51 {
52 public:
53 /// \brief Destroys the file object.
54 virtual void release() = 0;
55 /// \brief Returns the stream associated with this file.
56 /// Subsequent calls return the same stream.
57 /// The stream may be read forwards until it is exhausted.
58 /// The stream remains valid for the lifetime of the file.
59 virtual TextInputStream& getInputStream() = 0;
60 };
61
62 class ScopedArchiveFile
63 {
64 ArchiveFile& m_file;
65 public:
66 ScopedArchiveFile( ArchiveFile& file ) : m_file( file ){
67 }
68 ~ScopedArchiveFile(){
69         m_file.release();
70 }
71 };
72
73 class CustomArchiveVisitor;
74
75 class Archive
76 {
77 public:
78
79 class Visitor
80 {
81 public:
82 virtual void visit( const char* name ) = 0;
83 };
84
85 typedef CustomArchiveVisitor VisitorFunc;
86
87 enum EMode
88 {
89         eFiles = 0x01,
90         eDirectories = 0x02,
91         eFilesAndDirectories = 0x03,
92 };
93
94 /// \brief Destroys the archive object.
95 /// Any unreleased file object associated with the archive remains valid. */
96 virtual void release() = 0;
97 /// \brief Returns a new object associated with the file identified by \p name, or 0 if the file cannot be opened.
98 /// Name comparisons are case-insensitive.
99 virtual ArchiveFile* openFile( const char* name ) = 0;
100 /// \brief Returns a new object associated with the file identified by \p name, or 0 if the file cannot be opened.
101 /// Name comparisons are case-insensitive.
102 virtual ArchiveTextFile* openTextFile( const char* name ) = 0;
103 /// Returns true if the file identified by \p name can be opened.
104 /// Name comparisons are case-insensitive.
105 virtual bool containsFile( const char* name ) = 0;
106 /// \brief Performs a depth-first traversal of the archive tree starting at \p root.
107 /// Traverses the entire tree if \p root is "".
108 /// When a file is encountered, calls \c visitor.file passing the file name.
109 /// When a directory is encountered, calls \c visitor.directory passing the directory name.
110 /// Skips the directory if \c visitor.directory returned true.
111 /// Root comparisons are case-insensitive.
112 /// Names are mixed-case.
113 virtual void forEachFile( VisitorFunc visitor, const char* root ) = 0;
114 };
115
116 class CustomArchiveVisitor
117 {
118 Archive::Visitor* m_visitor;
119 Archive::EMode m_mode;
120 std::size_t m_depth;
121 public:
122 CustomArchiveVisitor( Archive::Visitor& visitor, Archive::EMode mode, std::size_t depth )
123         : m_visitor( &visitor ), m_mode( mode ), m_depth( depth ){
124 }
125 void file( const char* name ){
126         if ( ( m_mode & Archive::eFiles ) != 0 ) {
127                 m_visitor->visit( name );
128         }
129 }
130 bool directory( const char* name, std::size_t depth ){
131         if ( ( m_mode & Archive::eDirectories ) != 0 ) {
132                 m_visitor->visit( name );
133         }
134         if ( depth == m_depth ) {
135                 return true;
136         }
137         return false;
138 }
139 };
140
141 typedef Archive* ( *PFN_OPENARCHIVE )( const char* name );
142
143 class _QERArchiveTable
144 {
145 public:
146 INTEGER_CONSTANT( Version, 1 );
147 STRING_CONSTANT( Name, "archive" );
148
149 PFN_OPENARCHIVE m_pfnOpenArchive;
150 };
151
152 template<typename Type>
153 class Modules;
154 typedef Modules<_QERArchiveTable> ArchiveModules;
155
156 template<typename Type>
157 class ModulesRef;
158 typedef ModulesRef<_QERArchiveTable> ArchiveModulesRef;
159
160 #endif