From: Dale Weiler Date: Thu, 7 Feb 2013 17:21:15 +0000 (+0000) Subject: Cleanups and fixes to the PAK utility, can now extract/insert, tested and functional... X-Git-Tag: before-library~134 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=8ecfc51799037c962b35afbdfd20bc762c6eb1b3;p=xonotic%2Fgmqcc.git Cleanups and fixes to the PAK utility, can now extract/insert, tested and functional for the most part. --- diff --git a/pak.c b/pak.c index 7d2199c..7689c61 100644 --- a/pak.c +++ b/pak.c @@ -129,7 +129,7 @@ typedef struct { bool insert; } pak_file_t; -pak_file_t *pak_open_read(const char *file) { +static pak_file_t *pak_open_read(const char *file) { pak_file_t *pak; size_t itr; @@ -179,7 +179,7 @@ pak_file_t *pak_open_read(const char *file) { return pak; } -pak_file_t *pak_open_write(const char *file) { +static pak_file_t *pak_open_write(const char *file) { pak_file_t *pak; if (!(pak = mem_a(sizeof(pak_file_t)))) @@ -227,6 +227,18 @@ pak_file_t *pak_open_write(const char *file) { return pak; } +pak_file_t *pak_open(const char *file, const char *mode) { + if (!file || !mode) + return NULL; + + switch (*mode) { + case 'r': return pak_open_read (file); + case 'w': return pak_open_write(file); + } + + return NULL; +} + bool pak_exists(pak_file_t *pak, const char *file, pak_directory_t **dir) { size_t itr; @@ -343,6 +355,17 @@ bool pak_insert_one(pak_file_t *pak, const char *file) { dir.pos = ftell(pak->handle); + /* + * We're limited to 56 bytes for a file name string, that INCLUDES + * the directory and '/' seperators. + */ + if (strlen(file) >= 56) { + file_close(fp); + return false; + } + + strcpy(dir.name, file); + /* * Allocate some memory for loading in the data that will be * redirected into the PAK file. @@ -356,6 +379,12 @@ bool pak_insert_one(pak_file_t *pak, const char *file) { file_close(fp); file_write(dat, dir.len, 1, pak->handle); + /* + * Now add the directory to the directories vector, so pak_close + * can actually write it. + */ + vec_push(pak->directories, dir); + return true; } @@ -417,14 +446,15 @@ bool pak_close(pak_file_t *pak) { return true; } -/* test extraction */ #if 0 +/* test extraction */ int main() { - pak_file_t *pak = pak_open_read("pak0.pak"); + pak_file_t *pak = pak_open("pak0.pak", "r"); if (!pak) abort(); pak_extract_all(pak); + pak_close(pak); return 0; } #endif