Skip to content

Latest commit

 

History

History
203 lines (169 loc) · 5.37 KB

ignorecase.c

File metadata and controls

203 lines (169 loc) · 5.37 KB
 
Mar 30, 2003
Mar 30, 2003
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** \file ignorecase.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "physfs.h"
#include "ignorecase.h"
/**
* Please see ignorecase.h for details.
*
* License: this code is public domain. I make no warranty that it is useful,
* correct, harmless, or environmentally safe.
*
* This particular file may be used however you like, including copying it
* verbatim into a closed-source project, exploiting it commercially, and
* removing any trace of my name from the source (although I hope you won't
* do that). I welcome enhancements and corrections to this file, but I do
Jul 20, 2003
Jul 20, 2003
21
22
* not require you to send me patches if you make changes. This code has
* NO WARRANTY.
Mar 30, 2003
Mar 30, 2003
23
*
Jul 20, 2003
Jul 20, 2003
24
* Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
Mar 11, 2007
Mar 11, 2007
25
* Please see LICENSE.txt in the root of the source tree.
Mar 30, 2003
Mar 30, 2003
26
27
28
29
30
31
32
33
34
35
36
*
* \author Ryan C. Gordon.
*/
static int locateOneElement(char *buf)
{
char *ptr;
char **rc;
char **i;
if (PHYSFS_exists(buf))
Jan 28, 2010
Jan 28, 2010
37
return 1; /* quick rejection: exists in current case. */
Mar 30, 2003
Mar 30, 2003
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
ptr = strrchr(buf, '/'); /* find entry at end of path. */
if (ptr == NULL)
{
rc = PHYSFS_enumerateFiles("/");
ptr = buf;
} /* if */
else
{
*ptr = '\0';
rc = PHYSFS_enumerateFiles(buf);
*ptr = '/';
ptr++; /* point past dirsep to entry itself. */
} /* else */
Mar 9, 2018
Mar 9, 2018
53
if (rc != NULL)
Mar 30, 2003
Mar 30, 2003
54
{
Mar 9, 2018
Mar 9, 2018
55
for (i = rc; *i != NULL; i++)
Mar 30, 2003
Mar 30, 2003
56
{
Mar 9, 2018
Mar 9, 2018
57
58
59
60
61
62
63
64
65
66
if (PHYSFS_utf8stricmp(*i, ptr) == 0)
{
strcpy(ptr, *i); /* found a match. Overwrite with this case. */
PHYSFS_freeList(rc);
return 1;
} /* if */
} /* for */
PHYSFS_freeList(rc);
} /* if */
Mar 30, 2003
Mar 30, 2003
67
68
/* no match at all... */
Jan 28, 2010
Jan 28, 2010
69
return 0;
Mar 30, 2003
Mar 30, 2003
70
71
72
73
74
75
76
77
78
79
80
} /* locateOneElement */
int PHYSFSEXT_locateCorrectCase(char *buf)
{
int rc;
char *ptr;
while (*buf == '/') /* skip any '/' at start of string... */
buf++;
Oct 6, 2016
Oct 6, 2016
81
ptr = buf;
Mar 30, 2003
Mar 30, 2003
82
if (*ptr == '\0')
Jan 28, 2010
Jan 28, 2010
83
return 0; /* Uh...I guess that's success. */
Mar 30, 2003
Mar 30, 2003
84
Nov 18, 2011
Nov 18, 2011
85
while ( (ptr = strchr(ptr + 1, '/')) != NULL )
Mar 30, 2003
Mar 30, 2003
86
87
88
89
90
{
*ptr = '\0'; /* block this path section off */
rc = locateOneElement(buf);
*ptr = '/'; /* restore path separator */
if (!rc)
Jan 28, 2010
Jan 28, 2010
91
return -2; /* missing element in path. */
Mar 30, 2003
Mar 30, 2003
92
93
94
} /* while */
/* check final element... */
Feb 24, 2010
Feb 24, 2010
95
return locateOneElement(buf) ? 0 : -1;
Mar 30, 2003
Mar 30, 2003
96
97
98
99
100
101
102
103
} /* PHYSFSEXT_locateCorrectCase */
#ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
int main(int argc, char **argv)
{
int rc;
char buf[128];
Sep 26, 2004
Sep 26, 2004
104
PHYSFS_File *f;
Mar 30, 2003
Mar 30, 2003
105
106
107
if (!PHYSFS_init(argv[0]))
{
Feb 20, 2018
Feb 20, 2018
108
fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Jan 28, 2010
Jan 28, 2010
109
return 1;
Mar 30, 2003
Mar 30, 2003
110
111
112
113
} /* if */
if (!PHYSFS_addToSearchPath(".", 1))
{
Feb 20, 2018
Feb 20, 2018
114
fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Mar 30, 2003
Mar 30, 2003
115
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
116
return 1;
Mar 30, 2003
Mar 30, 2003
117
118
119
120
} /* if */
if (!PHYSFS_setWriteDir("."))
{
Feb 20, 2018
Feb 20, 2018
121
fprintf(stderr, "PHYSFS_setWriteDir(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Mar 30, 2003
Mar 30, 2003
122
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
123
return 1;
Mar 30, 2003
Mar 30, 2003
124
125
126
127
} /* if */
if (!PHYSFS_mkdir("/a/b/c"))
{
Feb 20, 2018
Feb 20, 2018
128
fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Mar 30, 2003
Mar 30, 2003
129
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
130
return 1;
Mar 30, 2003
Mar 30, 2003
131
132
133
134
} /* if */
if (!PHYSFS_mkdir("/a/b/C"))
{
Feb 20, 2018
Feb 20, 2018
135
fprintf(stderr, "PHYSFS_mkdir(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Mar 30, 2003
Mar 30, 2003
136
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
137
return 1;
Mar 30, 2003
Mar 30, 2003
138
139
140
141
142
143
} /* if */
f = PHYSFS_openWrite("/a/b/c/x.txt");
PHYSFS_close(f);
if (f == NULL)
{
Feb 20, 2018
Feb 20, 2018
144
fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Mar 30, 2003
Mar 30, 2003
145
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
146
return 1;
Mar 30, 2003
Mar 30, 2003
147
148
149
150
151
152
} /* if */
f = PHYSFS_openWrite("/a/b/C/X.txt");
PHYSFS_close(f);
if (f == NULL)
{
Feb 20, 2018
Feb 20, 2018
153
fprintf(stderr, "PHYSFS_openWrite(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Mar 30, 2003
Mar 30, 2003
154
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
155
return 1;
Mar 30, 2003
Mar 30, 2003
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
} /* if */
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();
Jan 28, 2010
Jan 28, 2010
198
return 0;
Mar 30, 2003
Mar 30, 2003
199
200
201
202
} /* main */
#endif
/* end of ignorecase.c ... */