2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
22 #if !defined ( INCLUDED_ARCHIVELIB_H )
23 #define INCLUDED_ARCHIVELIB_H
25 #include "debugging/debugging.h"
27 #include "stream/filestream.h"
28 #include "stream/textfilestream.h"
29 #include "memory/allocator.h"
30 #include "string/string.h"
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
38 typedef typename InputStreamType::byte_type byte_type;
40 InputStreamType& m_inputStream;
41 byte_type m_buffer[SIZE];
47 SingleByteInputStream( InputStreamType& inputStream ) : m_inputStream( inputStream ), m_cur( m_buffer + SIZE ), m_end( m_cur ){
49 bool readByte( byte_type& b ){
50 if ( m_cur == m_end ) {
51 if ( m_end != m_buffer + SIZE ) {
55 m_end = m_buffer + m_inputStream.read( m_buffer, SIZE );
58 if ( m_end == m_buffer ) {
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
74 SingleByteInputStream<BinaryInputStreamType> m_inputStream;
76 BinaryToTextInputStream( BinaryInputStreamType& inputStream ) : m_inputStream( inputStream ){
78 std::size_t read( char* buffer, std::size_t length ){
82 if ( length != 0 && m_inputStream.readByte( *reinterpret_cast<typename BinaryInputStreamType::byte_type*>( p ) ) ) {
96 /// \brief An ArchiveFile which is stored uncompressed as part of a larger archive file.
97 class StoredArchiveFile : public ArchiveFile
100 FileInputStream m_filestream;
101 SubFileInputStream m_substream;
102 FileInputStream::size_type m_size;
104 typedef FileInputStream::size_type size_type;
105 typedef FileInputStream::position_type position_type;
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 ){
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 );
116 Delete<StoredArchiveFile>().scalar( this );
118 size_type size() const {
121 const char* getName() const {
122 return m_name.c_str();
124 InputStream& getInputStream(){
129 /// \brief An ArchiveTextFile which is stored uncompressed as part of a larger archive file.
130 class StoredArchiveTextFile : public ArchiveTextFile
133 FileInputStream m_filestream;
134 SubFileInputStream m_substream;
135 BinaryToTextInputStream<SubFileInputStream> m_textStream;
137 typedef FileInputStream::size_type size_type;
138 typedef FileInputStream::position_type position_type;
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 ){
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 );
149 Delete<StoredArchiveTextFile>().scalar( this );
151 const char* getName() const {
152 return m_name.c_str();
154 TextInputStream& getInputStream(){
159 /// \brief An ArchiveFile which is stored as a single file on disk.
160 class DirectoryArchiveFile : public ArchiveFile
163 FileInputStream m_istream;
164 FileInputStream::size_type m_size;
166 typedef FileInputStream::size_type size_type;
168 DirectoryArchiveFile( const char* name, const char* filename )
169 : m_name( name ), m_istream( filename ){
171 m_istream.seek( 0, FileInputStream::end );
172 m_size = m_istream.tell();
180 bool failed() const {
181 return m_istream.failed();
187 size_type size() const {
190 const char* getName() const {
191 return m_name.c_str();
193 InputStream& getInputStream(){
198 /// \brief An ArchiveTextFile which is stored as a single file on disk.
199 class DirectoryArchiveTextFile : public ArchiveTextFile
202 TextFileInputStream m_inputStream;
205 DirectoryArchiveTextFile( const char* name, const char* filename )
206 : m_name( name ), m_inputStream( filename ){
208 bool failed() const {
209 return m_inputStream.failed();
215 const char* getName() const {
216 return m_name.c_str();
218 TextInputStream& getInputStream(){
219 return m_inputStream;