123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "physfs.h"
- #include "ignorecase.h"
- static int locateOneElement(char *buf)
- {
- char *ptr;
- char **rc;
- char **i;
- if (PHYSFS_exists(buf))
- return 1;
- ptr = strrchr(buf, '/');
- if (ptr == NULL)
- {
- rc = PHYSFS_enumerateFiles("/");
- ptr = buf;
- }
- else
- {
- *ptr = '\0';
- rc = PHYSFS_enumerateFiles(buf);
- *ptr = '/';
- ptr++;
- }
- if (rc != NULL)
- {
- for (i = rc; *i != NULL; i++)
- {
- if (PHYSFS_utf8stricmp(*i, ptr) == 0)
- {
- strcpy(ptr, *i);
- PHYSFS_freeList(rc);
- return 1;
- }
- }
- PHYSFS_freeList(rc);
- }
-
- return 0;
- }
- int PHYSFSEXT_locateCorrectCase(char *buf)
- {
- int rc;
- char *ptr;
- while (*buf == '/')
- buf++;
- ptr = buf;
- if (*ptr == '\0')
- return 0;
- while ( (ptr = strchr(ptr + 1, '/')) != NULL )
- {
- *ptr = '\0';
- rc = locateOneElement(buf);
- *ptr = '/';
- if (!rc)
- return -2;
- }
-
- return locateOneElement(buf) ? 0 : -1;
- }
- #ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
- int main(int argc, char **argv)
- {
- int rc;
- char buf[128];
- PHYSFS_File *f;
- if (!PHYSFS_init(argv[0]))
- {
- fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
- return 1;
- }
- if (!PHYSFS_addToSearchPath(".", 1))
- {
- fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
- PHYSFS_deinit();
- return 1;
- }
- if (!PHYSFS_setWriteDir("."))
- {
- fprintf(stderr, "PHYSFS_setWriteDir(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
- PHYSFS_deinit();
- return 1;
- }
- if (!PHYSFS_mkdir("/a/b/c"))
- {
- fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
- PHYSFS_deinit();
- return 1;
- }
- if (!PHYSFS_mkdir("/a/b/C"))
- {
- fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
- PHYSFS_deinit();
- return 1;
- }
- f = PHYSFS_openWrite("/a/b/c/x.txt");
- PHYSFS_close(f);
- if (f == NULL)
- {
- fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
- PHYSFS_deinit();
- return 1;
- }
- f = PHYSFS_openWrite("/a/b/C/X.txt");
- PHYSFS_close(f);
- if (f == NULL)
- {
- fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
- PHYSFS_deinit();
- return 1;
- }
- strcpy(buf, "/a/b/c/x.txt");
- rc = PHYSFSEXT_locateCorrectCase(buf);
- if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
- printf("test 1 failed\n");
- strcpy(buf, "/a/B/c/x.txt");
- rc = PHYSFSEXT_locateCorrectCase(buf);
- if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
- printf("test 2 failed\n");
- strcpy(buf, "/a/b/C/x.txt");
- rc = PHYSFSEXT_locateCorrectCase(buf);
- if ((rc != 0) || (strcmp(buf, "/a/b/C/X.txt") != 0))
- printf("test 3 failed\n");
- strcpy(buf, "/a/b/c/X.txt");
- rc = PHYSFSEXT_locateCorrectCase(buf);
- if ((rc != 0) || (strcmp(buf, "/a/b/c/x.txt") != 0))
- printf("test 4 failed\n");
- strcpy(buf, "/a/b/c/z.txt");
- rc = PHYSFSEXT_locateCorrectCase(buf);
- if ((rc != -1) || (strcmp(buf, "/a/b/c/z.txt") != 0))
- printf("test 5 failed\n");
- strcpy(buf, "/A/B/Z/z.txt");
- rc = PHYSFSEXT_locateCorrectCase(buf);
- if ((rc != -2) || (strcmp(buf, "/a/b/Z/z.txt") != 0))
- printf("test 6 failed\n");
- printf("Testing completed.\n");
- printf(" If no errors were reported, you're good to go.\n");
- PHYSFS_delete("/a/b/c/x.txt");
- PHYSFS_delete("/a/b/C/X.txt");
- PHYSFS_delete("/a/b/c");
- PHYSFS_delete("/a/b/C");
- PHYSFS_delete("/a/b");
- PHYSFS_delete("/a");
- PHYSFS_deinit();
- return 0;
- }
- #endif
|