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_STREAM_FILESTREAM_H)
23 #define INCLUDED_STREAM_FILESTREAM_H
25 #include "idatastream.h"
29 namespace FileStreamDetail
31 inline int whence_for_seekdir(SeekableStream::seekdir direction)
35 case SeekableStream::cur:
37 case SeekableStream::end:
47 /// \brief A wrapper around a file input stream opened for reading in binary mode. Similar to std::ifstream.
49 /// - Maintains a valid file handle associated with a name passed to the constructor.
50 /// - Implements SeekableInputStream.
51 class FileInputStream : public SeekableInputStream
55 FileInputStream(const char* name)
57 m_file = name[0] == '\0' ? 0 : fopen(name, "rb");
70 size_type read(byte_type* buffer, size_type length)
72 return fread(buffer, 1, length, m_file);
75 size_type seek(size_type position)
77 return fseek(m_file, static_cast<long>(position), SEEK_SET);
79 size_type seek(offset_type offset, seekdir direction)
81 return fseek(m_file, offset, FileStreamDetail::whence_for_seekdir(direction));
83 size_type tell() const
94 /// \brief A wrapper around a FileInputStream limiting access.
96 /// - Maintains an input stream.
97 /// - Provides input starting at an offset in the file for a limited range.
98 class SubFileInputStream : public InputStream
100 FileInputStream& m_istream;
101 size_type m_remaining;
103 typedef FileInputStream::position_type position_type;
105 SubFileInputStream(FileInputStream& istream, position_type offset, size_type size)
106 : m_istream(istream), m_remaining(size)
108 m_istream.seek(offset);
111 size_type read(byte_type* buffer, size_type length)
113 size_type result = m_istream.read(buffer, std::min(length, m_remaining));
114 m_remaining -= result;
120 /// \brief A wrapper around a stdc file stream opened for writing in binary mode. Similar to std::ofstream..
122 /// - Maintains a valid file handle associated with a name passed to the constructor.
123 /// - Implements SeekableInputStream.
124 class FileOutputStream : public SeekableOutputStream
128 FileOutputStream(const char* name)
130 m_file = name[0] == '\0' ? 0 : fopen(name, "wb");
143 size_type write(const byte_type* buffer, size_type length)
145 return fwrite(buffer, 1, length, m_file);
148 size_type seek(size_type position)
150 return fseek(m_file, static_cast<long>(position), SEEK_SET);
152 size_type seek(offset_type offset, seekdir direction)
154 return fseek(m_file, offset, FileStreamDetail::whence_for_seekdir(direction));
156 size_type tell() const
158 return ftell(m_file);
162 inline bool file_copy(const char* source, const char* target)
164 const std::size_t buffer_size = 1024;
165 unsigned char buffer[buffer_size];
167 FileInputStream sourceFile(source);
168 if(sourceFile.failed())
172 FileOutputStream targetFile(target);
173 if(targetFile.failed())
180 std::size_t size = sourceFile.read(buffer, buffer_size);
185 if(targetFile.write(buffer, size) != size)