Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : /*
24 : * helper functions for path strings.
25 : */
26 :
27 : #include "precompiled.h"
28 :
29 : #include "path.h"
30 :
31 : #include "lib/code_annotation.h"
32 :
33 : #include <climits>
34 : #include <utility>
35 :
36 : static const StatusDefinition pathStatusDefinitions[] = {
37 : { ERR::PATH_CHARACTER_ILLEGAL, L"illegal path character" },
38 : { ERR::PATH_CHARACTER_UNSAFE, L"unsafe path character" },
39 : { ERR::PATH_NOT_FOUND, L"path not found" },
40 : { ERR::PATH_MIXED_SEPARATORS, L"path contains both slash and backslash separators" }
41 : };
42 : STATUS_ADD_DEFINITIONS(pathStatusDefinitions);
43 :
44 :
45 7 : static bool path_is_dir_sep(wchar_t c)
46 : {
47 7 : if(c == '/' || c == '\\')
48 : return true;
49 : return false;
50 : }
51 :
52 : // is s2 a subpath of s1, or vice versa?
53 : // (equal counts as subpath)
54 9 : bool path_is_subpath(const wchar_t* s1, const wchar_t* s2)
55 : {
56 : // make sure s1 is the shorter string
57 9 : if(wcslen(s1) > wcslen(s2))
58 4 : std::swap(s1, s2);
59 :
60 : wchar_t c1 = 0, last_c1, c2;
61 36 : for(;;)
62 : {
63 36 : last_c1 = c1;
64 36 : c1 = *s1++, c2 = *s2++;
65 :
66 : // end of s1 reached:
67 36 : if(c1 == '\0')
68 : {
69 : // s1 matched s2 up until:
70 10 : if((c2 == '\0') || // its end (i.e. they're equal length) OR
71 6 : path_is_dir_sep(c2) || // start of next component OR
72 3 : path_is_dir_sep(last_c1)) // ", but both have a trailing slash
73 : // => is subpath
74 : return true;
75 : }
76 :
77 : // mismatch => is not subpath
78 31 : if(c1 != c2)
79 : return false;
80 : }
81 : }
82 :
83 :
84 : //-----------------------------------------------------------------------------
85 :
86 : // return pointer to the name component within path (i.e. skips over all
87 : // characters up to the last dir separator, if any).
88 8883 : const wchar_t* path_name_only(const wchar_t* path)
89 : {
90 8883 : const wchar_t* slash1 = wcsrchr(path, '/');
91 8883 : const wchar_t* slash2 = wcsrchr(path, '\\');
92 : // neither present, it's a filename only
93 8883 : if(!slash1 && !slash2)
94 : return path;
95 :
96 : // return name, i.e. component after the last slash
97 8881 : const wchar_t* name = std::max(slash1, slash2)+1;
98 8881 : return name;
99 : }
100 :
101 :
102 0 : /*static*/ Status Path::Validate(String::value_type c)
103 : {
104 0 : if(c < 32)
105 : return ERR::PATH_CHARACTER_UNSAFE;
106 :
107 : #if !OS_WIN
108 0 : if(c >= UCHAR_MAX)
109 : return ERR::PATH_CHARACTER_UNSAFE;
110 : #endif
111 :
112 0 : switch(c)
113 : {
114 : case '\\':
115 : case '/':
116 : case ':':
117 : case '"':
118 : case '?':
119 : case '*':
120 : case '<':
121 : case '>':
122 : case '|':
123 : case '^':
124 : return ERR::PATH_CHARACTER_ILLEGAL;
125 :
126 0 : default:
127 0 : return INFO::OK;
128 : }
129 : }
|