76c3be98b7ddaeb67342decfaf32f3b24842a224
[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 <unison/vfs/stream.hpp>
21 #include <unison/vfs/sdl/Utils.hpp>
22
23 #include <stdexcept>
24
25 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
26 {
27         // check this as PHYSFS seems to be buggy and still returns a
28         // valid pointer in this case
29         if(filename == "") {
30                 throw std::runtime_error("Couldn't open file: empty filename");
31         }
32   return Unison::VFS::SDL::Utils::open_physfs_in(filename);
33 }
34
35 #if 0
36 #include <config.h>
37
38 #include "physfs_sdl.hpp"
39
40 #include <physfs.h>
41
42 #include <stdexcept>
43 #include <sstream>
44 #include <iostream>
45
46 #include <assert.h>
47 #include "log.hpp"
48
49 static int funcSeek(struct SDL_RWops* context, int offset, int whence)
50 {
51     PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
52     int res;
53     switch(whence) {
54         case SEEK_SET:
55             res = PHYSFS_seek(file, offset);
56             break;
57         case SEEK_CUR:
58             res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
59             break;
60         case SEEK_END:
61             res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
62             break;
63         default:
64             res = 0;
65             assert(false);
66             break;
67     }
68     if(res == 0) {
69         log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
70         return -1;
71     }
72
73     return (int) PHYSFS_tell(file);
74 }
75
76 static int funcRead(struct SDL_RWops* context, void* ptr, int size, int maxnum)
77 {
78     PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
79
80     int res = PHYSFS_read(file, ptr, size, maxnum);
81     return res;
82 }
83
84 static int funcClose(struct SDL_RWops* context)
85 {
86     PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
87
88     PHYSFS_close(file);
89     delete context;
90
91     return 0;
92 }
93
94 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
95 {
96         // check this as PHYSFS seems to be buggy and still returns a
97         // valid pointer in this case
98         if(filename == "") {
99                 throw std::runtime_error("Couldn't open file: empty filename");
100         }
101
102     PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
103     if(!file) {
104         std::stringstream msg;
105         msg << "Couldn't open '" << filename << "': "
106             << PHYSFS_getLastError();
107         throw std::runtime_error(msg.str());
108     }
109
110     SDL_RWops* ops = new SDL_RWops();
111     ops->type = 0;
112     ops->hidden.unknown.data1 = file;
113     ops->seek = funcSeek;
114     ops->read = funcRead;
115     ops->write = 0;
116     ops->close = funcClose;
117     return ops;
118 }
119 #endif