src/archiver_grp.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:
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * GRP support routines for PhysicsFS.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles BUILD engine archives ("groupfiles"). This format
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *  (but not this driver) was put together by Ken Silverman.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * The format is simple enough. In Ken's words:
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *    What's the .GRP file format?
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *     The ".grp" file format is just a collection of a lot of files stored
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *     into 1 big one. I tried to make the format as simple as possible: The
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *     first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *     the number of files that were compacted into the group file. Then for
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *     each file, there is a 16 byte structure, where the first 12 bytes are
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *     the filename, and the last 4 bytes are the file's size. The rest of
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *     the group file is just the raw data packed one after the other in the
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *     same order as the list of files.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 * (That info is from http://www.advsys.net/ken/build.htm ...)
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
    22
 * Please see the file LICENSE.txt in the source's root directory.
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *  This file written by Ryan C. Gordon.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
214
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
    27
#if (defined PHYSFS_SUPPORTS_GRP)
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
    28
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
#include <stdio.h>
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#include <stdlib.h>
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#include <string.h>
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#include "physfs.h"
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#define __PHYSICSFS_INTERNAL__
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include "physfs_internal.h"
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
typedef struct
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
{
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    39
    char name[13];
480
2cf0b6edd200 Cleaned up some stuff, reduced some unnecessary 64-bit ints to 32.
Ryan C. Gordon <icculus@icculus.org>
parents: 467
diff changeset
    40
    PHYSFS_uint32 startPos;
2cf0b6edd200 Cleaned up some stuff, reduced some unnecessary 64-bit ints to 32.
Ryan C. Gordon <icculus@icculus.org>
parents: 467
diff changeset
    41
    PHYSFS_uint32 size;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    42
} GRPentry;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    43
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    44
typedef struct
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    45
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    46
    PHYSFS_Io *io;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    47
    PHYSFS_uint32 entryCount;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    48
    GRPentry *entries;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
} GRPinfo;
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
typedef struct
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    53
    PHYSFS_Io *io;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    54
    GRPentry *entry;
480
2cf0b6edd200 Cleaned up some stuff, reduced some unnecessary 64-bit ints to 32.
Ryan C. Gordon <icculus@icculus.org>
parents: 467
diff changeset
    55
    PHYSFS_uint32 curPos;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
} GRPfileinfo;
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    59
static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    60
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    61
    return (io->read(io, buf, len) == len);
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    62
} /* readAll */
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    63
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    64
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
    65
static void GRP_dirClose(dvoid *opaque)
93
74d1df359be3 Patched to compile with an ancient version of CodeWarrior.
Ryan C. Gordon <icculus@icculus.org>
parents: 69
diff changeset
    66
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 504
diff changeset
    67
    GRPinfo *info = ((GRPinfo *) opaque);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    68
    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
    69
    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
    70
    allocator.Free(info);
93
74d1df359be3 Patched to compile with an ancient version of CodeWarrior.
Ryan C. Gordon <icculus@icculus.org>
parents: 69
diff changeset
    71
} /* GRP_dirClose */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    74
static PHYSFS_sint64 GRP_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    76
    GRPfileinfo *finfo = (GRPfileinfo *) 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
    77
    const GRPentry *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
    78
    const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    79
    PHYSFS_sint64 rc;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    81
    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
    82
        len = bytesLeft;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    84
    rc = finfo->io->read(finfo->io, buffer, len);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    85
    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
    86
        finfo->curPos += (PHYSFS_uint32) rc;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    87
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
    88
    return rc;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
} /* GRP_read */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
b050804123a3 Initial add. Implemented, buggy, but not crashing.
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 GRP_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    93
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    94
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    95
} /* GRP_write */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    96
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    97
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    98
static PHYSFS_sint64 GRP_tell(PHYSFS_Io *io)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   100
    return ((GRPfileinfo *) io->opaque)->curPos;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
} /* GRP_tell */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   104
static int GRP_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   106
    GRPfileinfo *finfo = (GRPfileinfo *) io->opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   107
    const GRPentry *entry = finfo->entry;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   108
    int rc;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
480
2cf0b6edd200 Cleaned up some stuff, reduced some unnecessary 64-bit ints to 32.
Ryan C. Gordon <icculus@icculus.org>
parents: 467
diff changeset
   110
    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
   111
    rc = finfo->io->seek(finfo->io, entry->startPos + offset);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   112
    if (rc)
