Subversion Repositories Games.Descent

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /** \file ignorecase.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 "ignorecase.h"
  10.  
  11. /**
  12.  * Please see ignorecase.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. This code has
  22.  *  NO WARRANTY.
  23.  *
  24.  * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
  25.  *  Please see LICENSE.txt in the root of the source tree.
  26.  *
  27.  *  \author Ryan C. Gordon.
  28.  */
  29.  
  30. static int locateOneElement(char *buf)
  31. {
  32.     char *ptr;
  33.     char **rc;
  34.     char **i;
  35.  
  36.     if (PHYSFS_exists(buf))
  37.         return 1;  /* quick rejection: exists in current case. */
  38.  
  39.     ptr = strrchr(buf, '/');  /* find entry at end of path. */
  40.     if (ptr == NULL)
  41.     {
  42.         rc = PHYSFS_enumerateFiles("/");
  43.         ptr = buf;
  44.     } /* if */
  45.     else
  46.     {
  47.         *ptr = '\0';
  48.         rc = PHYSFS_enumerateFiles(buf);
  49.         *ptr = '/';
  50.         ptr++;  /* point past dirsep to entry itself. */
  51.     } /* else */
  52.  
  53.     if (rc != NULL)
  54.     {
  55.         for (i = rc; *i != NULL; i++)
  56.         {
  57.             if (PHYSFS_utf8stricmp(*i, ptr) == 0)
  58.             {
  59.                 strcpy(ptr, *i); /* found a match. Overwrite with this case. */
  60.                 PHYSFS_freeList(rc);
  61.                 return 1;
  62.             } /* if */
  63.         } /* for */
  64.  
  65.         PHYSFS_freeList(rc);
  66.     } /* if */
  67.  
  68.     /* no match at all... */
  69.     return 0;
  70. } /* locateOneElement */
  71.  
  72.  
  73. int PHYSFSEXT_locateCorrectCase(char *buf)
  74. {
  75.     int rc;
  76.     char *ptr;
  77.  
  78.     while (*buf == '/')  /* skip any '/' at start of string... */
  79.         buf++;
  80.  
  81.     ptr = buf;
  82.     if (*ptr == '\0')
  83.         return 0;  /* Uh...I guess that's success. */
  84.  
  85.     while ( (ptr = strchr(ptr + 1, '/')) != NULL )
  86.     {
  87.         *ptr = '\0';  /* block this path section off */
  88.         rc = locateOneElement(buf);
  89.         *ptr = '/'; /* restore path separator */
  90.         if (!rc)
  91.             return -2;  /* missing element in path. */
  92.     } /* while */
  93.  
  94.     /* check final element... */
  95.     return locateOneElement(buf) ? 0 : -1;
  96. } /* PHYSFSEXT_locateCorrectCase */
  97.  
  98.  
  99. #ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
  100. int main(int argc, char **argv)
  101. {
  102.     int rc;
  103.     char buf[128];
  104.     PHYSFS_File *f;
  105.  
  106.     if (!PHYSFS_init(argv[0]))
  107.     {
  108.         fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getLastError());
  109.         return 1;
  110.     } /* if */
  111.  
  112.     if (!PHYSFS_addToSearchPath(".", 1))
  113.     {
  114.         fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getLastError());
  115.         PHYSFS_deinit();
  116.         return 1;
  117.     } /* if */
  118.  
  119.     if (!PHYSFS_setWriteDir("."))
  120.     {
  121.         fprintf(stderr, "PHYSFS_setWriteDir(): %s\n", PHYSFS_getLastError());
  122.         PHYSFS_deinit();
  123.         return 1;
  124.     } /* if */
  125.  
  126.     if (!PHYSFS_mkdir("/a/b/c"))
  127.     {
  128.         fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
  129.         PHYSFS_deinit();
  130.         return 1;
  131.     } /* if */
  132.  
  133.     if (!PHYSFS_mkdir("/a/b/C"))
  134.     {
  135.         fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
  136.         PHYSFS_deinit();
  137.         return 1;
  138.     } /* if */
  139.  
  140.     f = PHYSFS_openWrite("/a/b/c/x.txt");
  141.     PHYSFS_close(f);
  142.     if (f == NULL)
  143.     {
  144.         fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
  145.         PHYSFS_deinit();
  146.         return 1;
  147.     } /* if */
  148.  
  149.     f = PHYSFS_openWrite("/a/b/C/X.txt");
  150.     PHYSFS_close(f);
  151.     if (f == NULL)
  152.     {
  153.         fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
  154.         PHYSFS_deinit();
  155.         return 1;
  156.     } /* if */
  157.  
  158.     strcpy(buf, "/a/b/c/x.txt");
  159.     rc = PHYSFSEXT_locateCorrectCase(buf);
  160.     if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  161.         printf("test 1 failed\n");
  162.  
  163.     strcpy(buf, "/a/B/c/x.txt");
  164.     rc = PHYSFSEXT_locateCorrectCase(buf);
  165.     if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  166.         printf("test 2 failed\n");
  167.  
  168.     strcpy(buf, "/a/b/C/x.txt");
  169.     rc = PHYSFSEXT_locateCorrectCase(buf);
  170.     if ((rc != 0) || (strcmp(buf, "/a/b/C/X.txt") != 0))
  171.         printf("test 3 failed\n");
  172.  
  173.     strcpy(buf, "/a/b/c/X.txt");
  174.     rc = PHYSFSEXT_locateCorrectCase(buf);
  175.     if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  176.         printf("test 4 failed\n");
  177.  
  178.     strcpy(buf, "/a/b/c/z.txt");
  179.     rc = PHYSFSEXT_locateCorrectCase(buf);
  180.     if ((rc != -1) || (strcmp(buf, "/a/b/c/z.txt") != 0))
  181.         printf("test 5 failed\n");
  182.  
  183.     strcpy(buf, "/A/B/Z/z.txt");
  184.     rc = PHYSFSEXT_locateCorrectCase(buf);
  185.     if ((rc != -2) || (strcmp(buf, "/a/b/Z/z.txt") != 0))
  186.         printf("test 6 failed\n");
  187.  
  188.     printf("Testing completed.\n");
  189.     printf("  If no errors were reported, you're good to go.\n");
  190.  
  191.     PHYSFS_delete("/a/b/c/x.txt");
  192.     PHYSFS_delete("/a/b/C/X.txt");
  193.     PHYSFS_delete("/a/b/c");
  194.     PHYSFS_delete("/a/b/C");
  195.     PHYSFS_delete("/a/b");
  196.     PHYSFS_delete("/a");
  197.     PHYSFS_deinit();
  198.     return 0;
  199. } /* main */
  200. #endif
  201.  
  202. /* end of ignorecase.c ... */
  203.  
  204.