Debian Codegraph @

libs/zziplib/zziplib-src/SDL/SDL_rwops_zzip.c

raw
1 /*
2 * Copyright (c) 2001 Guido Draheim <[email protected]>
3 * Use freely under the restrictions of the ZLIB License
4 *
5 * (this example uses errno which might not be multithreaded everywhere)
6 */
7
8 #include <SDL_rwops_zzip.h>
9 #include <zzip/zzip.h>
10 #include <string.h> /* strchr */
11
12 /* MSVC can not take a casted variable as an lvalue ! */
13 #define SDL_RWOPS_ZZIP_DATA(_context) \
14 ((_context)->hidden.unknown.data1)
15 #define SDL_RWOPS_ZZIP_FILE(_context) (ZZIP_FILE*) \
16 ((_context)->hidden.unknown.data1)
17
18 static int _zzip_seek(SDL_RWops *context, int offset, int whence)
19 {
20 return zzip_seek(SDL_RWOPS_ZZIP_FILE(context), offset, whence);
21 }
22
23 static int _zzip_read(SDL_RWops *context, void *ptr, int size, int maxnum)
24 {
25 return zzip_read(SDL_RWOPS_ZZIP_FILE(context), ptr, size*maxnum) / size;
26 }
27
28 static int _zzip_write(SDL_RWops *context, const void *ptr, int size, int num)
29 {
30 return 0; /* ignored */
31 }
32
33 static int _zzip_close(SDL_RWops *context)
34 {
35 if (! context) return 0; /* may be SDL_RWclose is called by atexit */
36
37 zzip_close (SDL_RWOPS_ZZIP_FILE(context));
38 SDL_FreeRW (context);
39 return 0;
40 }
41
42 SDL_RWops *SDL_RWFromZZIP(const char* file, const char* mode)
43 {
44 register SDL_RWops* rwops;
45 register ZZIP_FILE* zzip_file;
46
47 if (! strchr (mode, 'r'))
48 return SDL_RWFromFile(file, mode);
49
50 zzip_file = zzip_fopen (file, mode);
51 if (! zzip_file) return 0;
52
53 rwops = SDL_AllocRW ();
54 if (! rwops) { errno=ENOMEM; zzip_close (zzip_file); return 0; }
55
56 SDL_RWOPS_ZZIP_DATA(rwops) = zzip_file;
57 rwops->read = _zzip_read;
58 rwops->write = _zzip_write;
59 rwops->seek = _zzip_seek;
60 rwops->close = _zzip_close;
61 return rwops;
62 }