4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
22 #include "physfs_sdl.hpp"
33 static int funcSeek(struct SDL_RWops* context, int offset, int whence)
35 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
39 res = PHYSFS_seek(file, offset);
42 res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
45 res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
53 log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
57 return (int) PHYSFS_tell(file);
60 static int funcRead(struct SDL_RWops* context, void* ptr, int size, int maxnum)
62 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
64 int res = PHYSFS_read(file, ptr, size, maxnum);
68 static int funcClose(struct SDL_RWops* context)
70 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
78 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
80 // check this as PHYSFS seems to be buggy and still returns a
81 // valid pointer in this case
83 throw std::runtime_error("Couldn't open file: emtpy filename");
86 PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
88 std::stringstream msg;
89 msg << "Couldn't open '" << filename << "': "
90 << PHYSFS_getLastError();
91 throw std::runtime_error(msg.str());
94 SDL_RWops* ops = new SDL_RWops();
96 ops->hidden.unknown.data1 = file;
100 ops->close = funcClose;