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 ){
34 case SeekableStream::cur:
36 case SeekableStream::end:
46 /// \brief A wrapper around a file input stream opened for reading in binary mode. Similar to std::ifstream.
48 /// - Maintains a valid file handle associated with a name passed to the constructor.
49 /// - Implements SeekableInputStream.
50 class FileInputStream : public SeekableInputStream
54 FileInputStream( const char* name ){
55 m_file = name[0] == '\0' ? 0 : fopen( name, "rb" );
67 size_type read( byte_type* buffer, size_type length ){
68 return fread( buffer, 1, length, m_file );
71 size_type seek( size_type position ){
72 return fseek( m_file, static_cast<long>( position ), SEEK_SET );
74 size_type seek( offset_type offset, seekdir direction ){
75 return fseek( m_file, offset, FileStreamDetail::whence_for_seekdir( direction ) );
77 size_type tell() const {
78 return ftell( m_file );
86 /// \brief A wrapper around a FileInputStream limiting access.
88 /// - Maintains an input stream.
89 /// - Provides input starting at an offset in the file for a limited range.
90 class SubFileInputStream : public InputStream
92 FileInputStream& m_istream;
93 size_type m_remaining;
95 typedef FileInputStream::position_type position_type;
97 SubFileInputStream( FileInputStream& istream, position_type offset, size_type size )
98 : m_istream( istream ), m_remaining( size ){
99 m_istream.seek( offset );
102 size_type read( byte_type* buffer, size_type length ){
103 size_type result = m_istream.read( buffer, std::min( length, m_remaining ) );
104 m_remaining -= result;
110 /// \brief A wrapper around a stdc file stream opened for writing in binary mode. Similar to std::ofstream..
112 /// - Maintains a valid file handle associated with a name passed to the constructor.
113 /// - Implements SeekableInputStream.
114 class FileOutputStream : public SeekableOutputStream
118 FileOutputStream( const char* name ){
119 m_file = name[0] == '\0' ? 0 : fopen( name, "wb" );
127 bool failed() const {
131 size_type write( const byte_type* buffer, size_type length ){
132 return fwrite( buffer, 1, length, m_file );
135 size_type seek( size_type position ){
136 return fseek( m_file, static_cast<long>( position ), SEEK_SET );
138 size_type seek( offset_type offset, seekdir direction ){
139 return fseek( m_file, offset, FileStreamDetail::whence_for_seekdir( direction ) );
141 size_type tell() const {
142 return ftell( m_file );
146 inline bool file_copy( const char* source, const char* target ){
147 const std::size_t buffer_size = 1024;
148 unsigned char buffer[buffer_size];
150 FileInputStream sourceFile( source );
151 if ( sourceFile.failed() ) {
154 FileOutputStream targetFile( target );
155 if ( targetFile.failed() ) {
161 std::size_t size = sourceFile.read( buffer, buffer_size );
165 if ( targetFile.write( buffer, size ) != size ) {