1 | /* $Id: RTPathParsedReassemble.cpp 78048 2019-04-09 01:21:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathParsedReassemble.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/path.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | RTDECL(int) RTPathParsedReassemble(const char *pszSrcPath, PRTPATHPARSED pParsed, uint32_t fFlags,
|
---|
40 | char *pszDstPath, size_t cbDstPath)
|
---|
41 | {
|
---|
42 | /*
|
---|
43 | * Input validation.
|
---|
44 | */
|
---|
45 | AssertPtrReturn(pszSrcPath, VERR_INVALID_POINTER);
|
---|
46 | AssertPtrReturn(pParsed, VERR_INVALID_POINTER);
|
---|
47 | AssertReturn(pParsed->cComps > 0, VERR_INVALID_PARAMETER);
|
---|
48 | AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0) && !(fFlags & RTPATH_STR_F_MIDDLE), VERR_INVALID_FLAGS);
|
---|
49 | AssertPtrReturn(pszDstPath, VERR_INVALID_POINTER);
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * Recalculate the length.
|
---|
53 | */
|
---|
54 | uint32_t const cComps = pParsed->cComps;
|
---|
55 | uint32_t idxComp = 0;
|
---|
56 | uint32_t cchPath = 0;
|
---|
57 | if (RTPATH_PROP_HAS_ROOT_SPEC(pParsed->fProps))
|
---|
58 | {
|
---|
59 | cchPath = pParsed->aComps[0].cch;
|
---|
60 | idxComp++;
|
---|
61 | }
|
---|
62 | bool fNeedSlash = false;
|
---|
63 | while (idxComp < cComps)
|
---|
64 | {
|
---|
65 | uint32_t const cchComp = pParsed->aComps[idxComp].cch;
|
---|
66 | idxComp++;
|
---|
67 | if (cchComp > 0)
|
---|
68 | {
|
---|
69 | cchPath += cchComp + fNeedSlash;
|
---|
70 | fNeedSlash = true;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | if ((pParsed->fProps & RTPATH_PROP_DIR_SLASH) && fNeedSlash)
|
---|
74 | cchPath += 1;
|
---|
75 | pParsed->cchPath = cchPath;
|
---|
76 | if (cbDstPath > cchPath)
|
---|
77 | { /* likely */ }
|
---|
78 | else
|
---|
79 | {
|
---|
80 | if (cbDstPath)
|
---|
81 | *pszDstPath = '\0';
|
---|
82 | return VERR_BUFFER_OVERFLOW;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Figure which slash to use.
|
---|
87 | */
|
---|
88 | char chSlash;
|
---|
89 | switch (fFlags & RTPATH_STR_F_STYLE_MASK)
|
---|
90 | {
|
---|
91 | case RTPATH_STR_F_STYLE_HOST:
|
---|
92 | chSlash = RTPATH_SLASH;
|
---|
93 | break;
|
---|
94 |
|
---|
95 | case RTPATH_STR_F_STYLE_DOS:
|
---|
96 | chSlash = '\\';
|
---|
97 | break;
|
---|
98 |
|
---|
99 | case RTPATH_STR_F_STYLE_UNIX:
|
---|
100 | chSlash = '/';
|
---|
101 | break;
|
---|
102 |
|
---|
103 | default:
|
---|
104 | AssertFailedReturn(VERR_INVALID_FLAGS); /* impossible */
|
---|
105 | }
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Do the joining.
|
---|
109 | * Note! Using memmove here as we want to support pszSrcPath == pszDstPath.
|
---|
110 | */
|
---|
111 | char *pszDst = pszDstPath;
|
---|
112 | idxComp = 0;
|
---|
113 | fNeedSlash = false;
|
---|
114 |
|
---|
115 | if (RTPATH_PROP_HAS_ROOT_SPEC(pParsed->fProps))
|
---|
116 | {
|
---|
117 | uint32_t cchComp = pParsed->aComps[0].cch;
|
---|
118 | memmove(pszDst, &pszSrcPath[pParsed->aComps[0].off], cchComp);
|
---|
119 |
|
---|
120 | /* fix the slashes (harmless for unix style) */
|
---|
121 | char chOtherSlash = chSlash == '\\' ? '/' : '\\';
|
---|
122 | while (cchComp-- > 0)
|
---|
123 | {
|
---|
124 | if (*pszDst == chOtherSlash)
|
---|
125 | *pszDst = chSlash;
|
---|
126 | pszDst++;
|
---|
127 | }
|
---|
128 | idxComp = 1;
|
---|
129 | }
|
---|
130 |
|
---|
131 | while (idxComp < cComps)
|
---|
132 | {
|
---|
133 | uint32_t const cchComp = pParsed->aComps[idxComp].cch;
|
---|
134 | if (cchComp > 0)
|
---|
135 | {
|
---|
136 | if (fNeedSlash)
|
---|
137 | *pszDst++ = chSlash;
|
---|
138 | fNeedSlash = true;
|
---|
139 | memmove(pszDst, &pszSrcPath[pParsed->aComps[idxComp].off], cchComp);
|
---|
140 | pszDst += cchComp;
|
---|
141 | }
|
---|
142 | idxComp++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if ((pParsed->fProps & RTPATH_PROP_DIR_SLASH) && fNeedSlash)
|
---|
146 | *pszDst++ = chSlash;
|
---|
147 | *pszDst = '\0';
|
---|
148 |
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | }
|
---|
151 |
|
---|