archivers/qpak.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 22 Nov 2002 06:24:10 +0000
changeset 504 3420d82f9b01
parent 494 364e564a26e2
child 543 90607e53b9b2
permissions -rw-r--r--
Some cleanups for PocketPC port.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Quake PAK support routines for PhysicsFS.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles id Software Quake PAK files.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * Please see the file LICENSE in the source's root directory.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *  This file written by Ed Sinjiashvili.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#if HAVE_CONFIG_H
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#  include <config.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#endif
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#if (defined PHYSFS_SUPPORTS_QPAK)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <stdio.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include <stdlib.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#include <string.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include "physfs.h"
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
#define __PHYSICSFS_INTERNAL__
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#include "physfs_internal.h"
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
#define QPAK_MAXDIRLEN 60
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
typedef struct
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
    char          name[56];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    PHYSFS_uint32 offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    PHYSFS_uint32 size;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
} QPAKentry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
typedef struct tagQPAKdirentry
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    char                   *name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    QPAKentry              *entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    struct tagQPAKdirentry *next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
} QPAKdirentry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
typedef struct QPAKDirectory
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    char name[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    struct QPAKDirectory *dirs, *next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    QPAKdirentry *files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
} QPAKdirectory;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
typedef struct
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    void               *handle;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    char               *filename;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    PHYSFS_uint32       dirOffset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    PHYSFS_uint32       totalEntries;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    QPAKentry          *entries;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    QPAKdirectory      *root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
} QPAKinfo;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
typedef struct
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    void         *handle;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    QPAKentry    *entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    PHYSFS_sint64 curPos;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
} QPAKfileinfo;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
static int           QPAK_isArchive(const char *filename, int forWriting);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
static DirHandle    *QPAK_openArchive(const char *name, int forWriting);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
static void          QPAK_dirClose(DirHandle *h);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
                                             int omitSymLinks);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
