1 | /* $Id: RTPathParsedReassemble.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathParsedReassemble.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2022 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/errcore.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | RTDECL(int) RTPathParsedReassemble(const char *pszSrcPath, PRTPATHPARSED pParsed, uint32_t fFlags,
|
---|
50 | char *pszDstPath, size_t cbDstPath)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * Input validation.
|
---|
54 | */
|
---|
55 | AssertPtrReturn(pszSrcPath, VERR_INVALID_POINTER);
|
---|
56 | AssertPtrReturn(pParsed, VERR_INVALID_POINTER);
|
---|
57 | AssertReturn(pParsed->cComps > 0, VERR_INVALID_PARAMETER);
|
---|
58 | AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0) && !(fFlags & RTPATH_STR_F_MIDDLE), VERR_INVALID_FLAGS);
|
---|
59 | AssertPtrReturn(pszDstPath, VERR_INVALID_POINTER);
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Recalculate the length.
|
---|
63 | */
|
---|
64 | uint32_t const cComps = pParsed->cComps;
|
---|
65 | uint32_t idxComp = 0;
|
---|
66 | uint32_t cchPath = 0;
|
---|
67 | if (RTPATH_PROP_HAS_ROOT_SPEC(pParsed->fProps))
|
---|
68 | {
|
---|
69 | cchPath = pParsed->aComps[0].cch;
|
---|
70 | idxComp++;
|
---|
71 | }
|
---|
72 | bool fNeedSlash = false;
|
---|
73 | while (idxComp < cComps)
|
---|
74 | {
|
---|
75 | uint32_t const cchComp = pParsed->aComps[idxComp].cch;
|
---|
76 | idxComp++;
|
---|
77 | if (cchComp > 0)
|
---|
78 | {
|
---|
79 | cchPath += cchComp + fNeedSlash;
|
---|
80 | fNeedSlash = true;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | if ((pParsed->fProps & RTPATH_PROP_DIR_SLASH) && fNeedSlash)
|
---|
84 | cchPath += 1;
|
---|
85 | pParsed->cchPath = cchPath;
|
---|
86 | if (cbDstPath > cchPath)
|
---|
87 | { /* likely */ }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | if (cbDstPath)
|
---|
91 | *pszDstPath = '\0';
|
---|
92 | return VERR_BUFFER_OVERFLOW;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Figure which slash to use.
|
---|
97 | */
|
---|
98 | char chSlash;
|
---|
99 | switch (fFlags & RTPATH_STR_F_STYLE_MASK)
|
---|
100 | {
|
---|
101 | case RTPATH_STR_F_STYLE_HOST:
|
---|
102 | chSlash = RTPATH_SLASH;
|
---|
103 | break;
|
---|
104 |
|
---|
105 | case RTPATH_STR_F_STYLE_DOS:
|
---|
106 | chSlash = '\\';
|
---|
107 | break;
|
---|
108 |
|
---|
109 | case RTPATH_STR_F_STYLE_UNIX:
|
---|
110 | chSlash = '/';
|
---|
111 | break;
|
---|
112 |
|
---|
113 | default:
|
---|
114 | AssertFailedReturn(VERR_INVALID_FLAGS); /* impossible */
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Do the joining.
|
---|
119 | * Note! Using memmove here as we want to support pszSrcPath == pszDstPath.
|
---|
120 | */
|
---|
121 | char *pszDst = pszDstPath;
|
---|
122 | idxComp = 0;
|
---|
123 | fNeedSlash = false;
|
---|
124 |
|
---|
125 | if (RTPATH_PROP_HAS_ROOT_SPEC(pParsed->fProps))
|
---|
126 | {
|
---|
127 | uint32_t cchComp = pParsed->aComps[0].cch;
|
---|
128 | memmove(pszDst, &pszSrcPath[pParsed->aComps[0].off], cchComp);
|
---|
129 |
|
---|
130 | /* fix the slashes (harmless for unix style) */
|
---|
131 | char chOtherSlash = chSlash == '\\' ? '/' : '\\';
|
---|
132 | while (cchComp-- > 0)
|
---|
133 | {
|
---|
134 | if (*pszDst == chOtherSlash)
|
---|
135 | *pszDst = chSlash;
|
---|
136 | pszDst++;
|
---|
137 | }
|
---|
138 | idxComp = 1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | while (idxComp < cComps)
|
---|
142 | {
|
---|
143 | uint32_t const cchComp = pParsed->aComps[idxComp].cch;
|
---|
144 | if (cchComp > 0)
|
---|
145 | {
|
---|
146 | if (fNeedSlash)
|
---|
147 | *pszDst++ = chSlash;
|
---|
148 | fNeedSlash = true;
|
---|
149 | memmove(pszDst, &pszSrcPath[pParsed->aComps[idxComp].off], cchComp);
|
---|
150 | pszDst += cchComp;
|
---|
151 | }
|
---|
152 | idxComp++;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if ((pParsed->fProps & RTPATH_PROP_DIR_SLASH) && fNeedSlash)
|
---|
156 | *pszDst++ = chSlash;
|
---|
157 | *pszDst = '\0';
|
---|
158 |
|
---|
159 | return VINF_SUCCESS;
|
---|
160 | }
|
---|
161 |
|
---|