4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "physfs_stream.hpp"
29 IFileStreambuf::IFileStreambuf(const std::string& filename)
31 file = PHYSFS_openRead(filename.c_str());
33 std::stringstream msg;
34 msg << "Couldn't open file '" << filename << "': "
35 << PHYSFS_getLastError();
36 throw std::runtime_error(msg.str());
40 IFileStreambuf::~IFileStreambuf()
46 IFileStreambuf::underflow()
48 if(PHYSFS_eof(file)) {
49 return traits_type::eof();
52 PHYSFS_sint64 bytesread = PHYSFS_read(file, buf, 1, sizeof(buf));
54 return traits_type::eof();
56 setg(buf, buf, buf + bytesread);
61 IFileStreambuf::pos_type
62 IFileStreambuf::seekpos(pos_type pos, std::ios_base::openmode)
64 if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (pos)) == 0) {
65 return pos_type(off_type(-1));
68 // the seek invalidated the buffer
73 IFileStreambuf::pos_type
74 IFileStreambuf::seekoff(off_type off, std::ios_base::seekdir dir,
75 std::ios_base::openmode mode)
78 PHYSFS_sint64 ptell = PHYSFS_tell(file);
81 case std::ios_base::beg:
83 case std::ios_base::cur:
85 return static_cast<pos_type> (ptell) - static_cast<pos_type> (egptr() - gptr());
86 pos += static_cast<off_type> (ptell) - static_cast<off_type> (egptr() - gptr());
88 case std::ios_base::end:
89 pos += static_cast<off_type> (PHYSFS_fileLength(file));
95 return pos_type(off_type(-1));
99 return seekpos(static_cast<pos_type> (pos), mode);
102 //---------------------------------------------------------------------------
104 OFileStreambuf::OFileStreambuf(const std::string& filename)
106 file = PHYSFS_openWrite(filename.c_str());
108 std::stringstream msg;
109 msg << "Couldn't open file '" << filename << "': "
110 << PHYSFS_getLastError();
111 throw std::runtime_error(msg.str());
114 setp(buf, buf+sizeof(buf));
117 OFileStreambuf::~OFileStreambuf()
124 OFileStreambuf::overflow(int c)
126 if(pbase() == pptr())
129 size_t size = pptr() - pbase();
130 PHYSFS_sint64 res = PHYSFS_write(file, pbase(), 1, size);
132 return traits_type::eof();
134 if(c != traits_type::eof()) {
135 PHYSFS_sint64 res = PHYSFS_write(file, &c, 1, 1);
137 return traits_type::eof();
140 setp(buf, buf + res);
145 OFileStreambuf::sync()
147 return overflow(traits_type::eof());
150 //---------------------------------------------------------------------------
152 IFileStream::IFileStream(const std::string& filename)
153 : std::istream(new IFileStreambuf(filename))
157 IFileStream::~IFileStream()
162 //---------------------------------------------------------------------------
164 OFileStream::OFileStream(const std::string& filename)
165 : std::ostream(new OFileStreambuf(filename))
169 OFileStream::~OFileStream()