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;
39 static const int BUFFERSIZE = SIZE;
41 InputStreamType& m_inputStream;
42 byte_type m_buffer[BUFFERSIZE];
48 SingleByteInputStream(InputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer + BUFFERSIZE), m_end(m_cur)
51 bool readByte(byte_type& b)
55 if(m_end != m_buffer + BUFFERSIZE)
60 m_end = m_buffer + m_inputStream.read(m_buffer, BUFFERSIZE);
75 /// \brief A binary-to-text wrapper around an InputStream.
76 /// Converts CRLF or LFCR line-endings to LF line-endings.
77 template<typename BinaryInputStreamType>
78 class BinaryToTextInputStream : public TextInputStream
80 SingleByteInputStream<BinaryInputStreamType> m_inputStream;
82 BinaryToTextInputStream(BinaryInputStreamType& inputStream) : m_inputStream(inputStream)
85 std::size_t read(char* buffer, std::size_t length)
90 if(length != 0 && m_inputStream.readByte(*reinterpret_cast<typename BinaryInputStreamType::byte_type*>(p)))
106 /// \brief An ArchiveFile which is stored uncompressed as part of a larger archive file.
107 class StoredArchiveFile : public ArchiveFile
110 FileInputStream m_filestream;
111 SubFileInputStream m_substream;
112 FileInputStream::size_type m_size;
114 typedef FileInputStream::size_type size_type;
115 typedef FileInputStream::position_type position_type;
117 StoredArchiveFile(const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size)
118 : m_name(name), m_filestream(archiveName), m_substream(m_filestream, position, stream_size), m_size(file_size)
122 static StoredArchiveFile* create(const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size)
124 return New<StoredArchiveFile>().scalar(name, archiveName, position, stream_size, file_size);
129 Delete<StoredArchiveFile>().scalar(this);
131 size_type size() const
135 const char* getName() const
137 return m_name.c_str();
139 InputStream& getInputStream()
145 /// \brief An ArchiveTextFile which is stored uncompressed as part of a larger archive file.
146 class StoredArchiveTextFile : public ArchiveTextFile
149 FileInputStream m_filestream;
150 SubFileInputStream m_substream;
151 BinaryToTextInputStream<SubFileInputStream> m_textStream;
153 typedef FileInputStream::size_type size_type;
154 typedef FileInputStream::position_type position_type;
156 StoredArchiveTextFile(const char* name, const char* archiveName, position_type position, size_type stream_size)
157 : m_name(name), m_filestream(archiveName), m_substream(m_filestream, position, stream_size), m_textStream(m_substream)
161 static StoredArchiveTextFile* create(const char* name, const char* archiveName, position_type position, size_type stream_size)
163 return New<StoredArchiveTextFile>().scalar(name, archiveName, position, stream_size);
168 Delete<StoredArchiveTextFile>().scalar(this);
170 const char* getName() const
172 return m_name.c_str();
174 TextInputStream& getInputStream()
180 /// \brief An ArchiveFile which is stored as a single file on disk.
181 class DirectoryArchiveFile : public ArchiveFile
184 FileInputStream m_istream;
185 FileInputStream::size_type m_size;
187 typedef FileInputStream::size_type size_type;
189 DirectoryArchiveFile(const char* name, const char* filename)
190 : m_name(name), m_istream(filename)
194 m_istream.seek(0, FileInputStream::end);
195 m_size = m_istream.tell();
205 return m_istream.failed();
212 size_type size() const
216 const char* getName() const
218 return m_name.c_str();
220 InputStream& getInputStream()
226 /// \brief An ArchiveTextFile which is stored as a single file on disk.
227 class DirectoryArchiveTextFile : public ArchiveTextFile
230 TextFileInputStream m_inputStream;
233 DirectoryArchiveTextFile(const char* name, const char* filename)
234 : m_name(name), m_inputStream(filename)
239 return m_inputStream.failed();
246 const char* getName() const
248 return m_name.c_str();
250 TextInputStream& getInputStream()
252 return m_inputStream;