ignorecase.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /** \file ignorecase.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "physfs.h"
  7. #include "ignorecase.h"
  8. /**
  9. * Please see ignorecase.h for details.
  10. *
  11. * License: this code is public domain. I make no warranty that it is useful,
  12. * correct, harmless, or environmentally safe.
  13. *
  14. * This particular file may be used however you like, including copying it
  15. * verbatim into a closed-source project, exploiting it commercially, and
  16. * removing any trace of my name from the source (although I hope you won't
  17. * do that). I welcome enhancements and corrections to this file, but I do
  18. * not require you to send me patches if you make changes. This code has
  19. * NO WARRANTY.
  20. *
  21. * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
  22. * Please see LICENSE.txt in the root of the source tree.
  23. *
  24. * \author Ryan C. Gordon.
  25. */
  26. /* I'm not screwing around with stricmp vs. strcasecmp... */
  27. /* !!! FIXME: this will NOT work with UTF-8 strings in physfs2.0 */
  28. static int caseInsensitiveStringCompare(const char *x, const char *y)
  29. {
  30. int ux, uy;
  31. do
  32. {
  33. ux = toupper((int) *x);
  34. uy = toupper((int) *y);
  35. if (ux != uy)
  36. return((ux > uy) ? 1 : -1);
  37. x++;
  38. y++;
  39. } while ((ux) && (uy));
  40. return(0);
  41. } /* caseInsensitiveStringCompare */
  42. static int locateOneElement(char *buf)
  43. {
  44. char *ptr;
  45. char **rc;
  46. char **i;
  47. if (PHYSFS_exists(buf))
  48. return(1); /* quick rejection: exists in current case. */
  49. ptr = strrchr(buf, '/'); /* find entry at end of path. */
  50. if (ptr == NULL)
  51. {
  52. rc = PHYSFS_enumerateFiles("/");
  53. ptr = buf;
  54. } /* if */
  55. else
  56. {
  57. *ptr = '\0';
  58. rc = PHYSFS_enumerateFiles(buf);
  59. *ptr = '/';
  60. ptr++; /* point past dirsep to entry itself. */
  61. } /* else */
  62. for (i = rc; *i != NULL; i++)
  63. {
  64. if (caseInsensitiveStringCompare(*i, ptr) == 0)
  65. {
  66. strcpy(ptr, *i); /* found a match. Overwrite with this case. */
  67. PHYSFS_freeList(rc);
  68. return(1);
  69. } /* if */
  70. } /* for */
  71. /* no match at all... */
  72. PHYSFS_freeList(rc);
  73. return(0);
  74. } /* locateOneElement */
  75. int PHYSFSEXT_locateCorrectCase(char *buf)
  76. {
  77. int rc;
  78. char *ptr;
  79. char *prevptr;
  80. while (*buf == '/') /* skip any '/' at start of string... */
  81. buf++;
  82. ptr = prevptr = buf;
  83. if (*ptr == '\0')
  84. return(0); /* Uh...I guess that's success. */
  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. /* check final element... */
  94. return(locateOneElement(buf) ? 0 : -1);
  95. } /* PHYSFSEXT_locateCorrectCase */
  96. #ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
  97. int main(int argc, char **argv)
  98. {
  99. int rc;
  100. char buf[128];
  101. PHYSFS_File *f;
  102. if (!PHYSFS_init(argv[0]))
  103. {
  104. fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getLastError());
  105. return(1);
  106. } /* if */
  107. if (!PHYSFS_addToSearchPath(".", 1))
  108. {
  109. fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getLastError());
  110. PHYSFS_deinit();
  111. return(1);
  112. } /* if */
  113. if (!PHYSFS_setWriteDir("."))
  114. {
  115. fprintf(stderr, "PHYSFS_setWriteDir(): %s\n", PHYSFS_getLastError());
  116. PHYSFS_deinit();
  117. return(1);
  118. } /* if */
  119. if (!PHYSFS_mkdir("/a/b/c"))
  120. {
  121. fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
  122. PHYSFS_deinit();
  123. return(1);
  124. } /* if */
  125. if (!PHYSFS_mkdir("/a/b/C"))
  126. {
  127. fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
  128. PHYSFS_deinit();
  129. return(1);
  130. } /* if */
  131. f = PHYSFS_openWrite("/a/b/c/x.txt");
  132. PHYSFS_close(f);
  133. if (f == NULL)
  134. {
  135. fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
  136. PHYSFS_deinit();
  137. return(1);
  138. } /* if */
  139. f = PHYSFS_openWrite("/a/b/C/X.txt");
  140. PHYSFS_close(f);
  141. if (f == NULL)
  142. {
  143. fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
  144. PHYSFS_deinit();
  145. return(1);
  146. } /* if */
  147. strcpy(buf, "/a/b/c/x.txt");
  148. rc = PHYSFSEXT_locateCorrectCase(buf);
  149. if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  150. printf("test 1 failed\n");
  151. strcpy(buf, "/a/B/c/x.txt");
  152. rc = PHYSFSEXT_locateCorrectCase(buf);
  153. if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  154. printf("test 2 failed\n");
  155. strcpy(buf, "/a/b/C/x.txt");
  156. rc = PHYSFSEXT_locateCorrectCase(buf);
  157. if ((rc != 0) || (strcmp(buf, "/a/b/C/X.txt") != 0))
  158. printf("test 3 failed\n");
  159. strcpy(buf, "/a/b/c/X.txt");
  160. rc = PHYSFSEXT_locateCorrectCase(buf);
  161. if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  162. printf("test 4 failed\n");
  163. strcpy(buf, "/a/b/c/z.txt");
  164. rc = PHYSFSEXT_locateCorrectCase(buf);
  165. if ((rc != -1) || (strcmp(buf, "/a/b/c/z.txt") != 0))
  166. printf("test 5 failed\n");
  167. strcpy(buf, "/A/B/Z/z.txt");
  168. rc = PHYSFSEXT_locateCorrectCase(buf);
  169. if ((rc != -2) || (strcmp(buf, "/a/b/Z/z.txt") != 0))
  170. printf("test 6 failed\n");
  171. printf("Testing completed.\n");
  172. printf(" If no errors were reported, you're good to go.\n");
  173. PHYSFS_delete("/a/b/c/x.txt");
  174. PHYSFS_delete("/a/b/C/X.txt");
  175. PHYSFS_delete("/a/b/c");
  176. PHYSFS_delete("/a/b/C");
  177. PHYSFS_delete("/a/b");
  178. PHYSFS_delete("/a");
  179. PHYSFS_deinit();
  180. return(0);
  181. } /* main */
  182. #endif
  183. /* end of ignorecase.c ... */