]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/archivelib.h
CMake: Make dll bundling optional
[xonotic/netradiant.git] / libs / archivelib.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_ARCHIVELIB_H )
23 #define INCLUDED_ARCHIVELIB_H
24
25 #include "debugging/debugging.h"
26 #include "iarchive.h"
27 #include "stream/filestream.h"
28 #include "stream/textfilestream.h"
29 #include "memory/allocator.h"
30 #include "string/string.h"
31
32 /// \brief A single-byte-reader wrapper around an InputStream.
33 /// Optimised for reading one byte at a time.
34 /// Uses a buffer to reduce the number of times the wrapped stream must be read.
35 template<typename InputStreamType, int SIZE = 1024>
36 class SingleByteInputStream
37 {
38 typedef typename InputStreamType::byte_type byte_type;
39
40 InputStreamType& m_inputStream;
41 byte_type m_buffer[SIZE];
42 byte_type* m_cur;
43 byte_type* m_end;
44
45 public:
46
47 SingleByteInputStream( InputStreamType& inputStream ) : m_inputStream( inputStream ), m_cur( m_buffer + SIZE ), m_end( m_cur ){
48 }
49 bool readByte( byte_type& b ){
50         if ( m_cur == m_end ) {
51                 if ( m_end != m_buffer + SIZE ) {
52                         return false;
53                 }
54
55                 m_end = m_buffer + m_inputStream.read( m_buffer, SIZE );
56                 m_cur = m_buffer;
57
58                 if ( m_end == m_buffer ) {
59                         return false;
60                 }
61         }
62
63         b = *m_cur++;
64
65         return true;
66 }
67 };
68
69 /// \brief A binary-to-text wrapper around an InputStream.
70 /// Converts CRLF or LFCR line-endings to LF line-endings.
71 template<typename BinaryInputStreamType>
72 class BinaryToTextInputStream : public TextInputStream
73 {
74 SingleByteInputStream<BinaryInputStreamType> m_inputStream;
75 public:
76 BinaryToTextInputStream( BinaryInputStreamType& inputStream ) : m_inputStream( inputStream ){
77 }
78 std::size_t read( char* buffer, std::size_t length ){
79         char* p = buffer;
80         for (;; )
81         {
82                 if ( length != 0 && m_inputStream.readByte( *reinterpret_cast<typename BinaryInputStreamType::byte_type*>( p ) ) ) {
83                         if ( *p != '\r' ) {
84                                 ++p;
85                                 --length;
86                         }
87                 }
88                 else
89                 {
90                         return p - buffer;
91                 }
92         }
93 }
94 };
95
96 /// \brief An ArchiveFile which is stored uncompressed as part of a larger archive file.
97 class StoredArchiveFile : public ArchiveFile
98 {
99 CopiedString m_name;
100 FileInputStream m_filestream;
101 SubFileInputStream m_substream;
102 FileInputStream::size_type m_size;
103 public:
104 typedef FileInputStream::size_type size_type;
105 typedef FileInputStream::position_type position_type;
106
107 StoredArchiveFile( const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size )
108         : m_name( name ), m_filestream( archiveName ), m_substream( m_filestream, position, stream_size ), m_size( file_size ){
109 }
110
111 static StoredArchiveFile* create( const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size ){
112         return New<StoredArchiveFile>().scalar( name, archiveName, position, stream_size, file_size );
113 }
114
115 void release(){
116         Delete<StoredArchiveFile>().scalar( this );
117 }
118 size_type size() const {
119         return m_size;
120 }
121 const char* getName() const {
122         return m_name.c_str();
123 }
124 InputStream& getInputStream(){
125         return m_substream;
126 }
127 };
128
129 /// \brief An ArchiveTextFile which is stored uncompressed as part of a larger archive file.
130 class StoredArchiveTextFile : public ArchiveTextFile
131 {
132 CopiedString m_name;
133 FileInputStream m_filestream;
134 SubFileInputStream m_substream;
135 BinaryToTextInputStream<SubFileInputStream> m_textStream;
136 public:
137 typedef FileInputStream::size_type size_type;
138 typedef FileInputStream::position_type position_type;
139
140 StoredArchiveTextFile( const char* name, const char* archiveName, position_type position, size_type stream_size )
141         : m_name( name ), m_filestream( archiveName ), m_substream( m_filestream, position, stream_size ), m_textStream( m_substream ){
142 }
143
144 static StoredArchiveTextFile* create( const char* name, const char* archiveName, position_type position, size_type stream_size ){
145         return New<StoredArchiveTextFile>().scalar( name, archiveName, position, stream_size );
146 }
147
148 void release(){
149         Delete<StoredArchiveTextFile>().scalar( this );
150 }
151 const char* getName() const {
152         return m_name.c_str();
153 }
154 TextInputStream& getInputStream(){
155         return m_textStream;
156 }
157 };
158
159 /// \brief An ArchiveFile which is stored as a single file on disk.
160 class DirectoryArchiveFile : public ArchiveFile
161 {
162 CopiedString m_name;
163 FileInputStream m_istream;
164 FileInputStream::size_type m_size;
165 public:
166 typedef FileInputStream::size_type size_type;
167
168 DirectoryArchiveFile( const char* name, const char* filename )
169         : m_name( name ), m_istream( filename ){
170         if ( !failed() ) {
171                 m_istream.seek( 0, FileInputStream::end );
172                 m_size = m_istream.tell();
173                 m_istream.seek( 0 );
174         }
175         else
176         {
177                 m_size = 0;
178         }
179 }
180 bool failed() const {
181         return m_istream.failed();
182 }
183
184 void release(){
185         delete this;
186 }
187 size_type size() const {
188         return m_size;
189 }
190 const char* getName() const {
191         return m_name.c_str();
192 }
193 InputStream& getInputStream(){
194         return m_istream;
195 }
196 };
197
198 /// \brief An ArchiveTextFile which is stored as a single file on disk.
199 class DirectoryArchiveTextFile : public ArchiveTextFile
200 {
201 CopiedString m_name;
202 TextFileInputStream m_inputStream;
203 public:
204
205 DirectoryArchiveTextFile( const char* name, const char* filename )
206         : m_name( name ), m_inputStream( filename ){
207 }
208 bool failed() const {
209         return m_inputStream.failed();
210 }
211
212 void release(){
213         delete this;
214 }
215 const char* getName() const {
216         return m_name.c_str();
217 }
218 TextInputStream& getInputStream(){
219         return m_inputStream;
220 }
221 };
222
223
224 #endif