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)
50 bool readByte(byte_type& b)
54 if(m_end != m_buffer + SIZE)
59 m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
74 /// \brief A binary-to-text wrapper around an InputStream.
75 /// Converts CRLF or LFCR line-endings to LF line-endings.
76 template<typename BinaryInputStreamType>
77 class BinaryToTextInputStream : public TextInputStream
79 SingleByteInputStream<BinaryInputStreamType> m_inputStream;
81 BinaryToTextInputStream(BinaryInputStreamType& inputStream) : m_inputStream(inputStream)
84 std::size_t read(char* buffer, std::size_t length)
89 if(length != 0 && m_inputStream.readByte(*reinterpret_cast<typename BinaryInputStreamType::byte_type*>(p)))
105 /// \brief An ArchiveFile which is stored uncompressed as part of a larger archive file.
106 class StoredArchiveFile : public ArchiveFile
109 FileInputStream m_filestream;
110 SubFileInputStream m_substream;
111 FileInputStream::size_type m_size;
113 typedef FileInputStream::size_type size_type;
114 typedef FileInputStream::position_type position_type;
116 StoredArchiveFile(const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size)
117 : m_name(name), m_filestream(archiveName), m_substream(m_filestream, position, stream_size), m_size(file_size)
121 static StoredArchiveFile* create(const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size)
123 return New<StoredArchiveFile>().scalar(name, archiveName, position, stream_size, file_size);
128 Delete<StoredArchiveFile>().scalar(this);
130 size_type size() const
134 const char* getName() const
136 return m_name.c_str();
138 InputStream& getInputStream()
144 /// \brief An ArchiveTextFile which is stored uncompressed as part of a larger archive file.
145 class StoredArchiveTextFile : public ArchiveTextFile
148 FileInputStream m_filestream;
149 SubFileInputStream m_substream;
150 BinaryToTextInputStream<SubFileInputStream> m_textStream;
152 typedef FileInputStream::size_type size_type;
153 typedef FileInputStream::position_type position_type;
155 StoredArchiveTextFile(const char* name, const char* archiveName, position_type position, size_type stream_size)
156 : m_name(name), m_filestream(archiveName), m_substream(m_filestream, position, stream_size), m_textStream(m_substream)
160 static StoredArchiveTextFile* create(const char* name, const char* archiveName, position_type position, size_type stream_size)
162 return New<StoredArchiveTextFile>().scalar(name, archiveName, position, stream_size);
167 Delete<StoredArchiveTextFile>().scalar(this);
169 const char* getName() const
171 return m_name.c_str();
173 TextInputStream& getInputStream()
179 /// \brief An ArchiveFile which is stored as a single file on disk.
180 class DirectoryArchiveFile : public ArchiveFile
183 FileInputStream m_istream;
184 FileInputStream::size_type m_size;
186 typedef FileInputStream::size_type size_type;
188 DirectoryArchiveFile(const char* name, const char* filename)
189 : m_name(name), m_istream(filename)
193 m_istream.seek(0, FileInputStream::end);
194 m_size = m_istream.tell();
204 return m_istream.failed();
211 size_type size() const
215 const char* getName() const
217 return m_name.c_str();
219 InputStream& getInputStream()
225 /// \brief An ArchiveTextFile which is stored as a single file on disk.
226 class DirectoryArchiveTextFile : public ArchiveTextFile
229 TextFileInputStream m_inputStream;
232 DirectoryArchiveTextFile(const char* name, const char* filename)
233 : m_name(name), m_inputStream(filename)
238 return m_inputStream.failed();
245 const char* getName() const
247 return m_name.c_str();
249 TextInputStream& getInputStream()
251 return m_inputStream;