This commit is contained in:
twinaphex 2021-10-01 21:50:56 +02:00
parent 9aeae11841
commit a54f410b44

View File

@ -22,12 +22,14 @@
/*
* Private typedefs
*/
typedef struct {
typedef struct
{
char *name;
unzFile zip;
} zipped_t;
typedef struct {
typedef struct
{
char *name;
unzFile zip;
} path_t;
@ -43,13 +45,17 @@ static path_t path;
static int str_zipext(char *name)
{
int i = strlen(name) - 1;
if (i < 0 || name[i] != 'p' && name[i] != 'P') return 0;
if (i < 0 || name[i] != 'p' && name[i] != 'P')
return 0;
i--;
if (i < 0 || name[i] != 'i' && name[i] != 'I') return 0;
if (i < 0 || name[i] != 'i' && name[i] != 'I')
return 0;
i--;
if (i < 0 || name[i] != 'z' && name[i] != 'Z') return 0;
if (i < 0 || name[i] != 'z' && name[i] != 'Z')
return 0;
i--;
if (i < 0 || name[i] != '.') return 0;
if (i < 0 || name[i] != '.')
return 0;
i--;
if (i < 0)
return 0;
@ -67,9 +73,8 @@ static char *str_dup(char *s)
static char *str_slash(char *s)
{
#ifdef __WIN32__
int i, l;
l = strlen(s);
unsigned i;
int l = strlen(s);
for (i = 0; i < l; i++)
if (s[i] == '/') s[i] = '\\';
#endif
@ -79,24 +84,28 @@ static char *str_slash(char *s)
/*
*
*/
void
data_setpath(char *name)
void data_setpath(char *name)
{
unzFile zip;
char *n;
if (str_zipext(name)) {
if (str_zipext(name))
{
/* path has .zip extension */
n = str_slash(str_dup(name));
char *n = str_slash(str_dup(name));
zip = unzOpen(n);
if (!zip) {
if (!zip)
{
free(n);
sys_panic("(data) can not open data");
} else {
}
else
{
path.zip = zip;
path.name = n;
}
} else {
}
else
{
/* path has no .zip extension. it should be a directory */
/* FIXME check that it is a valid directory */
path.zip = NULL;
@ -127,24 +136,26 @@ data_file_t *data_file_open(char *name)
FILE *fh;
zipped_t *z;
if (path.zip) {
if (path.zip)
{
z = malloc(sizeof(zipped_t));
z->name = strdup(name);
z->zip = unzDup(path.zip);
if ( unzLocateFile(z->zip, name, 0) != UNZ_OK ||
unzOpenCurrentFile(z->zip) != UNZ_OK) {
unzOpenCurrentFile(z->zip) != UNZ_OK)
{
unzClose(z->zip);
z = NULL;
}
return (data_file_t *)z;
} else {
}
n = malloc(strlen(path.name) + strlen(name) + 2);
sprintf(n, "%s/%s", path.name, name);
str_slash(n);
fh = fopen(n, "rb");
return (data_file_t *)fh;
}
}
int data_file_size(data_file_t *file)
{
@ -183,19 +194,18 @@ int data_file_tell(data_file_t *file)
/*
* Read a file within a data archive.
*/
int
data_file_read(data_file_t *file, void *buf, size_t size, size_t count)
int data_file_read(data_file_t *file, void *buf, size_t size, size_t count)
{
if (path.zip)
return unzReadCurrentFile(((zipped_t *)file)->zip, buf, size * count) / size;
return unzReadCurrentFile(((zipped_t *)file)->zip,
buf, size * count) / size;
return fread(buf, size, count, (FILE *)file);
}
/*
* Close a file within a data archive.
*/
void
data_file_close(data_file_t *file)
void data_file_close(data_file_t *file)
{
if (path.zip)
{