2 // Copyright (C) 2006 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 3 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, see <http://www.gnu.org/licenses/>.
17 #include "physfs/physfs_stream.hpp"
25 IFileStreambuf::IFileStreambuf(const std::string& filename)
27 // check this as PHYSFS seems to be buggy and still returns a
28 // valid pointer in this case
30 throw std::runtime_error("Couldn't open file: empty filename");
32 file = PHYSFS_openRead(filename.c_str());
34 std::stringstream msg;
35 msg << "Couldn't open file '" << filename << "': "
36 << PHYSFS_getLastError();
37 throw std::runtime_error(msg.str());
41 IFileStreambuf::~IFileStreambuf()
47 IFileStreambuf::underflow()
49 if(PHYSFS_eof(file)) {
50 return traits_type::eof();
53 PHYSFS_sint64 bytesread = PHYSFS_read(file, buf, 1, sizeof(buf));
55 return traits_type::eof();
57 setg(buf, buf, buf + bytesread);
62 IFileStreambuf::pos_type
63 IFileStreambuf::seekpos(pos_type pos, std::ios_base::openmode)
65 if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (pos)) == 0) {
66 return pos_type(off_type(-1));
69 // the seek invalidated the buffer
74 IFileStreambuf::pos_type
75 IFileStreambuf::seekoff(off_type off, std::ios_base::seekdir dir,
76 std::ios_base::openmode mode)
79 PHYSFS_sint64 ptell = PHYSFS_tell(file);
82 case std::ios_base::beg:
84 case std::ios_base::cur:
86 return static_cast<pos_type> (ptell) - static_cast<pos_type> (egptr() - gptr());
87 pos += static_cast<off_type> (ptell) - static_cast<off_type> (egptr() - gptr());
89 case std::ios_base::end:
90 pos += static_cast<off_type> (PHYSFS_fileLength(file));
96 return pos_type(off_type(-1));
100 return seekpos(static_cast<pos_type> (pos), mode);
103 //---------------------------------------------------------------------------
105 OFileStreambuf::OFileStreambuf(const std::string& filename)
107 file = PHYSFS_openWrite(filename.c_str());
109 std::stringstream msg;
110 msg << "Couldn't open file '" << filename << "': "
111 << PHYSFS_getLastError();
112 throw std::runtime_error(msg.str());
115 setp(buf, buf+sizeof(buf));
118 OFileStreambuf::~OFileStreambuf()
125 OFileStreambuf::overflow(int c)
129 if(pbase() == pptr())
132 size_t size = pptr() - pbase();
133 PHYSFS_sint64 res = PHYSFS_write(file, pbase(), 1, size);
135 return traits_type::eof();
137 if(c != traits_type::eof()) {
138 PHYSFS_sint64 res = PHYSFS_write(file, &c2, 1, 1);
140 return traits_type::eof();
143 setp(buf, buf + res);
148 OFileStreambuf::sync()
150 return overflow(traits_type::eof());
153 //---------------------------------------------------------------------------
155 IFileStream::IFileStream(const std::string& filename)
156 : std::istream(new IFileStreambuf(filename))
160 IFileStream::~IFileStream()
165 //---------------------------------------------------------------------------
167 OFileStream::OFileStream(const std::string& filename)
168 : std::ostream(new OFileStreambuf(filename))
172 OFileStream::~OFileStream()