2 * Copyright (c) 2001 Guido Draheim <guidod@gmx.de>
3 * Use freely under the restrictions of the ZLIB License
5 * (this example uses errno which might not be multithreaded everywhere)
8 #include <SDL_rwops_zzip.h>
10 #include <string.h> /* strchr */
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)
18 static int _zzip_seek(SDL_RWops
*context
, int offset
, int whence
)
20 return zzip_seek(SDL_RWOPS_ZZIP_FILE(context
), offset
, whence
);
23 static int _zzip_read(SDL_RWops
*context
, void *ptr
, int size
, int maxnum
)
25 return zzip_read(SDL_RWOPS_ZZIP_FILE(context
), ptr
, size
*maxnum
) / size
;
28 static int _zzip_write(SDL_RWops
*context
, const void *ptr
, int size
, int num
)
30 return 0; /* ignored */
33 static int _zzip_close(SDL_RWops
*context
)
35 if (! context
) return 0; /* may be SDL_RWclose is called by atexit */
37 zzip_close (SDL_RWOPS_ZZIP_FILE(context
));
42 SDL_RWops
*SDL_RWFromZZIP(const char* file
, const char* mode
)
44 register SDL_RWops
* rwops
;
45 register ZZIP_FILE
* zzip_file
;
47 if (! strchr (mode
, 'r'))
48 return SDL_RWFromFile(file
, mode
);
50 zzip_file
= zzip_fopen (file
, mode
);
51 if (! zzip_file
) return 0;
53 rwops
= SDL_AllocRW ();
54 if (! rwops
) { errno
=ENOMEM
; zzip_close (zzip_file
); return 0; }
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
;