Fix accidental switch/teleporter behavior changes
[neverball] / share / fs_rwops.c
1 /*
2  * Copyright (C) 2003-2010 Neverball authors
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include "fs_rwops.h"
16
17 static int rwops_seek(SDL_RWops *ctx, int offset, int whence)
18 {
19     fs_file fh = ctx->hidden.unknown.data1;
20     return fs_seek(fh, offset, whence) ? fs_tell(fh) : -1;
21 }
22
23 static int rwops_read(SDL_RWops *ctx, void *ptr, int size, int maxnum)
24 {
25     return fs_read(ptr, size, maxnum, ctx->hidden.unknown.data1);
26 }
27
28 static int rwops_write(SDL_RWops *ctx, const void *ptr, int size, int num)
29 {
30     return fs_write(ptr, size, num, ctx->hidden.unknown.data1);
31 }
32
33 static int rwops_close(SDL_RWops *ctx)
34 {
35     fs_file fh = ctx->hidden.unknown.data1;
36
37     if (!fs_close(fh))
38         return -1;
39
40     SDL_FreeRW(ctx);
41     return 0;
42 }
43
44 SDL_RWops *fs_rwops_make(fs_file fh)
45 {
46     SDL_RWops *ctx;
47
48     if ((ctx = SDL_AllocRW()))
49     {
50         ctx->seek  = rwops_seek;
51         ctx->read  = rwops_read;
52         ctx->write = rwops_write;
53         ctx->close = rwops_close;
54
55         ctx->hidden.unknown.data1 = fh;
56     }
57
58     return ctx;
59 }
60
61 SDL_RWops *fs_rwops_open(const char *path, const char *mode)
62 {
63     fs_file fh;
64
65     if ((fh = fs_open(path, mode)))
66         return fs_rwops_make(fh);
67
68     return NULL;
69 }