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