ignorecase.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. while (*buf == '/') /* skip any '/' at start of string... */
  80. buf++;
  81. ptr = buf;
  82. if (*ptr == '\0')
  83. return 0; /* Uh...I guess that's success. */
  84. while ( (ptr = strchr(ptr + 1, '/')) != NULL )
  85. {
  86. *ptr = '\0'; /* block this path section off */
  87. rc = locateOneElement(buf);
  88. *ptr = '/'; /* restore path separator */
  89. if (!rc)
  90. return -2; /* missing element in path. */
  91. } /* while */
  92. /* check final element... */
  93. return locateOneElement(buf) ? 0 : -1;
  94. } /* PHYSFSEXT_locateCorrectCase */
  95. #ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
  96. int main(int argc, char **argv)
  97. {
  98. int rc;
  99. char buf[128];
  100. PHYSFS_File *f;
  101. if (!PHYSFS_init(argv[0]))
  102. {
  103. fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getLastError());
  104. return 1;
  105. } /* if */
  106. if (!PHYSFS_addToSearchPath(".", 1))
  107. {
  108. fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getLastError());
  109. PHYSFS_deinit();
  110. return 1;
  111. } /* if */
  112. if (!PHYSFS_setWriteDir("."))
  113. {
  114. fprintf(stderr, "PHYSFS_setWriteDir(): %s\n", PHYSFS_getLastError());
  115. PHYSFS_deinit();
  116. return 1;
  117. } /* if */
  118. if (!PHYSFS_mkdir("/a/b/c"))
  119. {
  120. fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
  121. PHYSFS_deinit();
  122. return 1;
  123. } /* if */
  124. if (!PHYSFS_mkdir("/a/b/C"))
  125. {
  126. fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
  127. PHYSFS_deinit();
  128. return 1;
  129. } /* if */
  130. f = PHYSFS_openWrite("/a/b/c/x.txt");
  131. PHYSFS_close(f);
  132. if (f == NULL)
  133. {
  134. fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
  135. PHYSFS_deinit();
  136. return 1;
  137. } /* if */
  138. f = PHYSFS_openWrite("/a/b/C/X.txt");
  139. PHYSFS_close(f);
  140. if (f == NULL)
  141. {
  142. fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
  143. PHYSFS_deinit();
  144. return 1;
  145. } /* if */
  146. strcpy(buf, "/a/b/c/x.txt");
  147. rc = PHYSFSEXT_locateCorrectCase(buf);
  148. if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  149. printf("test 1 failed\n");
  150. strcpy(buf, "/a/B/c/x.txt");
  151. rc = PHYSFSEXT_locateCorrectCase(buf);
  152. if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
  153. printf("test 2 failed\n");
  154. strcpy(buf, "/a/b/C/x.txt");
  155. rc = PHYSFSEXT_locateCorrectCase(buf);
  156. if ((rc != 0) || (strcmp(buf, "/a/b/C/X.txt") != 0))
  157. printf("test 3 failed\n");
  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 4 failed\n");
  162. strcpy(buf, "/a/b/c/z.txt");
  163. rc = PHYSFSEXT_locateCorrectCase(buf);
  164. if ((rc != -1) || (strcmp(buf, "/a/b/c/z.txt") != 0))
  165. printf("test 5 failed\n");
  166. strcpy(buf, "/A/B/Z/z.txt");
  167. rc = PHYSFSEXT_locateCorrectCase(buf);
  168. if ((rc != -2) || (strcmp(buf, "/a/b/Z/z.txt") != 0))
  169. printf("test 6 failed\n");
  170. printf("Testing completed.\n");
  171. printf(" If no errors were reported, you're good to go.\n");
  172. PHYSFS_delete("/a/b/c/x.txt");
  173. PHYSFS_delete("/a/b/C/X.txt");
  174. PHYSFS_delete("/a/b/c");
  175. PHYSFS_delete("/a/b/C");
  176. PHYSFS_delete("/a/b");
  177. PHYSFS_delete("/a");
  178. PHYSFS_deinit();
  179. return 0;
  180. } /* main */
  181. #endif
  182. /* end of ignorecase.c ... */