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.
23 #include "physfs_stream.hpp"
30 IFileStreambuf::IFileStreambuf(const std::string& filename)
32 // check this as PHYSFS seems to be buggy and still returns a
33 // valid pointer in this case
35 throw std::runtime_error("Couldn't open file: empty filename");
37 file = PHYSFS_openRead(filename.c_str());
39 std::stringstream msg;
40 msg << "Couldn't open file '" << filename << "': "
41 << PHYSFS_getLastError();
42 throw std::runtime_error(msg.str());
46 IFileStreambuf::~IFileStreambuf()
52 IFileStreambuf::underflow()
54 if(PHYSFS_eof(file)) {
55 return traits_type::eof();
58 PHYSFS_sint64 bytesread = PHYSFS_read(file, buf, 1, sizeof(buf));
60 return traits_type::eof();
62 setg(buf, buf, buf + bytesread);
67 IFileStreambuf::pos_type
68 IFileStreambuf::seekpos(pos_type pos, std::ios_base::openmode)
70 if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (pos)) == 0) {
71 return pos_type(off_type(-1));
74 // the seek invalidated the buffer
79 IFileStreambuf::pos_type
80 IFileStreambuf::seekoff(off_type off, std::ios_base::seekdir dir,
81 std::ios_base::openmode mode)
84 PHYSFS_sint64 ptell = PHYSFS_tell(file);
87 case std::ios_base::beg:
89 case std::ios_base::cur:
91 return static_cast<pos_type> (ptell) - static_cast<pos_type> (egptr() - gptr());
92 pos += static_cast<off_type> (ptell) - static_cast<off_type> (egptr() - gptr());
94 case std::ios_base::end:
95 pos += static_cast<off_type> (PHYSFS_fileLength(file));
101 return pos_type(off_type(-1));
105 return seekpos(static_cast<pos_type> (pos), mode);
108 //---------------------------------------------------------------------------
110 OFileStreambuf::OFileStreambuf(const std::string& filename)
112 file = PHYSFS_openWrite(filename.c_str());
114 std::stringstream msg;
115 msg << "Couldn't open file '" << filename << "': "
116 << PHYSFS_getLastError();
117 throw std::runtime_error(msg.str());
120 setp(buf, buf+sizeof(buf));
123 OFileStreambuf::~OFileStreambuf()
130 OFileStreambuf::overflow(int c)
134 if(pbase() == pptr())
137 size_t size = pptr() - pbase();
138 PHYSFS_sint64 res = PHYSFS_write(file, pbase(), 1, size);
140 return traits_type::eof();
142 if(c != traits_type::eof()) {
143 PHYSFS_sint64 res = PHYSFS_write(file, &c2, 1, 1);
145 return traits_type::eof();
148 setp(buf, buf + res);
153 OFileStreambuf::sync()
155 return overflow(traits_type::eof());
158 //---------------------------------------------------------------------------
160 IFileStream::IFileStream(const std::string& filename)
161 : std::istream(new IFileStreambuf(filename))
165 IFileStream::~IFileStream()
170 //---------------------------------------------------------------------------
172 OFileStream::OFileStream(const std::string& filename)
173 : std::ostream(new OFileStreambuf(filename))
177 OFileStream::~OFileStream()