VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/RTPathParse.cpp.h@ 93115

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: RTPathParse.cpp.h 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - RTPathParse - Code Template.
4 *
5 * This file included multiple times with different path style macros.
6 */
7
8/*
9 * Copyright (C) 2006-2022 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * The contents of this file may alternatively be used under the terms
20 * of the Common Development and Distribution License Version 1.0
21 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 * VirtualBox OSE distribution, in which case the provisions of the
23 * CDDL are applicable instead of those of the GPL.
24 *
25 * You may elect to license modified versions of this file under the
26 * terms and conditions of either the GPL or the CDDL or both.
27 */
28
29
30
31/**
32 * @copydoc RTPathParse
33 */
34static int RTPATH_STYLE_FN(rtPathParse)(const char *pszPath, PRTPATHPARSED pParsed, size_t cbParsed, uint32_t fFlags)
35{
36 /*
37 * Parse the root specification if present and initialize the parser state
38 * (keep it on the stack for speed).
39 */
40 uint32_t const cMaxComps = cbParsed < RT_UOFFSETOF(RTPATHPARSED, aComps[0xfff0])
41 ? (uint32_t)((cbParsed - RT_UOFFSETOF(RTPATHPARSED, aComps)) / sizeof(pParsed->aComps[0]))
42 : 0xfff0;
43 uint32_t idxComp = 0;
44 uint32_t cchPath;
45 uint32_t offCur;
46 uint16_t fProps;
47
48 if (RTPATH_IS_SLASH(pszPath[0]))
49 {
50 if (fFlags & RTPATH_STR_F_NO_START)
51 {
52 offCur = 1;
53 while (RTPATH_IS_SLASH(pszPath[offCur]))
54 offCur++;
55 if (!pszPath[offCur])
56 return VERR_PATH_ZERO_LENGTH;
57 fProps = RTPATH_PROP_RELATIVE | RTPATH_PROP_EXTRA_SLASHES;
58 cchPath = 0;
59 }
60#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
61 else if (RTPATH_IS_SLASH(pszPath[1]))
62 {
63 /* UNC - there are exactly two prefix slashes followed by a namespace
64 or computer name, which can be empty on windows. */
65 offCur = 2;
66 while (!RTPATH_IS_SLASH(pszPath[offCur]) && pszPath[offCur])
67 offCur++;
68
69 /* Special fun for windows. */
70 fProps = RTPATH_PROP_UNC | RTPATH_PROP_ABSOLUTE;
71 if ( offCur == 3
72 && (pszPath[2] == '.' || pszPath[2] == '?'))
73 fProps |= RTPATH_PROP_SPECIAL_UNC;
74
75 if (RTPATH_IS_SLASH(pszPath[offCur]))
76 {
77 fProps |= RTPATH_PROP_ROOT_SLASH;
78 offCur++;
79 }
80 cchPath = offCur;
81 }
82#endif
83 else
84 {
85#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
86 fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
87#else
88 fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
89#endif
90 offCur = 1;
91 cchPath = 1;
92 }
93 }
94#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
95 else if (RT_C_IS_ALPHA(pszPath[0]) && pszPath[1] == ':')
96 {
97 if (!RTPATH_IS_SLASH(pszPath[2]))
98 {
99 fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_RELATIVE;
100 offCur = 2;
101 }
102 else
103 {
104 fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
105 offCur = 3;
106 }
107 cchPath = offCur;
108 }
109#endif
110 else
111 {
112 fProps = RTPATH_PROP_RELATIVE;
113 offCur = 0;
114 cchPath = 0;
115 }
116
117 /* Add it to the component array . */
118 if (offCur && !(fFlags & RTPATH_STR_F_NO_START))
119 {
120 cchPath = offCur;
121 if (idxComp < cMaxComps)
122 {
123 pParsed->aComps[idxComp].off = 0;
124 pParsed->aComps[idxComp].cch = offCur;
125 }
126 idxComp++;
127
128 /* Skip unnecessary slashes following the root-spec. */
129 if (RTPATH_IS_SLASH(pszPath[offCur]))
130 {
131 fProps |= RTPATH_PROP_EXTRA_SLASHES;
132 do
133 offCur++;
134 while (RTPATH_IS_SLASH(pszPath[offCur]));
135 }
136 }
137
138 /*
139 * Parse the rest.
140 */
141 if (pszPath[offCur])
142 {
143 for (;;)
144 {
145 Assert(!RTPATH_IS_SLASH(pszPath[offCur]));
146
147 /* Find the end of the component. */
148 uint32_t offStart = offCur;
149 char ch;
150 while ((ch = pszPath[offCur]) != '\0' && !RTPATH_IS_SLASH(ch))
151 offCur++;
152 if (offCur >= _64K)
153 return VERR_FILENAME_TOO_LONG;
154
155 /* Add it. */
156 uint32_t cchComp = offCur - offStart;
157 if (idxComp < cMaxComps)
158 {
159 pParsed->aComps[idxComp].off = offStart;
160 pParsed->aComps[idxComp].cch = cchComp;
161 }
162 idxComp++;
163 cchPath += cchComp;
164
165 /* Look for '.' and '..' references. */
166 if (cchComp == 1 && pszPath[offCur - 1] == '.')
167 fProps |= RTPATH_PROP_DOT_REFS;
168 else if (cchComp == 2 && pszPath[offCur - 1] == '.' && pszPath[offCur - 2] == '.')
169 {
170 fProps &= ~RTPATH_PROP_ABSOLUTE;
171 fProps |= RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_RELATIVE;
172 }
173
174 /* Skip unnecessary slashes. Leave ch unchanged! */
175 char ch2 = ch;
176 if (ch2)
177 {
178 ch2 = pszPath[++offCur];
179 if (RTPATH_IS_SLASH(ch2))
180 {
181 fProps |= RTPATH_PROP_EXTRA_SLASHES;
182 do
183 ch2 = pszPath[++offCur];
184 while (RTPATH_IS_SLASH(ch2));
185 }
186 }
187
188 /* The end? */
189 if (ch2 == '\0')
190 {
191 pParsed->offSuffix = offCur;
192 pParsed->cchSuffix = 0;
193 if (ch)
194 {
195 if (!(fFlags & RTPATH_STR_F_NO_END))
196 {
197 fProps |= RTPATH_PROP_DIR_SLASH; /* (not counted in component, but in cchPath) */
198 cchPath++;
199 }
200 else
201 fProps |= RTPATH_PROP_EXTRA_SLASHES;
202 }
203 else if (!(fFlags & RTPATH_STR_F_NO_END))
204 {
205 fProps |= RTPATH_PROP_FILENAME;
206
207 /* Look for a suffix: */
208 uint32_t offSuffix = offStart + cchComp;
209 while (--offSuffix > offStart)
210 if (pszPath[offSuffix] == '.')
211 {
212 uint32_t cchSuffix = offStart + cchComp - offSuffix;
213 if (cchSuffix > 1)
214 {
215 pParsed->cchSuffix = cchSuffix;
216 pParsed->offSuffix = offSuffix;
217 fProps |= RTPATH_PROP_SUFFIX;
218 }
219 break;
220 }
221 }
222 break;
223 }
224
225 /* No, not the end. Account for an separator before we restart the loop. */
226 cchPath += sizeof(RTPATH_SLASH_STR) - 1;
227 }
228 }
229 else
230 {
231 pParsed->offSuffix = offCur;
232 pParsed->cchSuffix = 0;
233 }
234 if (offCur >= _64K)
235 return VERR_FILENAME_TOO_LONG;
236
237 /*
238 * Store the remainder of the state and we're done.
239 */
240 pParsed->fProps = fProps;
241 pParsed->cchPath = cchPath;
242 pParsed->cComps = idxComp;
243
244 return idxComp <= cMaxComps ? VINF_SUCCESS : VERR_BUFFER_OVERFLOW;
245}
246
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette