src/archiver_wad.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 30 Aug 2010 03:01:57 -0400
changeset 1118 2e09fc635fdd
parent 1115 bc7257f4f8da
child 1125 bcff76dbd9fd
permissions -rw-r--r--
Abstracted file i/o into PHYSFS_Io interface. This touched a lot of code, and involved cleaning up a lot of stuff.
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
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
    39
 * Please see the file LICENSE.txt in the source's root directory.
609
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 (defined PHYSFS_SUPPORTS_WAD)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#include <stdio.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
#include <stdlib.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
#include <string.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
#include "physfs.h"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#define __PHYSICSFS_INTERNAL__
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
#include "physfs_internal.h"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
{
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
    57
    char name[18];
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    PHYSFS_uint32 startPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    PHYSFS_uint32 size;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
} WADentry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    64
    PHYSFS_Io *io;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    PHYSFS_uint32 entryCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    WADentry *entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
} WADinfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    71
    PHYSFS_Io *io;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    WADentry *entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    PHYSFS_uint32 curPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
} WADfileinfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    77
static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    78
{
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    79
    return (io->read(io, buf, len) == len);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    80
} /* readAll */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    81
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
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);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    86
    info->io->destroy(info->io);
691
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
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    92
static PHYSFS_sint64 WAD_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    94
    WADfileinfo *finfo = (WADfileinfo *) io->opaque;
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    95
    const WADentry *entry = finfo->entry;
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    96
    const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    PHYSFS_sint64 rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    99
    if (bytesLeft < len)
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   100
        len = bytesLeft;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   102
    rc = finfo->io->read(finfo->io, buffer, len);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    if (rc > 0)
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   104
        finfo->curPos += (PHYSFS_uint32) rc;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   106
    return rc;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
} /* WAD_read */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   110
static PHYSFS_sint64 WAD_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
} /* WAD_write */
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
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   116
static PHYSFS_sint64 WAD_tell(PHYSFS_Io *io)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   118
    return ((WADfileinfo *) io->opaque)->curPos;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
} /* WAD_tell */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   122
static int WAD_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   124
    WADfileinfo *finfo = (WADfileinfo *) io->opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   125
    const WADentry *entry = finfo->entry;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    int rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   129
    rc = finfo->io->seek(finfo->io, entry->startPos + offset);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    if (rc)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
        finfo->curPos = (PHYSFS_uint32) offset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   133
    return rc;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
} /* WAD_seek */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   137
static PHYSFS_sint64 WAD_length(PHYSFS_Io *io)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   139
    const WADfileinfo *finfo = (WADfileinfo *) io->opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   140
    return ((PHYSFS_sint64) finfo->entry->size);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   141
} /* WAD_length */
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   142
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   143
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   144
static PHYSFS_Io *WAD_duplicate(PHYSFS_Io *_io)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   146
    WADfileinfo *origfinfo = (WADfileinfo *) _io->opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   147
    PHYSFS_Io *io = NULL;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   148
    PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   149
    WADfileinfo *finfo = (WADfileinfo *) allocator.Malloc(sizeof (WADfileinfo));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   150
    GOTO_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, WAD_duplicate_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   151
    GOTO_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, WAD_duplicate_failed);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   153
    io = origfinfo->io->duplicate(origfinfo->io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   154
    GOTO_IF_MACRO(io == NULL, NULL, WAD_duplicate_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   155
    finfo->io = io;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   156
    finfo->entry = origfinfo->entry;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   157
    finfo->curPos = 0;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   158
    memcpy(retval, _io, sizeof (PHYSFS_Io));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   159
    retval->opaque = finfo;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   160
    return retval;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   162
WAD_duplicate_failed:
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   163
    if (finfo != NULL) allocator.Free(finfo);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   164
    if (retval != NULL) allocator.Free(retval);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   165
    if (io != NULL) io->destroy(io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   166
    return NULL;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   167
} /* WAD_duplicate */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   169
static int WAD_flush(PHYSFS_Io *io) { return 1;  /* no write support. */ }
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   171
static void WAD_destroy(PHYSFS_Io *io)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   172
{
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   173
    WADfileinfo *finfo = (WADfileinfo *) io->opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   174
    finfo->io->destroy(finfo->io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   175
    allocator.Free(finfo);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   176
    allocator.Free(io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   177
} /* WAD_destroy */
609
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
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   180
static const PHYSFS_Io WAD_Io =
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   181
{
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   182
    WAD_read,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   183
    WAD_write,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   184
    WAD_seek,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   185
    WAD_tell,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   186
    WAD_length,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   187
    WAD_duplicate,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   188
    WAD_flush,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   189
    WAD_destroy,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   190
    NULL
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   191
};
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   192
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   193
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   194
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   195
static int wadEntryCmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
{
928
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   197
    if (one != two)
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   198
    {
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   199
        const WADentry *a = (const WADentry *) _a;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   200
        return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
928
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   201
    } /* if */
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   202
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   203
    return 0;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   204
} /* wadEntryCmp */
609
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
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   207
static void wadEntrySwap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
{
928
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   209
    if (one != two)
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   210
    {
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   211
        WADentry tmp;
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   212
        WADentry *first = &(((WADentry *) _a)[one]);
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   213
        WADentry *second = &(((WADentry *) _a)[two]);
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   214
        memcpy(&tmp, first, sizeof (WADentry));
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   215
        memcpy(first, second, sizeof (WADentry));
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   216
        memcpy(second, &tmp, sizeof (WADentry));
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   217
    } /* if */
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   218
} /* wadEntrySwap */
609
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
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   221
static int wad_load_entries(PHYSFS_Io *io, WADinfo *info)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   223
    PHYSFS_uint32 fileCount = info->entryCount;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    PHYSFS_uint32 directoryOffset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    WADentry *entry;
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   226
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   227
    BAIL_IF_MACRO(!readAll(io, &directoryOffset, 4), NULL, 0);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   228
    directoryOffset = PHYSFS_swapULE32(directoryOffset);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   230
    info->entries = (WADentry *) allocator.Malloc(sizeof(WADentry)*fileCount);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   231
    BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   232
    BAIL_IF_MACRO(!io->seek(io, directoryOffset), NULL, 0);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    for (entry = info->entries; fileCount > 0; fileCount--, entry++)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    {
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   236
        BAIL_IF_MACRO(!readAll(io, &entry->startPos, 4), NULL, 0);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   237
        BAIL_IF_MACRO(!readAll(io, &entry->size, 4), NULL, 0);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   238
        BAIL_IF_MACRO(!readAll(io, &entry->name, 8), NULL, 0);
609
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
        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
   241
        entry->size = PHYSFS_swapULE32(entry->size);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
        entry->startPos = PHYSFS_swapULE32(entry->startPos);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    } /* for */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   245
    __PHYSFS_sort(info->entries, info->entryCount, wadEntryCmp, wadEntrySwap);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   246
    return 1;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
} /* wad_load_entries */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   250
static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   252
    PHYSFS_uint8 buf[4];
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   253
    WADinfo *info = NULL;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   254
    PHYSFS_uint32 val = 0;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   256
    assert(io != NULL);  /* shouldn't ever happen. */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   257
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   258
    BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   260
    BAIL_IF_MACRO(!readAll(io, buf, sizeof (buf)), NULL, NULL);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   261
    if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   262
        GOTO_MACRO(ERR_NOT_AN_ARCHIVE, WAD_openArchive_failed);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   264
    info = (WADinfo *) allocator.Malloc(sizeof (WADinfo));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   265
    GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, WAD_openArchive_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   266
    memset(info, '\0', sizeof (WADinfo));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   267
    info->io = io;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   269
    GOTO_IF_MACRO(!readAll(io,&val,sizeof(val)), NULL, WAD_openArchive_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   270
    info->entryCount = PHYSFS_swapULE32(val);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   271
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   272
    GOTO_IF_MACRO(!wad_load_entries(io, info), NULL, WAD_openArchive_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   273
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   274
    return info;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
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
   277
    if (info != NULL)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    {
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   279
        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
   280
            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
   281
        allocator.Free(info);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   284
    return NULL;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
} /* WAD_openArchive */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   288
static void WAD_enumerateFiles(dvoid *opaque, const char *dname,
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 694
diff changeset
   289
                               int omitSymLinks, PHYSFS_EnumFilesCallback cb,
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 694
diff changeset
   290
                               const char *origdir, void *callbackdata)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   292
    /* no directories in WAD files. */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   293
    if (*dname == '\0')
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   294
    {
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   295
        WADinfo *info = (WADinfo *) opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   296
        WADentry *entry = info->entries;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   297
        PHYSFS_uint32 max = info->entryCount;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   298
        PHYSFS_uint32 i;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   300
        for (i = 0; i < max; i++, entry++)
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   301
            cb(callbackdata, origdir, entry->name);
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   302
    } /* if */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
} /* WAD_enumerateFiles */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   306
static WADentry *wad_find_entry(const WADinfo *info, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   308
    char *ptr = strchr(name, '.');
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    WADentry *a = info->entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    PHYSFS_sint32 lo = 0;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    PHYSFS_sint32 middle;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    int rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   315
    /*
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   316
     * Rule out filenames to avoid unneeded processing...no dirs,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   317
     *   big filenames, or extensions.
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   318
     */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   319
    BAIL_IF_MACRO(ptr != NULL, ERR_NO_SUCH_FILE, NULL);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   320
    BAIL_IF_MACRO(strlen(name) > 8, ERR_NO_SUCH_FILE, NULL);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   321
    BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   322
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    while (lo <= hi)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
        middle = lo + ((hi - lo) / 2);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   326
        rc = __PHYSFS_stricmpASCII(name, a[middle].name);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        if (rc == 0)  /* found it! */
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   328
            return &a[middle];
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        else if (rc > 0)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
            lo = middle + 1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
        else
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
            hi = middle - 1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    } /* while */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
    BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
} /* wad_find_entry */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
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
   339
static int WAD_exists(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   341
    return (wad_find_entry((WADinfo *) opaque, name) != NULL);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
} /* WAD_exists */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
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
   345
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
   346
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   347
    WADentry *entry = wad_find_entry(((WADinfo *) opaque), name);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   348
    const int exists = (entry != NULL);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   349
    *fileExists = exists;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   350
    if (exists)
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   351
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   352
        char *n;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   353
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   354
        /* Can't be a directory if it's a subdirectory. */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   355
        if (strchr(entry->name, '/') != NULL)
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   356
            return 0;
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   357
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   358
        /* !!! FIXME: this isn't really something we should do. */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   359
        /* !!! FIXME: I probably broke enumeration up there, too. */
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   360
        /* Check if it matches "MAP??" or "E?M?" ... */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   361
        n = entry->name;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   362
        if ((n[0] == 'E' && n[2] == 'M') ||
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   363
            (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
   364
        {
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   365
            return 1;
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   366
        } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   367
    } /* if */
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   368
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   369
    return 0;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
} /* WAD_isDirectory */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
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
   373
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
   374
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   375
    *fileExists = WAD_exists(opaque, name);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   376
    return 0;  /* never symlinks in a wad. */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
} /* WAD_isSymLink */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   380
static PHYSFS_Io *WAD_openRead(dvoid *opaque, const char *fnm, int *fileExists)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   382
    PHYSFS_Io *retval = NULL;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   383
    WADinfo *info = (WADinfo *) opaque;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    WADfileinfo *finfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
    WADentry *entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    entry = wad_find_entry(info, fnm);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    *fileExists = (entry != NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   389
    GOTO_IF_MACRO(entry == NULL, NULL, WAD_openRead_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   390
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   391
    retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   392
    GOTO_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, WAD_openRead_failed);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   394
    finfo = (WADfileinfo *) allocator.Malloc(sizeof (WADfileinfo));
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   395
    GOTO_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, WAD_openRead_failed);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   397
    finfo->io = info->io->duplicate(info->io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   398
    GOTO_IF_MACRO(finfo->io == NULL, NULL, WAD_openRead_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   399
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   400
    if (!finfo->io->seek(finfo->io, entry->startPos))
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   401
        GOTO_MACRO(NULL, WAD_openRead_failed);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
    finfo->curPos = 0;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
    finfo->entry = entry;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   405
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   406
    memcpy(retval, &WAD_Io, sizeof (*retval));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   407
    retval->opaque = finfo;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   408
    return retval;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   409
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   410
WAD_openRead_failed:
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   411
    if (finfo != NULL)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   412
    {
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   413
        if (finfo->io != NULL)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   414
            finfo->io->destroy(finfo->io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   415
        allocator.Free(finfo);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   416
    } /* if */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   417
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   418
    if (retval != NULL)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   419
        allocator.Free(retval);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   420
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   421
    return NULL;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
} /* WAD_openRead */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   425
static PHYSFS_Io *WAD_openWrite(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
} /* WAD_openWrite */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   431
static PHYSFS_Io *WAD_openAppend(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
} /* WAD_openAppend */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
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
   437
static int WAD_remove(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
} /* WAD_remove */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
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
   443
static int WAD_mkdir(dvoid *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
} /* WAD_mkdir */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   448
1108
b63e39d5be04 Fixed details of PHYSFS_Archiver's stat method.
Ryan C. Gordon <icculus@icculus.org>
parents: 1106
diff changeset
   449
static int WAD_stat(dvoid *opaque, const char *filename, int *exists,
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   450
                    PHYSFS_Stat *stat)
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   451
{
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   452
    const WADinfo *info = (const WADinfo *) opaque;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   453
    const WADentry *entry = wad_find_entry(info, filename);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   454
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   455
    *exists = (entry != 0);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   456
    if (!entry)
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   457
        return 0;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   458
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   459
    stat->filesize = entry->size;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   460
    stat->filetype = PHYSFS_FILETYPE_REGULAR;
1115
bc7257f4f8da Removed __PHYSFS_platformGetLastModTime().
Ryan C. Gordon <icculus@icculus.org>
parents: 1113
diff changeset
   461
    stat->modtime = -1;
bc7257f4f8da Removed __PHYSFS_platformGetLastModTime().
Ryan C. Gordon <icculus@icculus.org>
parents: 1113
diff changeset
   462
    stat->createtime = -1;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   463
    stat->accesstime = -1;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   464
    stat->readonly = 1;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   465
1106
94c3669d0311 Fixed PHYSFS_stat()'s return value to match rest of PhysicsFS API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1098
diff changeset
   466
    return 1;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   467
} /* WAD_stat */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   468
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   469
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   470
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
   471
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   472
    "WAD",
694
80bc8858b4ab Added translation for WAD file type.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   473
    WAD_ARCHIVE_DESCRIPTION,
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   474
    "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
   475
    "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
   476
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   477
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   478
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   479
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
   480
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   481
    &__PHYSFS_ArchiveInfo_WAD,
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   482
    WAD_openArchive,        /* openArchive() method    */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   483
    WAD_enumerateFiles,     /* enumerateFiles() method */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   484
    WAD_exists,             /* exists() method         */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   485
    WAD_isDirectory,        /* isDirectory() method    */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   486
    WAD_isSymLink,          /* isSymLink() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   487
    WAD_openRead,           /* openRead() method       */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   488
    WAD_openWrite,          /* openWrite() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   489
    WAD_openAppend,         /* openAppend() method     */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   490
    WAD_remove,             /* remove() method         */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   491
    WAD_mkdir,              /* mkdir() method          */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   492
    WAD_dirClose,           /* dirClose() method       */
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   493
    WAD_stat                /* stat() method           */
658
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
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
#endif  /* defined PHYSFS_SUPPORTS_WAD */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
/* end of wad.c ... */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499