2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 3 of the License, or
7 // (at your option) any later version.
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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "physfs/physfs_sdl.hpp"
24 #include "util/log.hpp"
26 static Sint64 funcSeek(struct SDL_RWops* context, Sint64 offset, int whence)
28 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
32 res = PHYSFS_seek(file, offset);
35 res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
38 res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
46 log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
50 return (int) PHYSFS_tell(file);
53 static size_t funcRead(struct SDL_RWops* context, void* ptr, size_t size, size_t maxnum)
55 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
57 int res = PHYSFS_read(file, ptr, size, maxnum);
61 static int funcClose(struct SDL_RWops* context)
63 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
71 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
73 // check this as PHYSFS seems to be buggy and still returns a
74 // valid pointer in this case
76 throw std::runtime_error("Couldn't open file: empty filename");
79 PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
81 std::stringstream msg;
82 msg << "Couldn't open '" << filename << "': "
83 << PHYSFS_getLastError();
84 throw std::runtime_error(msg.str());
87 SDL_RWops* ops = new SDL_RWops();
89 ops->hidden.unknown.data1 = file;
93 ops->close = funcClose;