From 381eb808722dec15c71ee90ddcf9bbc0a794d88b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 11 Jan 2001 00:56:31 +0000 Subject: [PATCH] beginnings of the new pak util --- tools/pak/pakfile.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tools/pak/pakfile.c diff --git a/tools/pak/pakfile.c b/tools/pak/pakfile.c new file mode 100644 index 000000000..2f2ca3bfd --- /dev/null +++ b/tools/pak/pakfile.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include + +/* + Structs for pack files on disk +*/ + +#define PAK_PATH_LENGTH + +typedef struct { + char name[PAK_PATH_LENGTH]; + int filepos, filelen; +} dpackfile_t; + +typedef struct { + char id[4]; + int dirofs; + int dirlen; +} dpackheader_t; + +#define MAX_FILES_IN_PACK 2048 + +/* + In-memory pack file structs +*/ + +typedef struct { + char name[PAK_PATH_LENGTH]; + int filepos, filelen; +} packfile_t; + +typedef struct pack_s { + char filename[MAX_PATH]; + FILE *handle; + int numfiles; + packfile_t *files; +} pack_t; + +pack_t * +new_pack (const char *name) +{ + pack_t *pack = calloc (sizeof (*pack), 1); + + if (!pack) + return 0; + strncpy (pack->filename, name, sizeof (pack->filename)); + pack->filename[sizeof (pack->filename) - 1] = 0; + return pack; +} + +void +del_pack (pack_t *pack) +{ + if (pack->files) + free (pack->files); + if (pack->handle) + fclose (pack->handle); + free (pack); +} + +pack_t * +open_pack (const char *name) +{ + dpackheader_t header; + pack_t *pack = new_pack (name); + + if (!pack) + return 0; + pack->handle = fopen (name, "rb"); + if (!pack->handle) { + goto error; + } + if (fread (&header, sizeof (header), 1, pack->handle) != sizeof (header)) { + fprintf (stderr, "%s: not a pack file", name); + goto error; + } + if (strncmp (header.id, "PACK", 4)) { + fprintf (stderr, "%s: not a pack file", name); + goto error; + } + pack->numfiles = header.dirlen / sizeof (dpackfile_t); + if (pack->numfiles > MAX_FILES_IN_PACK) { + fprintf (stderr, "%s: too many files in pack: %d", name, pack->numfiles); + goto error; + } + pack->files = malloc (numpackfiles * sizeof (packfile_t)); + if (! +error: + del_pack (pack); + return 0; +}