480
2cf0b6edd200 Cleaned up some stuff, reduced some unnecessary 64-bit ints to 32.
Ryan C. Gordon <icculus@icculus.org>
parents: 467
diff changeset
   113
        finfo->curPos = (PHYSFS_uint32) offset;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   114
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
   115
    return rc;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
} /* GRP_seek */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   119
static PHYSFS_sint64 GRP_length(PHYSFS_Io *io)
28
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   120
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   121
    const GRPfileinfo *finfo = (GRPfileinfo *) io->opaque;
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
   122
    return ((PHYSFS_sint64) finfo->entry->size);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   123
} /* GRP_length */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   126
static PHYSFS_Io *GRP_duplicate(PHYSFS_Io *_io)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   128
    GRPfileinfo *origfinfo = (GRPfileinfo *) _io->opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   129
    PHYSFS_Io *io = NULL;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   130
    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
   131
    GRPfileinfo *finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   132
    GOTO_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, GRP_duplicate_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   133
    GOTO_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, GRP_duplicate_failed);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   135
    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
   136
    GOTO_IF_MACRO(io == NULL, NULL, GRP_duplicate_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   137
    finfo->io = io;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   138
    finfo->entry = origfinfo->entry;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   139
    finfo->curPos = 0;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   140
    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
   141
    retval->opaque = finfo;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   142
    return retval;
175
45e423c38ff8 Fixed byte ordering bugs.
Ryan C. Gordon <icculus@icculus.org>
parents: 163
diff changeset
   143
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   144
GRP_duplicate_failed:
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   145
    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
   146
    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
   147
    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
   148
    return NULL;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   149
} /* GRP_duplicate */
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   150
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   151
static int GRP_flush(PHYSFS_Io *io) { return 1;  /* no write support. */ }
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   152
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   153
static void GRP_destroy(PHYSFS_Io *io)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   154
{
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   155
    GRPfileinfo *finfo = (GRPfileinfo *) io->opaque;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   156
    finfo->io->destroy(finfo->io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   157
    allocator.Free(finfo);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   158
    allocator.Free(io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   159
} /* GRP_destroy */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
b050804123a3 Initial add. Implemented, buggy, but not crashing.
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
static const PHYSFS_Io GRP_Io =
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   163
{
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   164
    GRP_read,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   165
    GRP_write,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   166
    GRP_seek,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   167
    GRP_tell,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   168
    GRP_length,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   169
    GRP_duplicate,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   170
    GRP_flush,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   171
    GRP_destroy,
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   172
    NULL
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   173
};
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   174
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   175
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   176
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   177
static int grpEntryCmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   178
{
928
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   179
    if (one != two)
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   180
    {
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   181
        const GRPentry *a = (const GRPentry *) _a;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   182
        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: 866
diff changeset
   183
    } /* if */
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   184
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   185
    return 0;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   186
} /* grpEntryCmp */
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   187
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   188
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   189
static void grpEntrySwap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   190
{
928
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   191
    if (one != two)
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   192
    {
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   193
        GRPentry tmp;
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   194
        GRPentry *first = &(((GRPentry *) _a)[one]);
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   195
        GRPentry *second = &(((GRPentry *) _a)[two]);
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   196
        memcpy(&tmp, first, sizeof (GRPentry));
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   197
        memcpy(first, second, sizeof (GRPentry));
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   198
        memcpy(second, &tmp, sizeof (GRPentry));
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 866
diff changeset
   199
    } /* if */
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   200
} /* grpEntrySwap */
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   201
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   202
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   203
static int grp_load_entries(PHYSFS_Io *io, GRPinfo *info)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   205
    PHYSFS_uint32 fileCount = info->entryCount;
370
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
   206
    PHYSFS_uint32 location = 16;  /* sizeof sig. */
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   207
    GRPentry *entry;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   208
    char *ptr;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   209
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   210
    info->entries = (GRPentry *) allocator.Malloc(sizeof(GRPentry)*fileCount);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   211
    BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   212
370
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
   213
    location += (16 * fileCount);
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
   214
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   215
    for (entry = info->entries; fileCount > 0; fileCount--, entry++)
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   216
    {
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   217
        BAIL_IF_MACRO(!readAll(io, &entry->name, 12), NULL, 0);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   218
        BAIL_IF_MACRO(!readAll(io, &entry->size, 4), NULL, 0);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   219
        entry->name[12] = '\0';  /* name isn't null-terminated in file. */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   220
        if ((ptr = strchr(entry->name, ' ')) != NULL)
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   221
            *ptr = '\0';  /* trim extra spaces. */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   222
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   223
        entry->size = PHYSFS_swapULE32(entry->size);
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   224
        entry->startPos = location;
370
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
   225
        location += entry->size;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   226
    } /* for */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   227
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   228
    __PHYSFS_sort(info->entries, info->entryCount, grpEntryCmp, grpEntrySwap);
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
   229
    return 1;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   230
} /* grp_load_entries */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   231
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   232
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   233
static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   234
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   235
    PHYSFS_uint8 buf[12];
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   236
    GRPinfo *info = NULL;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   237
    PHYSFS_uint32 val = 0;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   238
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   239
    assert(io != NULL);  /* shouldn't ever happen. */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   241
    BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   242
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   243
    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
   244
    if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   245
        GOTO_MACRO(ERR_NOT_AN_ARCHIVE, GRP_openArchive_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   246
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   247
    info = (GRPinfo *) allocator.Malloc(sizeof (GRPinfo));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   248
    GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, GRP_openArchive_failed);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   249
    memset(info, '\0', sizeof (GRPinfo));
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   250
    info->io = io;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
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
    GOTO_IF_MACRO(!readAll(io,&val,sizeof(val)), NULL, GRP_openArchive_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   253
    info->entryCount = PHYSFS_swapULE32(val);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   255
    GOTO_IF_MACRO(!grp_load_entries(io, info), NULL, GRP_openArchive_failed);
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 504
diff changeset
   256
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
   257
    return info;
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   258
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   259
GRP_openArchive_failed:
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 504
diff changeset
   260
    if (info != NULL)
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   261
    {
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 504
diff changeset
   262
        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
   263
            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
   264
        allocator.Free(info);
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   265
    } /* if */
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   266
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
   267
    return NULL;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
} /* GRP_openArchive */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   271
static void GRP_enumerateFiles(dvoid *opaque, const char *dname,
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   272
                               int omitSymLinks, PHYSFS_EnumFilesCallback cb,
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   273
                               const char *origdir, void *callbackdata)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
{
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   275
    /* no directories in GRP files. */
866
d790fca8f7f7 Logic bug in MVL/HOG/GRP archivers: these archives never contain subdirs...but they
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   276
    if (*dname == '\0')
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   277
    {
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   278
        GRPinfo *info = (GRPinfo *) opaque;
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   279
        GRPentry *entry = info->entries;
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   280
        PHYSFS_uint32 max = info->entryCount;
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   281
        PHYSFS_uint32 i;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   283
        for (i = 0; i < max; i++, entry++)
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   284
            cb(callbackdata, origdir, entry->name);
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 650
diff changeset
   285
    } /* if */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
} /* GRP_enumerateFiles */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   289
static GRPentry *grp_find_entry(const GRPinfo *info, const char *name)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
{
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   291
    char *ptr = strchr(name, '.');
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   292
    GRPentry *a = info->entries;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   293
    PHYSFS_sint32 lo = 0;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   294
    PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   295
    PHYSFS_sint32 middle;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   296
    int rc;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   298
    /*
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   299
     * Rule out filenames to avoid unneeded processing...no dirs,
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   300
     *   big filenames, or extensions > 3 chars.
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   301
     */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   302
    BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   303
    BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   304
    BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   306
    while (lo <= hi)
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   307
    {
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   308
        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
   309
        rc = __PHYSFS_stricmpASCII(name, a[middle].name);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   310
        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
   311
            return &a[middle];
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   312
        else if (rc > 0)
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   313
            lo = middle + 1;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   314
        else
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   315
            hi = middle - 1;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   316
    } /* while */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   318
    BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   319
} /* grp_find_entry */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
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
   322
static int GRP_exists(dvoid *opaque, const char *name)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
{
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
   324
    return (grp_find_entry((GRPinfo *) opaque, name) != NULL);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
} /* GRP_exists */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
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
   328
static int GRP_isDirectory(dvoid *opaque, const char *name, int *fileExists)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 504
diff changeset
   330
    *fileExists = GRP_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
   331
    return 0;  /* never directories in a groupfile. */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
} /* GRP_isDirectory */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
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
   335
