]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - pak.c
Flatten more external functions
[xonotic/gmqcc.git] / pak.c
diff --git a/pak.c b/pak.c
index 494ad7fd466679791f24fa40c640b51322fc7303..a9ef112869066920a06b46608926fc3cdf47f823 100644 (file)
--- a/pak.c
+++ b/pak.c
@@ -20,8 +20,6 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include <sys/stat.h>
-#include <dirent.h>
 #include "gmqcc.h"
 
 /*
@@ -96,7 +94,6 @@ static void pak_tree_build(const char *entry) {
     directory = (char *)mem_a(56);
 
     memset(pathsplit, 0, 56);
-    memset(directory, 0, 56);
 
     strncpy(directory, entry, 56);
     for (itr = 0; (token = pak_tree_sep(&directory, "/")) != NULL; itr++) {
@@ -197,6 +194,7 @@ static pak_file_t *pak_open_write(const char *file) {
          */   
         /* TODO backup directory clean */
 
+        mem_d(pak);
         return NULL;
     }
 
@@ -210,6 +208,9 @@ static pak_file_t *pak_open_write(const char *file) {
     pak->insert       = true;
     pak->header.magic = PAK_FOURCC;
 
+    /* on BE systems we need to swap the byte order of the FOURCC */
+    util_endianswap(&pak->header.magic, 1, sizeof(uint32_t));
+
     /*
      * We need to write out the header since files will be wrote out to
      * this even with directory entries, and that not wrote.  The header
@@ -258,9 +259,10 @@ bool pak_exists(pak_file_t *pak, const char *file, pak_directory_t **dir) {
 /*
  * Extraction abilities.  These work as you expect them to.
  */ 
-bool pak_extract_one(pak_file_t *pak, const char *file) {
-    pak_directory_t *dir = NULL;
-    unsigned char   *dat = NULL;
+bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdir) {
+    pak_directory_t *dir   = NULL;
+    unsigned char   *dat   = NULL;
+    char            *local = NULL;
     FILE            *out;
 
     if (!pak_exists(pak, file, &dir)) {
@@ -277,15 +279,20 @@ bool pak_extract_one(pak_file_t *pak, const char *file) {
      */   
     pak_tree_build(file);
 
+    /* TODO portable path seperators */
+    util_asprintf(&local, "%s/%s", outdir, file);
+
     /*
      * Now create the file, if this operation fails.  Then abort
      * It shouldn't fail though.
      */   
-    if (!(out = fs_file_open(file, "wb"))) {
+    if (!(out = fs_file_open(local, "wb"))) {
         mem_d(dat);
         return false;
     }
 
+    /* free memory for directory string */
+    mem_d(local);
 
     /* read */
     fs_file_seek (pak->handle, dir->pos, SEEK_SET);
@@ -309,11 +316,8 @@ bool pak_extract_all(pak_file_t *pak, const char *dir) {
     if (!fs_dir_make(dir))
         return false;
 
-    if (fs_dir_change(dir))
-        return false;
-
     for (itr = 0; itr < vec_size(pak->directories); itr++) {
-        if (!pak_extract_one(pak, pak->directories[itr].name))
+        if (!pak_extract_one(pak, pak->directories[itr].name, dir))
             return false;
     }
 
@@ -360,7 +364,7 @@ bool pak_insert_one(pak_file_t *pak, const char *file) {
         return false;
     }
 
-    strcpy(dir.name, file);
+    strncpy(dir.name, file, strlen(file));
 
     /*
      * Allocate some memory for loading in the data that will be
@@ -476,7 +480,6 @@ int main(int argc, char **argv) {
     bool          extract   = true;
     char         *redirout  = (char*)stdout;
     char         *redirerr  = (char*)stderr;
-    char         *directory = NULL;
     char         *file      = NULL;
     char        **files     = NULL;
     pak_file_t   *pak       = NULL;
@@ -497,8 +500,6 @@ int main(int argc, char **argv) {
                 continue;
             if (parsecmd("redirerr",  &argc, &argv, &redirerr,  1, false))
                 continue;
-            if (parsecmd("directory", &argc, &argv, &directory, 1, false))
-                continue;
             if (parsecmd("file",      &argc, &argv, &file,      1, false))
                 continue;
 
@@ -541,7 +542,7 @@ int main(int argc, char **argv) {
             return EXIT_FAILURE;
         }
 
-        if (!pak_extract_all(pak, (directory) ? directory : "./")) {
+        if (!pak_extract_all(pak, "./")) {
             con_err("failed to extract PAK %s (files may be missing)\n", file);
             pak_close(pak);
             vec_free(files);
@@ -561,13 +562,6 @@ int main(int argc, char **argv) {
         return EXIT_FAILURE;
     }
 
-    if (directory && !fs_dir_change(directory)) {
-        con_err("failed to change directory %s\n", directory);
-        pak_close(pak);
-        vec_free(files);
-        return EXIT_FAILURE;
-    }
-
     for (iter = 0; iter < vec_size(files); iter++) {
         if (!(pak_insert_one(pak, files[iter]))) {
             con_err("failed inserting %s for PAK %s\n", files[iter], file);