Debian Codegraph @

common/misc/ignorecase.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /** \file ignorecase.c */
8
9 #include <cstddef>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <optional>
15
16 #include "physfsx.h"
17 #include "physfs_list.h"
18 #include "ignorecase.h"
19
20 /**
21 * Please see ignorecase.h for details.
22 *
23 * License: this code is public domain. I make no warranty that it is useful,
24 * correct, harmless, or environmentally safe.
25 *
26 * This particular file may be used however you like, including copying it
27 * verbatim into a closed-source project, exploiting it commercially, and
28 * removing any trace of my name from the source (although I hope you won't
29 * do that). I welcome enhancements and corrections to this file, but I do
30 * not require you to send me patches if you make changes. This code has
31 * NO WARRANTY.
32 *
33 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
34 * Please see LICENSE in the root of the source tree.
35 *
36 * \author Ryan C. Gordon.
37 */
38
39 namespace dcx {
40
41 namespace {
42
43 /* I'm not screwing around with stricmp vs. strcasecmp... */
44 static std::optional<std::size_t> caseInsensitiveStringCompare(const char *x, const char *y)
45 {
46 const auto sx{x};
47 for (;;)
48 {
49 const auto ux = toupper(static_cast<unsigned>(*x));
50 const auto uy = toupper(static_cast<unsigned>(*y));
51 if (ux != uy)
52 return std::nullopt;
53 if (!ux)
54 return std::distance(sx, x);
55 x++;
56 y++;
57 }
58 } /* caseInsensitiveStringCompare */
59
60 static PHYSFSX_case_search_result locateOneElement(char *const sptr, char *const ptr, const char *buf)
61 {
62 if (PHYSFS_exists(buf))
63 return PHYSFSX_case_search_result::success; /* quick rejection: exists in current case. */
64
65 struct search_result : PHYSFSX_uncounted_list
66 {
67 search_result(char *ptr, const char *buf) :
68 PHYSFSX_uncounted_list(PHYSFS_enumerateFiles(ptr ? (*ptr = 0, buf) : "/"))
69 {
70 if (ptr)
71 *ptr = '/';
72 }
73 };
74 const search_result s{ptr, buf};
75 if (!s)
76 /* Failed to list the directory. No names are available, so there is
77 * no list to iterate.
78 */
79 return PHYSFSX_case_search_result::file_missing;
80 for (const auto i : s)
81 {
82 if (const auto o = caseInsensitiveStringCompare(i, sptr))
83 {
84 std::memcpy(sptr, i, *o); /* found a match. Overwrite with this case. */
85 return PHYSFSX_case_search_result::success;
86 } /* if */
87 } /* for */
88
89 /* no match at all... */
90 return PHYSFSX_case_search_result::file_missing;
91 } /* locateOneElement */
92
93 }
94
95 PHYSFSX_case_search_result PHYSFSEXT_locateCorrectCase(char *buf)
96 {
97 while (*buf == '/') /* skip any '/' at start of string... */
98 buf++;
99 if (!*buf)
100 return PHYSFSX_case_search_result::success; /* Uh...I guess that's success. */
101
102 char *prevptr = nullptr;
103 const auto step{[&prevptr, buf]{
104 return locateOneElement(prevptr ? prevptr + 1 : buf, prevptr, buf);
105 }};
106 for (auto ptr = buf; (ptr = strchr(ptr + 1, '/')); prevptr = ptr)
107 {
108 *ptr = '\0'; /* block this path section off */
109 const auto rc{step()};
110 *ptr = '/'; /* restore path separator */
111 if (rc != PHYSFSX_case_search_result::success)
112 return PHYSFSX_case_search_result::directory_missing; /* missing element in path. */
113 } /* while */
114
115 /* check final element... */
116 return step();
117 } /* PHYSFSEXT_locateCorrectCase */
118
119 }
120
121 #ifdef TEST_PHYSFSEXT_LOCATECORRECTCASE
122 #define con_printf(A,B,...) printf(B "\n", ##__VA_ARGS__)
123 int main(int argc, char **argv)
124 {
125 PHYSFSX_case_search_result rc;
126 char buf[128];
127 PHYSFS_File *f;
128
129 if (!PHYSFS_init(argv[0]))
130 {
131 con_printf(CON_CRITICAL, "PHYSFS_init(): %s", PHYSFS_getLastError());
132 return(1);
133 } /* if */
134
135 if (!PHYSFS_addToSearchPath(".", 1))
136 {
137 con_printf(CON_CRITICAL, "PHYSFS_addToSearchPath(): %s", PHYSFS_getLastError());
138 PHYSFS_deinit();
139 return(1);
140 } /* if */
141
142 if (!PHYSFS_setWriteDir("."))
143 {
144 con_printf(CON_CRITICAL, "PHYSFS_setWriteDir(): %s", PHYSFS_getLastError());
145 PHYSFS_deinit();
146 return(1);
147 } /* if */
148
149 if (!PHYSFS_mkdir("/a/b/c"))
150 {
151 con_printf(CON_CRITICAL, "PHYSFS_mkdir(): %s", PHYSFS_getLastError());
152 PHYSFS_deinit();
153 return(1);
154 } /* if */
155
156 if (!PHYSFS_mkdir("/a/b/C"))
157 {
158 con_printf(CON_CRITICAL, "PHYSFS_mkdir(): %s", PHYSFS_getLastError());
159 PHYSFS_deinit();
160 return(1);
161 } /* if */
162
163 f = PHYSFS_openWrite("/a/b/c/x.txt");
164 PHYSFS_close(f);
165 if (f == NULL)
166 {
167 con_printf(CON_CRITICAL, "PHYSFS_openWrite(): %s", PHYSFS_getLastError());
168 PHYSFS_deinit();
169 return(1);
170 } /* if */
171
172 f = PHYSFS_openWrite("/a/b/C/X.txt");
173 PHYSFS_close(f);
174 if (f == NULL)
175 {
176 con_printf(CON_CRITICAL, "PHYSFS_openWrite(): %s", PHYSFS_getLastError());
177 PHYSFS_deinit();
178 return(1);
179 } /* if */
180
181 strcpy(buf, "/a/b/c/x.txt");
182 rc = PHYSFSEXT_locateCorrectCase(buf);
183 if ((rc != PHYSFSX_case_search_result::success) || (strcmp(buf, "/a/b/c/x.txt") != 0))
184 con_printf(CON_DEBUG,"test 1 failed");
185
186 strcpy(buf, "/a/B/c/x.txt");
187 rc = PHYSFSEXT_locateCorrectCase(buf);
188 if ((rc != PHYSFSX_case_search_result::success) || (strcmp(buf, "/a/b/c/x.txt") != 0))
189 con_printf(CON_DEBUG,"test 2 failed");
190
191 strcpy(buf, "/a/b/C/x.txt");
192 rc = PHYSFSEXT_locateCorrectCase(buf);
193 if ((rc != PHYSFSX_case_search_result::success) || (strcmp(buf, "/a/b/C/X.txt") != 0))
194 con_printf(CON_DEBUG,"test 3 failed");
195
196 strcpy(buf, "/a/b/c/X.txt");
197 rc = PHYSFSEXT_locateCorrectCase(buf);
198 if ((rc != PHYSFSX_case_search_result::success) || (strcmp(buf, "/a/b/c/x.txt") != 0))
199 con_printf(CON_DEBUG,"test 4 failed");
200
201 strcpy(buf, "/a/b/c/z.txt");
202 rc = PHYSFSEXT_locateCorrectCase(buf);
203 if ((rc != PHYSFSX_case_search_result::file_missing) || (strcmp(buf, "/a/b/c/z.txt") != 0))
204 con_printf(CON_DEBUG,"test 5 failed");
205
206 strcpy(buf, "/A/B/Z/z.txt");
207 rc = PHYSFSEXT_locateCorrectCase(buf);
208 if ((rc != PHYSFSX_case_search_result::directory_missing) || (strcmp(buf, "/a/b/Z/z.txt") != 0))
209 con_printf(CON_DEBUG,"test 6 failed");
210
211 con_printf(CON_DEBUG,"Testing completed.");
212 con_printf(CON_DEBUG," If no errors were reported, you're good to go.");
213
214 PHYSFS_delete("/a/b/c/x.txt");
215 PHYSFS_delete("/a/b/C/X.txt");
216 PHYSFS_delete("/a/b/c");
217 PHYSFS_delete("/a/b/C");
218 PHYSFS_delete("/a/b");
219 PHYSFS_delete("/a");
220 PHYSFS_deinit();
221 return(0);
222 } /* main */
223 #endif
224
225 /* end of ignorecase.c ... */
226