extras/globbing.c
changeset 573 01112353ffad
child 575 3bf69b30fe74
equal deleted inserted replaced
572:2728e1a3e75d 573:01112353ffad
       
     1 /** \file globbing.c */
       
     2 
       
     3 #include <stdio.h>
       
     4 #include <stdlib.h>
       
     5 #include <string.h>
       
     6 #include <ctype.h>
       
     7 
       
     8 #include "physfs.h"
       
     9 #include "globbing.h"
       
    10 
       
    11 /**
       
    12  * Please see globbing.h for details.
       
    13  *
       
    14  * License: this code is public domain. I make no warranty that it is useful,
       
    15  *  correct, harmless, or environmentally safe.
       
    16  *
       
    17  * This particular file may be used however you like, including copying it
       
    18  *  verbatim into a closed-source project, exploiting it commercially, and
       
    19  *  removing any trace of my name from the source (although I hope you won't
       
    20  *  do that). I welcome enhancements and corrections to this file, but I do
       
    21  *  not require you to send me patches if you make changes.
       
    22  *
       
    23  * Unless otherwise stated, the rest of PhysicsFS falls under the GNU Lesser
       
    24  *  General Public License: http://www.gnu.org/licenses/lgpl.txt
       
    25  *
       
    26  *  \author Ryan C. Gordon.
       
    27  */
       
    28 
       
    29 
       
    30 static int matchesPattern(const char *fname, const char *wildcard,
       
    31                           int caseSensitive)
       
    32 {
       
    33     char x, y;
       
    34     const char *fnameptr = fname;
       
    35     const char *wildptr = wildcard;
       
    36 
       
    37     while ((*wildptr) && (*fnameptr))
       
    38     {
       
    39         y = *wildptr;
       
    40         if (y == '*')
       
    41         {
       
    42             wildptr++;
       
    43             y = (caseSensitive) ? *wildptr : (char) tolower(*wildptr);
       
    44 
       
    45             while (1)
       
    46             {
       
    47                 x = (caseSensitive) ? *fnameptr : (char) tolower(*fnameptr);
       
    48                 if ((!x) || (x == y))
       
    49                     break;
       
    50                 else
       
    51                     fnameptr++;
       
    52             } /* while */
       
    53         } /* if */
       
    54 
       
    55         else if (y == '?')
       
    56         {
       
    57             wildptr++;
       
    58             fnameptr++;
       
    59         } /* else if */
       
    60 
       
    61         else
       
    62         {
       
    63             if (caseSensitive)
       
    64                 x = *fnameptr;
       
    65             else
       
    66             {
       
    67                 x = tolower(*fnameptr);
       
    68                 y = tolower(y);
       
    69             } /* if */
       
    70 
       
    71             wildptr++;
       
    72             fnameptr++;
       
    73 
       
    74             if (x != y)
       
    75                 return(0);
       
    76         } /* else */
       
    77 
       
    78     } /* while */
       
    79 
       
    80     return(*fnameptr == *wildptr);
       
    81 } /* matchesPattern */
       
    82 
       
    83 
       
    84 char **PHYSFSEXT_enumerateFilesWildcard(const char *dir, const char *wildcard,
       
    85                                         int caseSensitive)
       
    86 {
       
    87     char **rc = PHYSFS_enumerateFiles(dir);
       
    88     char **i = rc;
       
    89     char **j;
       
    90 
       
    91     while (*i != NULL)
       
    92     {
       
    93         if (matchesPattern(*i, wildcard, caseSensitive))
       
    94             i++;
       
    95         else
       
    96         {
       
    97             /* FIXME: This counts on physfs's allocation method not changing! */
       
    98             free(*i);
       
    99             for (j = i; *j != NULL; j++)
       
   100                 j[0] = j[1];
       
   101         } /* else */
       
   102     } /* for */
       
   103 
       
   104     return(rc);
       
   105 } /* PHYSFSEXT_enumerateFilesWildcard */
       
   106 
       
   107 
       
   108 #ifdef TEST_PHYSFSEXT_ENUMERATEFILESWILDCARD
       
   109 int main(int argc, char **argv)
       
   110 {
       
   111     int rc;
       
   112     char **flist;
       
   113     char **i;
       
   114 
       
   115     if (argc != 3)
       
   116     {
       
   117         printf("USAGE: %s <pattern> <caseSen>\n"
       
   118                "   where <caseSen> is 1 or 0.\n", argv[0]);
       
   119         return(1);
       
   120     } /* if */
       
   121 
       
   122     if (!PHYSFS_init(argv[0]))
       
   123     {
       
   124         fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getLastError());
       
   125         return(1);
       
   126     } /* if */
       
   127 
       
   128     if (!PHYSFS_addToSearchPath(".", 1))
       
   129     {
       
   130         fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getLastError());
       
   131         PHYSFS_deinit();
       
   132         return(1);
       
   133     } /* if */
       
   134 
       
   135     flist = PHYSFSEXT_enumerateFilesWildcard("/", argv[1], atoi(argv[2]));
       
   136     rc = 0;
       
   137     for (i = flist; *i; i++)
       
   138     {
       
   139         printf("%s\n", *i);
       
   140         rc++;
       
   141     } /* for */
       
   142     printf("\n  total %d files.\n\n", rc);
       
   143 
       
   144     PHYSFS_freeList(flist);
       
   145     PHYSFS_deinit();
       
   146 
       
   147     return(0);
       
   148 } /* main */
       
   149 #endif
       
   150 
       
   151 /* end of globbing.c ... */
       
   152