misc/libphysfs/physfs.c
author Wuzzy <almikes@aol.com>
Sun, 08 Oct 2017 23:34:14 +0200
changeset 12678 fb240fa9f178
parent 12453 89423b1db329
permissions -rw-r--r--
Remove spaces from grave names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/**
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * PhysicsFS; a portable, flexible file i/o abstraction.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * Documentation is in physfs.h. It's verbose, honest.  :)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
 *  This file written by Ryan C. Gordon.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
/* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
/* !!! FIXME: use snprintf(), not sprintf(). */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
typedef struct __PHYSFS_DIRHANDLE__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
    void *opaque;  /* Instance data unique to the archiver. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
    char *dirName;  /* Path to archive in platform-dependent notation. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
    char *mountPoint; /* Mountpoint in virtual file tree. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
    const PHYSFS_Archiver *funcs;  /* Ptr to archiver info for this handle. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
    struct __PHYSFS_DIRHANDLE__ *next;  /* linked list stuff. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
} DirHandle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
typedef struct __PHYSFS_FILEHANDLE__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
    PHYSFS_Io *io;  /* Instance data unique to the archiver for this file. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
    PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
    const DirHandle *dirHandle;  /* Archiver instance that created this */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
    PHYSFS_uint8 *buffer;  /* Buffer, if set (NULL otherwise). Don't touch! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
    PHYSFS_uint32 bufsize;  /* Bufsize, if set (0 otherwise). Don't touch! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
    PHYSFS_uint32 buffill;  /* Buffer fill size. Don't touch! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
    PHYSFS_uint32 bufpos;  /* Buffer position. Don't touch! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
    struct __PHYSFS_FILEHANDLE__ *next;  /* linked list stuff. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
} FileHandle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
typedef struct __PHYSFS_ERRSTATETYPE__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
    void *tid;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
    PHYSFS_ErrorCode code;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
    struct __PHYSFS_ERRSTATETYPE__ *next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
} ErrState;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
/* General PhysicsFS state ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
static int initialized = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
static ErrState *errorStates = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
static DirHandle *searchPath = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
static DirHandle *writeDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
static FileHandle *openWriteList = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
static FileHandle *openReadList = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
static char *baseDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
static char *userDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
static char *prefDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
static int allowSymLinks = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
static const PHYSFS_Archiver **archivers = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
static const PHYSFS_ArchiveInfo **archiveInfo = NULL;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
    62
static volatile size_t numArchivers = 0;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
/* mutexes ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
static void *errorLock = NULL;     /* protects error message list.        */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
static void *stateLock = NULL;     /* protects other PhysFS static state. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
/* allocator ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
static int externalAllocator = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
PHYSFS_Allocator allocator;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
/* PHYSFS_Io implementation for i/o to physical filesystem... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
/* !!! FIXME: maybe refcount the paths in a string pool? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
typedef struct __PHYSFS_NativeIoInfo
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
    void *handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
    const char *path;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
    int mode;   /* 'r', 'w', or 'a' */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
} NativeIoInfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
static PHYSFS_sint64 nativeIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
    NativeIoInfo *info = (NativeIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
    return __PHYSFS_platformRead(info->handle, buf, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
} /* nativeIo_read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
static PHYSFS_sint64 nativeIo_write(PHYSFS_Io *io, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
                                    PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
    NativeIoInfo *info = (NativeIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
    return __PHYSFS_platformWrite(info->handle, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
} /* nativeIo_write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
static int nativeIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
    NativeIoInfo *info = (NativeIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
    return __PHYSFS_platformSeek(info->handle, offset);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
} /* nativeIo_seek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
static PHYSFS_sint64 nativeIo_tell(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
    NativeIoInfo *info = (NativeIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
    return __PHYSFS_platformTell(info->handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
} /* nativeIo_tell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
static PHYSFS_sint64 nativeIo_length(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
    NativeIoInfo *info = (NativeIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
    return __PHYSFS_platformFileLength(info->handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
} /* nativeIo_length */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
static PHYSFS_Io *nativeIo_duplicate(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
    NativeIoInfo *info = (NativeIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
    return __PHYSFS_createNativeIo(info->path, info->mode);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
} /* nativeIo_duplicate */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
static int nativeIo_flush(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
    return __PHYSFS_platformFlush(io->opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
} /* nativeIo_flush */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
static void nativeIo_destroy(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
    NativeIoInfo *info = (NativeIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
    __PHYSFS_platformClose(info->handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
    allocator.Free((void *) info->path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
    allocator.Free(info);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
    allocator.Free(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
} /* nativeIo_destroy */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
static const PHYSFS_Io __PHYSFS_nativeIoInterface =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
    CURRENT_PHYSFS_IO_API_VERSION, NULL,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
    nativeIo_read,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
    nativeIo_write,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
    nativeIo_seek,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
    nativeIo_tell,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
    nativeIo_length,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
    nativeIo_duplicate,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
    nativeIo_flush,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
    nativeIo_destroy
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
    PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
    NativeIoInfo *info = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
    void *handle = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
    char *pathdup = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
    assert((mode == 'r') || (mode == 'w') || (mode == 'a'));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
    io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
    GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
    info = (NativeIoInfo *) allocator.Malloc(sizeof (NativeIoInfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
    GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
    pathdup = (char *) allocator.Malloc(strlen(path) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
    GOTO_IF_MACRO(!pathdup, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
    if (mode == 'r')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
        handle = __PHYSFS_platformOpenRead(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
    else if (mode == 'w')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
        handle = __PHYSFS_platformOpenWrite(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
    else if (mode == 'a')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
        handle = __PHYSFS_platformOpenAppend(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
    GOTO_IF_MACRO(!handle, ERRPASS, createNativeIo_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
    strcpy(pathdup, path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
    info->handle = handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
    info->path = pathdup;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
    info->mode = mode;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
    memcpy(io, &__PHYSFS_nativeIoInterface, sizeof (*io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
    io->opaque = info;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
    return io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
createNativeIo_failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
    if (handle != NULL) __PHYSFS_platformClose(handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
    if (pathdup != NULL) allocator.Free(pathdup);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
    if (info != NULL) allocator.Free(info);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
    if (io != NULL) allocator.Free(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
} /* __PHYSFS_createNativeIo */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
/* PHYSFS_Io implementation for i/o to a memory buffer... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
typedef struct __PHYSFS_MemoryIoInfo
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
    const PHYSFS_uint8 *buf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
    PHYSFS_uint64 len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
    PHYSFS_uint64 pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
    PHYSFS_Io *parent;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
    volatile PHYSFS_uint32 refcount;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
    void (*destruct)(void *);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
} MemoryIoInfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
static PHYSFS_sint64 memoryIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
    MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
    const PHYSFS_uint64 avail = info->len - info->pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
    assert(avail <= info->len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
    if (avail == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
        return 0;  /* we're at EOF; nothing to do. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
    if (len > avail)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
        len = avail;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
    memcpy(buf, info->buf + info->pos, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
    info->pos += len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
    return len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
} /* memoryIo_read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
static PHYSFS_sint64 memoryIo_write(PHYSFS_Io *io, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
                                    PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
    BAIL_MACRO(PHYSFS_ERR_OPEN_FOR_READING, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
} /* memoryIo_write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
static int memoryIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
    MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
    BAIL_IF_MACRO(offset > info->len, PHYSFS_ERR_PAST_EOF, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
    info->pos = offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
} /* memoryIo_seek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
static PHYSFS_sint64 memoryIo_tell(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
    const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
    return (PHYSFS_sint64) info->pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
} /* memoryIo_tell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
static PHYSFS_sint64 memoryIo_length(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
    const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
    return (PHYSFS_sint64) info->len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
} /* memoryIo_length */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
static PHYSFS_Io *memoryIo_duplicate(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
    MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
    MemoryIoInfo *newinfo = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
    PHYSFS_Io *parent = info->parent;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
    PHYSFS_Io *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
    /* avoid deep copies. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
    assert((!parent) || (!((MemoryIoInfo *) parent->opaque)->parent) );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   253
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   254
    /* share the buffer between duplicates. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   255
    if (parent != NULL)  /* dup the parent, increment its refcount. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   256
        return parent->duplicate(parent);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   257
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   258
    /* we're the parent. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   259
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   260
    retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   261
    BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
    newinfo = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
    if (!newinfo)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   264
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   265
        allocator.Free(retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   266
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   267
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   268
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   269
    /* !!! FIXME: want lockless atomic increment. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   270
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   271
    info->refcount++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   272
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   273
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   274
    memset(newinfo, '\0', sizeof (*info));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   275
    newinfo->buf = info->buf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   276
    newinfo->len = info->len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   277
    newinfo->pos = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   278
    newinfo->parent = io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   279
    newinfo->refcount = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
    newinfo->destruct = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   281
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   282
    memcpy(retval, io, sizeof (*retval));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
    retval->opaque = newinfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   284
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   285
} /* memoryIo_duplicate */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   286
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   287
static int memoryIo_flush(PHYSFS_Io *io) { return 1;  /* it's read-only. */ }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   288
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   289
static void memoryIo_destroy(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   290
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   291
    MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   292
    PHYSFS_Io *parent = info->parent;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   293
    int should_die = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   294
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   295
    if (parent != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
        assert(info->buf == ((MemoryIoInfo *) info->parent->opaque)->buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
        assert(info->len == ((MemoryIoInfo *) info->parent->opaque)->len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
        assert(info->refcount == 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
        assert(info->destruct == NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
        allocator.Free(info);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
        allocator.Free(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   303
        parent->destroy(parent);  /* decrements refcount. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   307
    /* we _are_ the parent. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
    assert(info->refcount > 0);  /* even in a race, we hold a reference. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   310
    /* !!! FIXME: want lockless atomic decrement. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
    info->refcount--;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
    should_die = (info->refcount == 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   315
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   316
    if (should_die)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
        void (*destruct)(void *) = info->destruct;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
        void *buf = (void *) info->buf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
        io->opaque = NULL;  /* kill this here in case of race. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   321
        allocator.Free(info);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   322
        allocator.Free(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   323
        if (destruct != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   324
            destruct(buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   325
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   326
} /* memoryIo_destroy */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
static const PHYSFS_Io __PHYSFS_memoryIoInterface =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
    CURRENT_PHYSFS_IO_API_VERSION, NULL,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
    memoryIo_read,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
    memoryIo_write,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
    memoryIo_seek,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
    memoryIo_tell,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
    memoryIo_length,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
    memoryIo_duplicate,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
    memoryIo_flush,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
    memoryIo_destroy
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
                                   void (*destruct)(void *))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
    PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
    MemoryIoInfo *info = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   348
    io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
    GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
    info = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
    GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
    memset(info, '\0', sizeof (*info));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
    info->buf = (const PHYSFS_uint8 *) buf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
    info->len = len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
    info->pos = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
    info->parent = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
    info->refcount = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
    info->destruct = destruct;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
    memcpy(io, &__PHYSFS_memoryIoInterface, sizeof (*io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
    io->opaque = info;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
    return io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
createMemoryIo_failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
    if (info != NULL) allocator.Free(info);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
    if (io != NULL) allocator.Free(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
} /* __PHYSFS_createMemoryIo */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
/* PHYSFS_Io implementation for i/o to a PHYSFS_File... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   373
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
static PHYSFS_sint64 handleIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
    return PHYSFS_readBytes((PHYSFS_File *) io->opaque, buf, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
} /* handleIo_read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379
static PHYSFS_sint64 handleIo_write(PHYSFS_Io *io, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   380
                                    PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   381
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   382
    return PHYSFS_writeBytes((PHYSFS_File *) io->opaque, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   383
} /* handleIo_write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   384
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   385
static int handleIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   386
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   387
    return PHYSFS_seek((PHYSFS_File *) io->opaque, offset);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   388
} /* handleIo_seek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   389
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   390
static PHYSFS_sint64 handleIo_tell(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   391
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   392
    return PHYSFS_tell((PHYSFS_File *) io->opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   393
} /* handleIo_tell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   394
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   395
static PHYSFS_sint64 handleIo_length(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   396
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   397
    return PHYSFS_fileLength((PHYSFS_File *) io->opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   398
} /* handleIo_length */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   399
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   400
static PHYSFS_Io *handleIo_duplicate(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   401
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   402
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   403
     * There's no duplicate at the PHYSFS_File level, so we break the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   404
     *  abstraction. We're allowed to: we're physfs.c!
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   405
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   406
    FileHandle *origfh = (FileHandle *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   407
    FileHandle *newfh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   408
    PHYSFS_Io *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   409
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   410
    GOTO_IF_MACRO(!newfh, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   411
    memset(newfh, '\0', sizeof (*newfh));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   412
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   413
    retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   414
    GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   415
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   416
#if 0  /* we don't buffer the duplicate, at least not at the moment. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   417
    if (origfh->buffer != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   418
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   419
        newfh->buffer = (PHYSFS_uint8 *) allocator.Malloc(origfh->bufsize);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   420
        if (!newfh->buffer)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   421
            GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   422
        newfh->bufsize = origfh->bufsize;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   423
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   424
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   425
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   426
    newfh->io = origfh->io->duplicate(origfh->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   427
    GOTO_IF_MACRO(!newfh->io, ERRPASS, handleIo_dupe_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   428
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   429
    newfh->forReading = origfh->forReading;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   430
    newfh->dirHandle = origfh->dirHandle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   431
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   432
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   433
    if (newfh->forReading)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   434
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   435
        newfh->next = openReadList;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   436
        openReadList = newfh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   437
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   438
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   439
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   440
        newfh->next = openWriteList;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   441
        openWriteList = newfh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   442
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   443
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   444
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   445
    memcpy(retval, io, sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   446
    retval->opaque = newfh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   447
    return retval;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   448
    
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   449
handleIo_dupe_failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   450
    if (newfh)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   451
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   452
        if (newfh->io != NULL) newfh->io->destroy(newfh->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   453
        if (newfh->buffer != NULL) allocator.Free(newfh->buffer);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   454
        allocator.Free(newfh);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   455
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   456
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   457
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   458
} /* handleIo_duplicate */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   459
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   460
static int handleIo_flush(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   461
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   462
    return PHYSFS_flush((PHYSFS_File *) io->opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   463
} /* handleIo_flush */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   464
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   465
static void handleIo_destroy(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   466
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   467
    if (io->opaque != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   468
        PHYSFS_close((PHYSFS_File *) io->opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   469
    allocator.Free(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   470
} /* handleIo_destroy */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   471
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   472
static const PHYSFS_Io __PHYSFS_handleIoInterface =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   473
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   474
    CURRENT_PHYSFS_IO_API_VERSION, NULL,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   475
    handleIo_read,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   476
    handleIo_write,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   477
    handleIo_seek,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   478
    handleIo_tell,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   479
    handleIo_length,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   480
    handleIo_duplicate,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   481
    handleIo_flush,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   482
    handleIo_destroy
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   483
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   484
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   485
static PHYSFS_Io *__PHYSFS_createHandleIo(PHYSFS_File *f)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   486
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   487
    PHYSFS_Io *io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   488
    BAIL_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   489
    memcpy(io, &__PHYSFS_handleIoInterface, sizeof (*io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   490
    io->opaque = f;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   491
    return io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   492
} /* __PHYSFS_createHandleIo */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   493
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   494
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   495
/* functions ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   496
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   497
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   498
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   499
    char **list;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   500
    PHYSFS_uint32 size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   501
    PHYSFS_ErrorCode errcode;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   502
} EnumStringListCallbackData;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   503
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   504
static void enumStringListCallback(void *data, const char *str)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   505
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   506
    void *ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   507
    char *newstr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   508
    EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   509
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   510
    if (pecd->errcode)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   511
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   512
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   513
    ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   514
    newstr = (char *) allocator.Malloc(strlen(str) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   515
    if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   516
        pecd->list = (char **) ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   517
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   518
    if ((ptr == NULL) || (newstr == NULL))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   519
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   520
        pecd->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   521
        pecd->list[pecd->size] = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   522
        PHYSFS_freeList(pecd->list);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   523
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   524
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   525
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   526
    strcpy(newstr, str);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   527
    pecd->list[pecd->size] = newstr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   528
    pecd->size++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   529
} /* enumStringListCallback */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   530
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   531
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   532
static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   533
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   534
    EnumStringListCallbackData ecd;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   535
    memset(&ecd, '\0', sizeof (ecd));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   536
    ecd.list = (char **) allocator.Malloc(sizeof (char *));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   537
    BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   538
    func(enumStringListCallback, &ecd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   539
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   540
    if (ecd.errcode)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   541
    {
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   542
        PHYSFS_setErrorCode(ecd.errcode);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   543
        return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   544
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   545
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   546
    ecd.list[ecd.size] = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   547
    return ecd.list;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   548
} /* doEnumStringList */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   549
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   550
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   551
static void __PHYSFS_bubble_sort(void *a, size_t lo, size_t hi,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   552
                                 int (*cmpfn)(void *, size_t, size_t),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   553
                                 void (*swapfn)(void *, size_t, size_t))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   554
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   555
    size_t i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   556
    int sorted;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   557
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   558
    do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   559
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   560
        sorted = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   561
        for (i = lo; i < hi; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   562
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   563
            if (cmpfn(a, i, i + 1) > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   564
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   565
                swapfn(a, i, i + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   566
                sorted = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   567
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   568
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   569
    } while (!sorted);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   570
} /* __PHYSFS_bubble_sort */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   571
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   572
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   573
static void __PHYSFS_quick_sort(void *a, size_t lo, size_t hi,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   574
                         int (*cmpfn)(void *, size_t, size_t),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   575
                         void (*swapfn)(void *, size_t, size_t))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   576
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   577
    size_t i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   578
    size_t j;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   579
    size_t v;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   580
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   581
    if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   582
        __PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   583
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   584
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   585
        i = (hi + lo) / 2;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   586
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   587
        if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   588
        if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   589
        if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   590
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   591
        j = hi - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   592
        swapfn(a, i, j);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   593
        i = lo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   594
        v = j;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   595
        while (1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   596
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   597
            while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   598
            while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   599
            if (j < i)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   600
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   601
            swapfn(a, i, j);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   602
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   603
        if (i != (hi-1))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   604
            swapfn(a, i, hi-1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   605
        __PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   606
        __PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   607
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   608
} /* __PHYSFS_quick_sort */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   609
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   610
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   611
void __PHYSFS_sort(void *entries, size_t max,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   612
                   int (*cmpfn)(void *, size_t, size_t),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   613
                   void (*swapfn)(void *, size_t, size_t))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   614
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   615
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   616
     * Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   617
     *   https://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   618
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   619
    if (max > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   620
        __PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   621
} /* __PHYSFS_sort */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   622
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   623
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   624
static ErrState *findErrorForCurrentThread(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   625
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   626
    ErrState *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   627
    void *tid;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   628
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   629
    if (errorLock != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   630
        __PHYSFS_platformGrabMutex(errorLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   631
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   632
    if (errorStates != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   633
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   634
        tid = __PHYSFS_platformGetThreadID();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   635
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   636
        for (i = errorStates; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   637
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   638
            if (i->tid == tid)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   639
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   640
                if (errorLock != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   641
                    __PHYSFS_platformReleaseMutex(errorLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   642
                return i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   643
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   644
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   645
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   646
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   647
    if (errorLock != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   648
        __PHYSFS_platformReleaseMutex(errorLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   649
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   650
    return NULL;   /* no error available. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   651
} /* findErrorForCurrentThread */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   652
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   653
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   654
/* this doesn't reset the error state. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   655
static inline PHYSFS_ErrorCode currentErrorCode(void)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   656
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   657
    const ErrState *err = findErrorForCurrentThread();
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   658
    return err ? err->code : PHYSFS_ERR_OK;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   659
} /* currentErrorCode */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   660
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   661
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   662
PHYSFS_ErrorCode PHYSFS_getLastErrorCode(void)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   663
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   664
    ErrState *err = findErrorForCurrentThread();
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   665
    const PHYSFS_ErrorCode retval = (err) ? err->code : PHYSFS_ERR_OK;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   666
    if (err)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   667
        err->code = PHYSFS_ERR_OK;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   668
    return retval;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   669
} /* PHYSFS_getLastErrorCode */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   670
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   671
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   672
PHYSFS_DECL const char *PHYSFS_getErrorByCode(PHYSFS_ErrorCode code)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   673
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   674
    switch (code)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   675
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   676
        case PHYSFS_ERR_OK: return "no error";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   677
        case PHYSFS_ERR_OTHER_ERROR: return "unknown error";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   678
        case PHYSFS_ERR_OUT_OF_MEMORY: return "out of memory";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   679
        case PHYSFS_ERR_NOT_INITIALIZED: return "not initialized";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   680
        case PHYSFS_ERR_IS_INITIALIZED: return "already initialized";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   681
        case PHYSFS_ERR_ARGV0_IS_NULL: return "argv[0] is NULL";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   682
        case PHYSFS_ERR_UNSUPPORTED: return "unsupported";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   683
        case PHYSFS_ERR_PAST_EOF: return "past end of file";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   684
        case PHYSFS_ERR_FILES_STILL_OPEN: return "files still open";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   685
        case PHYSFS_ERR_INVALID_ARGUMENT: return "invalid argument";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   686
        case PHYSFS_ERR_NOT_MOUNTED: return "not mounted";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   687
        case PHYSFS_ERR_NOT_FOUND: return "not found";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   688
        case PHYSFS_ERR_SYMLINK_FORBIDDEN: return "symlinks are forbidden";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   689
        case PHYSFS_ERR_NO_WRITE_DIR: return "write directory is not set";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   690
        case PHYSFS_ERR_OPEN_FOR_READING: return "file open for reading";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   691
        case PHYSFS_ERR_OPEN_FOR_WRITING: return "file open for writing";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   692
        case PHYSFS_ERR_NOT_A_FILE: return "not a file";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   693
        case PHYSFS_ERR_READ_ONLY: return "read-only filesystem";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   694
        case PHYSFS_ERR_CORRUPT: return "corrupted";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   695
        case PHYSFS_ERR_SYMLINK_LOOP: return "infinite symbolic link loop";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   696
        case PHYSFS_ERR_IO: return "i/o error";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   697
        case PHYSFS_ERR_PERMISSION: return "permission denied";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   698
        case PHYSFS_ERR_NO_SPACE: return "no space available for writing";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   699
        case PHYSFS_ERR_BAD_FILENAME: return "filename is illegal or insecure";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   700
        case PHYSFS_ERR_BUSY: return "tried to modify a file the OS needs";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   701
        case PHYSFS_ERR_DIR_NOT_EMPTY: return "directory isn't empty";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   702
        case PHYSFS_ERR_OS_ERROR: return "OS reported an error";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   703
        case PHYSFS_ERR_DUPLICATE: return "duplicate resource";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   704
        case PHYSFS_ERR_BAD_PASSWORD: return "bad password";
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   705
    } /* switch */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   706
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   707
    return NULL;  /* don't know this error code. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   708
} /* PHYSFS_getErrorByCode */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   709
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   710
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   711
void PHYSFS_setErrorCode(PHYSFS_ErrorCode errcode)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   712
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   713
    ErrState *err;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   714
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   715
    if (!errcode)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   716
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   717
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   718
    err = findErrorForCurrentThread();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   719
    if (err == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   720
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   721
        err = (ErrState *) allocator.Malloc(sizeof (ErrState));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   722
        if (err == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   723
            return;   /* uhh...? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   724
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   725
        memset(err, '\0', sizeof (ErrState));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   726
        err->tid = __PHYSFS_platformGetThreadID();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   727
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   728
        if (errorLock != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   729
            __PHYSFS_platformGrabMutex(errorLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   730
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   731
        err->next = errorStates;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   732
        errorStates = err;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   733
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   734
        if (errorLock != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   735
            __PHYSFS_platformReleaseMutex(errorLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   736
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   737
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   738
    err->code = errcode;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   739
} /* PHYSFS_setErrorCode */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   740
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   741
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   742
const char *PHYSFS_getLastError(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   743
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   744
    const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   745
    return (err) ? PHYSFS_getErrorByCode(err) : NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   746
} /* PHYSFS_getLastError */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   747
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   748
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   749
/* MAKE SURE that errorLock is held before calling this! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   750
static void freeErrorStates(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   751
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   752
    ErrState *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   753
    ErrState *next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   754
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   755
    for (i = errorStates; i != NULL; i = next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   756
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   757
        next = i->next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   758
        allocator.Free(i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   759
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   760
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   761
    errorStates = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   762
} /* freeErrorStates */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   763
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   764
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   765
void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   766
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   767
    if (ver != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   768
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   769
        ver->major = PHYSFS_VER_MAJOR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   770
        ver->minor = PHYSFS_VER_MINOR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   771
        ver->patch = PHYSFS_VER_PATCH;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   772
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   773
} /* PHYSFS_getLinkedVersion */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   774
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   775
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   776
static const char *find_filename_extension(const char *fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   777
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   778
    const char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   779
    if (fname != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   780
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   781
        const char *p = strchr(fname, '.');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   782
        retval = p;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   783
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   784
        while (p != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   785
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   786
            p = strchr(p + 1, '.');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   787
            if (p != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   788
                retval = p;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   789
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   790
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   791
        if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   792
            retval++;  /* skip '.' */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   793
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   794
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   795
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   796
} /* find_filename_extension */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   797
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   798
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   799
static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   800
                             const char *d, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   801
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   802
    DirHandle *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   803
    void *opaque = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   804
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   805
    if (io != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   806
        BAIL_IF_MACRO(!io->seek(io, 0), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   807
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   808
    opaque = funcs->openArchive(io, d, forWriting);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   809
    if (opaque != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   810
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   811
        retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   812
        if (retval == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   813
            funcs->closeArchive(opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   814
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   815
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   816
            memset(retval, '\0', sizeof (DirHandle));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   817
            retval->mountPoint = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   818
            retval->funcs = funcs;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   819
            retval->opaque = opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   820
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   821
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   822
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   823
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   824
} /* tryOpenDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   825
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   826
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   827
static DirHandle *openDirectory(PHYSFS_Io *io, const char *d, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   828
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   829
    DirHandle *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   830
    const PHYSFS_Archiver **i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   831
    const char *ext;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   832
    int created_io = 0;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   833
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   834
    assert((io != NULL) || (d != NULL));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   835
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   836
    if (io == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   837
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   838
        /* DIR gets first shot (unlike the rest, it doesn't deal with files). */
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   839
        extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   840
        retval = tryOpenDir(io, &__PHYSFS_Archiver_DIR, d, forWriting);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   841
        if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   842
            return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   843
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   844
        io = __PHYSFS_createNativeIo(d, forWriting ? 'w' : 'r');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   845
        BAIL_IF_MACRO(!io, ERRPASS, 0);
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   846
        created_io = 1;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   847
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   848
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   849
    ext = find_filename_extension(d);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   850
    if (ext != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   851
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   852
        /* Look for archivers with matching file extensions first... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   853
        for (i = archivers; (*i != NULL) && (retval == NULL); i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   854
        {
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   855
            if (__PHYSFS_utf8stricmp(ext, (*i)->info.extension) == 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   856
                retval = tryOpenDir(io, *i, d, forWriting);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   857
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   858
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   859
        /* failing an exact file extension match, try all the others... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   860
        for (i = archivers; (*i != NULL) && (retval == NULL); i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   861
        {
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   862
            if (__PHYSFS_utf8stricmp(ext, (*i)->info.extension) != 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   863
                retval = tryOpenDir(io, *i, d, forWriting);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   864
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   865
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   866
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   867
    else  /* no extension? Try them all. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   868
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   869
        for (i = archivers; (*i != NULL) && (retval == NULL); i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   870
            retval = tryOpenDir(io, *i, d, forWriting);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   871
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   872
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   873
    if ((!retval) && (created_io))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   874
        io->destroy(io);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   875
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   876
    BAIL_IF_MACRO(!retval, PHYSFS_ERR_UNSUPPORTED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   877
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   878
} /* openDirectory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   879
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   880
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   881
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   882
 * Make a platform-independent path string sane. Doesn't actually check the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   883
 *  file hierarchy, it just cleans up the string.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   884
 *  (dst) must be a buffer at least as big as (src), as this is where the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   885
 *  cleaned up string is deposited.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   886
 * If there are illegal bits in the path (".." entries, etc) then we
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   887
 *  return zero and (dst) is undefined. Non-zero if the path was sanitized.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   888
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   889
static int sanitizePlatformIndependentPath(const char *src, char *dst)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   890
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   891
    char *prev;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   892
    char ch;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   893
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   894
    while (*src == '/')  /* skip initial '/' chars... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   895
        src++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   896
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   897
    prev = dst;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   898
    do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   899
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   900
        ch = *(src++);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   901
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   902
        if ((ch == ':') || (ch == '\\'))  /* illegal chars in a physfs path. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   903
            BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   904
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   905
        if (ch == '/')   /* path separator. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   906
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   907
            *dst = '\0';  /* "." and ".." are illegal pathnames. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   908
            if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   909
                BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   910
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   911
            while (*src == '/')   /* chop out doubles... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   912
                src++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   913
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   914
            if (*src == '\0') /* ends with a pathsep? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   915
                break;  /* we're done, don't add final pathsep to dst. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   916
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   917
            prev = dst + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   918
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   919
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   920
        *(dst++) = ch;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   921
    } while (ch != '\0');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   922
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   923
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   924
} /* sanitizePlatformIndependentPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   925
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   926
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   927
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   928
 * Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   929
 *  output from sanitizePlatformIndependentPath(), so that it is in a known
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   930
 *  state.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   931
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   932
 * This only finds legitimate segments of a mountpoint. If the mountpoint is
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   933
 *  "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   934
 *  all zero. "/a/b" will succeed, though.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   935
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   936
static int partOfMountPoint(DirHandle *h, char *fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   937
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   938
    /* !!! FIXME: This code feels gross. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   939
    int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   940
    size_t len, mntpntlen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   941
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   942
    if (h->mountPoint == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   943
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   944
    else if (*fname == '\0')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   945
        return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   946
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   947
    len = strlen(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   948
    mntpntlen = strlen(h->mountPoint);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   949
    if (len > mntpntlen)  /* can't be a subset of mountpoint. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   950
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   951
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   952
    /* if true, must be not a match or a complete match, but not a subset. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   953
    if ((len + 1) == mntpntlen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   954
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   955
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   956
    rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   957
    if (rc != 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   958
        return 0;  /* not a match. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   959
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   960
    /* make sure /a/b matches /a/b/ and not /a/bc ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   961
    return h->mountPoint[len] == '/';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   962
} /* partOfMountPoint */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   963
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   964
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   965
static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   966
                                  const char *mountPoint, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   967
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   968
    DirHandle *dirHandle = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   969
    char *tmpmntpnt = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   970
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   971
    if (mountPoint != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   972
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   973
        const size_t len = strlen(mountPoint) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   974
        tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   975
        GOTO_IF_MACRO(!tmpmntpnt, PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   976
        if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   977
            goto badDirHandle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   978
        mountPoint = tmpmntpnt;  /* sanitized version. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   979
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   980
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   981
    dirHandle = openDirectory(io, newDir, forWriting);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   982
    GOTO_IF_MACRO(!dirHandle, ERRPASS, badDirHandle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   983
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   984
    if (newDir == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   985
        dirHandle->dirName = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   986
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   987
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   988
        dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   989
        if (!dirHandle->dirName)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   990
            GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   991
        strcpy(dirHandle->dirName, newDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   992
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   993
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   994
    if ((mountPoint != NULL) && (*mountPoint != '\0'))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   995
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   996
        dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   997
        if (!dirHandle->mountPoint)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   998
            GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   999
        strcpy(dirHandle->mountPoint, mountPoint);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1000
        strcat(dirHandle->mountPoint, "/");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1001
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1002
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1003
    __PHYSFS_smallFree(tmpmntpnt);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1004
    return dirHandle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1005
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1006
badDirHandle:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1007
    if (dirHandle != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1008
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1009
        dirHandle->funcs->closeArchive(dirHandle->opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1010
        allocator.Free(dirHandle->dirName);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1011
        allocator.Free(dirHandle->mountPoint);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1012
        allocator.Free(dirHandle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1013
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1014
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1015
    __PHYSFS_smallFree(tmpmntpnt);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1016
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1017
} /* createDirHandle */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1018
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1019
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1020
/* MAKE SURE you've got the stateLock held before calling this! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1021
static int freeDirHandle(DirHandle *dh, FileHandle *openList)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1022
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1023
    FileHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1024
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1025
    if (dh == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1026
        return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1027
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1028
    for (i = openList; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1029
        BAIL_IF_MACRO(i->dirHandle == dh, PHYSFS_ERR_FILES_STILL_OPEN, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1030
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1031
    dh->funcs->closeArchive(dh->opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1032
    allocator.Free(dh->dirName);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1033
    allocator.Free(dh->mountPoint);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1034
    allocator.Free(dh);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1035
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1036
} /* freeDirHandle */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1037
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1038
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1039
static char *calculateBaseDir(const char *argv0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1040
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1041
    const char dirsep = __PHYSFS_platformDirSeparator;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1042
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1043
    char *ptr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1044
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1045
    /* Give the platform layer first shot at this. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1046
    retval = __PHYSFS_platformCalcBaseDir(argv0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1047
    if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1048
        return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1049
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1050
    /* We need argv0 to go on. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1051
    BAIL_IF_MACRO(argv0 == NULL, PHYSFS_ERR_ARGV0_IS_NULL, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1052
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1053
    ptr = strrchr(argv0, dirsep);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1054
    if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1055
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1056
        const size_t size = ((size_t) (ptr - argv0)) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1057
        retval = (char *) allocator.Malloc(size + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1058
        BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1059
        memcpy(retval, argv0, size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1060
        retval[size] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1061
        return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1062
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1063
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1064
    /* argv0 wasn't helpful. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1065
    BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1066
} /* calculateBaseDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1067
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1068
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1069
static int initializeMutexes(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1070
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1071
    errorLock = __PHYSFS_platformCreateMutex();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1072
    if (errorLock == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1073
        goto initializeMutexes_failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1074
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1075
    stateLock = __PHYSFS_platformCreateMutex();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1076
    if (stateLock == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1077
        goto initializeMutexes_failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1078
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1079
    return 1;  /* success. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1080
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1081
initializeMutexes_failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1082
    if (errorLock != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1083
        __PHYSFS_platformDestroyMutex(errorLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1084
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1085
    if (stateLock != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1086
        __PHYSFS_platformDestroyMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1087
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1088
    errorLock = stateLock = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1089
    return 0;  /* failed. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1090
} /* initializeMutexes */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1091
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1092
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1093
static int doRegisterArchiver(const PHYSFS_Archiver *_archiver);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1094
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1095
static int initStaticArchivers(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1096
{
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1097
    #define REGISTER_STATIC_ARCHIVER(arc) { \
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1098
        extern const PHYSFS_Archiver __PHYSFS_Archiver_##arc; \
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1099
        if (!doRegisterArchiver(&__PHYSFS_Archiver_##arc)) { \
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1100
            return 0; \
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1101
        } \
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1102
    }
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1103
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1104
    #if PHYSFS_SUPPORTS_ZIP
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1105
        REGISTER_STATIC_ARCHIVER(ZIP);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1106
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1107
    #if PHYSFS_SUPPORTS_7Z
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1108
        REGISTER_STATIC_ARCHIVER(LZMA);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1109
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1110
    #if PHYSFS_SUPPORTS_GRP
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1111
        REGISTER_STATIC_ARCHIVER(GRP);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1112
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1113
    #if PHYSFS_SUPPORTS_QPAK
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1114
        REGISTER_STATIC_ARCHIVER(QPAK);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1115
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1116
    #if PHYSFS_SUPPORTS_HOG
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1117
        REGISTER_STATIC_ARCHIVER(HOG);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1118
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1119
    #if PHYSFS_SUPPORTS_MVL
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1120
        REGISTER_STATIC_ARCHIVER(MVL);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1121
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1122
    #if PHYSFS_SUPPORTS_WAD
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1123
        REGISTER_STATIC_ARCHIVER(WAD);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1124
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1125
    #if PHYSFS_SUPPORTS_SLB
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1126
        REGISTER_STATIC_ARCHIVER(SLB);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1127
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1128
    #if PHYSFS_SUPPORTS_ISO9660
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1129
        REGISTER_STATIC_ARCHIVER(ISO9660);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1130
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1131
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1132
    #undef REGISTER_STATIC_ARCHIVER
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1133
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1134
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1135
} /* initStaticArchivers */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1136
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1137
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1138
static void setDefaultAllocator(void);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1139
static int doDeinit(void);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1140
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1141
int PHYSFS_init(const char *argv0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1142
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1143
    BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1144
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1145
    if (!externalAllocator)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1146
        setDefaultAllocator();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1147
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1148
    if ((allocator.Init != NULL) && (!allocator.Init())) return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1149
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1150
    if (!__PHYSFS_platformInit())
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1151
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1152
        if (allocator.Deinit != NULL) allocator.Deinit();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1153
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1154
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1155
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1156
    /* everything below here can be cleaned up safely by doDeinit(). */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1157
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1158
    if (!initializeMutexes()) goto initFailed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1159
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1160
    baseDir = calculateBaseDir(argv0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1161
    if (!baseDir) goto initFailed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1162
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1163
    userDir = __PHYSFS_platformCalcUserDir();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1164
    if (!userDir) goto initFailed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1165
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1166
    /* Platform layer is required to append a dirsep. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1167
    assert(baseDir[strlen(baseDir) - 1] == __PHYSFS_platformDirSeparator);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1168
    assert(userDir[strlen(userDir) - 1] == __PHYSFS_platformDirSeparator);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1169
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1170
    if (!initStaticArchivers()) goto initFailed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1171
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1172
    initialized = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1173
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1174
    /* This makes sure that the error subsystem is initialized. */
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1175
    PHYSFS_setErrorCode(PHYSFS_getLastErrorCode());
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1176
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1177
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1178
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1179
initFailed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1180
    doDeinit();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1181
    return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1182
} /* PHYSFS_init */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1183
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1184
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1185
/* MAKE SURE you hold stateLock before calling this! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1186
static int closeFileHandleList(FileHandle **list)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1187
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1188
    FileHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1189
    FileHandle *next = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1190
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1191
    for (i = *list; i != NULL; i = next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1192
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1193
        PHYSFS_Io *io = i->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1194
        next = i->next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1195
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1196
        if (io->flush && !io->flush(io))
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1197
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1198
            *list = i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1199
            return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1200
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1201
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1202
        io->destroy(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1203
        allocator.Free(i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1204
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1205
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1206
    *list = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1207
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1208
} /* closeFileHandleList */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1209
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1210
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1211
/* MAKE SURE you hold the stateLock before calling this! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1212
static void freeSearchPath(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1213
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1214
    DirHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1215
    DirHandle *next = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1216
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1217
    closeFileHandleList(&openReadList);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1218
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1219
    if (searchPath != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1220
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1221
        for (i = searchPath; i != NULL; i = next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1222
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1223
            next = i->next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1224
            freeDirHandle(i, openReadList);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1225
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1226
        searchPath = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1227
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1228
} /* freeSearchPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1229
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1230
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1231
/* MAKE SURE you hold stateLock before calling this! */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1232
static int archiverInUse(const PHYSFS_Archiver *arc, const DirHandle *list)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1233
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1234
    const DirHandle *i;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1235
    for (i = list; i != NULL; i = i->next)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1236
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1237
        if (i->funcs == arc)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1238
            return 1;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1239
    } /* for */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1240
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1241
    return 0;  /* not in use */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1242
} /* archiverInUse */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1243
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1244
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1245
/* MAKE SURE you hold stateLock before calling this! */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1246
static int doDeregisterArchiver(const size_t idx)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1247
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1248
    const size_t len = (numArchivers - idx) * sizeof (void *);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1249
    const PHYSFS_ArchiveInfo *info = archiveInfo[idx];
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1250
    const PHYSFS_Archiver *arc = archivers[idx];
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1251
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1252
    /* make sure nothing is still using this archiver */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1253
    if (archiverInUse(arc, searchPath) || archiverInUse(arc, writeDir))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1254
        BAIL_MACRO(PHYSFS_ERR_FILES_STILL_OPEN, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1255
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1256
    allocator.Free((void *) info->extension);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1257
    allocator.Free((void *) info->description);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1258
    allocator.Free((void *) info->author);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1259
    allocator.Free((void *) info->url);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1260
    allocator.Free((void *) arc);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1261
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1262
    memmove(&archiveInfo[idx], &archiveInfo[idx+1], len);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1263
    memmove(&archivers[idx], &archivers[idx+1], len);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1264
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1265
    assert(numArchivers > 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1266
    numArchivers--;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1267
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1268
    return 1;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1269
} /* doDeregisterArchiver */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1270
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1271
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1272
/* Does NOT hold the state lock; we're shutting down. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1273
static void freeArchivers(void)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1274
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1275
    while (numArchivers > 0)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1276
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1277
        if (!doDeregisterArchiver(numArchivers - 1))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1278
            assert(!"nothing should be mounted during shutdown.");
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1279
    } /* while */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1280
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1281
    allocator.Free(archivers);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1282
    allocator.Free(archiveInfo);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1283
    archivers = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1284
    archiveInfo = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1285
} /* freeArchivers */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1286
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1287
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1288
static int doDeinit(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1289
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1290
    closeFileHandleList(&openWriteList);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1291
    BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), PHYSFS_ERR_FILES_STILL_OPEN, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1292
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1293
    freeSearchPath();
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1294
    freeArchivers();
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1295
    freeErrorStates();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1296
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1297
    if (baseDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1298
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1299
        allocator.Free(baseDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1300
        baseDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1301
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1302
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1303
    if (userDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1304
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1305
        allocator.Free(userDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1306
        userDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1307
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1308
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1309
    if (prefDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1310
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1311
        allocator.Free(prefDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1312
        prefDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1313
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1314
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1315
    if (archiveInfo != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1316
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1317
        allocator.Free(archiveInfo);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1318
        archiveInfo = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1319
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1320
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1321
    if (archivers != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1322
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1323
        allocator.Free(archivers);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1324
        archivers = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1325
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1326
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1327
    allowSymLinks = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1328
    initialized = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1329
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1330
    if (errorLock) __PHYSFS_platformDestroyMutex(errorLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1331
    if (stateLock) __PHYSFS_platformDestroyMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1332
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1333
    if (allocator.Deinit != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1334
        allocator.Deinit();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1335
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1336
    errorLock = stateLock = NULL;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1337
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1338
    /* !!! FIXME: what on earth are you supposed to do if this fails? */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1339
    BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), ERRPASS, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1340
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1341
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1342
} /* doDeinit */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1343
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1344
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1345
int PHYSFS_deinit(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1346
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1347
    BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1348
    return doDeinit();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1349
} /* PHYSFS_deinit */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1350
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1351
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1352
int PHYSFS_isInit(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1353
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1354
    return initialized;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1355
} /* PHYSFS_isInit */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1356
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1357
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1358
char *__PHYSFS_strdup(const char *str)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1359
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1360
    char *retval = (char *) allocator.Malloc(strlen(str) + 1);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1361
    if (retval)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1362
        strcpy(retval, str);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1363
    return retval;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1364
} /* __PHYSFS_strdup */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1365
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1366
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1367
PHYSFS_uint32 __PHYSFS_hashString(const char *str, size_t len)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1368
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1369
    PHYSFS_uint32 hash = 5381;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1370
    while (len--)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1371
        hash = ((hash << 5) + hash) ^ *(str++);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1372
    return hash;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1373
} /* __PHYSFS_hashString */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1374
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1375
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1376
/* MAKE SURE you hold stateLock before calling this! */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1377
static int doRegisterArchiver(const PHYSFS_Archiver *_archiver)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1378
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1379
    const PHYSFS_uint32 maxver = CURRENT_PHYSFS_ARCHIVER_API_VERSION;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1380
    const size_t len = (numArchivers + 2) * sizeof (void *);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1381
    PHYSFS_Archiver *archiver = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1382
    PHYSFS_ArchiveInfo *info = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1383
    const char *ext = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1384
    void *ptr = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1385
    size_t i;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1386
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1387
    BAIL_IF_MACRO(!_archiver, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1388
    BAIL_IF_MACRO(_archiver->version > maxver, PHYSFS_ERR_UNSUPPORTED, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1389
    BAIL_IF_MACRO(!_archiver->info.extension, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1390
    BAIL_IF_MACRO(!_archiver->info.description, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1391
    BAIL_IF_MACRO(!_archiver->info.author, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1392
    BAIL_IF_MACRO(!_archiver->info.url, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1393
    BAIL_IF_MACRO(!_archiver->openArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1394
    BAIL_IF_MACRO(!_archiver->enumerateFiles, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1395
    BAIL_IF_MACRO(!_archiver->openRead, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1396
    BAIL_IF_MACRO(!_archiver->openWrite, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1397
    BAIL_IF_MACRO(!_archiver->openAppend, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1398
    BAIL_IF_MACRO(!_archiver->remove, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1399
    BAIL_IF_MACRO(!_archiver->mkdir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1400
    BAIL_IF_MACRO(!_archiver->closeArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1401
    BAIL_IF_MACRO(!_archiver->stat, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1402
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1403
    ext = _archiver->info.extension;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1404
    for (i = 0; i < numArchivers; i++)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1405
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1406
        if (__PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1407
            BAIL_MACRO(PHYSFS_ERR_DUPLICATE, 0);  /* !!! FIXME: better error? ERR_IN_USE? */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1408
    } /* for */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1409
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1410
    /* make a copy of the data. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1411
    archiver = (PHYSFS_Archiver *) allocator.Malloc(sizeof (*archiver));
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1412
    GOTO_IF_MACRO(!archiver, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1413
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1414
    /* Must copy sizeof (OLD_VERSION_OF_STRUCT) when version changes! */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1415
    memcpy(archiver, _archiver, sizeof (*archiver));
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1416
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1417
    info = (PHYSFS_ArchiveInfo *) &archiver->info;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1418
    memset(info, '\0', sizeof (*info));  /* NULL in case an alloc fails. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1419
    #define CPYSTR(item) \
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1420
        info->item = __PHYSFS_strdup(_archiver->info.item); \
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1421
        GOTO_IF_MACRO(!info->item, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1422
    CPYSTR(extension);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1423
    CPYSTR(description);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1424
    CPYSTR(author);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1425
    CPYSTR(url);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1426
    info->supportsSymlinks = _archiver->info.supportsSymlinks;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1427
    #undef CPYSTR
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1428
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1429
    ptr = allocator.Realloc(archiveInfo, len);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1430
    GOTO_IF_MACRO(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1431
    archiveInfo = (const PHYSFS_ArchiveInfo **) ptr;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1432
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1433
    ptr = allocator.Realloc(archivers, len);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1434
    GOTO_IF_MACRO(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1435
    archivers = (const PHYSFS_Archiver **) ptr;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1436
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1437
    archiveInfo[numArchivers] = info;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1438
    archiveInfo[numArchivers + 1] = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1439
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1440
    archivers[numArchivers] = archiver;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1441
    archivers[numArchivers + 1] = NULL;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1442
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1443
    numArchivers++;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1444
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1445
    return 1;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1446
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1447
regfailed:
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1448
    if (info != NULL)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1449
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1450
        allocator.Free((void *) info->extension);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1451
        allocator.Free((void *) info->description);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1452
        allocator.Free((void *) info->author);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1453
        allocator.Free((void *) info->url);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1454
    } /* if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1455
    allocator.Free(archiver);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1456
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1457
    return 0;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1458
} /* doRegisterArchiver */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1459
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1460
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1461
int PHYSFS_registerArchiver(const PHYSFS_Archiver *archiver)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1462
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1463
    int retval;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1464
    BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1465
    __PHYSFS_platformGrabMutex(stateLock);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1466
    retval = doRegisterArchiver(archiver);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1467
    __PHYSFS_platformReleaseMutex(stateLock);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1468
    return retval;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1469
} /* PHYSFS_registerArchiver */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1470
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1471
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1472
int PHYSFS_deregisterArchiver(const char *ext)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1473
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1474
    size_t i;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1475
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1476
    BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1477
    BAIL_IF_MACRO(!ext, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1478
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1479
    __PHYSFS_platformGrabMutex(stateLock);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1480
    for (i = 0; i < numArchivers; i++)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1481
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1482
        if (__PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1483
        {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1484
            const int retval = doDeregisterArchiver(i);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1485
            __PHYSFS_platformReleaseMutex(stateLock);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1486
            return retval;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1487
        } /* if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1488
    } /* for */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1489
    __PHYSFS_platformReleaseMutex(stateLock);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1490
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1491
    BAIL_MACRO(PHYSFS_ERR_NOT_FOUND, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1492
} /* PHYSFS_deregisterArchiver */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1493
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1494
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1495
const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1496
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1497
    BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1498
    return archiveInfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1499
} /* PHYSFS_supportedArchiveTypes */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1500
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1501
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1502
void PHYSFS_freeList(void *list)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1503
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1504
    void **i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1505
    if (list != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1506
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1507
        for (i = (void **) list; *i != NULL; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1508
            allocator.Free(*i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1509
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1510
        allocator.Free(list);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1511
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1512
} /* PHYSFS_freeList */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1513
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1514
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1515
const char *PHYSFS_getDirSeparator(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1516
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1517
    static char retval[2] = { __PHYSFS_platformDirSeparator, '\0' };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1518
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1519
} /* PHYSFS_getDirSeparator */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1520
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1521
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1522
char **PHYSFS_getCdRomDirs(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1523
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1524
    return doEnumStringList(__PHYSFS_platformDetectAvailableCDs);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1525
} /* PHYSFS_getCdRomDirs */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1526
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1527
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1528
void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1529
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1530
    __PHYSFS_platformDetectAvailableCDs(callback, data);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1531
} /* PHYSFS_getCdRomDirsCallback */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1532
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1533
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1534
const char *PHYSFS_getPrefDir(const char *org, const char *app)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1535
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1536
    const char dirsep = __PHYSFS_platformDirSeparator;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1537
    PHYSFS_Stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1538
    char *ptr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1539
    char *endstr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1540
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1541
    BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1542
    BAIL_IF_MACRO(!org, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1543
    BAIL_IF_MACRO(*org == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1544
    BAIL_IF_MACRO(!app, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1545
    BAIL_IF_MACRO(*app == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1546
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1547
    allocator.Free(prefDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1548
    prefDir = __PHYSFS_platformCalcPrefDir(org, app);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1549
    BAIL_IF_MACRO(!prefDir, ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1550
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1551
    assert(strlen(prefDir) > 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1552
    endstr = prefDir + (strlen(prefDir) - 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1553
    assert(*endstr == dirsep);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1554
    *endstr = '\0';  /* mask out the final dirsep for now. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1555
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1556
    if (!__PHYSFS_platformStat(prefDir, &statbuf))
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1557
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1558
        for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1559
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1560
            *ptr = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1561
            __PHYSFS_platformMkDir(prefDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1562
            *ptr = dirsep;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1563
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1564
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1565
        if (!__PHYSFS_platformMkDir(prefDir))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1566
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1567
            allocator.Free(prefDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1568
            prefDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1569
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1570
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1571
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1572
    *endstr = dirsep;  /* readd the final dirsep. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1573
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1574
    return prefDir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1575
} /* PHYSFS_getPrefDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1576
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1577
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1578
const char *PHYSFS_getBaseDir(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1579
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1580
    return baseDir;   /* this is calculated in PHYSFS_init()... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1581
} /* PHYSFS_getBaseDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1582
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1583
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1584
const char *__PHYSFS_getUserDir(void)  /* not deprecated internal version. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1585
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1586
    return userDir;   /* this is calculated in PHYSFS_init()... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1587
} /* __PHYSFS_getUserDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1588
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1589
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1590
const char *PHYSFS_getUserDir(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1591
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1592
    return __PHYSFS_getUserDir();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1593
} /* PHYSFS_getUserDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1594
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1595
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1596
const char *PHYSFS_getWriteDir(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1597
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1598
    const char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1599
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1600
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1601
    if (writeDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1602
        retval = writeDir->dirName;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1603
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1604
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1605
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1606
} /* PHYSFS_getWriteDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1607
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1608
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1609
int PHYSFS_setWriteDir(const char *newDir)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1610
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1611
    int retval = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1612
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1613
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1614
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1615
    if (writeDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1616
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1617
        BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), ERRPASS,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1618
                            stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1619
        writeDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1620
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1621
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1622
    if (newDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1623
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1624
        /* !!! FIXME: PHYSFS_Io shouldn't be NULL */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1625
        writeDir = createDirHandle(NULL, newDir, NULL, 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1626
        retval = (writeDir != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1627
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1628
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1629
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1630
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1631
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1632
} /* PHYSFS_setWriteDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1633
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1634
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1635
static int doMount(PHYSFS_Io *io, const char *fname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1636
                   const char *mountPoint, int appendToPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1637
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1638
    DirHandle *dh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1639
    DirHandle *prev = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1640
    DirHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1641
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1642
    if (mountPoint == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1643
        mountPoint = "/";
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1644
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1645
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1646
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1647
    if (fname != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1648
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1649
        for (i = searchPath; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1650
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1651
            /* already in search path? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1652
            if ((i->dirName != NULL) && (strcmp(fname, i->dirName) == 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1653
                BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1654
            prev = i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1655
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1656
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1657
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1658
    dh = createDirHandle(io, fname, mountPoint, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1659
    BAIL_IF_MACRO_MUTEX(!dh, ERRPASS, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1660
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1661
    if (appendToPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1662
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1663
        if (prev == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1664
            searchPath = dh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1665
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1666
            prev->next = dh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1667
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1668
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1669
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1670
        dh->next = searchPath;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1671
        searchPath = dh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1672
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1673
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1674
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1675
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1676
} /* doMount */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1677
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1678
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1679
int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1680
                   const char *mountPoint, int appendToPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1681
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1682
    BAIL_IF_MACRO(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1683
    BAIL_IF_MACRO(io->version != 0, PHYSFS_ERR_UNSUPPORTED, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1684
    return doMount(io, fname, mountPoint, appendToPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1685
} /* PHYSFS_mountIo */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1686
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1687
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1688
int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1689
                       const char *fname, const char *mountPoint,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1690
                       int appendToPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1691
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1692
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1693
    PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1694
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1695
    BAIL_IF_MACRO(!buf, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1696
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1697
    io = __PHYSFS_createMemoryIo(buf, len, del);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1698
    BAIL_IF_MACRO(!io, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1699
    retval = doMount(io, fname, mountPoint, appendToPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1700
    if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1701
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1702
        /* docs say not to call (del) in case of failure, so cheat. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1703
        MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1704
        info->destruct = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1705
        io->destroy(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1706
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1707
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1708
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1709
} /* PHYSFS_mountMemory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1710
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1711
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1712
int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1713
                       const char *mountPoint, int appendToPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1714
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1715
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1716
    PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1717
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1718
    BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1719
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1720
    io = __PHYSFS_createHandleIo(file);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1721
    BAIL_IF_MACRO(!io, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1722
    retval = doMount(io, fname, mountPoint, appendToPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1723
    if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1724
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1725
        /* docs say not to destruct in case of failure, so cheat. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1726
        io->opaque = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1727
        io->destroy(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1728
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1729
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1730
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1731
} /* PHYSFS_mountHandle */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1732
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1733
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1734
int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1735
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1736
    BAIL_IF_MACRO(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1737
    return doMount(NULL, newDir, mountPoint, appendToPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1738
} /* PHYSFS_mount */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1739
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1740
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1741
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1742
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1743
    return doMount(NULL, newDir, NULL, appendToPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1744
} /* PHYSFS_addToSearchPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1745
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1746
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1747
int PHYSFS_removeFromSearchPath(const char *oldDir)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1748
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1749
    return PHYSFS_unmount(oldDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1750
} /* PHYSFS_removeFromSearchPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1751
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1752
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1753
int PHYSFS_unmount(const char *oldDir)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1754
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1755
    DirHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1756
    DirHandle *prev = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1757
    DirHandle *next = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1758
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1759
    BAIL_IF_MACRO(oldDir == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1760
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1761
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1762
    for (i = searchPath; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1763
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1764
        if (strcmp(i->dirName, oldDir) == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1765
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1766
            next = i->next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1767
            BAIL_IF_MACRO_MUTEX(!freeDirHandle(i, openReadList), ERRPASS,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1768
                                stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1769
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1770
            if (prev == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1771
                searchPath = next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1772
            else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1773
                prev->next = next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1774
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1775
            BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1776
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1777
        prev = i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1778
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1779
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1780
    BAIL_MACRO_MUTEX(PHYSFS_ERR_NOT_MOUNTED, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1781
} /* PHYSFS_unmount */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1782
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1783
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1784
char **PHYSFS_getSearchPath(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1785
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1786
    return doEnumStringList(PHYSFS_getSearchPathCallback);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1787
} /* PHYSFS_getSearchPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1788
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1789
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1790
const char *PHYSFS_getMountPoint(const char *dir)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1791
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1792
    DirHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1793
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1794
    for (i = searchPath; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1795
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1796
        if (strcmp(i->dirName, dir) == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1797
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1798
            const char *retval = ((i->mountPoint) ? i->mountPoint : "/");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1799
            __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1800
            return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1801
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1802
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1803
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1804
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1805
    BAIL_MACRO(PHYSFS_ERR_NOT_MOUNTED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1806
} /* PHYSFS_getMountPoint */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1807
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1808
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1809
void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1810
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1811
    DirHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1812
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1813
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1814
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1815
    for (i = searchPath; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1816
        callback(data, i->dirName);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1817
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1818
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1819
} /* PHYSFS_getSearchPathCallback */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1820
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1821
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1822
/* Split out to avoid stack allocation in a loop. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1823
static void setSaneCfgAddPath(const char *i, const size_t l, const char *dirsep,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1824
                              int archivesFirst)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1825
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1826
    const char *d = PHYSFS_getRealDir(i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1827
    const size_t allocsize = strlen(d) + strlen(dirsep) + l + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1828
    char *str = (char *) __PHYSFS_smallAlloc(allocsize);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1829
    if (str != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1830
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1831
        sprintf(str, "%s%s%s", d, dirsep, i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1832
        PHYSFS_mount(str, NULL, archivesFirst == 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1833
        __PHYSFS_smallFree(str);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1834
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1835
} /* setSaneCfgAddPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1836
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1837
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1838
int PHYSFS_setSaneConfig(const char *organization, const char *appName,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1839
                         const char *archiveExt, int includeCdRoms,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1840
                         int archivesFirst)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1841
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1842
    const char *dirsep = PHYSFS_getDirSeparator();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1843
    const char *basedir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1844
    const char *prefdir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1845
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1846
    BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1847
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1848
    prefdir = PHYSFS_getPrefDir(organization, appName);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1849
    BAIL_IF_MACRO(!prefdir, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1850
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1851
    basedir = PHYSFS_getBaseDir();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1852
    BAIL_IF_MACRO(!basedir, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1853
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1854
    BAIL_IF_MACRO(!PHYSFS_setWriteDir(prefdir), PHYSFS_ERR_NO_WRITE_DIR, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1855
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1856
    /* Put write dir first in search path... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1857
    PHYSFS_mount(prefdir, NULL, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1858
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1859
    /* Put base path on search path... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1860
    PHYSFS_mount(basedir, NULL, 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1861
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1862
    /* handle CD-ROMs... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1863
    if (includeCdRoms)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1864
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1865
        char **cds = PHYSFS_getCdRomDirs();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1866
        char **i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1867
        for (i = cds; *i != NULL; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1868
            PHYSFS_mount(*i, NULL, 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1869
        PHYSFS_freeList(cds);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1870
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1871
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1872
    /* Root out archives, and add them to search path... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1873
    if (archiveExt != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1874
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1875
        char **rc = PHYSFS_enumerateFiles("/");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1876
        char **i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1877
        size_t extlen = strlen(archiveExt);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1878
        char *ext;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1879
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1880
        for (i = rc; *i != NULL; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1881
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1882
            size_t l = strlen(*i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1883
            if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1884
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1885
                ext = (*i) + (l - extlen);
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1886
                if (__PHYSFS_utf8stricmp(ext, archiveExt) == 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1887
                    setSaneCfgAddPath(*i, l, dirsep, archivesFirst);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1888
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1889
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1890
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1891
        PHYSFS_freeList(rc);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1892
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1893
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1894
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1895
} /* PHYSFS_setSaneConfig */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1896
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1897
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1898
void PHYSFS_permitSymbolicLinks(int allow)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1899
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1900
    allowSymLinks = allow;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1901
} /* PHYSFS_permitSymbolicLinks */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1902
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1903
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1904
int PHYSFS_symbolicLinksPermitted(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1905
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1906
    return allowSymLinks;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1907
} /* PHYSFS_symbolicLinksPermitted */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1908
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1909
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1910
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1911
 * Verify that (fname) (in platform-independent notation), in relation
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1912
 *  to (h) is secure. That means that each element of fname is checked
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1913
 *  for symlinks (if they aren't permitted). This also allows for quick
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1914
 *  rejection of files that exist outside an archive's mountpoint.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1915
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1916
 * With some exceptions (like PHYSFS_mkdir(), which builds multiple subdirs
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1917
 *  at a time), you should always pass zero for "allowMissing" for efficiency.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1918
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1919
 * (fname) must point to an output from sanitizePlatformIndependentPath(),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1920
 *  since it will make sure that path names are in the right format for
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1921
 *  passing certain checks. It will also do checks for "insecure" pathnames
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1922
 *  like ".." which should be done once instead of once per archive. This also
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1923
 *  gives us license to treat (fname) as scratch space in this function.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1924
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1925
 * Returns non-zero if string is safe, zero if there's a security issue.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1926
 *  PHYSFS_getLastError() will specify what was wrong. (*fname) will be
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1927
 *  updated to point past any mount point elements so it is prepared to
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1928
 *  be used with the archiver directly.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1929
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1930
static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1931
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1932
    char *fname = *_fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1933
    int retval = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1934
    char *start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1935
    char *end;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1936
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1937
    if (*fname == '\0')  /* quick rejection. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1938
        return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1939
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1940
    /* !!! FIXME: This codeblock sucks. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1941
    if (h->mountPoint != NULL)  /* NULL mountpoint means "/". */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1942
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1943
        size_t mntpntlen = strlen(h->mountPoint);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1944
        size_t len = strlen(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1945
        assert(mntpntlen > 1); /* root mount points should be NULL. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1946
        /* not under the mountpoint, so skip this archive. */
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1947
        BAIL_IF_MACRO(len < mntpntlen-1, PHYSFS_ERR_NOT_FOUND, 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1948
        /* !!! FIXME: Case insensitive? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1949
        retval = strncmp(h->mountPoint, fname, mntpntlen-1);
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1950
        BAIL_IF_MACRO(retval != 0, PHYSFS_ERR_NOT_FOUND, 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1951
        if (len > mntpntlen-1)  /* corner case... */
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1952
            BAIL_IF_MACRO(fname[mntpntlen-1]!='/', PHYSFS_ERR_NOT_FOUND, 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1953
        fname += mntpntlen-1;  /* move to start of actual archive path. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1954
        if (*fname == '/')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1955
            fname++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1956
        *_fname = fname;  /* skip mountpoint for later use. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1957
        retval = 1;  /* may be reset, below. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1958
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1959
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1960
    start = fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1961
    if (!allowSymLinks)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1962
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1963
        while (1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1964
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1965
            PHYSFS_Stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1966
            int rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1967
            end = strchr(start, '/');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1968
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1969
            if (end != NULL) *end = '\0';
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1970
            rc = h->funcs->stat(h->opaque, fname, &statbuf);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1971
            if (rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1972
                rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1973
            else if (currentErrorCode() == PHYSFS_ERR_NOT_FOUND)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1974
                retval = 0;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  1975
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1976
            if (end != NULL) *end = '/';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1977
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1978
            /* insecure path (has a disallowed symlink in it)? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1979
            BAIL_IF_MACRO(rc, PHYSFS_ERR_SYMLINK_FORBIDDEN, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1980
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1981
            /* break out early if path element is missing. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1982
            if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1983
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1984
                /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1985
                 * We need to clear it if it's the last element of the path,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1986
                 *  since this might be a non-existant file we're opening
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1987
                 *  for writing...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1988
                 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1989
                if ((end == NULL) || (allowMissing))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1990
                    retval = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1991
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1992
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1993
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1994
            if (end == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1995
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1996
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1997
            start = end + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1998
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1999
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2000
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2001
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2002
} /* verifyPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2003
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2004
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2005
static int doMkdir(const char *_dname, char *dname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2006
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2007
    DirHandle *h;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2008
    char *start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2009
    char *end;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2010
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2011
    int exists = 1;  /* force existance check on first path element. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2012
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2013
    BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_dname, dname), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2014
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2015
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2016
    BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2017
    h = writeDir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2018
    BAIL_IF_MACRO_MUTEX(!verifyPath(h, &dname, 1), ERRPASS, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2019
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2020
    start = dname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2021
    while (1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2022
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2023
        end = strchr(start, '/');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2024
        if (end != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2025
            *end = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2026
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2027
        /* only check for existance if all parent dirs existed, too... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2028
        if (exists)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2029
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2030
            PHYSFS_Stat statbuf;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2031
            const int rc = h->funcs->stat(h->opaque, dname, &statbuf);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2032
            if ((!rc) && (currentErrorCode() == PHYSFS_ERR_NOT_FOUND))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2033
                exists = 0;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2034
            retval = ((rc) && (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2035
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2036
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2037
        if (!exists)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2038
            retval = h->funcs->mkdir(h->opaque, dname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2039
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2040
        if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2041
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2042
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2043
        if (end == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2044
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2045
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2046
        *end = '/';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2047
        start = end + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2048
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2049
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2050
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2051
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2052
} /* doMkdir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2053
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2054
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2055
int PHYSFS_mkdir(const char *_dname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2056
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2057
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2058
    char *dname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2059
    size_t len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2060
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2061
    BAIL_IF_MACRO(!_dname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2062
    len = strlen(_dname) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2063
    dname = (char *) __PHYSFS_smallAlloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2064
    BAIL_IF_MACRO(!dname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2065
    retval = doMkdir(_dname, dname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2066
    __PHYSFS_smallFree(dname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2067
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2068
} /* PHYSFS_mkdir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2069
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2070
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2071
static int doDelete(const char *_fname, char *fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2072
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2073
    int retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2074
    DirHandle *h;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2075
    BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2076
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2077
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2078
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2079
    BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2080
    h = writeDir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2081
    BAIL_IF_MACRO_MUTEX(!verifyPath(h, &fname, 0), ERRPASS, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2082
    retval = h->funcs->remove(h->opaque, fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2083
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2084
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2085
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2086
} /* doDelete */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2087
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2088
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2089
int PHYSFS_delete(const char *_fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2090
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2091
    int retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2092
    char *fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2093
    size_t len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2094
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2095
    BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2096
    len = strlen(_fname) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2097
    fname = (char *) __PHYSFS_smallAlloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2098
    BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2099
    retval = doDelete(_fname, fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2100
    __PHYSFS_smallFree(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2101
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2102
} /* PHYSFS_delete */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2103
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2104
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2105
const char *PHYSFS_getRealDir(const char *_fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2106
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2107
    const char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2108
    char *fname = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2109
    size_t len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2110
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2111
    BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2112
    len = strlen(_fname) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2113
    fname = __PHYSFS_smallAlloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2114
    BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2115
    if (sanitizePlatformIndependentPath(_fname, fname))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2116
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2117
        DirHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2118
        __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2119
        for (i = searchPath; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2120
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2121
            char *arcfname = fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2122
            if (partOfMountPoint(i, arcfname))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2123
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2124
                retval = i->dirName;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2125
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2126
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2127
            else if (verifyPath(i, &arcfname, 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2128
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2129
                PHYSFS_Stat statbuf;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2130
                if (i->funcs->stat(i->opaque, arcfname, &statbuf))
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2131
                {
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2132
                    retval = i->dirName;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2133
                    break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2134
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2135
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2136
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2137
        __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2138
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2139
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2140
    __PHYSFS_smallFree(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2141
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2142
} /* PHYSFS_getRealDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2143
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2144
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2145
static int locateInStringList(const char *str,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2146
                              char **list,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2147
                              PHYSFS_uint32 *pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2148
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2149
    PHYSFS_uint32 len = *pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2150
    PHYSFS_uint32 half_len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2151
    PHYSFS_uint32 lo = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2152
    PHYSFS_uint32 middle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2153
    int cmp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2154
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2155
    while (len > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2156
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2157
        half_len = len >> 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2158
        middle = lo + half_len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2159
        cmp = strcmp(list[middle], str);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2160
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2161
        if (cmp == 0)  /* it's in the list already. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2162
            return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2163
        else if (cmp > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2164
            len = half_len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2165
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2166
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2167
            lo = middle + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2168
            len -= half_len + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2169
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2170
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2171
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2172
    *pos = lo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2173
    return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2174
} /* locateInStringList */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2175
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2176
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2177
static void enumFilesCallback(void *data, const char *origdir, const char *str)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2178
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2179
    PHYSFS_uint32 pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2180
    void *ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2181
    char *newstr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2182
    EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2183
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2184
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2185
     * See if file is in the list already, and if not, insert it in there
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2186
     *  alphabetically...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2187
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2188
    pos = pecd->size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2189
    if (locateInStringList(str, pecd->list, &pos))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2190
        return;  /* already in the list. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2191
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2192
    ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2193
    newstr = (char *) allocator.Malloc(strlen(str) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2194
    if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2195
        pecd->list = (char **) ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2196
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2197
    if ((ptr == NULL) || (newstr == NULL))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2198
        return;  /* better luck next time. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2199
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2200
    strcpy(newstr, str);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2201
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2202
    if (pos != pecd->size)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2203
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2204
        memmove(&pecd->list[pos+1], &pecd->list[pos],
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2205
                 sizeof (char *) * ((pecd->size) - pos));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2206
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2207
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2208
    pecd->list[pos] = newstr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2209
    pecd->size++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2210
} /* enumFilesCallback */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2211
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2212
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2213
char **PHYSFS_enumerateFiles(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2214
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2215
    EnumStringListCallbackData ecd;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2216
    memset(&ecd, '\0', sizeof (ecd));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2217
    ecd.list = (char **) allocator.Malloc(sizeof (char *));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2218
    BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2219
    PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2220
    ecd.list[ecd.size] = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2221
    return ecd.list;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2222
} /* PHYSFS_enumerateFiles */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2223
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2224
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2225
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2226
 * Broke out to seperate function so we can use stack allocation gratuitously.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2227
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2228
static void enumerateFromMountPoint(DirHandle *i, const char *arcfname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2229
                                    PHYSFS_EnumFilesCallback callback,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2230
                                    const char *_fname, void *data)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2231
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2232
    const size_t len = strlen(arcfname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2233
    char *ptr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2234
    char *end = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2235
    const size_t slen = strlen(i->mountPoint) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2236
    char *mountPoint = (char *) __PHYSFS_smallAlloc(slen);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2237
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2238
    if (mountPoint == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2239
        return;  /* oh well. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2240
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2241
    strcpy(mountPoint, i->mountPoint);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2242
    ptr = mountPoint + ((len) ? len + 1 : 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2243
    end = strchr(ptr, '/');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2244
    assert(end);  /* should always find a terminating '/'. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2245
    *end = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2246
    callback(data, _fname, ptr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2247
    __PHYSFS_smallFree(mountPoint);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2248
} /* enumerateFromMountPoint */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2249
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2250
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2251
typedef struct SymlinkFilterData
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2252
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2253
    PHYSFS_EnumFilesCallback callback;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2254
    void *callbackData;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2255
    DirHandle *dirhandle;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2256
} SymlinkFilterData;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2257
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2258
/* !!! FIXME: broken if in a virtual mountpoint (stat call fails). */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2259
static void enumCallbackFilterSymLinks(void *_data, const char *origdir,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2260
                                       const char *fname)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2261
{
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2262
    const char *trimmedDir = (*origdir == '/') ? (origdir+1) : origdir;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2263
    const size_t slen = strlen(trimmedDir) + strlen(fname) + 2;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2264
    char *path = (char *) __PHYSFS_smallAlloc(slen);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2265
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2266
    if (path != NULL)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2267
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2268
        SymlinkFilterData *data = (SymlinkFilterData *) _data;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2269
        const DirHandle *dh = data->dirhandle;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2270
        PHYSFS_Stat statbuf;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2271
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2272
        sprintf(path, "%s%s%s", trimmedDir, *trimmedDir ? "/" : "", fname);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2273
        if (dh->funcs->stat(dh->opaque, path, &statbuf))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2274
        {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2275
            /* Pass it on to the application if it's not a symlink. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2276
            if (statbuf.filetype != PHYSFS_FILETYPE_SYMLINK)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2277
                data->callback(data->callbackData, origdir, fname);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2278
        } /* if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2279
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2280
        __PHYSFS_smallFree(path);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2281
    } /* if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2282
} /* enumCallbackFilterSymLinks */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2283
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2284
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2285
/* !!! FIXME: this should report error conditions. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2286
void PHYSFS_enumerateFilesCallback(const char *_fname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2287
                                   PHYSFS_EnumFilesCallback callback,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2288
                                   void *data)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2289
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2290
    size_t len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2291
    char *fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2292
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2293
    BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2294
    BAIL_IF_MACRO(!callback, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2295
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2296
    len = strlen(_fname) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2297
    fname = (char *) __PHYSFS_smallAlloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2298
    BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, ) /*0*/;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2299
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2300
    if (sanitizePlatformIndependentPath(_fname, fname))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2301
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2302
        DirHandle *i;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2303
        SymlinkFilterData filterdata;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2304
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2305
        __PHYSFS_platformGrabMutex(stateLock);
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2306
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2307
        if (!allowSymLinks)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2308
        {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2309
            memset(&filterdata, '\0', sizeof (filterdata));
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2310
            filterdata.callback = callback;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2311
            filterdata.callbackData = data;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2312
        } /* if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2313
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2314
        for (i = searchPath; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2315
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2316
            char *arcfname = fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2317
            if (partOfMountPoint(i, arcfname))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2318
                enumerateFromMountPoint(i, arcfname, callback, _fname, data);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2319
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2320
            else if (verifyPath(i, &arcfname, 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2321
            {
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2322
                if ((!allowSymLinks) && (i->funcs->info.supportsSymlinks))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2323
                {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2324
                    filterdata.dirhandle = i;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2325
                    i->funcs->enumerateFiles(i->opaque, arcfname,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2326
                                             enumCallbackFilterSymLinks,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2327
                                             _fname, &filterdata);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2328
                } /* if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2329
                else
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2330
                {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2331
                    i->funcs->enumerateFiles(i->opaque, arcfname,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2332
                                             callback, _fname, data);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2333
                } /* else */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2334
            } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2335
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2336
        __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2337
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2338
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2339
    __PHYSFS_smallFree(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2340
} /* PHYSFS_enumerateFilesCallback */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2341
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2342
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2343
int PHYSFS_exists(const char *fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2344
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2345
    return (PHYSFS_getRealDir(fname) != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2346
} /* PHYSFS_exists */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2347
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2348
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2349
PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2350
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2351
    PHYSFS_Stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2352
    BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2353
    return statbuf.modtime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2354
} /* PHYSFS_getLastModTime */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2355
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2356
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2357
int PHYSFS_isDirectory(const char *fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2358
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2359
    PHYSFS_Stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2360
    BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2361
    return (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2362
} /* PHYSFS_isDirectory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2363
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2364
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2365
int PHYSFS_isSymbolicLink(const char *fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2366
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2367
    PHYSFS_Stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2368
    BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2369
    return (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2370
} /* PHYSFS_isSymbolicLink */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2371
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2372
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2373
static PHYSFS_File *doOpenWrite(const char *_fname, int appending)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2374
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2375
    FileHandle *fh = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2376
    size_t len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2377
    char *fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2378
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2379
    BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2380
    len = strlen(_fname) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2381
    fname = (char *) __PHYSFS_smallAlloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2382
    BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2383
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2384
    if (sanitizePlatformIndependentPath(_fname, fname))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2385
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2386
        PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2387
        DirHandle *h = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2388
        const PHYSFS_Archiver *f;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2389
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2390
        __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2391
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2392
        GOTO_IF_MACRO(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, doOpenWriteEnd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2393
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2394
        h = writeDir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2395
        GOTO_IF_MACRO(!verifyPath(h, &fname, 0), ERRPASS, doOpenWriteEnd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2396
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2397
        f = h->funcs;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2398
        if (appending)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2399
            io = f->openAppend(h->opaque, fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2400
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2401
            io = f->openWrite(h->opaque, fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2402
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2403
        GOTO_IF_MACRO(!io, ERRPASS, doOpenWriteEnd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2404
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2405
        fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2406
        if (fh == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2407
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2408
            io->destroy(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2409
            GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, doOpenWriteEnd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2410
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2411
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2412
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2413
            memset(fh, '\0', sizeof (FileHandle));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2414
            fh->io = io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2415
            fh->dirHandle = h;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2416
            fh->next = openWriteList;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2417
            openWriteList = fh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2418
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2419
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2420
        doOpenWriteEnd:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2421
        __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2422
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2423
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2424
    __PHYSFS_smallFree(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2425
    return ((PHYSFS_File *) fh);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2426
} /* doOpenWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2427
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2428
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2429
PHYSFS_File *PHYSFS_openWrite(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2430
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2431
    return doOpenWrite(filename, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2432
} /* PHYSFS_openWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2433
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2434
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2435
PHYSFS_File *PHYSFS_openAppend(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2436
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2437
    return doOpenWrite(filename, 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2438
} /* PHYSFS_openAppend */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2439
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2440
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2441
PHYSFS_File *PHYSFS_openRead(const char *_fname)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2442
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2443
    FileHandle *fh = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2444
    char *fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2445
    size_t len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2446
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2447
    BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2448
    len = strlen(_fname) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2449
    fname = (char *) __PHYSFS_smallAlloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2450
    BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2451
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2452
    if (sanitizePlatformIndependentPath(_fname, fname))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2453
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2454
        DirHandle *i = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2455
        PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2456
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2457
        __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2458
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2459
        GOTO_IF_MACRO(!searchPath, PHYSFS_ERR_NOT_FOUND, openReadEnd);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2460
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2461
        for (i = searchPath; i != NULL; i = i->next)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2462
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2463
            char *arcfname = fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2464
            if (verifyPath(i, &arcfname, 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2465
            {
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2466
                io = i->funcs->openRead(i->opaque, arcfname);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2467
                if (io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2468
                    break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2469
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2470
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2471
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2472
        GOTO_IF_MACRO(!io, ERRPASS, openReadEnd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2473
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2474
        fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2475
        if (fh == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2476
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2477
            io->destroy(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2478
            GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, openReadEnd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2479
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2480
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2481
        memset(fh, '\0', sizeof (FileHandle));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2482
        fh->io = io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2483
        fh->forReading = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2484
        fh->dirHandle = i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2485
        fh->next = openReadList;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2486
        openReadList = fh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2487
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2488
        openReadEnd:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2489
        __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2490
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2491
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2492
    __PHYSFS_smallFree(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2493
    return ((PHYSFS_File *) fh);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2494
} /* PHYSFS_openRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2495
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2496
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2497
static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2498
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2499
    FileHandle *prev = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2500
    FileHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2501
    int rc = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2502
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2503
    for (i = *list; i != NULL; i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2504
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2505
        if (i == handle)  /* handle is in this list? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2506
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2507
            PHYSFS_Io *io = handle->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2508
            PHYSFS_uint8 *tmp = handle->buffer;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2509
            rc = PHYSFS_flush((PHYSFS_File *) handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2510
            if (!rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2511
                return -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2512
            io->destroy(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2513
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2514
            if (tmp != NULL)  /* free any associated buffer. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2515
                allocator.Free(tmp);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2516
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2517
            if (prev == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2518
                *list = handle->next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2519
            else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2520
                prev->next = handle->next;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2521
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2522
            allocator.Free(handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2523
            return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2524
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2525
        prev = i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2526
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2527
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2528
    return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2529
} /* closeHandleInOpenList */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2530
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2531
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2532
int PHYSFS_close(PHYSFS_File *_handle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2533
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2534
    FileHandle *handle = (FileHandle *) _handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2535
    int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2536
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2537
    __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2538
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2539
    /* -1 == close failure. 0 == not found. 1 == success. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2540
    rc = closeHandleInOpenList(&openReadList, handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2541
    BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2542
    if (!rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2543
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2544
        rc = closeHandleInOpenList(&openWriteList, handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2545
        BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2546
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2547
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2548
    __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2549
    BAIL_IF_MACRO(!rc, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2550
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2551
} /* PHYSFS_close */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2552
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2553
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2554
static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2555
                                    PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2556
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2557
    PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2558
    PHYSFS_sint64 retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2559
    PHYSFS_uint32 buffered = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2560
    PHYSFS_sint64 rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2561
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2562
    if (len == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2563
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2564
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2565
    buffered = fh->buffill - fh->bufpos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2566
    if (buffered >= len)  /* totally in the buffer, just copy and return! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2567
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2568
        memcpy(buffer, fh->buffer + fh->bufpos, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2569
        fh->bufpos += (PHYSFS_uint32) len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2570
        return (PHYSFS_sint64) len;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2571
    } /* if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2572
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2573
    else if (buffered > 0) /* partially in the buffer... */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2574
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2575
        memcpy(buffer, fh->buffer + fh->bufpos, (size_t) buffered);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2576
        buffer = ((PHYSFS_uint8 *) buffer) + buffered;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2577
        len -= buffered;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2578
        retval = buffered;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2579
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2580
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2581
    /* if you got here, the buffer is drained and we still need bytes. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2582
    assert(len > 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2583
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2584
    fh->buffill = fh->bufpos = 0;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2585
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2586
    io = fh->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2587
    if (len >= fh->bufsize)  /* need more than the buffer takes. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2588
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2589
        /* leave buffer empty, go right to output instead. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2590
        rc = io->read(io, buffer, len);
12453
89423b1db329 fix buffered read going into inf recursion loop
sheepluva
parents: 12213
diff changeset
  2591
        if (rc <= 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2592
            return ((retval == 0) ? rc : retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2593
        return retval + rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2594
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2595
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2596
    /* need less than buffer can take. Fill buffer. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2597
    rc = io->read(io, fh->buffer, fh->bufsize);
12453
89423b1db329 fix buffered read going into inf recursion loop
sheepluva
parents: 12213
diff changeset
  2598
    if (rc <= 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2599
        return ((retval == 0) ? rc : retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2600
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2601
    assert(fh->bufpos == 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2602
    fh->buffill = (PHYSFS_uint32) rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2603
    rc = doBufferedRead(fh, buffer, len);  /* go from the start, again. */
12453
89423b1db329 fix buffered read going into inf recursion loop
sheepluva
parents: 12213
diff changeset
  2604
    if (rc <= 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2605
        return ((retval == 0) ? rc : retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2606
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2607
    return retval + rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2608
} /* doBufferedRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2609
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2610
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2611
PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2612
                          PHYSFS_uint32 size, PHYSFS_uint32 count)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2613
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2614
    const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2615
    const PHYSFS_sint64 retval = PHYSFS_readBytes(handle, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2616
    return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2617
} /* PHYSFS_read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2618
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2619
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2620
PHYSFS_sint64 PHYSFS_readBytes(PHYSFS_File *handle, void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2621
                               PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2622
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2623
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2624
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2625
#ifdef PHYSFS_NO_64BIT_SUPPORT
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2626
    const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2627
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2628
    const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2629
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2630
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2631
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2632
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2633
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2634
    BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2635
    BAIL_IF_MACRO(!fh->forReading, PHYSFS_ERR_OPEN_FOR_WRITING, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2636
    BAIL_IF_MACRO(len == 0, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2637
    if (fh->buffer)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2638
        return doBufferedRead(fh, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2639
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2640
    return fh->io->read(fh->io, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2641
} /* PHYSFS_readBytes */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2642
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2643
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2644
static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2645
                                     PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2646
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2647
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2648
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2649
    /* whole thing fits in the buffer? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2650
    if ( (((PHYSFS_uint64) fh->buffill) + len) < fh->bufsize )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2651
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2652
        memcpy(fh->buffer + fh->buffill, buffer, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2653
        fh->buffill += (PHYSFS_uint32) len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2654
        return (PHYSFS_sint64) len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2655
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2656
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2657
    /* would overflow buffer. Flush and then write the new objects, too. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2658
    BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2659
    return fh->io->write(fh->io, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2660
} /* doBufferedWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2661
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2662
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2663
PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2664
                           PHYSFS_uint32 size, PHYSFS_uint32 count)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2665
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2666
    const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2667
    const PHYSFS_sint64 retval = PHYSFS_writeBytes(handle, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2668
    return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2669
} /* PHYSFS_write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2670
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2671
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2672
PHYSFS_sint64 PHYSFS_writeBytes(PHYSFS_File *handle, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2673
                                PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2674
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2675
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2676
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2677
#ifdef PHYSFS_NO_64BIT_SUPPORT
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2678
    const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2679
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2680
    const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2681
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2682
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2683
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2684
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2685
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2686
    BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2687
    BAIL_IF_MACRO(fh->forReading, PHYSFS_ERR_OPEN_FOR_READING, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2688
    BAIL_IF_MACRO(len == 0, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2689
    if (fh->buffer)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2690
        return doBufferedWrite(handle, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2691
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2692
    return fh->io->write(fh->io, buffer, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2693
} /* PHYSFS_write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2694
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2695
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2696
int PHYSFS_eof(PHYSFS_File *handle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2697
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2698
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2699
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2700
    if (!fh->forReading)  /* never EOF on files opened for write/append. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2701
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2702
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2703
    /* can't be eof if buffer isn't empty */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2704
    if (fh->bufpos == fh->buffill)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2705
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2706
        /* check the Io. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2707
        PHYSFS_Io *io = fh->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2708
        const PHYSFS_sint64 pos = io->tell(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2709
        const PHYSFS_sint64 len = io->length(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2710
        if ((pos < 0) || (len < 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2711
            return 0;  /* beats me. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2712
        return (pos >= len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2713
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2714
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2715
    return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2716
} /* PHYSFS_eof */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2717
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2718
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2719
PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2720
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2721
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2722
    const PHYSFS_sint64 pos = fh->io->tell(fh->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2723
    const PHYSFS_sint64 retval = fh->forReading ?
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2724
                                 (pos - fh->buffill) + fh->bufpos :
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2725
                                 (pos + fh->buffill);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2726
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2727
} /* PHYSFS_tell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2728
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2729
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2730
int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2731
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2732
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2733
    BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2734
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2735
    if (fh->buffer && fh->forReading)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2736
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2737
        /* avoid throwing away our precious buffer if seeking within it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2738
        PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2739
        if ( /* seeking within the already-buffered range? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2740
            ((offset >= 0) && (offset <= fh->buffill - fh->bufpos)) /* fwd */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2741
            || ((offset < 0) && (-offset <= fh->bufpos)) /* backward */ )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2742
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2743
            fh->bufpos += (PHYSFS_uint32) offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2744
            return 1; /* successful seek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2745
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2746
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2747
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2748
    /* we have to fall back to a 'raw' seek. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2749
    fh->buffill = fh->bufpos = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2750
    return fh->io->seek(fh->io, pos);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2751
} /* PHYSFS_seek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2752
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2753
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2754
PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2755
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2756
    PHYSFS_Io *io = ((FileHandle *) handle)->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2757
    return io->length(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2758
} /* PHYSFS_filelength */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2759
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2760
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2761
int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2762
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2763
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2764
    PHYSFS_uint32 bufsize;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2765
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2766
    /* !!! FIXME: actually, why use 32 bits here? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2767
    /*BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);*/
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2768
    BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, PHYSFS_ERR_INVALID_ARGUMENT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2769
    bufsize = (PHYSFS_uint32) _bufsize;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2770
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2771
    BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2772
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2773
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2774
     * For reads, we need to move the file pointer to where it would be
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2775
     *  if we weren't buffering, so that the next read will get the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2776
     *  right chunk of stuff from the file. PHYSFS_flush() handles writes.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2777
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2778
    if ((fh->forReading) && (fh->buffill != fh->bufpos))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2779
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2780
        PHYSFS_uint64 pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2781
        const PHYSFS_sint64 curpos = fh->io->tell(fh->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2782
        BAIL_IF_MACRO(curpos == -1, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2783
        pos = ((curpos - fh->buffill) + fh->bufpos);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2784
        BAIL_IF_MACRO(!fh->io->seek(fh->io, pos), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2785
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2786
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2787
    if (bufsize == 0)  /* delete existing buffer. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2788
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2789
        if (fh->buffer)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2790
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2791
            allocator.Free(fh->buffer);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2792
            fh->buffer = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2793
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2794
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2795
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2796
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2797
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2798
        PHYSFS_uint8 *newbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2799
        newbuf = (PHYSFS_uint8 *) allocator.Realloc(fh->buffer, bufsize);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2800
        BAIL_IF_MACRO(!newbuf, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2801
        fh->buffer = newbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2802
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2803
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2804
    fh->bufsize = bufsize;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2805
    fh->buffill = fh->bufpos = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2806
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2807
} /* PHYSFS_setBuffer */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2808
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2809
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2810
int PHYSFS_flush(PHYSFS_File *handle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2811
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2812
    FileHandle *fh = (FileHandle *) handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2813
    PHYSFS_Io *io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2814
    PHYSFS_sint64 rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2815
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2816
    if ((fh->forReading) || (fh->bufpos == fh->buffill))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2817
        return 1;  /* open for read or buffer empty are successful no-ops. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2818
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2819
    /* dump buffer to disk. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2820
    io = fh->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2821
    rc = io->write(io, fh->buffer + fh->bufpos, fh->buffill - fh->bufpos);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2822
    BAIL_IF_MACRO(rc <= 0, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2823
    fh->bufpos = fh->buffill = 0;
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2824
    return io->flush ? io->flush(io) : 1;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2825
} /* PHYSFS_flush */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2826
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2827
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2828
int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2829
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2830
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2831
    char *fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2832
    size_t len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2833
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2834
    BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2835
    BAIL_IF_MACRO(!stat, PHYSFS_ERR_INVALID_ARGUMENT, 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2836
    len = strlen(_fname) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2837
    fname = (char *) __PHYSFS_smallAlloc(len);
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2838
    BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2839
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2840
    /* set some sane defaults... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2841
    stat->filesize = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2842
    stat->modtime = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2843
    stat->createtime = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2844
    stat->accesstime = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2845
    stat->filetype = PHYSFS_FILETYPE_OTHER;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2846
    stat->readonly = 1;  /* !!! FIXME */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2847
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2848
    if (sanitizePlatformIndependentPath(_fname, fname))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2849
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2850
        if (*fname == '\0')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2851
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2852
            stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2853
            stat->readonly = !writeDir; /* Writeable if we have a writeDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2854
            retval = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2855
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2856
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2857
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2858
            DirHandle *i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2859
            int exists = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2860
            __PHYSFS_platformGrabMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2861
            for (i = searchPath; ((i != NULL) && (!exists)); i = i->next)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2862
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2863
                char *arcfname = fname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2864
                exists = partOfMountPoint(i, arcfname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2865
                if (exists)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2866
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2867
                    stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2868
                    stat->readonly = 1;  /* !!! FIXME */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2869
                    retval = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2870
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2871
                else if (verifyPath(i, &arcfname, 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2872
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2873
                    /* !!! FIXME: this test is wrong and should be elsewhere. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2874
                    stat->readonly = !(writeDir &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2875
                                 (strcmp(writeDir->dirName, i->dirName) == 0));
12213
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2876
                    retval = i->funcs->stat(i->opaque, arcfname, stat);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2877
                    if ((retval) || (currentErrorCode() != PHYSFS_ERR_NOT_FOUND))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
  2878
                        exists = 1;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2879
                } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2880
            } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2881
            __PHYSFS_platformReleaseMutex(stateLock);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2882
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2883
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2884
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2885
    __PHYSFS_smallFree(fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2886
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2887
} /* PHYSFS_stat */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2888
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2889
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2890
int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2891
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2892
    return (io->read(io, buf, len) == len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2893
} /* __PHYSFS_readAll */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2894
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2895
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2896
void *__PHYSFS_initSmallAlloc(void *ptr, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2897
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2898
    void *useHeap = ((ptr == NULL) ? ((void *) 1) : ((void *) 0));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2899
    if (useHeap)  /* too large for stack allocation or alloca() failed. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2900
        ptr = allocator.Malloc(len+sizeof (void *));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2901
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2902
    if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2903
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2904
        void **retval = (void **) ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2905
        /*printf("%s alloc'd (%d) bytes at (%p).\n",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2906
                useHeap ? "heap" : "stack", (int) len, ptr);*/
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2907
        *retval = useHeap;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2908
        return retval + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2909
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2910
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2911
    return NULL;  /* allocation failed. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2912
} /* __PHYSFS_initSmallAlloc */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2913
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2914
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2915
void __PHYSFS_smallFree(void *ptr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2916
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2917
    if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2918
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2919
        void **block = ((void **) ptr) - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2920
        const int useHeap = (*block != 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2921
        if (useHeap)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2922
            allocator.Free(block);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2923
        /*printf("%s free'd (%p).\n", useHeap ? "heap" : "stack", block);*/
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2924
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2925
} /* __PHYSFS_smallFree */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2926
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2927
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2928
int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2929
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2930
    BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2931
    externalAllocator = (a != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2932
    if (externalAllocator)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2933
        memcpy(&allocator, a, sizeof (PHYSFS_Allocator));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2934
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2935
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2936
} /* PHYSFS_setAllocator */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2937
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2938
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2939
const PHYSFS_Allocator *PHYSFS_getAllocator(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2940
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2941
    BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2942
    return &allocator;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2943
} /* PHYSFS_getAllocator */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2944
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2945
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2946
static void *mallocAllocatorMalloc(PHYSFS_uint64 s)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2947
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2948
    if (!__PHYSFS_ui64FitsAddressSpace(s))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2949
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2950
    #undef malloc
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2951
    return malloc((size_t) s);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2952
} /* mallocAllocatorMalloc */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2953
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2954
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2955
static void *mallocAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2956
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2957
    if (!__PHYSFS_ui64FitsAddressSpace(s))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2958
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2959
    #undef realloc
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2960
    return realloc(ptr, (size_t) s);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2961
} /* mallocAllocatorRealloc */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2962
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2963
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2964
static void mallocAllocatorFree(void *ptr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2965
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2966
    #undef free
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2967
    free(ptr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2968
} /* mallocAllocatorFree */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2969
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2970
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2971
static void setDefaultAllocator(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2972
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2973
    assert(!externalAllocator);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2974
    if (!__PHYSFS_platformSetDefaultAllocator(&allocator))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2975
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2976
        allocator.Init = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2977
        allocator.Deinit = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2978
        allocator.Malloc = mallocAllocatorMalloc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2979
        allocator.Realloc = mallocAllocatorRealloc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2980
        allocator.Free = mallocAllocatorFree;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2981
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2982
} /* setDefaultAllocator */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2983
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2984
/* end of physfs.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  2985