1
0

ignorecase.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 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. static int caseInsensitiveStringCompare(const char *x, const char *y)
  28. {
  29. int ux, uy;
  30. do
  31. {
  32. ux = toupper((int) *x);
  33. uy = toupper((int) *y);
  34. if (ux != uy)
  35. return((ux > uy) ? 1 : -1);
  36. x++;
  37. y++;
  38. } while ((ux) && (uy));
  39. return(0);
  40. } /* caseInsensitiveStringCompare */
  41. static int locateOneElement(char *buf)
  42. {
  43. char *ptr;
  44. char **rc;
  45. char **i;
  46. if (PHYSFS_exists(buf))
  47. return(1); /* quick rejection: exists in current case. */
  48. ptr = strrchr(buf, '/'); /* find entry at end of path. */
  49. if (ptr == NULL)
  50. {
  51. rc = PHYSFS_enumerateFiles("/");
  52. ptr = buf;
  53. } /* if */
  54. else
  55. {
  56. *ptr = '\0';
  57. rc = PHYSFS_enumerateFiles(buf);
  58. *ptr = '/';
  59. ptr++; /* point past dirsep to entry itself. */
  60. } /* else */
  61. for (i = rc; *i != NULL; i++)
  62. {
  63. if (caseInsensitiveStringCompare(*i, ptr) == 0)
  64. {
  65. strcpy(ptr, *i); /* found a match. Overwrite with this case. */
  66. PHYSFS_freeList(rc);
  67. return(1);
  68. } /* if */
  69. } /* for */
  70. /* no match at all... */
  71. PHYSFS_freeList(rc);
  72. return(0);
  73. } /* locateOneElement */
  74. int PHYSFSEXT_locateCorrectCase(char *buf)
  75. {
  76. int rc;
  77. char *ptr;
  78. char *prevptr;
  79. while (*buf == '/') /* skip any '/' at start of string... */
  80. buf++;
  81. ptr = prevptr = buf;
  82. if (*ptr == '\0')
  83. return(0); /* Uh...I guess that's success. */
  84. while (ptr = strchr(ptr + 1, '/'))
  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 ... */