move wad_extract into the wad utility and make QFS_CreatePath more

generally usable
This commit is contained in:
Bill Currie 2004-05-09 22:58:37 +00:00
parent 3483b3b2b6
commit 67f913289c
8 changed files with 57 additions and 96 deletions

View file

@ -100,7 +100,7 @@ byte *QFS_LoadTempFile (const char *path);
byte *QFS_LoadHunkFile (const char *path);
void QFS_LoadCacheFile (const char *path, struct cache_user_s *cu);
void QFS_CreatePath (const char *path);
int QFS_CreatePath (const char *path);
int QFS_Rename (const char *old, const char *new);
int QFS_Remove (const char *path);
int QFS_NextFilename (struct dstring_s *filename, const char *prefix,

View file

@ -53,7 +53,7 @@ typedef struct date_s {
} date_t;
int Sys_FileTime (const char *path);
void Sys_mkdir (const char *path);
int Sys_mkdir (const char *path);
typedef void (*sys_printf_t) (const char *fmt, va_list args);

View file

@ -96,7 +96,6 @@ int wad_add (wad_t *wad, const char *filename, const char *lumpname,
byte type);
int wad_add_data (wad_t *wad, const char *lumpname, byte type,
const void *data, int bytes);
int wad_extract (wad_t *wad, lumpinfo_t *pf);
lumpinfo_t *wad_find_lump (wad_t *wad, const char *filename);
#endif//__QF_wadfile_h

View file

@ -46,6 +46,7 @@ static __attribute__ ((unused)) const char rcsid[] =
#include "QF/pakfile.h"
#include "QF/qendian.h"
#include "QF/quakefs.h"
static const char *
pack_get_key (void *p, void *unused)
@ -248,29 +249,6 @@ pack_add (pack_t *pack, const char *filename)
return 0;
}
static int
make_parents (const char *_path)
{
char *path;
char *d, *p, t;
path = (char *) alloca (strlen (_path) + 1);
strcpy (path, _path);
for (p = path; *p && (d = strchr (p, '/')); p = d + 1) {
t = *d;
*d = 0;
#ifdef _WIN32
if (mkdir (path) < 0)
#else
if (mkdir (path, 0777) < 0)
#endif
if (errno != EEXIST)
return -1;
*d = t;
}
return 0;
}
int
pack_extract (pack_t *pack, dpackfile_t *pf)
{
@ -280,7 +258,7 @@ pack_extract (pack_t *pack, dpackfile_t *pf)
QFile *file;
char buffer[16384];
if (make_parents (name) == -1)
if (QFS_CreatePath (name) == -1)
return -1;
if (!(file = Qopen (name, "wb")))
return -1;

View file

@ -629,12 +629,7 @@ QFS_WriteBuffers (const char *filename, int count, ...)
va_end (args);
}
/*
QFS_CreatePath
Only used for CopyFile and download
*/
void
int
QFS_CreatePath (const char *path)
{
char *ofs;
@ -644,10 +639,12 @@ QFS_CreatePath (const char *path)
for (ofs = e_path + 1; *ofs; ofs++) {
if (*ofs == '/') { // create the directory
*ofs = 0;
Sys_mkdir (e_path);
if (Sys_mkdir (e_path) == -1)
return -1;
*ofs = '/';
}
}
return 0;
}
static QFile *
@ -1319,7 +1316,7 @@ QFile *
QFS_Open (const char *path, const char *mode)
{
dstring_t *full_path = dstring_new ();
QFile *file;
QFile *file = 0;
const char *m;
char *cpath;
int write = 0;
@ -1327,7 +1324,6 @@ QFS_Open (const char *path, const char *mode)
cpath = QFS_CompressPath (path);
if (contains_updir (cpath, 0)) {
errno = EACCES;
file = 0;
} else {
dsprintf (full_path, "%s/%s", qfs_userpath, cpath);
Sys_DPrintf ("QFS_Open: %s %s\n", full_path->str, mode);
@ -1335,9 +1331,11 @@ QFS_Open (const char *path, const char *mode)
if (*m == 'w' || *m == '+' || *m == 'a')
write = 1;
if (write)
QFS_CreatePath (full_path->str);
if (QFS_CreatePath (full_path->str) == -1)
goto done;
file = Qopen (full_path->str, mode);
}
done:
dstring_delete (full_path);
free (cpath);
return file;
@ -1364,8 +1362,8 @@ QFS_Rename (const char *old, const char *new)
dsprintf (full_old, "%s/%s", qfs_userpath, old);
dsprintf (full_new, "%s/%s", qfs_userpath, new);
QFS_CreatePath (full_new->str);
ret = Qrename (full_old->str, full_new->str);
if ((ret = QFS_CreatePath (full_new->str)) != -1)
ret = Qrename (full_old->str, full_new->str);
dstring_delete (full_old);
dstring_delete (full_new);
return ret;

View file

@ -145,27 +145,28 @@ Sys_MaskFPUExceptions (void)
}
#endif
void
int
Sys_mkdir (const char *path)
{
#ifdef HAVE_MKDIR
# ifdef _WIN32
if (mkdir (path) == 0)
return;
return 0;
# else
if (mkdir (path, 0777) == 0)
return;
return 0;
# endif
#else
# ifdef HAVE__MKDIR
if (_mkdir (path) == 0)
return;
return 0;
# else
# error do not know how to make directories
# endif
#endif
if (errno != EEXIST)
Sys_Error ("mkdir %s: %s", path, strerror (errno));
return -1;
return 0;
}
int

