2 Copyright (C) 2005 Matthias Braun <matze@braunis.de>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "physfs_stream.h"
26 IFileStreambuf::IFileStreambuf(const std::string& filename)
28 file = PHYSFS_openRead(filename.c_str());
30 std::stringstream msg;
31 msg << "Couldn't open file '" << filename << "': "
32 << PHYSFS_getLastError();
33 throw std::runtime_error(msg.str());
37 IFileStreambuf::~IFileStreambuf()
43 IFileStreambuf::underflow()
46 return traits_type::eof();
48 size_t bytesread = (size_t) PHYSFS_read(file, buf, 1, sizeof(buf));
50 return traits_type::eof();
51 setg(buf, buf, buf + bytesread);
56 //---------------------------------------------------------------------------
58 OFileStreambuf::OFileStreambuf(const std::string& filename)
60 file = PHYSFS_openWrite(filename.c_str());
62 std::stringstream msg;
63 msg << "Couldn't open file '" << filename << "': "
64 << PHYSFS_getLastError();
65 throw std::runtime_error(msg.str());
68 setp(buf, buf+sizeof(buf));
71 OFileStreambuf::~OFileStreambuf()
78 OFileStreambuf::overflow(int c)
83 size_t size = pptr() - pbase();
84 PHYSFS_sint64 res = PHYSFS_write(file, pbase(), 1, size);
86 return traits_type::eof();
88 if(c != traits_type::eof()) {
89 PHYSFS_sint64 res = PHYSFS_write(file, &c, 1, 1);
91 return traits_type::eof();
99 OFileStreambuf::sync()
101 return overflow(traits_type::eof());
104 //---------------------------------------------------------------------------
106 IFileStream::IFileStream(const std::string& filename)
107 : std::istream(new IFileStreambuf(filename))
111 IFileStream::~IFileStream()
116 //---------------------------------------------------------------------------
118 OFileStream::OFileStream(const std::string& filename)
119 : std::ostream(new OFileStreambuf(filename))
123 OFileStream::~OFileStream()