static int GRP_isSymLink(dvoid *opaque, const char *name, int *fileExists)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 504
diff changeset
   337
    *fileExists = GRP_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
   338
    return 0;  /* never symlinks in a groupfile. */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
} /* GRP_isSymLink */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   342
static PHYSFS_Io *GRP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   344
    PHYSFS_Io *retval = NULL;
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
    GRPinfo *info = (GRPinfo *) opaque;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    GRPfileinfo *finfo;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   347
    GRPentry *entry;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   349
    entry = grp_find_entry(info, fnm);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   350
    *fileExists = (entry != NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   351
    GOTO_IF_MACRO(entry == NULL, NULL, GRP_openRead_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   352
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   353
    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
   354
    GOTO_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, GRP_openRead_failed);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   356
    finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   357
    GOTO_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, GRP_openRead_failed);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   359
    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
   360
    GOTO_IF_MACRO(finfo->io == NULL, NULL, GRP_openRead_failed);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   361
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   362
    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
   363
        GOTO_MACRO(NULL, GRP_openRead_failed);
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
   364
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   365
    finfo->curPos = 0;
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   366
    finfo->entry = entry;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   367
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   368
    memcpy(retval, &GRP_Io, sizeof (*retval));
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   369
    retval->opaque = finfo;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   370
    return retval;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   371
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   372
GRP_openRead_failed:
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   373
    if (finfo != NULL)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   374
    {
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   375
        if (finfo->io != NULL)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   376
            finfo->io->destroy(finfo->io);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   377
        allocator.Free(finfo);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   378
    } /* if */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   379
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   380
    if (retval != NULL)
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   381
        allocator.Free(retval);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   382
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   383
    return NULL;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
} /* GRP_openRead */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   386
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   387
static PHYSFS_Io *GRP_openWrite(dvoid *opaque, const char *name)
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   388
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   389
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   390
} /* GRP_openWrite */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   391
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   392
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   393
static PHYSFS_Io *GRP_openAppend(dvoid *opaque, const char *name)
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   394
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   395
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   396
} /* GRP_openAppend */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   397
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   398
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
   399
