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.txt in the root of the source tree.
27 * \author Ryan C. Gordon.
30 /* I'm not screwing around with stricmp vs. strcasecmp... */
31 /* !!! FIXME: this will NOT work with UTF-8 strings in physfs2.0 */
32 static int caseInsensitiveStringCompare(const char *x, const char *y)
37 ux = toupper((int) *x);
38 uy = toupper((int) *y);
40 return((ux > uy) ? 1 : -1);
43 } while ((ux) && (uy));
46 } /* caseInsensitiveStringCompare */
49 static int locateOneElement(char *buf)
55 if (PHYSFS_exists(buf))
56 return(1); /* quick rejection: exists in current case. */
58 ptr = strrchr(buf, '/'); /* find entry at end of path. */
61 rc = PHYSFS_enumerateFiles("/");
67 rc = PHYSFS_enumerateFiles(buf);
69 ptr++; /* point past dirsep to entry itself. */
72 for (i = rc; *i != NULL; i++)
74 if (caseInsensitiveStringCompare(*i, ptr) == 0)
76 strcpy(ptr, *i); /* found a match. Overwrite with this case. */
82 /* no match at all... */
85 } /* locateOneElement */
88 int PHYSFSEXT_locateCorrectCase(char *buf)
94 while (*buf == '/') /* skip any '/' at start of string... */
99 return(0); /* Uh...I guess that's success. */
101 while ( (ptr = strchr(ptr + 1, '/')) != NULL )
103 *ptr = '\0'; /* block this path section off */
104 rc = locateOneElement(buf);
105 *ptr = '/'; /* restore path separator */
107 return(-2); /* missing element in path. */
110 /* check final element... */
111 return(locateOneElement(buf) ? 0 : -1);
112 } /* PHYSFSEXT_locateCorrectCase */
115 #ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
116 int main(int argc, char **argv)
122 if (!PHYSFS_init(argv[0]))
124 fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getLastError());
128 if (!PHYSFS_addToSearchPath(".", 1))
130 fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getLastError());
135 if (!PHYSFS_setWriteDir("."))
137 fprintf(stderr, "PHYSFS_setWriteDir(): %s\n", PHYSFS_getLastError());
142 if (!PHYSFS_mkdir("/a/b/c"))
144 fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
149 if (!PHYSFS_mkdir("/a/b/C"))
151 fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getLastError());
156 f = PHYSFS_openWrite("/a/b/c/x.txt");
160 fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
165 f = PHYSFS_openWrite("/a/b/C/X.txt");
169 fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getLastError());
174 strcpy(buf, "/a/b/c/x.txt");
175 rc = PHYSFSEXT_locateCorrectCase(buf);
176 if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
177 printf("test 1 failed\n");
179 strcpy(buf, "/a/B/c/x.txt");
180 rc = PHYSFSEXT_locateCorrectCase(buf);
181 if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
182 printf("test 2 failed\n");
184 strcpy(buf, "/a/b/C/x.txt");
185 rc = PHYSFSEXT_locateCorrectCase(buf);
186 if ((rc != 0) || (strcmp(buf, "/a/b/C/X.txt") != 0))
187 printf("test 3 failed\n");
189 strcpy(buf, "/a/b/c/X.txt");
190 rc = PHYSFSEXT_locateCorrectCase(buf);
191 if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
192 printf("test 4 failed\n");
194 strcpy(buf, "/a/b/c/z.txt");
195 rc = PHYSFSEXT_locateCorrectCase(buf);
196 if ((rc != -1) || (strcmp(buf, "/a/b/c/z.txt") != 0))
197 printf("test 5 failed\n");
199 strcpy(buf, "/A/B/Z/z.txt");
200 rc = PHYSFSEXT_locateCorrectCase(buf);
201 if ((rc != -2) || (strcmp(buf, "/a/b/Z/z.txt") != 0))
202 printf("test 6 failed\n");
204 printf("Testing completed.\n");
205 printf(" If no errors were reported, you're good to go.\n");
207 PHYSFS_delete("/a/b/c/x.txt");
208 PHYSFS_delete("/a/b/C/X.txt");
209 PHYSFS_delete("/a/b/c");
210 PHYSFS_delete("/a/b/C");
211 PHYSFS_delete("/a/b");
218 /* end of ignorecase.c ... */