forgot to add some files
[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.h"
21
22 #include <physfs.h>
23 #include <stdexcept>
24 #include <sstream>
25
26 IFileStreambuf::IFileStreambuf(const std::string& filename)
27 {
28     file = PHYSFS_openRead(filename.c_str());
29     if(file == 0) {
30         std::stringstream msg;
31         msg << "Couldn't open file '" << filename << "': "
32             << PHYSFS_getLastError();
33         throw std::runtime_error(msg.str());
34     }
35 }
36
37 IFileStreambuf::~IFileStreambuf()
38 {
39     PHYSFS_close(file);
40 }
41
42 int
43 IFileStreambuf::underflow()
44 {
45     if(PHYSFS_eof(file))
46         return traits_type::eof();
47     
48     size_t bytesread = (size_t) PHYSFS_read(file, buf, 1, sizeof(buf));
49     if(bytesread == 0)
50         return traits_type::eof();
51     setg(buf, buf, buf + bytesread);
52
53     return buf[0];
54 }
55
56 //---------------------------------------------------------------------------
57
58 OFileStreambuf::OFileStreambuf(const std::string& filename)
59 {
60     file = PHYSFS_openWrite(filename.c_str());
61     if(file == 0) {
62         std::stringstream msg;
63         msg << "Couldn't open file '" << filename << "': "
64             << PHYSFS_getLastError();
65         throw std::runtime_error(msg.str());
66     }
67     
68     setp(buf, buf+sizeof(buf));
69 }
70
71 OFileStreambuf::~OFileStreambuf()
72 {
73     sync();
74     PHYSFS_close(file);
75 }
76
77 int
78 OFileStreambuf::overflow(int c)
79 {
80     if(pbase() == pptr())
81         return 0;
82
83     size_t size = pptr() - pbase();
84     PHYSFS_sint64 res = PHYSFS_write(file, pbase(), 1, size);
85     if(res <= 0)
86         return traits_type::eof();
87     
88     if(c != traits_type::eof()) {
89         PHYSFS_sint64 res = PHYSFS_write(file, &c, 1, 1);
90         if(res <= 0)
91             return traits_type::eof();
92     }
93
94     setp(buf, buf + res);
95     return 0;
96 }
97
98 int
99 OFileStreambuf::sync()
100 {
101     return overflow(traits_type::eof());
102 }
103
104 //---------------------------------------------------------------------------
105
106 IFileStream::IFileStream(const std::string& filename)
107     : std::istream(new IFileStreambuf(filename))
108 {
109 }
110
111 IFileStream::~IFileStream()
112 {
113     delete rdbuf();
114 }
115
116 //---------------------------------------------------------------------------
117
118 OFileStream::OFileStream(const std::string& filename)
119     : std::ostream(new OFileStreambuf(filename))
120 {
121 }
122
123 OFileStream::~OFileStream()
124 {
125     delete rdbuf();
126 }
127