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"
25 #include "util/log.hpp"
27 static Sint64 funcSeek(struct SDL_RWops* context, Sint64 offset, int whence)
29 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
33 res = PHYSFS_seek(file, offset);
36 res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
39 res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
47 log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
51 return (int) PHYSFS_tell(file);
54 static size_t funcRead(struct SDL_RWops* context, void* ptr, size_t size, size_t maxnum)
56 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
58 int res = PHYSFS_read(file, ptr, size, maxnum);
62 static int funcClose(struct SDL_RWops* context)
64 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
72 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
74 // check this as PHYSFS seems to be buggy and still returns a
75 // valid pointer in this case
77 throw std::runtime_error("Couldn't open file: empty filename");
80 PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
82 std::stringstream msg;
83 msg << "Couldn't open '" << filename << "': "
84 << PHYSFS_getLastError();
85 throw std::runtime_error(msg.str());
88 SDL_RWops* ops = new SDL_RWops();
90 ops->hidden.unknown.data1 = file;
94 ops->close = funcClose;