VirtualBox

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

Last change on this file since 45390 was 45389, checked in by vboxsync, 12 years ago

IPRT: A Study in Paths - Chapter 1: The cross platform parser.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: RTPathParse.cpp.h 45389 2013-04-07 16:11:30Z 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-2013 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 ? (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 & RTPATHPARSE_FLAGS_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#ifdef RTPATH_STYLE_DOS
61 else if ( RTPATH_IS_SLASH(pszPath[1])
62 && !RTPATH_IS_SLASH(pszPath[2])
63 && pszPath[2])
64 {
65 /* UNC - skip to the end of the potential namespace or computer name. */
66 offCur = 2;
67 while (!RTPATH_IS_SLASH(pszPath[offCur]) && pszPath[offCur])
68 offCur++;
69
70 /* If there is another slash, we considered it a valid UNC path, if
71 not it's just a root path with an extra slash thrown in. */
72 if (RTPATH_IS_SLASH(pszPath[offCur]))
73 {
74 fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_UNC | RTPATH_PROP_ABSOLUTE;
75 offCur++;
76 }
77 else
78 {
79 fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
80 offCur = 1;
81 }
82 }
83#endif
84 else
85 {
86#ifdef RTPATH_STYLE_DOS
87 fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
88#else
89 fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
90#endif
91 offCur = 1;
92 }
93 }
94#ifdef RTPATH_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 }
108#endif
109 else
110 {
111 fProps = RTPATH_PROP_RELATIVE;
112 offCur = 0;
113 cchPath = 0;
114 }
115
116 /* Add it to the component array . */
117 if (offCur && !(fFlags & RTPATHPARSE_FLAGS_NO_START))
118 {
119 cchPath = offCur;
120 if (idxComp < cMaxComps)
121 {
122 pParsed->aComps[idxComp].off = 0;
123 pParsed->aComps[idxComp].cch = offCur;
124 }
125 idxComp++;
126
127 /* Skip unnecessary slashes following the root-spec. */
128 if (RTPATH_IS_SLASH(pszPath[offCur]))
129 {
130 fProps |= RTPATH_PROP_EXTRA_SLASHES;
131 do
132 offCur++;
133 while (RTPATH_IS_SLASH(pszPath[offCur]));
134 }
135 }
136
137 /*
138 * Parse the rest.
139 */
140 if (pszPath[offCur])
141 {
142 for (;;)
143 {
144 Assert(!RTPATH_IS_SLASH(pszPath[offCur]));
145
146 /* Find the end of the component. */
147 uint32_t offStart = offCur;
148 char ch;
149 while ((ch = pszPath[offCur]) != '\0' && !RTPATH_IS_SLASH(ch))
150 offCur++;
151 if (offCur >= _64K)
152 return VERR_FILENAME_TOO_LONG;
153
154 /* Add it. */
155 uint32_t cchComp = offCur - offStart;
156 if (idxComp < cMaxComps)
157 {
158 pParsed->aComps[idxComp].off = offStart;
159 pParsed->aComps[idxComp].cch = cchComp;
160 }
161 idxComp++;
162 cchPath += cchComp;
163
164 /* Look for '.' and '..' references. */
165 if (cchComp == 1 && pszPath[offCur - 1] == '.')
166 fProps |= RTPATH_PROP_DOT_REFS;
167 else if (cchComp == 2 && pszPath[offCur - 1] == '.' && pszPath[offCur - 2] == '.')
168 {
169 fProps &= ~RTPATH_PROP_ABSOLUTE;
170 fProps |= RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_RELATIVE;
171 }
172
173 /* Skip unnecessary slashes. Leave ch unchanged! */
174 char ch2 = ch;
175 if (ch2)
176 {
177 ch2 = pszPath[++offCur];
178 if (RTPATH_IS_SLASH(ch2))
179 {
180 fProps |= RTPATH_PROP_EXTRA_SLASHES;
181 do
182 ch2 = pszPath[++offCur];
183 while (RTPATH_IS_SLASH(ch2));
184 }
185 }
186
187 /* The end? */
188 if (ch2 == '\0')
189 {
190 pParsed->offSuffix = offCur;
191 pParsed->cchSuffix = 0;
192 if (ch)
193 {
194 if (!(fFlags & RTPATHPARSE_FLAGS_NO_END))
195 fProps |= RTPATH_PROP_DIR_SLASH; /* (not counted) */
196 else
197 fProps |= RTPATH_PROP_EXTRA_SLASHES;
198 }
199 else if (!(fFlags & RTPATHPARSE_FLAGS_NO_END))
200 {
201 fProps |= RTPATH_PROP_FILENAME;
202
203 /* look for an ? */
204 uint16_t cDots = 0;
205 uint32_t offSuffix = offStart + cchComp;
206 while (offSuffix-- > offStart)
207 if (pszPath[offSuffix] == '.')
208 {
209 uint32_t cchSuffix = offStart + cchComp - offSuffix;
210 if (cchSuffix > 1 && offStart != offSuffix)
211 {
212 pParsed->cchSuffix = cchSuffix;
213 pParsed->offSuffix = offSuffix;
214 fProps |= RTPATH_PROP_SUFFIX;
215 }
216 break;
217 }
218 }
219 break;
220 }
221
222 /* No, not the end. Account for an separator before we restart the loop. */
223 cchPath += sizeof(RTPATH_SLASH_STR) - 1;
224 }
225 }
226 else
227 {
228 pParsed->offSuffix = offCur;
229 pParsed->cchSuffix = 0;
230 }
231 if (offCur >= _64K)
232 return VERR_FILENAME_TOO_LONG;
233
234 /*
235 * Store the remainder of the state and we're done.
236 */
237 pParsed->fProps = fProps;
238 pParsed->cchPath = cchPath;
239 pParsed->cComps = idxComp;
240
241 return idxComp <= cMaxComps ? VINF_SUCCESS : VERR_BUFFER_OVERFLOW;
242}
243
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