static int GRP_remove(dvoid *opaque, const char *name)
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   400
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   401
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   402
} /* GRP_remove */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   403
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   404
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
   405
static int GRP_mkdir(dvoid *opaque, const char *name)
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   406
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   407
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   408
} /* GRP_mkdir */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   409
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   410
1108
b63e39d5be04 Fixed details of PHYSFS_Archiver's stat method.
Ryan C. Gordon <icculus@icculus.org>
parents: 1106
diff changeset
   411
static int GRP_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
   412
                    PHYSFS_Stat *stat)
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   413
{
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   414
    const GRPinfo *info = (const GRPinfo *) opaque;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   415
    const GRPentry *entry = grp_find_entry(info, filename);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   416
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   417
    *exists = (entry != 0);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   418
    if (!entry)
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   419
        return 0;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   420
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   421
    stat->filesize = entry->size;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   422
    stat->filetype = PHYSFS_FILETYPE_REGULAR;
1115
bc7257f4f8da Removed __PHYSFS_platformGetLastModTime().
Ryan C. Gordon <icculus@icculus.org>
parents: 1113
diff changeset
   423
    stat->modtime = -1;
bc7257f4f8da Removed __PHYSFS_platformGetLastModTime().
Ryan C. Gordon <icculus@icculus.org>
parents: 1113
diff changeset
   424
    stat->createtime = -1;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   425
    stat->accesstime = -1;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   426
    stat->readonly = 1;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   427
1106
94c3669d0311 Fixed PHYSFS_stat()'s return value to match rest of PhysicsFS API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1098
diff changeset
   428
    return 1;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   429
} /* GRP_stat */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   430
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   431
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   432
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   433
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   434
    "GRP",
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   435
    GRP_ARCHIVE_DESCRIPTION,
767
db29bf06d171 Changed my email address.
Ryan C. Gordon <icculus@icculus.org>
parents: 754
diff changeset
   436
    "Ryan C. Gordon <icculus@icculus.org>",
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   437
    "http://icculus.org/physfs/",
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   438
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   439
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   440
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   441
const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   442
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   443
    &__PHYSFS_ArchiveInfo_GRP,
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   444
    GRP_openArchive,        /* openArchive() method    */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   445
    GRP_enumerateFiles,     /* enumerateFiles() method */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   446
    GRP_exists,             /* exists() method         */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   447
    GRP_isDirectory,        /* isDirectory() method    */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   448
    GRP_isSymLink,          /* isSymLink() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   449
    GRP_openRead,           /* openRead() method       */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   450
    GRP_openWrite,          /* openWrite() method      */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   451
    GRP_openAppend,         /* openAppend() method     */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   452
    GRP_remove,             /* remove() method         */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   453
    GRP_mkdir,              /* mkdir() method          */
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   454
    GRP_dirClose,           /* dirClose() method       */
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   455
    GRP_stat                /* stat() method           */
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   456
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   457
214
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
   458
#endif  /* defined PHYSFS_SUPPORTS_GRP */
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
   459
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
/* end of grp.c ... */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461