28d92c567a632085a65a5bb7ee3beb8c9b879111
[supertux.git] / src / physfs / physfs_stream.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "physfs_stream.hpp"
23
24 #include <assert.h>
25 #include <physfs.h>
26 #include <stdexcept>
27 #include <sstream>
28
29 IFileStreambuf::IFileStreambuf(const std::string& filename)
30 {
31     // check this as PHYSFS seems to be buggy and still returns a
32     // valid pointer in this case
33     if(filename == "") {
34         throw std::runtime_error("Couldn't open file: empty filename");
35     }
36     file = PHYSFS_openRead(filename.c_str());
37     if(file == 0) {
38         std::stringstream msg;
39         msg << "Couldn't open file '" << filename << "': "
40             << PHYSFS_getLastError();
41         throw std::runtime_error(msg.str());
42     }
43 }
44
45 IFileStreambuf::~IFileStreambuf()
46 {
47     PHYSFS_close(file);
48 }
49
50 int
51 IFileStreambuf::underflow()
52 {
53     if(PHYSFS_eof(file)) {
54         return traits_type::eof();
55     }
56
57     PHYSFS_sint64 bytesread = PHYSFS_read(file, buf, 1, sizeof(buf));
58     if(bytesread <= 0) {
59         return traits_type::eof();
60     }
61     setg(buf, buf, buf + bytesread);
62
63     return buf[0];
64 }
65
66 IFileStreambuf::pos_type
67 IFileStreambuf::seekpos(pos_type pos, std::ios_base::openmode)
68 {
69   if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (pos)) == 0) {
70     return pos_type(off_type(-1));
71   }
72
73   // the seek invalidated the buffer
74   setg(buf, buf, buf);
75   return pos;
76 }
77
78 IFileStreambuf::pos_type
79 IFileStreambuf::seekoff(off_type off, std::ios_base::seekdir dir,
80                         std::ios_base::openmode mode)
81 {
82   off_type pos = off;
83   PHYSFS_sint64 ptell = PHYSFS_tell(file);
84
85   switch(dir) {
86     case std::ios_base::beg:
87       break;
88     case std::ios_base::cur:
89       if(off == 0)
90         return static_cast<pos_type> (ptell) - static_cast<pos_type> (egptr() - gptr());
91       pos += static_cast<off_type> (ptell) - static_cast<off_type> (egptr() - gptr());
92       break;
93     case std::ios_base::end:
94       pos += static_cast<off_type> (PHYSFS_fileLength(file));
95       break;
96     default:
97 #ifdef DEBUG
98       assert(false);
99 #else
100       return pos_type(off_type(-1));
101 #endif
102   }
103
104   return seekpos(static_cast<pos_type> (pos), mode);
105 }
106
107 //---------------------------------------------------------------------------
108
109 OFileStreambuf::OFileStreambuf(const std::string& filename)
110 {
111     file = PHYSFS_openWrite(filename.c_str());
112     if(file == 0) {
113         std::stringstream msg;
114         msg << "Couldn't open file '" << filename << "': "
115             << PHYSFS_getLastError();
116         throw std::runtime_error(msg.str());
117     }
118
119     setp(buf, buf+sizeof(buf));
120 }
121
122 OFileStreambuf::~OFileStreambuf()
123 {
124     sync();
125     PHYSFS_close(file);
126 }
127
128 int
129 OFileStreambuf::overflow(int c)
130 {
131     char c2 = (char)c;
132
133     if(pbase() == pptr())
134         return 0;
135
136     size_t size = pptr() - pbase();
137     PHYSFS_sint64 res = PHYSFS_write(file, pbase(), 1, size);
138     if(res <= 0)
139         return traits_type::eof();
140
141     if(c != traits_type::eof()) {
142         PHYSFS_sint64 res = PHYSFS_write(file, &c2, 1, 1);
143         if(res <= 0)
144             return traits_type::eof();
145     }
146
147     setp(buf, buf + res);
148     return 0;
149 }
150
151 int
152 OFileStreambuf::sync()
153 {
154     return overflow(traits_type::eof());
155 }
156
157 //---------------------------------------------------------------------------
158
159 IFileStream::IFileStream(const std::string& filename)
160     : std::istream(new IFileStreambuf(filename))
161 {
162 }
163
164 IFileStream::~IFileStream()
165 {
166     delete rdbuf();
167 }
168
169 //---------------------------------------------------------------------------
170
171 OFileStream::OFileStream(const std::string& filename)
172     : std::ostream(new OFileStreambuf(filename))
173 {
174 }
175
176 OFileStream::~OFileStream()
177 {
178     delete rdbuf();
179 }