Skip to content

Commit

Permalink
+format SDL/*
Browse files Browse the repository at this point in the history
  • Loading branch information
gdraheim committed Aug 12, 2024
1 parent 931bc43 commit 2813dd8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 63 deletions.
86 changes: 51 additions & 35 deletions SDL/SDL_rwops_zzcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,72 @@
#include <SDL_rwops_zzip.h>
#include <stdlib.h> /* exit */

#ifndef EX_USAGE
#define EX_USAGE 64
#endif

#ifndef EX_NOINPUT
#define EX_NOINPUT 66
#endif

#ifndef EX_SOFTWARE
#define EX_SOFTWARE 70
#endif

#ifndef EX_CANTCREAT
#define EX_CANTCREAT 73
#endif

#ifndef EX_IOERR
#define EX_IOERR 74
#endif

/* mostly a copy from zzcat.c */

int main (int argc, char** argv)
int
main(int argc, char** argv)
{
static const char usage[] =
" zzcat <file>... \n"
" - prints the file to stdout. the file can be a normal file\n"
" or an inflated part of a zip-archive \n"
;

int argn;
if (argc <= 1)
{
printf (usage);
exit (0);
static const char usage[] = " zzcat <file>... \n"
" - prints the file to stdout. the file can be a normal file\n"
" or an inflated part of a zip-archive \n";
int exitcode = 0;
int argn;
if (argc <= 1) {
printf(usage);
exit(EX_USAGE);
}

for (argn=1; argn < argc; argn++)
{
SDL_RWops* rwops;
for (argn = 1; argn < argc; argn++) {
SDL_RWops* rwops;

rwops = SDL_RWFromZZIP (argv[argn], "rb");
if (! rwops)
{
perror (argv[argn]);
rwops = SDL_RWFromZZIP(argv[argn], "rb");
if (! rwops) {
perror(argv[argn]);
exitcode = EX_NOINPUT;
continue;
}else{
}
else {
char buf[17];
int n;
int n;

/* read chunks of 16 bytes into buf and print them to stdout */
while (0 < (n = SDL_RWread(rwops, buf, 1, 16)))
{
while (0 < (n = SDL_RWread(rwops, buf, 1, 16))) {
buf[n] = '\0';
# ifdef STDOUT_FILENO
write (STDOUT_FILENO, buf, n);
# else
fwrite (buf, 1, n, stdout);
# endif
#ifdef STDOUT_FILENO
write(STDOUT_FILENO, buf, n);
#else
fwrite(buf, 1, n, stdout);
#endif
}

if (n == -1)
perror (argv[argn]);
if (n == -1) {
perror(argv[argn]);
exitcode = EX_IOERR;
}

SDL_RWclose (rwops);
SDL_RWclose(rwops);
}
}

return 0;
return exitcode;
}



57 changes: 33 additions & 24 deletions SDL/SDL_rwops_zzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,67 @@
* (this example uses errno which might not be multithreaded everywhere)
*/

#include <SDL_rwops_zzip.h>
#include <zzip/zzip.h>
#include <SDL_rwops_zzip.h>
#include <string.h> /* strchr */

/* MSVC can not take a casted variable as an lvalue ! */
#define SDL_RWOPS_ZZIP_DATA(_context) \
((_context)->hidden.unknown.data1)
#define SDL_RWOPS_ZZIP_FILE(_context) (ZZIP_FILE*) \
((_context)->hidden.unknown.data1)
#define SDL_RWOPS_ZZIP_DATA(_context) ((_context)->hidden.unknown.data1)
#define SDL_RWOPS_ZZIP_FILE(_context) (ZZIP_FILE*) ((_context)->hidden.unknown.data1)

static Sint64 _zzip_seek(SDL_RWops *context, Sint64 offset, int whence)
static Sint64
_zzip_seek(SDL_RWops* context, Sint64 offset, int whence)
{
return zzip_seek(SDL_RWOPS_ZZIP_FILE(context), offset, whence);
}

static size_t _zzip_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
static size_t
_zzip_read(SDL_RWops* context, void* ptr, size_t size, size_t maxnum)
{
return zzip_read(SDL_RWOPS_ZZIP_FILE(context), ptr, size*maxnum) / size;
return zzip_read(SDL_RWOPS_ZZIP_FILE(context), ptr, size * maxnum) / size;
}

static size_t _zzip_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
static size_t
_zzip_write(SDL_RWops* context, const void* ptr, size_t size, size_t num)
{
return 0; /* ignored */
}

static int _zzip_close(SDL_RWops *context)
static int
_zzip_close(SDL_RWops* context)
{
if (! context) return 0; /* may be SDL_RWclose is called by atexit */
if (! context)
return 0; /* may be SDL_RWclose is called by atexit */

zzip_close (SDL_RWOPS_ZZIP_FILE(context));
SDL_FreeRW (context);
zzip_close(SDL_RWOPS_ZZIP_FILE(context));
SDL_FreeRW(context);
return 0;
}

SDL_RWops *SDL_RWFromZZIP(const char* file, const char* mode)
SDL_RWops*
SDL_RWFromZZIP(const char* file, const char* mode)
{
register SDL_RWops* rwops;
register ZZIP_FILE* zzip_file;

if (! strchr (mode, 'r'))
return SDL_RWFromFile(file, mode);
if (! strchr(mode, 'r'))
return SDL_RWFromFile(file, mode);

zzip_file = zzip_fopen (file, mode);
if (! zzip_file) return 0;
zzip_file = zzip_fopen(file, mode);
if (! zzip_file)
return 0;

rwops = SDL_AllocRW ();
if (! rwops) { errno=ENOMEM; zzip_close (zzip_file); return 0; }
rwops = SDL_AllocRW();
if (! rwops) {
errno = ENOMEM;
zzip_close(zzip_file);
return 0;
}

SDL_RWOPS_ZZIP_DATA(rwops) = zzip_file;
rwops->read = _zzip_read;
rwops->write = _zzip_write;
rwops->seek = _zzip_seek;
rwops->close = _zzip_close;
rwops->read = _zzip_read;
rwops->write = _zzip_write;
rwops->seek = _zzip_seek;
rwops->close = _zzip_close;
return rwops;
}
8 changes: 4 additions & 4 deletions SDL/SDL_rwops_zzip.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* You should be able to drop it in the place of a SDL_RWFromFile. Then
* go to X/share/myapp and do `cd graphics && zip -9r ../graphics.zip .`
* and rename the graphics/ subfolder - and still all your files
* are found: a filepath like X/shared/graphics/game/greetings.bmp
* will open X/shared/graphics.zip and return the zipped file
* are found: a filepath like X/shared/graphics/game/greetings.bmp
* will open X/shared/graphics.zip and return the zipped file
* game/greetings.bmp in the zip-archive (for reading that is).
*
*/
Expand All @@ -26,8 +26,8 @@
extern "C" {
#endif

extern ZZIP_DECLSPEC
SDL_RWops *SDL_RWFromZZIP(const char* file, const char* mode);
extern ZZIP_DECLSPEC SDL_RWops*
SDL_RWFromZZIP(const char* file, const char* mode);

#ifdef __cplusplus
} /* extern C */
Expand Down

0 comments on commit 2813dd8

Please sign in to comment.