ignorecase.c 5.5 KB

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