9fa1ccda95f2d989e688a268232ec949ee29acf8
[supertux.git] / src / physfs / physfs_sdl.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "physfs_sdl.hpp"
23
24 #include <physfs.h>
25
26 #include <stdexcept>
27 #include <sstream>
28 #include <iostream>
29
30 #include <assert.h>
31 #include "log.hpp"
32
33 static int funcSeek(struct SDL_RWops* context, int offset, int whence)
34 {
35     PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
36     int res;
37     switch(whence) {
38         case SEEK_SET:
39             res = PHYSFS_seek(file, offset);
40             break;
41         case SEEK_CUR:
42             res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
43             break;
44         case SEEK_END:
45             res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
46             break;
47         default:
48             res = 0;
49             assert(false);
50             break;
51     }
52     if(res == 0) {
53         log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
54         return -1;
55     }
56
57     return (int) PHYSFS_tell(file);
58 }
59
60 static int funcRead(struct SDL_RWops* context, void* ptr, int size, int maxnum)
61 {
62     PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
63
64     int res = PHYSFS_read(file, ptr, size, maxnum);
65     return res;
66 }
67
68 static int funcClose(struct SDL_RWops* context)
69 {
70     PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
71
72     PHYSFS_close(file);
73     delete context;
74
75     return 0;
76 }
77
78 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
79 {
80     // check this as PHYSFS seems to be buggy and still returns a
81     // valid pointer in this case
82     if(filename == "") {
83         throw std::runtime_error("Couldn't open file: empty filename");
84     }
85
86     PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
87     if(!file) {
88         std::stringstream msg;
89         msg << "Couldn't open '" << filename << "': "
90             << PHYSFS_getLastError();
91         throw std::runtime_error(msg.str());
92     }
93
94     SDL_RWops* ops = new SDL_RWops();
95     ops->type = 0;
96     ops->hidden.unknown.data1 = file;
97     ops->seek = funcSeek;
98     ops->read = funcRead;
99     ops->write = 0;
100     ops->close = funcClose;
101     return ops;
102 }