🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.2 KiB

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