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