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

View File

@ -22,14 +22,16 @@
/*
* Private typedefs
*/
typedef struct {
char *name;
unzFile zip;
typedef struct
{
char *name;
unzFile zip;
} zipped_t;
typedef struct {
char *name;
unzFile zip;
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,29 +84,33 @@ static char *str_slash(char *s)
/*
*
*/
void
data_setpath(char *name)
void data_setpath(char *name)
{
unzFile zip;
char *n;
if (str_zipext(name)) {
/* path has .zip extension */
n = str_slash(str_dup(name));
zip = unzOpen(n);
if (!zip) {
free(n);
sys_panic("(data) can not open data");
} else {
path.zip = zip;
path.name = n;
}
} else {
/* path has no .zip extension. it should be a directory */
/* FIXME check that it is a valid directory */
path.zip = NULL;
path.name = str_dup(name);
}
if (str_zipext(name))
{
/* path has .zip extension */
char *n = str_slash(str_dup(name));
zip = unzOpen(n);
if (!zip)
{
free(n);
sys_panic("(data) can not open data");
}
else
{
path.zip = zip;
path.name = n;
}
}
else
{
/* path has no .zip extension. it should be a directory */
/* FIXME check that it is a valid directory */
path.zip = NULL;
path.name = str_dup(name);
}
}
/*
@ -123,39 +132,41 @@ void data_closepath(void)
*/
data_file_t *data_file_open(char *name)
{
char *n;
FILE *fh;
zipped_t *z;
char *n;
FILE *fh;
zipped_t *z;
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) {
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;
}
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)
{
unzClose(z->zip);
z = NULL;
}
return (data_file_t *)z;
}
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)
{
int s;
if (!path.zip)
{
fseek((FILE *)file, 0, SEEK_END);
s = ftell((FILE *)file);
fseek((FILE *)file, 0, SEEK_SET);
}
return s;
int s;
if (!path.zip)
{
fseek((FILE *)file, 0, SEEK_END);
s = ftell((FILE *)file);
fseek((FILE *)file, 0, SEEK_SET);
}
return s;
}
/*
@ -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)
{