archivers/wad.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Mar 2005 11:27:31 +0000
changeset 694 80bc8858b4ab
parent 691 71d9affe0d8a
child 754 e7cd7411eadf
permissions -rw-r--r--
Added translation for WAD file type.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * WAD support routines for PhysicsFS.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles DOOM engine archives ("wads"). 
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * This format (but not this driver) was designed by id Software for use
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  with the DOOM engine.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * The specs of the format are from the unofficial doom specs v1.666
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 * found here: http://www.gamers.org/dhs/helpdocs/dmsp1666.html
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 * The format of the archive: (from the specs)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *  A WAD file has three parts:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *  (1) a twelve-byte header
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *  (2) one or more "lumps"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *  (3) a directory or "info table" that contains the names, offsets, and
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *      sizes of all the lumps in the WAD
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *  The header consists of three four-byte parts:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *    (a) an ASCII string which must be either "IWAD" or "PWAD"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *    (b) a 4-byte (long) integer which is the number of lumps in the wad
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *    (c) a long integer which is the file offset to the start of
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *    the directory
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *  The directory has one 16-byte entry for every lump. Each entry consists
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *  of three parts:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *    (a) a long integer, the file offset to the start of the lump
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *    (b) a long integer, the size of the lump in bytes
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *    (c) an 8-byte ASCII string, the name of the lump, padded with zeros.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 *        For example, the "DEMO1" entry in hexadecimal would be
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
 *        (44 45 4D 4F 31 00 00 00)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 * 
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
 * Note that there is no way to tell if an opened WAD archive is a
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
 *  IWAD or PWAD with this archiver.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
 * I couldn't think of a way to provide that information, without being too
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
 *  hacky.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
 * I don't think it's really that important though.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
 * Please see the file LICENSE in the source's root directory.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
 * This file written by Travis Wells, based on the GRP archiver by
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
 *  Ryan C. Gordon.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
 */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#if HAVE_CONFIG_H
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#  include <config.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#endif
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
#if (defined PHYSFS_SUPPORTS_WAD)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
#include <stdio.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#include <stdlib.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
#include <string.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
#include "physfs.h"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
#define __PHYSICSFS_INTERNAL__
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
#include "physfs_internal.h"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
    61
    char name[18];
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    PHYSFS_uint32 startPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    PHYSFS_uint32 size;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
} WADentry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    char *filename;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    PHYSFS_sint64 last_mod_time;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    PHYSFS_uint32 entryCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    PHYSFS_uint32 entryOffset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    WADentry *entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
} WADinfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    void *handle;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    WADentry *entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    PHYSFS_uint32 curPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
} WADfileinfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
    83
static void WAD_dirClose(dvoid *opaque)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
    85
    WADinfo *info = ((WADinfo *) opaque);
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
    86
    allocator.Free(info->filename);
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
    87
    allocator.Free(info->entries);
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
    88
    allocator.Free(info);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
} /* WAD_dirClose */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
    92
