extras/globbing.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Feb 2018 10:26:33 -0500
changeset 1631 1aec60db9326
parent 1016 957c97389257
permissions -rw-r--r--
Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/** \file globbing.c */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <stdio.h>
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <stdlib.h>
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include <string.h>
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#include <ctype.h>
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
     7
#include <assert.h>
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
#include "globbing.h"
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
/**
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 * Please see globbing.h for details.
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 * License: this code is public domain. I make no warranty that it is useful,
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *  correct, harmless, or environmentally safe.
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 * This particular file may be used however you like, including copying it
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *  verbatim into a closed-source project, exploiting it commercially, and
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *  removing any trace of my name from the source (although I hope you won't
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *  do that). I welcome enhancements and corrections to this file, but I do
576
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 575
diff changeset
    21
 *  not require you to send me patches if you make changes. This code has
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 575
diff changeset
    22
 *  NO WARRANTY.
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *
576
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 575
diff changeset
    24
 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    25
 *  Please see the file LICENSE.txt in the source's root directory.
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *  \author Ryan C. Gordon.
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
static int matchesPattern(const char *fname, const char *wildcard,
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
                          int caseSensitive)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
{
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    char x, y;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    const char *fnameptr = fname;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    const char *wildptr = wildcard;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    while ((*wildptr) && (*fnameptr))
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
        y = *wildptr;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
        if (y == '*')
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
        {
575
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    43
            do
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    44
            {
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    45
                wildptr++;  /* skip multiple '*' in a row... */
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    46
            } while (*wildptr == '*');
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    47
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
            y = (caseSensitive) ? *wildptr : (char) tolower(*wildptr);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
            while (1)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
            {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
                x = (caseSensitive) ? *fnameptr : (char) tolower(*fnameptr);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
                if ((!x) || (x == y))
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
                    break;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
                else
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
                    fnameptr++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
            } /* while */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
        } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        else if (y == '?')
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
            wildptr++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
            fnameptr++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
        } /* else if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
        else
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
            if (caseSensitive)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
                x = *fnameptr;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
            else
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
            {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
                x = tolower(*fnameptr);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
                y = tolower(y);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
            } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
            wildptr++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
            fnameptr++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
            if (x != y)
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 977
diff changeset
    80
                return 0;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
        } /* else */
575
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    82
    } /* while */
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
575
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    84
    while (*wildptr == '*')
3bf69b30fe74 Fixes for corner cases (thanks, Bradley!)
Ryan C. Gordon <icculus@icculus.org>
parents: 573
diff changeset
    85
        wildptr++;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 977
diff changeset
    87
    return (*fnameptr == *wildptr);
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
} /* matchesPattern */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    90
typedef struct
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    91
{
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    92
    const PHYSFS_Allocator *allocator;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    93
    const char *wildcard;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    94
    int caseSensitive;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    95
    PHYSFS_EnumFilesCallback callback;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    96
    void *origData;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    97
} WildcardCallbackData;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    98
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    99
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   100
/*
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   101
 * This callback sits between the enumerator and the enduser callback,
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   102
 *  filtering out files that don't match the wildcard pattern.
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   103
 */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   104
