1 | /* $Id: RTPathParseSimple.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathParseSimple
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/path.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/ctype.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | RTDECL(size_t) RTPathParseSimple(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff)
|
---|
49 | {
|
---|
50 | /*
|
---|
51 | * First deal with the root as it is always more fun that you'd think.
|
---|
52 | */
|
---|
53 | const char *psz = pszPath;
|
---|
54 | size_t cchRoot = 0;
|
---|
55 |
|
---|
56 | #if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
|
---|
57 | if (RT_C_IS_ALPHA(*psz) && RTPATH_IS_VOLSEP(psz[1]))
|
---|
58 | {
|
---|
59 | /* Volume specifier. */
|
---|
60 | cchRoot = 2;
|
---|
61 | psz += 2;
|
---|
62 | }
|
---|
63 | else if (RTPATH_IS_SLASH(*psz) && RTPATH_IS_SLASH(psz[1]))
|
---|
64 | {
|
---|
65 | /* UNC - there are exactly two prefix slashes followed by a namespace
|
---|
66 | or computer name, which can be empty on windows. */
|
---|
67 | cchRoot = 2;
|
---|
68 | psz += 2;
|
---|
69 | while (!RTPATH_IS_SLASH(*psz) && *psz)
|
---|
70 | {
|
---|
71 | cchRoot++;
|
---|
72 | psz++;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | #endif
|
---|
76 | while (RTPATH_IS_SLASH(*psz))
|
---|
77 | {
|
---|
78 | cchRoot++;
|
---|
79 | psz++;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Do the remainder.
|
---|
84 | */
|
---|
85 | const char *pszName = psz;
|
---|
86 | const char *pszLastDot = NULL;
|
---|
87 | for (;; psz++)
|
---|
88 | {
|
---|
89 | switch (*psz)
|
---|
90 | {
|
---|
91 | default:
|
---|
92 | break;
|
---|
93 |
|
---|
94 | /* handle separators. */
|
---|
95 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
96 | case '\\':
|
---|
97 | #endif
|
---|
98 | case '/':
|
---|
99 | pszName = psz + 1;
|
---|
100 | pszLastDot = NULL;
|
---|
101 | break;
|
---|
102 |
|
---|
103 | case '.':
|
---|
104 | pszLastDot = psz;
|
---|
105 | break;
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * The end. Complete the results.
|
---|
109 | */
|
---|
110 | case '\0':
|
---|
111 | {
|
---|
112 | ssize_t offName = *pszName != '\0' ? pszName - pszPath : -1;
|
---|
113 | if (poffName)
|
---|
114 | *poffName = offName;
|
---|
115 |
|
---|
116 | if (poffSuff)
|
---|
117 | {
|
---|
118 | ssize_t offSuff = -1;
|
---|
119 | if ( pszLastDot
|
---|
120 | && pszLastDot != pszName
|
---|
121 | && pszLastDot[1] != '\0')
|
---|
122 | {
|
---|
123 | offSuff = pszLastDot - pszPath;
|
---|
124 | Assert(offSuff > offName);
|
---|
125 | }
|
---|
126 | *poffSuff = offSuff;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (pcchDir)
|
---|
130 | {
|
---|
131 | size_t cch = offName < 0 ? psz - pszPath : offName - 1 < (ssize_t)cchRoot ? cchRoot : offName - 1;
|
---|
132 | while (cch > cchRoot && RTPATH_IS_SLASH(pszPath[cch - 1]))
|
---|
133 | cch--;
|
---|
134 | *pcchDir = cch;
|
---|
135 | }
|
---|
136 |
|
---|
137 | return psz - pszPath;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* will never get here */
|
---|
143 | }
|
---|
144 |
|
---|