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