View file

@ -316,56 +316,6 @@ wad_add_data (wad_t *wad, const char *lumpname, byte type, const void *data,
return 0;
}
static int
make_parents (const char *_path)
{
char *path;
char *d, *p, t;
path = (char *) alloca (strlen (_path) + 1);
strcpy (path, _path);
for (p = path; *p && (d = strchr (p, '/')); p = d + 1) {
t = *d;
*d = 0;
#ifdef _WIN32
if (mkdir (path) < 0)
#else
if (mkdir (path, 0777) < 0)
#endif
if (errno != EEXIST)
return -1;
*d = t;
}
return 0;
}
int
wad_extract (wad_t *wad, lumpinfo_t *pf)
{
const char *name = pf->name;
size_t count;
int len;
QFile *file;
char buffer[16384];
if (make_parents (name) == -1)
return -1;
if (!(file = Qopen (name, "wb")))
return -1;
Qseek (wad->handle, pf->filepos, SEEK_SET);
len = pf->size;
while (len) {
count = len;
if (count > sizeof (buffer))
count = sizeof (buffer);
count = Qread (wad->handle, buffer, count);
Qwrite (file, buffer, count);
len -= count;
}
Qclose (file);
return 0;
}
lumpinfo_t *
wad_find_lump (wad_t *wad, const char *lumpname)
{

View file

@ -46,6 +46,7 @@ static __attribute__ ((unused)) const char rcsid[] =
#include <stdlib.h>
#include <QF/qtypes.h>
#include <QF/quakefs.h>
#include <QF/wadfile.h>
#include "wad.h"
@ -147,6 +148,33 @@ decode_args (int argc, char **argv)
return optind;
}
static int
wad_extract (wad_t *wad, lumpinfo_t *pf)
{
const char *name = pf->name;
size_t count;
int len;
QFile *file;
char buffer[16384];
if (QFS_CreatePath (name) == -1)
return -1;
if (!(file = Qopen (name, "wb")))
return -1;
Qseek (wad->handle, pf->filepos, SEEK_SET);
len = pf->size;
while (len) {
count = len;
if (count > sizeof (buffer))
count = sizeof (buffer);
count = Qread (wad->handle, buffer, count);
Qwrite (file, buffer, count);
len -= count;
}
Qclose (file);
return 0;
}
int
main (int argc, char **argv)
{
@ -203,11 +231,18 @@ main (int argc, char **argv)
return 1;
}
for (i = 0; i < wad->numlumps; i++) {
if (options.verbosity >= 2)
printf ("%6d ", wad->lumps[i].filepos);
if (options.verbosity >= 3)
printf ("%6d ", wad->lumps[i].disksize);
if (options.verbosity >= 1)
printf ("%6d ", wad->lumps[i].size);
if (options.verbosity >= 0)
printf ("%3d %s\n", wad->lumps[i].type,
wad->lumps[i].name);
printf ("%3d ", wad->lumps[i].type);
if (options.verbosity >= 3)
printf ("%02x ", wad->lumps[i].compression);
if (options.verbosity >= 0)
printf ("%s\n", wad->lumps[i].name);
}
wad_close (wad);
break;