static int           QPAK_exists(DirHandle *h, const char *name);
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    73
static int           QPAK_isDirectory(DirHandle *h, const char *name, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    74
static int           QPAK_isSymLink(DirHandle *h, const char *name, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    75
static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *n, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    76
static FileHandle   *QPAK_openRead(DirHandle *h, const char *name, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    77
static FileHandle   *QPAK_openWrite(DirHandle *h, const char *name);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    78
static FileHandle   *QPAK_openAppend(DirHandle *h, const char *name);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    82
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    83
static PHYSFS_sint64 QPAK_write(FileHandle *handle, const void *buffer,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    84
                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
static int           QPAK_eof(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
static PHYSFS_sint64 QPAK_tell(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
static int           QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
static int           QPAK_fileClose(FileHandle *handle);
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    90
static int           QPAK_remove(DirHandle *h, const char *name);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    91
static int           QPAK_mkdir(DirHandle *h, const char *name);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    "PAK",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    "Quake PAK file format",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    "Ed Sinjiashvili <slimb@swes.saren.ru>",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    "http://icculus.org/physfs/",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
static const FileFunctions __PHYSFS_FileFunctions_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    QPAK_read,               /* read() method       */
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   105
    QPAK_write,              /* write() method      */
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
    QPAK_eof,                /* eof() method        */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    QPAK_tell,               /* tell() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    QPAK_seek,               /* seek() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    QPAK_fileLength,         /* fileLength() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    QPAK_fileClose           /* fileClose() method  */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
const DirFunctions __PHYSFS_DirFunctions_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    &__PHYSFS_ArchiveInfo_QPAK,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    QPAK_isArchive,          /* isArchive() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    QPAK_openArchive,        /* openArchive() method    */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    QPAK_enumerateFiles,     /* enumerateFiles() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    QPAK_exists,             /* exists() method         */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    QPAK_isDirectory,        /* isDirectory() method    */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    QPAK_isSymLink,          /* isSymLink() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    QPAK_getLastModTime,     /* getLastModTime() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    QPAK_openRead,           /* openRead() method       */
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   124
    QPAK_openWrite,          /* openWrite() method      */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   125
    QPAK_openAppend,         /* openAppend() method     */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   126
    QPAK_remove,             /* remove() method         */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   127
    QPAK_mkdir,              /* mkdir() method          */
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    QPAK_dirClose            /* dirClose() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
#define QPAK_MAGIC 0x4B434150  /* look like "PACK" in ascii. */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
/*
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
 * Read an unsigned 32-bit int and swap to native byte order.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
 */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
static int readui32(void *in, PHYSFS_uint32 *val)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    PHYSFS_uint32 v;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    *val = PHYSFS_swapULE32(v);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
} /* readui32 */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
static int openQPak(const char *filename, int forWriting, void **fileHandle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    PHYSFS_uint32 sig;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    *fileHandle = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    *fileHandle = __PHYSFS_platformOpenRead(filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    BAIL_IF_MACRO(*fileHandle == NULL, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    if (!readui32(*fileHandle, &sig))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
        goto openPak_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    
456
ecaab6f9e19f Fixed logic bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   160
    if (sig != QPAK_MAGIC)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
        goto openPak_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
openPak_failed:
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    if (*fileHandle != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
        __PHYSFS_platformClose(*fileHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    *fileHandle = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
} /* openQPak */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
static int QPAK_isArchive(const char *filename, int forWriting)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    void *fileHandle;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    int retval = openQPak(filename, forWriting, &fileHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    if (fileHandle != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
        __PHYSFS_platformClose(fileHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
} /* QPAK_isArchive */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
static int qpak_loadEntries(void *fh, int dirOffset, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
                            QPAKentry *entries)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
{
494
364e564a26e2 Win32 fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 467
diff changeset
   192
    PHYSFS_sint32 i;
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    BAIL_IF_MACRO(__PHYSFS_platformSeek(fh, dirOffset) == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    for (i = 0; i < numEntries; i++, entries++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        PHYSFS_sint64 r = __PHYSFS_platformRead(fh, entries->name, 56, 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        BAIL_IF_MACRO(r == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        BAIL_IF_MACRO(!readui32(fh, &entries->offset), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
        BAIL_IF_MACRO(!readui32(fh, &entries->size), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    } /* for */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
} /* qpak_loadEntries */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
static QPAKdirentry *qpak_newDirentry(char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
    QPAKdirentry *retval = (QPAKdirentry *) malloc(sizeof (QPAKdirentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    retval->name  = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    retval->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    retval->next  = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
} /* qpak_newDirentry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
static void qpak_deleteDirentry(QPAKdirentry *e)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    while (e != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
        QPAKdirentry *next = e->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
        free(e);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
        e = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
} /* qpak_deleteDirentry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
static QPAKdirectory *qpak_newDirectory(char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    QPAKdirectory *dir = (QPAKdirectory *) malloc(sizeof (QPAKdirectory));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    strcpy(dir->name, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    dir->dirs = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    dir->next = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    dir->files = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    return dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
} /* qpak_newDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
static void qpak_deleteDirectory(QPAKdirectory *d)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    while (d != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
        QPAKdirectory *next = d->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
        qpak_deleteDirentry(d->files);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
        qpak_deleteDirectory(d->dirs);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        free(d);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
        d = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
} /* qpak_deleteDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
static int qpak_addFile(QPAKdirectory *dir, char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    QPAKdirentry *file = qpak_newDirentry(name, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    if (file == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    /* !!! FIXME: Traversing a linkedlist gets slower with each added file. */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    if (dir->files == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        dir->files = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
        QPAKdirentry *tail = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
            tail = tail->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
        tail->next = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
} /* qpak_addFile */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
static QPAKdirectory *qpak_findDirectory(QPAKdirectory *root, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
    if (p == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
        while (thisDir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
            if (strcmp(thisDir->name, name) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
                return(thisDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
        strncpy (temp, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        temp[p - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
        while (thisDir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
            if (strcmp(thisDir->name, temp) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
                return(qpak_findDirectory(thisDir, p + 1));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   314
    BAIL_MACRO(ERR_NO_SUCH_PATH, 0);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
} /* qpak_findDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
static QPAKdirectory *qpak_addDir(QPAKdirectory *dir, char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    QPAKdirectory *newDir = qpak_findDirectory(dir, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    if (newDir != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
        return(newDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    newDir = qpak_newDirectory(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    if (newDir == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
        return 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
    if (dir->dirs == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        dir->dirs = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
        QPAKdirectory *tail = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
            tail = tail->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        tail->next = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
    return(newDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
} /* qpak_addDir */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
static int qpak_addEntry(QPAKdirectory *dir, char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    char tempName[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    QPAKdirectory *child;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    if (p == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
        return(qpak_addFile(dir, name, entry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    strncpy(tempName, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    tempName[p - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
    child = qpak_addDir(dir, tempName);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    return(qpak_addEntry(child, p + 1, entry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
} /* qpak_addEntry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
static QPAKentry *qpak_findEntry(QPAKdirectory *root, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    QPAKdirectory *dir = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    QPAKdirentry *thisFile = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    const char *t = strrchr (name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    if (t == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
        dir = root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
        t = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
        strncpy(temp, name, t - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
        temp[t - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
        dir = qpak_findDirectory(root, temp);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
        t++;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    if (dir == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    thisFile = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    while (thisFile != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
        if (strcmp(thisFile->name, t) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
            return(thisFile->entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
        thisFile = thisFile->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   396
    BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
} /* qpak_findEntry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
static int qpak_populateDirectories(QPAKentry *entries, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
                                    QPAKdirectory *root)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
{
494
364e564a26e2 Win32 fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 467
diff changeset
   403
    PHYSFS_sint32 i;
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
    QPAKentry *entry = entries;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
    for (i = 0; i < numEntries; i++, entry++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
        if (qpak_addEntry(root, entry->name, entry) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
            return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    } /* for */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
} /* qpak_populateDirectories */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   416
static void qpak_deletePakInfo(QPAKinfo *pakInfo)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
{
457
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   418
    if (pakInfo->handle != NULL)
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   419
        __PHYSFS_platformClose(pakInfo->handle);
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   420
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    if (pakInfo->filename != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
        free(pakInfo->filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    if (pakInfo->entries != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
        free(pakInfo->entries);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    qpak_deleteDirectory(pakInfo->root);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
    free(pakInfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
} /* qpak_deletePakInfo */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   433
static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   434
{
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   435
    QPAKentry *a = (QPAKentry *) _a;
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   436
    return(strcmp(a[one].name, a[two].name));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   437
} /* qpak_entry_cmp */
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   438
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   439
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   440
static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   441
{
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   442
    QPAKentry tmp;
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   443
    QPAKentry *first = &(((QPAKentry *) _a)[one]);
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   444
    QPAKentry *second = &(((QPAKentry *) _a)[two]);
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   445
    memcpy(&tmp, first, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   446
    memcpy(first, second, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   447
    memcpy(second, &tmp, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   448
} /* qpak_entry_swap */
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   449
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   450
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
static DirHandle *QPAK_openArchive(const char *name, int forWriting)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
    void *fh = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
    PHYSFS_uint32 dirOffset, dirLength;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    QPAKinfo *pi;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    DirHandle *retval;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    retval = (DirHandle *) malloc(sizeof (DirHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    pi = (QPAKinfo *) malloc(sizeof (QPAKinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    if (pi == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    retval->opaque = pi;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
    pi->filename = (char *) malloc(strlen(name) + 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
    if (pi->filename == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    if (!openQPak(name, forWriting, &fh))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
    if (!readui32(fh, &dirOffset))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
    if (!readui32(fh, &dirLength))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
    if (__PHYSFS_platformFileLength(fh) < dirOffset + dirLength)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    strcpy(pi->filename, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
    pi->handle = fh;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    pi->dirOffset = dirOffset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    pi->totalEntries = dirLength / 64;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
    pi->entries = (QPAKentry *) malloc(pi->totalEntries * sizeof (QPAKentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
    if (pi->entries == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
    if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   504
    __PHYSFS_sort(pi->entries, pi->totalEntries,
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   505
                  qpak_entry_cmp, qpak_entry_swap);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
    pi->root = qpak_newDirectory("");
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
    if (pi->root == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    if (qpak_populateDirectories(pi->entries, pi->totalEntries, pi->root) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
    retval->funcs = &__PHYSFS_DirFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
QPAK_openArchive_failed:
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
    if (retval != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
        if (retval->opaque != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
            qpak_deletePakInfo((QPAKinfo *) retval->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    if (fh != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
        __PHYSFS_platformClose(fh);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
} /* QPAK_openArchive */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
static void QPAK_dirClose(DirHandle *dirHandle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
{
457
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   535
    qpak_deletePakInfo((QPAKinfo *) dirHandle->opaque);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
    free(dirHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
} /* QPAK_dirClose */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
                                             int omitSymLinks)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
    LinkedStringList *retval = NULL, *p = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
    QPAKdirectory *dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
    QPAKinfo *info = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
    if ((dirname == NULL) || (*dirname == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
        dir = info->root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
        dir = qpak_findDirectory(info->root, dirname);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    if (dir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
        QPAKdirectory *child = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
        QPAKdirentry *file = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
        while (child != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
            retval = __PHYSFS_addToLinkedStringList(retval, &p, child->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
            child = child->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
        while (file != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
            retval = __PHYSFS_addToLinkedStringList(retval, &p, file->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
            file = file->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
} /* QPAK_enumerateFiles */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
static int QPAK_exists(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    if ((name == NULL) || (*name == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
    if (qpak_findDirectory(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
        return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
    if (qpak_findEntry(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
        return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
} /* QPAK_exists */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   591
static int QPAK_isDirectory(DirHandle *h, const char *name, int *fileExists)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    QPAKinfo *info = (QPAKinfo *) h->opaque;
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   594
    *fileExists = (qpak_findDirectory(info->root, name) != 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   595
    return(*fileExists);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
} /* QPAK_isDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   599
static int QPAK_isSymLink(DirHandle *h, const char *name, int *fileExists)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
{
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   601
    *fileExists = QPAK_exists(h, name);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
    return(0); /* we don't support symlinks for now */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
} /* QPAK_isSymlink */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   606
static int QPAK_remove(DirHandle *h, const char *name)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   607
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   608
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   609
} /* QPAK_remove */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   610
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   611
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   612
static int QPAK_mkdir(DirHandle *h, const char *name)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
{
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   614
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   615
} /* QPAK_mkdir */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   616
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   617
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   618
static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   619
                                         const char *name,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   620
                                         int *fileExists)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   621
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   622
    QPAKinfo *info = (QPAKinfo *) h->opaque;
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   623
    PHYSFS_sint64 retval = -1;
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   624
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   625
    *fileExists = QPAK_exists(h, name);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   626
    if (*fileExists)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   627
        retval = __PHYSFS_platformGetLastModTime(info->filename);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   628
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   629
    return(retval);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
} /* QPAK_getLastModTime */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
static void *qpak_getFileHandle(const char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
    void *retval = __PHYSFS_platformOpenRead(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
    if (retval == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
    if (!__PHYSFS_platformSeek(retval, entry->offset))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
        __PHYSFS_platformClose(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
} /* qpak_getFileHandle */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   649
static FileHandle *QPAK_openRead(DirHandle *h, const char *fnm, int *fileExists)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   652
    QPAKentry *entry = qpak_findEntry(driver->root, fnm);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
    QPAKfileinfo *fileDriver = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    FileHandle *result = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   656
    *fileExists = (entry != NULL);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
    if (entry == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
 
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
    fileDriver = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
    BAIL_IF_MACRO(fileDriver == NULL, ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
    fileDriver->handle = qpak_getFileHandle(driver->filename, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
    if (fileDriver->handle == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
        free(fileDriver);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
    fileDriver->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
    fileDriver->curPos = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
    result = (FileHandle *)malloc(sizeof (FileHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
    if (result == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
        __PHYSFS_platformClose(fileDriver->handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
        free(fileDriver);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
    result->opaque = fileDriver;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
    result->dirHandle = h;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
    result->funcs = &__PHYSFS_FileFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
    return(result);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
} /* QPAK_openRead */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   688
static FileHandle *QPAK_openWrite(DirHandle *h, const char *name)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   689
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   690
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   691
} /* QPAK_openWrite */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   692
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   693
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   694
static FileHandle *QPAK_openAppend(DirHandle *h, const char *name)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   695
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   696
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   697
} /* QPAK_openAppend */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   698
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   699
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   702
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
    PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
    PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
    PHYSFS_sint64 rc;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
    if (objsLeft < objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
        objCount = (PHYSFS_uint32) objsLeft;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
    rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
    if (rc > 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
        finfo->curPos += (rc * objSize);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
} /* QPAK_read */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   720
static PHYSFS_sint64 QPAK_write(FileHandle *handle, const void *buffer,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   721
                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   722
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   723
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   724
} /* QPAK_write */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   725
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   726
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
static int QPAK_eof(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
    return(finfo->curPos >= (PHYSFS_sint64) entry->size);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
} /* QPAK_eof */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   734
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
static PHYSFS_sint64 QPAK_tell(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
    return(((QPAKfileinfo *) handle->opaque)->curPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
} /* QPAK_tell */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
    PHYSFS_uint64 newPos = entry->offset + offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
    int rc;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
    BAIL_IF_MACRO(newPos > entry->offset + entry->size, ERR_PAST_EOF, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
    rc = __PHYSFS_platformSeek(finfo->handle, newPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   752
    if (rc)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   753
        finfo->curPos = offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   754
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   755
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   756
}  /* QPAK_seek */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   757
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   758
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   759
static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   760
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   761
    return ((QPAKfileinfo *) handle->opaque)->entry->size;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
} /* QPAK_fileLength */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   763
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   765
static int QPAK_fileClose(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   766
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   767
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   768
    BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   769
    free(finfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   770
    free(handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
} /* QPAK_fileClose */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
#endif  /* defined PHYSFS_SUPPORTS_QPAK */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
/* end of qpak.c ... */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778