extras/globbing.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 03 Oct 2018 22:45:05 -0400
branchstable-1.0
changeset 1648 93b4c3bc6b9d
parent 576 5da65f8e9a50
child 809 116b8fe30371
permissions -rw-r--r--
Fixed Win10's GetUserProfileDirectory() bug in stable-1.0 branch.
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>
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#include "physfs.h"
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.
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 575
diff changeset
    25
 *  Please see LICENSE in the root of the source tree.
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)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
                return(0);
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
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    return(*fnameptr == *wildptr);
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
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
char **PHYSFSEXT_enumerateFilesWildcard(const char *dir, const char *wildcard,
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
                                        int caseSensitive)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
{
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    char **rc = PHYSFS_enumerateFiles(dir);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    char **i = rc;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    char **j;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    while (*i != NULL)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
        if (matchesPattern(*i, wildcard, caseSensitive))
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
            i++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        else
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
        {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
            /* FIXME: This counts on physfs's allocation method not changing! */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
            free(*i);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
            for (j = i; *j != NULL; j++)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
                j[0] = j[1];
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
        } /* else */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    } /* for */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    return(rc);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
} /* PHYSFSEXT_enumerateFilesWildcard */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
#ifdef TEST_PHYSFSEXT_ENUMERATEFILESWILDCARD
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
int main(int argc, char **argv)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
{
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    int rc;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    char **flist;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    char **i;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    if (argc != 3)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
        printf("USAGE: %s <pattern> <caseSen>\n"
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
               "   where <caseSen> is 1 or 0.\n", argv[0]);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
        return(1);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
    } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    if (!PHYSFS_init(argv[0]))
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
        fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getLastError());
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
        return(1);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    if (!PHYSFS_addToSearchPath(".", 1))
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
        fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getLastError());
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        PHYSFS_deinit();
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        return(1);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    } /* if */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    flist = PHYSFSEXT_enumerateFilesWildcard("/", argv[1], atoi(argv[2]));
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    rc = 0;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    for (i = flist; *i; i++)
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    {
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
        printf("%s\n", *i);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
        rc++;
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    } /* for */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    printf("\n  total %d files.\n\n", rc);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    PHYSFS_freeList(flist);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    PHYSFS_deinit();
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    return(0);
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
} /* main */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
#endif
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
/* end of globbing.c ... */
01112353ffad First stab at globbing extension.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159