static PHYSFS_sint64 WAD_read(fvoid *opaque, void *buffer,
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
                              PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
{
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
    95
    WADfileinfo *finfo = (WADfileinfo *) opaque;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    WADentry *entry = finfo->entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    PHYSFS_sint64 rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    if (objsLeft < objCount)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        objCount = objsLeft;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    if (rc > 0)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        finfo->curPos += (PHYSFS_uint32) (rc * objSize);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    return(rc);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
} /* WAD_read */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   112
static PHYSFS_sint64 WAD_write(fvoid *opaque, const void *buffer,
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
} /* WAD_write */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   119
static int WAD_eof(fvoid *opaque)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
{
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   121
    WADfileinfo *finfo = (WADfileinfo *) opaque;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    WADentry *entry = finfo->entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    return(finfo->curPos >= entry->size);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
} /* WAD_eof */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   127
static PHYSFS_sint64 WAD_tell(fvoid *opaque)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
{
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   129
    return(((WADfileinfo *) opaque)->curPos);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
} /* WAD_tell */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   133
static int WAD_seek(fvoid *opaque, PHYSFS_uint64 offset)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
{
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   135
    WADfileinfo *finfo = (WADfileinfo *) opaque;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    WADentry *entry = finfo->entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    int rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    if (rc)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
        finfo->curPos = (PHYSFS_uint32) offset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    return(rc);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
} /* WAD_seek */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   149
static PHYSFS_sint64 WAD_fileLength(fvoid *opaque)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
{
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   151
    WADfileinfo *finfo = (WADfileinfo *) opaque;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    return((PHYSFS_sint64) finfo->entry->size);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
} /* WAD_fileLength */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   156
static int WAD_fileClose(fvoid *opaque)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
{
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   158
    WADfileinfo *finfo = (WADfileinfo *) opaque;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   160
    allocator.Free(finfo);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    return(1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
} /* WAD_fileClose */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
static int wad_open(const char *filename, int forWriting,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
                    void **fh, PHYSFS_uint32 *count,PHYSFS_uint32 *offset)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    PHYSFS_uint8 buf[4];
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    *fh = NULL;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    *fh = __PHYSFS_platformOpenRead(filename);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    BAIL_IF_MACRO(*fh == NULL, NULL, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    if (memcmp(buf, "IWAD", 4) != 0 && memcmp(buf, "PWAD", 4) != 0)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
        __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    *count = PHYSFS_swapULE32(*count);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    if (__PHYSFS_platformRead(*fh, offset, sizeof (PHYSFS_uint32), 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    *offset = PHYSFS_swapULE32(*offset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    return(1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
openWad_failed:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    if (*fh != NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        __PHYSFS_platformClose(*fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    *count = -1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    *fh = NULL;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
} /* wad_open */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
static int WAD_isArchive(const char *filename, int forWriting)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
    void *fh;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
    PHYSFS_uint32 fileCount,offset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
    int retval = wad_open(filename, forWriting, &fh, &fileCount,&offset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    if (fh != NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    return(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
} /* WAD_isArchive */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
static int wad_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    WADentry *a = (WADentry *) _a;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    return(strcmp(a[one].name, a[two].name));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
} /* wad_entry_cmp */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
static void wad_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
    WADentry tmp;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    WADentry *first = &(((WADentry *) _a)[one]);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    WADentry *second = &(((WADentry *) _a)[two]);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    memcpy(&tmp, first, sizeof (WADentry));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    memcpy(first, second, sizeof (WADentry));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    memcpy(second, &tmp, sizeof (WADentry));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
} /* wad_entry_swap */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
static int wad_load_entries(const char *name, int forWriting, WADinfo *info)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    void *fh = NULL;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    PHYSFS_uint32 fileCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    PHYSFS_uint32 directoryOffset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    WADentry *entry;
622
c8e67ca63ad6 Patches to get this building on Mac Classic again.
Ryan C. Gordon <icculus@icculus.org>
parents: 610
diff changeset
   244
    char lastDirectory[9];
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   245
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   246
    lastDirectory[8] = 0; /* Make sure lastDirectory stays null-terminated. */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    info->entryCount = fileCount;
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   250
    info->entries = (WADentry *) allocator.Malloc(sizeof(WADentry)*fileCount);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    if (info->entries == NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
        BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    __PHYSFS_platformSeek(fh,directoryOffset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    for (entry = info->entries; fileCount > 0; fileCount--, entry++)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
        if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
        {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
            __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
            return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
        } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
        
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
        if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
            __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
            return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
        } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
            __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
            return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
        } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
        entry->name[8] = '\0'; /* name might not be null-terminated in file. */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
        entry->size = PHYSFS_swapULE32(entry->size);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
        entry->startPos = PHYSFS_swapULE32(entry->startPos);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    } /* for */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    __PHYSFS_sort(info->entries, info->entryCount,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
                  wad_entry_cmp, wad_entry_swap);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    return(1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
} /* wad_load_entries */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   292
static void *WAD_openArchive(const char *name, int forWriting)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   295
    WADinfo *info = (WADinfo *) allocator.Malloc(sizeof (WADinfo));
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   297
    BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    memset(info, '\0', sizeof (WADinfo));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   300
    info->filename = (char *) allocator.Malloc(strlen(name) + 1);
678
73a2641375a0 Cleaned up some minor bloat with my new evil GOTO_*_MACRO macros.
Ryan C. Gordon <icculus@icculus.org>
parents: 658
diff changeset
   301
    GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, WAD_openArchive_failed);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    if (!wad_load_entries(name, forWriting, info))
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        goto WAD_openArchive_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    strcpy(info->filename, name);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    info->last_mod_time = modtime;
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   308
    return(info);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
WAD_openArchive_failed:
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   311
    if (info != NULL)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    {
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   313
        if (info->filename != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   314
            allocator.Free(info->filename);
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   315
        if (info->entries != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   316
            allocator.Free(info->entries);
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   317
        allocator.Free(info);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    return(NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
} /* WAD_openArchive */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   324
static void WAD_enumerateFiles(dvoid *opaque, const char *dname,
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   325
                               int omitSymLinks, PHYSFS_StringCallback cb,
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   326
                               void *callbackdata)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   328
    WADinfo *info = ((WADinfo *) opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
    WADentry *entry = info->entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    PHYSFS_uint32 max = info->entryCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    PHYSFS_uint32 i;
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   332
    const char *name;
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   333
    char *sep;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   335
    if (*dname == '\0')  /* root directory enumeration? */
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   336
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   337
        for (i = 0; i < max; i++, entry++)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   338
        {
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   339
            name = entry->name;
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   340
            if (strchr(name, '/') == NULL)
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   341
                cb(callbackdata, name);
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   342
        } /* for */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   343
    } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   344
    else
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   345
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   346
        for (i = 0; i < max; i++, entry++)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   347
        {
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   348
            name = entry->name;
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   349
            sep = strchr(name, '/');
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   350
            if (sep != NULL)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   351
            {
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   352
                if (strncmp(dname, name, (sep - name)) == 0)
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   353
                    cb(callbackdata, sep + 1);
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   354
            } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   355
        } /* for */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   356
    } /* else */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
} /* WAD_enumerateFiles */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
static WADentry *wad_find_entry(WADinfo *info, const char *name)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
    WADentry *a = info->entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    PHYSFS_sint32 lo = 0;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    PHYSFS_sint32 middle;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    int rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    while (lo <= hi)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
        middle = lo + ((hi - lo) / 2);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
        rc = strcmp(name, a[middle].name);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
        if (rc == 0)  /* found it! */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
            return(&a[middle]);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
        else if (rc > 0)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
            lo = middle + 1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
        else
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
            hi = middle - 1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    } /* while */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
} /* wad_find_entry */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   384
static int WAD_exists(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   386
    return(wad_find_entry(((WADinfo *) opaque), name) != NULL);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
} /* WAD_exists */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   390
static int WAD_isDirectory(dvoid *opaque, const char *name, int *fileExists)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   392
    WADentry *entry = wad_find_entry(((WADinfo *) opaque), name);
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   393
    if (entry != NULL)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   394
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   395
        char *n;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   396
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   397
        *fileExists = 1;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   398
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   399
        /* Can't be a directory if it's a subdirectory. */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   400
        if (strchr(entry->name, '/') != NULL)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   401
            return(0);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   402
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   403
        /* Check if it matches "MAP??" or "E?M?" ... */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   404
        n = entry->name;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   405
        if ((n[0] == 'E' && n[2] == 'M') ||
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   406
            (n[0] == 'M' && n[1] == 'A' && n[2] == 'P' && n[6] == 0))
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   407
        {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   408
            return(1);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   409
        } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   410
        return(0);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   411
    } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   412
    else
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   413
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   414
        *fileExists = 0;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   415
        return(0);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   416
    } /* else */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
} /* WAD_isDirectory */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   420
static int WAD_isSymLink(dvoid *opaque, const char *name, int *fileExists)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   422
    *fileExists = WAD_exists(opaque, name);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    return(0);  /* never symlinks in a wad. */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
} /* WAD_isSymLink */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   427
static PHYSFS_sint64 WAD_getLastModTime(dvoid *opaque,
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
                                        const char *name,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
                                        int *fileExists)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   431
    WADinfo *info = ((WADinfo *) opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    PHYSFS_sint64 retval = -1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
    *fileExists = (wad_find_entry(info, name) != NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    if (*fileExists)  /* use time of WAD itself in the physical filesystem. */
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   436
        retval = info->last_mod_time;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    return(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
} /* WAD_getLastModTime */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   442
static fvoid *WAD_openRead(dvoid *opaque, const char *fnm, int *fileExists)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   444
    WADinfo *info = ((WADinfo *) opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
    WADfileinfo *finfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
    WADentry *entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
    entry = wad_find_entry(info, fnm);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
    *fileExists = (entry != NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
    BAIL_IF_MACRO(entry == NULL, NULL, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   452
    finfo = (WADfileinfo *) allocator.Malloc(sizeof (WADfileinfo));
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   453
    BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    finfo->handle = __PHYSFS_platformOpenRead(info->filename);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    if ( (finfo->handle == NULL) ||
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
         (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   459
        allocator.Free(finfo);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
        return(NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    finfo->curPos = 0;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    finfo->entry = entry;
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   465
    return(finfo);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
} /* WAD_openRead */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   469
static fvoid *WAD_openWrite(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
} /* WAD_openWrite */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   475
static fvoid *WAD_openAppend(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
} /* WAD_openAppend */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   481
static int WAD_remove(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
} /* WAD_remove */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
650
298b8bb26775 Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   487
static int WAD_mkdir(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
} /* WAD_mkdir */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   492
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   493
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   494
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   495
    "WAD",
694
80bc8858b4ab Added translation for WAD file type.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   496
    WAD_ARCHIVE_DESCRIPTION,
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   497
    "Travis Wells <traviswells@mchsi.com>",
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   498
    "http://www.3dmm2.com/doom/",
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   499
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   500
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   501
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   502
const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   503
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   504
    &__PHYSFS_ArchiveInfo_WAD,
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   505
    WAD_isArchive,          /* isArchive() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   506
    WAD_openArchive,        /* openArchive() method    */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   507
    WAD_enumerateFiles,     /* enumerateFiles() method */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   508
    WAD_exists,             /* exists() method         */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   509
    WAD_isDirectory,        /* isDirectory() method    */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   510
    WAD_isSymLink,          /* isSymLink() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   511
    WAD_getLastModTime,     /* getLastModTime() method */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   512
    WAD_openRead,           /* openRead() method       */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   513
    WAD_openWrite,          /* openWrite() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   514
    WAD_openAppend,         /* openAppend() method     */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   515
    WAD_remove,             /* remove() method         */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   516
    WAD_mkdir,              /* mkdir() method          */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   517
    WAD_dirClose,           /* dirClose() method       */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   518
    WAD_read,               /* read() method           */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   519
    WAD_write,              /* write() method          */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   520
    WAD_eof,                /* eof() method            */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   521
    WAD_tell,               /* tell() method           */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   522
    WAD_seek,               /* seek() method           */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   523
    WAD_fileLength,         /* fileLength() method     */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   524
    WAD_fileClose           /* fileClose() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   525
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   526
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
#endif  /* defined PHYSFS_SUPPORTS_WAD */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
/* end of wad.c ... */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530