static void wildcardCallback(void *_d, const char *origdir, const char *fname)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   105
{
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   106
    const WildcardCallbackData *data = (const WildcardCallbackData *) _d;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   107
    if (matchesPattern(fname, data->wildcard, data->caseSensitive))
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   108
        data->callback(data->origData, origdir, fname);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   109
} /* wildcardCallback */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   110
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   111
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   112
void PHYSFSEXT_enumerateFilesCallbackWildcard(const char *dir,
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   113
                                              const char *wildcard,
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   114
                                              int caseSensitive,
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   115
                                              PHYSFS_EnumFilesCallback c,
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   116
                                              void *d)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   117
{
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   118
    WildcardCallbackData data;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   119
    data.allocator = PHYSFS_getAllocator();
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   120
    data.wildcard = wildcard;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   121
    data.caseSensitive = caseSensitive;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   122
    data.callback = c;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   123
    data.origData = d;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   124
    PHYSFS_enumerateFilesCallback(dir, wildcardCallback, &data);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   125
} /* PHYSFSEXT_enumerateFilesCallbackWildcard */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   126
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   127
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   128
void PHYSFSEXT_freeEnumeration(char **list)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   129
{
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   130
    const PHYSFS_Allocator *allocator = PHYSFS_getAllocator();
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   131
    int i;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   132
    if (list != NULL)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   133
    {
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   134
        for (i = 0; list[i] != NULL; i++)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   135
            allocator->Free(list[i]);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   136
        allocator->Free(list);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   137
    } /* if */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   138
} /* PHYSFSEXT_freeEnumeration */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   139
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
char **PHYSFSEXT_enumerateFilesWildcard(const char *dir, const char *wildcard,
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
                                        int caseSensitive)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
{
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   144
    const PHYSFS_Allocator *allocator = PHYSFS_getAllocator();
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   145
    char **list = PHYSFS_enumerateFiles(dir);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   146
    char **retval = NULL;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   147
    int totalmatches = 0;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   148
    int matches = 0;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   149
    char **i;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   151
    for (i = list; *i != NULL; i++)
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    {
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   153
        #if 0
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   154
        printf("matchesPattern: '%s' vs '%s' (%s) ... %s\n", *i, wildcard,
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   155
               caseSensitive ? "case" : "nocase",
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   156
               matchesPattern(*i, wildcard, caseSensitive) ? "true" : "false");
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   157
        #endif
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
        if (matchesPattern(*i, wildcard, caseSensitive))
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   159
            totalmatches++;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    } /* for */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   162
    retval = (char **) allocator->Malloc(sizeof (char *) * (totalmatches+1));
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   163
    if (retval != NULL)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   164
    {
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   165
        for (i = list; ((matches < totalmatches) && (*i != NULL)); i++)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   166
        {
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   167
            if (matchesPattern(*i, wildcard, caseSensitive))
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   168
            {
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   169
                retval[matches] = (char *) allocator->Malloc(strlen(*i) + 1);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   170
                if (retval[matches] == NULL)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   171
                {
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   172
                    while (matches--)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   173
                        allocator->Free(retval[matches]);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   174
                    allocator->Free(retval);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   175
                    retval = NULL;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   176
                    break;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   177
                } /* if */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   178
                strcpy(retval[matches], *i);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   179
                matches++;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   180
            } /* if */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   181
        } /* for */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   182
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   183
        if (retval != NULL)
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   184
        {
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   185
            assert(totalmatches == matches);
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   186
            retval[matches] = NULL;
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   187
        } /* if */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   188
    } /* if */
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   189
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   190
    PHYSFS_freeList(list);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 977
diff changeset
   191
    return retval;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
} /* PHYSFSEXT_enumerateFilesWildcard */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
#ifdef TEST_PHYSFSEXT_ENUMERATEFILESWILDCARD
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
int main(int argc, char **argv)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
{
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    int rc;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    char **flist;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    char **i;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    if (argc != 3)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        printf("USAGE: %s <pattern> <caseSen>\n"
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
               "   where <caseSen> is 1 or 0.\n", argv[0]);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 977
diff changeset
   206
        return 1;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
    if (!PHYSFS_init(argv[0]))
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
    {
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1016
diff changeset
   211
        fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 977
diff changeset
   212
        return 1;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    if (!PHYSFS_addToSearchPath(".", 1))
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    {
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1016
diff changeset
   217
        fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        PHYSFS_deinit();
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 977
diff changeset
   219
        return 1;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    flist = PHYSFSEXT_enumerateFilesWildcard("/", argv[1], atoi(argv[2]));
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    rc = 0;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    for (i = flist; *i; i++)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
        printf("%s\n", *i);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
        rc++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    } /* for */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
    printf("\n  total %d files.\n\n", rc);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
977
5a2f49a5f804 Improved globbing extra.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   231
    PHYSFSEXT_freeEnumeration(flist);
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    PHYSFS_deinit();
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 977
diff changeset
   234
    return 0;
573
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
} /* main */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
#endif
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
/* end of globbing.c ... */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239