1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
/*
physfs_stream.hpp
Copyright (C) 2009 Poul Sander
Copied from Warzone 2100
http://svn.gna.org/viewcvs/warzone/branches/sound/lib/sound/general/physfs_stream.hpp?rev=3623
Copyright (C) 2007 Warzone Resurrection Project
Copyright (C) 2007 Giel van Schijndel <me@mortis.eu>
Originally part of SuperTux:
http://svn.berlios.de/svnroot/repos/supertux/trunk/supertux/src/physfs/physfs_stream.cpp revision 4176
Copyright (C) 2006 Matthias Braun <matze@braunis.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Poul Sander
R�veh�jvej 36, V. 1111
2800 Kgs. Lyngby
DENMARK
blockattack@poulsander.com
*/
#ifndef __INCLUDED_PHYSFS_STREAM_HPP__
#define __INCLUDED_PHYSFS_STREAM_HPP__
#define BUF_SIZE 1024
#include <physfs.h>
#include <streambuf>
#include <istream>
#include <ostream>
/*Compared to Warzone 2100 boost:array has been replaced by vector to limit
the compile requirements*/
#include <vector>
/** This an implementation of "File-based streams" as described in the
* ISO/IEC 14882:1998(E) C++ Standard in chapter 27 "Input/output library",
* section 8 "File-based streams". This implementation however uses PhysicsFS
* instead of the C-library's FILE functions. Also this implementation lacks
* an implementation of basic_fstream because PhysicsFS does not support
* opening a file both for reading as well as writing.
*
* This header contains similar classes and typedefs as <fstream> does. Both
* similar in names as well as operation, the main difference is that these
* classes do _not_ reside in namespace std.
*/
namespace PhysFS
{
/** Typedefinitions mandated by the C++ standard in section 27.8.1
*/
template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
class physfs_filebuf;
typedef physfs_filebuf<char> filebuf;
typedef physfs_filebuf<wchar_t> wfilebuf;
template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
class physfs_ifstream;
typedef physfs_ifstream<char> ifstream;
typedef physfs_ifstream<wchar_t> wifstream;
template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
class physfs_ofstream;
typedef physfs_ofstream<char> ofstream;
typedef physfs_ofstream<wchar_t> wofstream;
/** This class implements a C++ streambuf object for physfs files.
* So that you can use normal istream and ostream operations on them.
* This class is similar to std::basic_filebuf as defined in
* section 27.8.1.1, except in name.
*/
template<typename _CharT, typename _Traits>
class physfs_filebuf : public std::basic_streambuf<_CharT, _Traits>
{
public:
// Types:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
typedef std::basic_streambuf<_CharT, _Traits> __streambuf_type;
typedef physfs_filebuf<_CharT, _Traits> __filebuf_type;
physfs_filebuf() :
_file(0)
{
_buf.resize(BUF_SIZE);
}
virtual ~physfs_filebuf()
{
close();
}
bool is_open() const throw()
{
return (_file != 0);
}
__filebuf_type* open(const char* fileName, std::ios_base::openmode mode)
{
// Return failure if this file buf is open already
if (is_open() != false)
return 0;
// Return failure if we are requested to open a file in an unsupported mode
if (!(mode & std::ios_base::binary) ||
(mode & std::ios_base::in &&
mode & std::ios_base::out))
return 0;
// Open the file
if (mode & std::ios_base::out &&
mode & std::ios_base::app)
{
_file = PHYSFS_openAppend(fileName);
_writeStream = true;
}
else if (mode & std::ios_base::out)
{
_file = PHYSFS_openWrite(fileName);
_writeStream = true;
}
else if (mode & std::ios_base::in)
{
_file = PHYSFS_openRead(fileName);
_writeStream = false;
}
else
{
return 0;
}
if (!_file)
return 0;
if (mode & std::ios_base::ate &&
mode & std::ios_base::in)
{
if (!PHYSFS_seek(_file, PHYSFS_fileLength(_file)))
{
close();
return 0;
}
}
return this;
}
__filebuf_type* close()
{
// Return failure if this file buf is closed already
if (is_open() == false)
return 0;
sync();
if (!PHYSFS_close(_file))
{
return 0;
}
_file = 0;
return this;
}
protected:
// Read stuff:
virtual int_type underflow()
{
if (!is_open() || _writeStream)
return traits_type::eof();
if(PHYSFS_eof(_file))
{
return traits_type::eof();
}
PHYSFS_sint64 objectsRead = PHYSFS_read(_file, &*_buf.begin(), sizeof(char_type), BUF_SIZE);
if(objectsRead <= 0)
{
return traits_type::eof();
}
char_type* xend = (static_cast<size_t> (objectsRead) == BUF_SIZE) ? &*_buf.end() : &_buf[objectsRead];
this->setg(&*_buf.begin(), &*_buf.begin(), xend);
return traits_type::to_int_type(_buf.front());
}
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode)
{
if (!is_open() || _writeStream)
return pos_type(off_type(-1));
if(PHYSFS_seek(_file, static_cast<PHYSFS_uint64> (pos)) == 0)
{
return pos_type(off_type(-1));
}
// the seek invalidated the buffer
this->setg(&*_buf.begin(), &*_buf.begin(), &*_buf.begin());
return pos;
}
virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode mode)
{
if (!is_open() || _writeStream)
return pos_type(off_type(-1));
off_type pos = off;
switch (dir)
{
case std::ios_base::beg:
break;
case std::ios_base::cur:
{
PHYSFS_sint64 ptell = PHYSFS_tell(_file);
pos_type buf_tell = static_cast<pos_type> (ptell) - static_cast<pos_type> (__streambuf_type::egptr() - __streambuf_type::gptr());
if(off == 0)
{
return buf_tell;
}
pos += static_cast<off_type> (buf_tell);
break;
}
case std::ios_base::end:
pos = static_cast<off_type> (PHYSFS_fileLength(_file)) - pos;
break;
default:
//assert(!"invalid seekdirection");
return pos_type(off_type(-1));
}
return seekpos(static_cast<pos_type> (pos), mode);
}
// Write stuff:
virtual int_type overflow(int_type c = traits_type::eof())
{
if (!is_open() || !_writeStream)
return traits_type::eof();
size_t size = __streambuf_type::pptr() - __streambuf_type::pbase();
if(size == 0)
return 0;
PHYSFS_sint64 res = PHYSFS_write(_file, __streambuf_type::pbase(), sizeof(char_type), size);
if(res <= 0)
return traits_type::eof();
if(!traits_type::eq_int_type(c, traits_type::eof()))
{
PHYSFS_sint64 res = PHYSFS_write(_file, &c, sizeof(char_type), 1);
if(res <= 0)
return traits_type::eof();
}
char_type* xend = (static_cast<size_t> (res) == BUF_SIZE) ? &*_buf.end() : &_buf[res];
this->setp(&*_buf.begin(), xend);
return 0;
}
virtual int sync()
{
if (!is_open() || !_writeStream)
return -1;
return overflow(traits_type::eof());
}
virtual std::streamsize showmanyc()
{
if (!is_open() || _writeStream)
return -1;
PHYSFS_sint64 fileSize = PHYSFS_fileLength(_file);
return static_cast<int> (fileSize);
}
private:
PHYSFS_file* _file;
bool _writeStream;
vector<char_type> _buf;
};
/** This class implements a C++ istream object for physfs files.
* This class is similar to std::basic_ifstream as defined in
* section 27.8.1.5, except in name.
*/
template<typename _CharT, typename _Traits>
class physfs_ifstream : public std::basic_istream<_CharT, _Traits>
{
public:
// Types:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
typedef physfs_filebuf<_CharT, _Traits> __filebuf_type;
typedef std::basic_istream<_CharT, _Traits> __istream_type;
physfs_ifstream() throw() :
__istream_type(&_sb)
{
}
explicit physfs_ifstream(const char* fileName, std::ios_base::openmode mode = std::ios_base::binary) :
__istream_type(&_sb)
{
open(fileName, mode);
}
const __filebuf_type* rdbuf() const throw()
{
return &_sb;
}
__filebuf_type* rdbuf() throw()
{
return &_sb;
}
bool is_open() const throw()
{
return rdbuf()->is_open();
}
void open(const char* fileName, std::ios_base::openmode mode = std::ios_base::binary)
{
if (!rdbuf()->open(fileName, mode | std::ios_base::in))
{
__istream_type::setstate(std::ios_base::failbit);
}
}
void close()
{
if (!rdbuf()->close())
{
__istream_type::setstate(std::ios_base::failbit);
}
}
private:
__filebuf_type _sb;
};
/** This class implements a C++ ostream object for physfs files.
* This class is similar to std::basic_ofstream as defined in
* section 27.8.1.8, except in name.
*/
template<typename _CharT, typename _Traits>
class physfs_ofstream : public std::basic_ostream<_CharT, _Traits>
{
public:
// Types:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
typedef physfs_filebuf<_CharT, _Traits> __filebuf_type;
typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
physfs_ofstream() throw() :
__ostream_type(&_sb)
{
}
explicit physfs_ofstream(const char* fileName, std::ios_base::openmode mode = std::ios_base::binary) :
__ostream_type(&_sb)
{
open(fileName, mode);
}
const __filebuf_type* rdbuf() const throw()
{
return &_sb;
}
__filebuf_type* rdbuf() throw()
{
return &_sb;
}
bool is_open() const throw()
{
return rdbuf()->is_open();
}
void open(const char* fileName, std::ios_base::openmode mode = std::ios_base::binary)
{
if (!rdbuf()->open(fileName, mode | std::ios_base::out))
{
__ostream_type::setstate(std::ios_base::failbit);
}
}
void close()
{
if (!rdbuf()->close())
{
__ostream_type::setstate(std::ios_base::failbit);
}
}
private:
__filebuf_type _sb;
};
}
#endif // __INCLUDED_PHYSFS_STREAM_HPP__
|