1 /** \file ignorecase.c */
9 #include "ignorecase.h"
12 * Please see ignorecase.h for details.
14 * License: this code is public domain. I make no warranty that it is useful,
15 * correct, harmless, or environmentally safe.
17 * This particular file may be used however you like, including copying it
18 * verbatim into a closed-source project, exploiting it commercially, and
19 * removing any trace of my name from the source (although I hope you won't
20 * do that). I welcome enhancements and corrections to this file, but I do
21 * not require you to send me patches if you make changes. This code has
24 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
25 * Please see LICENSE in the root of the source tree.
27 * \author Ryan C. Gordon.
30 /* I'm not screwing around with stricmp vs. strcasecmp... */
31 static int caseInsensitiveStringCompare(const char *x, const char *y)
36 ux = toupper((int) *x);
37 uy = toupper((int) *y);
39 return((ux > uy) ? 1 : -1);
42 } while ((ux) && (uy));
45 } /* caseInsensitiveStringCompare */
48 static int locateOneElement(char *buf)
54 if (PHYSFS_exists(buf))
55 return(1); /* quick rejection: exists in current case. */
57 ptr = strrchr(buf, '/'); /* find entry at end of path. */
60 rc = PHYSFS_enumerateFiles("/");
66 rc = PHYSFS_enumerateFiles(buf);
68 ptr++; /* point past dirsep to entry itself. */
71 for (i = rc; *i != NULL; i++)
73 if (caseInsensitiveStringCompare(*i, ptr) == 0)
75 strcpy(ptr, *i); /* found a match. Overwrite with this case. */
81 /* no match at all... */
84 } /* locateOneElement */
87 int PHYSFSEXT_locateCorrectCase(char *buf)
93 while (*buf == '/') /* skip any '/' at start of string... */
98 return(0); /* Uh...I guess that's success. */
100 while ((ptr = strchr(ptr + 1, '/')))
102 *ptr = '\0'; /* block this path section off */
103 rc = locateOneElement(buf);
104 *ptr = '/'; /* restore path separator */
106 return(-2); /* missing element in path. */
109 /* check final element... */
110 return(locateOneElement(buf) ? 0 : -1);
111 } /* PHYSFSEXT_locateCorrectCase */
114 #ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
115 int main(int argc, char **argv)
121 if (!PHYSFS_init(argv[0]))
123 fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getLastError());
127 if (!PHYSFS_addToSearchPath(".", 1))
129 fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getLastError());
134 if (!PHYSFS_setWriteDir("."))
136 fprintf(stderr, "PHYSFS_setWriteDir(): %s\n", PHYSFS_getLastError());
141 if (!PHYSFS_mkdir("/a/b/c"))
143 fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
148 if (!PHYSFS_mkdir("/a/b/C"))
150 fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
155 f = PHYSFS_openWrite("/a/b/c/x.txt");
159 fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
164 f = PHYSFS_openWrite("/a/b/C/X.txt");
168 fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
173 strcpy(buf, "/a/b/c/x.txt");
174 rc = PHYSFSEXT_locateCorrectCase(buf);
175 if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
176 printf("test 1 failed\n");
178 strcpy(buf, "/a/B/c/x.txt");
179 rc = PHYSFSEXT_locateCorrectCase(buf);
180 if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
181 printf("test 2 failed\n");
183 strcpy(buf, "/a/b/C/x.txt");
184 rc = PHYSFSEXT_locateCorrectCase(buf);
185 if ((rc != 0) || (strcmp(buf, "/a/b/C/X.txt") != 0))
186 printf("test 3 failed\n");
188 strcpy(buf, "/a/b/c/X.txt");
189 rc = PHYSFSEXT_locateCorrectCase(buf);
190 if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
191 printf("test 4 failed\n");
193 strcpy(buf, "/a/b/c/z.txt");
194 rc = PHYSFSEXT_locateCorrectCase(buf);
195 if ((rc != -1) || (strcmp(buf, "/a/b/c/z.txt") != 0))
196 printf("test 5 failed\n");
198 strcpy(buf, "/A/B/Z/z.txt");
199 rc = PHYSFSEXT_locateCorrectCase(buf);
200 if ((rc != -2) || (strcmp(buf, "/a/b/Z/z.txt") != 0))
201 printf("test 6 failed\n");
203 printf("Testing completed.\n");
204 printf(" If no errors were reported, you're good to go.\n");
206 PHYSFS_delete("/a/b/c/x.txt");
207 PHYSFS_delete("/a/b/C/X.txt");
208 PHYSFS_delete("/a/b/c");
209 PHYSFS_delete("/a/b/C");
210 PHYSFS_delete("/a/b");
217 /* end of ignorecase.c ... */