VirtualBox

source: vbox/trunk/include/iprt/cpp/path.h@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1/** @file
2 * IPRT - C++ path utilities.
3 */
4
5/*
6 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_cpp_path_h
37#define IPRT_INCLUDED_cpp_path_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/assert.h>
43#include <iprt/errcore.h>
44#include <iprt/path.h>
45#include <iprt/cpp/ministring.h>
46
47
48/** @defgroup grp_rt_cpp_path C++ Path Utilities
49 * @ingroup grp_rt_cpp
50 * @{
51 */
52
53/**
54 * RTPathAbs() wrapper for working directly on a RTCString instance.
55 *
56 * @returns IPRT status code.
57 * @param rStrAbs Reference to the destination string.
58 * @param pszRelative The relative source string.
59 */
60DECLINLINE(int) RTPathAbsCxx(RTCString &rStrAbs, const char *pszRelative)
61{
62 Assert(rStrAbs.c_str() != pszRelative);
63 int rc = rStrAbs.reserveNoThrow(RTPATH_MAX);
64 if (RT_SUCCESS(rc))
65 {
66 unsigned cTries = 8;
67 for (;;)
68 {
69 char *pszDst = rStrAbs.mutableRaw();
70 size_t cbCap = rStrAbs.capacity();
71 rc = RTPathAbsEx(NULL, pszRelative, RTPATH_STR_F_STYLE_HOST, pszDst, &cbCap);
72 if (RT_SUCCESS(rc))
73 break;
74 *pszDst = '\0';
75 if (rc != VERR_BUFFER_OVERFLOW)
76 break;
77 if (--cTries == 0)
78 break;
79 rc = rStrAbs.reserveNoThrow(RT_MIN(RT_ALIGN_Z(cbCap, 64), RTPATH_MAX));
80 if (RT_FAILURE(rc))
81 break;
82 }
83 rStrAbs.jolt();
84 }
85 return rc;
86}
87
88
89/**
90 * RTPathAbs() wrapper for working directly on a RTCString instance.
91 *
92 * @returns IPRT status code.
93 * @param rStrAbs Reference to the destination string.
94 * @param rStrRelative Reference to the relative source string.
95 */
96DECLINLINE(int) RTPathAbsCxx(RTCString &rStrAbs, RTCString const &rStrRelative)
97{
98 return RTPathAbsCxx(rStrAbs, rStrRelative.c_str());
99}
100
101
102
103/**
104 * RTPathAbsEx() wrapper for working directly on a RTCString instance.
105 *
106 * @returns IPRT status code.
107 * @param rStrAbs Reference to the destination string.
108 * @param pszBase The base path, optional.
109 * @param pszRelative The relative source string.
110 * @param fFlags RTPATH_STR_F_STYLE_XXX and RTPATHABS_F_XXX flags.
111 */
112DECLINLINE(int) RTPathAbsExCxx(RTCString &rStrAbs, const char *pszBase, const char *pszRelative, uint32_t fFlags = RTPATH_STR_F_STYLE_HOST)
113{
114 Assert(rStrAbs.c_str() != pszRelative);
115 int rc = rStrAbs.reserveNoThrow(RTPATH_MAX);
116 if (RT_SUCCESS(rc))
117 {
118 unsigned cTries = 8;
119 for (;;)
120 {
121 char *pszDst = rStrAbs.mutableRaw();
122 size_t cbCap = rStrAbs.capacity();
123 rc = RTPathAbsEx(pszBase, pszRelative, fFlags, pszDst, &cbCap);
124 if (RT_SUCCESS(rc))
125 break;
126 *pszDst = '\0';
127 if (rc != VERR_BUFFER_OVERFLOW)
128 break;
129 if (--cTries == 0)
130 break;
131 rc = rStrAbs.reserveNoThrow(RT_MIN(RT_ALIGN_Z(cbCap, 64), RTPATH_MAX));
132 if (RT_FAILURE(rc))
133 break;
134 }
135 rStrAbs.jolt();
136 }
137 return rc;
138}
139
140
141DECLINLINE(int) RTPathAbsExCxx(RTCString &rStrAbs, RTCString const &rStrBase, RTCString const &rStrRelative, uint32_t fFlags = RTPATH_STR_F_STYLE_HOST)
142{
143 return RTPathAbsExCxx(rStrAbs, rStrBase.c_str(), rStrRelative.c_str(), fFlags);
144}
145
146
147DECLINLINE(int) RTPathAbsExCxx(RTCString &rStrAbs, const char *pszBase, RTCString const &rStrRelative, uint32_t fFlags = RTPATH_STR_F_STYLE_HOST)
148{
149 return RTPathAbsExCxx(rStrAbs, pszBase, rStrRelative.c_str(), fFlags);
150}
151
152
153DECLINLINE(int) RTPathAbsExCxx(RTCString &rStrAbs, RTCString const &rStrBase, const char *pszRelative, uint32_t fFlags = RTPATH_STR_F_STYLE_HOST)
154{
155 return RTPathAbsExCxx(rStrAbs, rStrBase.c_str(), pszRelative, fFlags);
156}
157
158
159
160/**
161 * RTPathAppPrivateNoArch() wrapper for working directly on a RTCString instance.
162 *
163 * @returns IPRT status code.
164 * @param rStrDst Reference to the destination string.
165 */
166DECLINLINE(int) RTPathAppPrivateNoArchCxx(RTCString &rStrDst)
167{
168 int rc = rStrDst.reserveNoThrow(RTPATH_MAX);
169 if (RT_SUCCESS(rc))
170 {
171 char *pszDst = rStrDst.mutableRaw();
172 rc = RTPathAppPrivateNoArch(pszDst, rStrDst.capacity());
173 if (RT_FAILURE(rc))
174 *pszDst = '\0';
175 rStrDst.jolt();
176 }
177 return rc;
178
179}
180
181
182/**
183 * RTPathAppend() wrapper for working directly on a RTCString instance.
184 *
185 * @returns IPRT status code.
186 * @param rStrDst Reference to the destination string.
187 * @param pszAppend One or more components to append to the path already
188 * present in @a rStrDst.
189 */
190DECLINLINE(int) RTPathAppendCxx(RTCString &rStrDst, const char *pszAppend)
191{
192 Assert(rStrDst.c_str() != pszAppend);
193 size_t cbEstimate = rStrDst.length() + 1 + strlen(pszAppend) + 1;
194 int rc;
195 if (rStrDst.capacity() >= cbEstimate)
196 rc = VINF_SUCCESS;
197 else
198 rc = rStrDst.reserveNoThrow(RT_ALIGN_Z(cbEstimate, 8));
199 if (RT_SUCCESS(rc))
200 {
201 rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), pszAppend);
202 if (rc == VERR_BUFFER_OVERFLOW)
203 {
204 rc = rStrDst.reserveNoThrow(RTPATH_MAX);
205 if (RT_SUCCESS(rc))
206 rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), pszAppend);
207 }
208 rStrDst.jolt();
209 }
210 return rc;
211}
212
213
214/**
215 * RTPathAppend() wrapper for working directly on a RTCString instance.
216 *
217 * @returns IPRT status code.
218 * @param rStrDst Reference to the destination string.
219 * @param rStrAppend One or more components to append to the path already
220 * present in @a rStrDst.
221 */
222DECLINLINE(int) RTPathAppendCxx(RTCString &rStrDst, RTCString const &rStrAppend)
223{
224 Assert(&rStrDst != &rStrAppend);
225 size_t cbEstimate = rStrDst.length() + 1 + rStrAppend.length() + 1;
226 int rc;
227 if (rStrDst.capacity() >= cbEstimate)
228 rc = VINF_SUCCESS;
229 else
230 rc = rStrDst.reserveNoThrow(RT_ALIGN_Z(cbEstimate, 8));
231 if (RT_SUCCESS(rc))
232 {
233 rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), rStrAppend.c_str());
234 if (rc == VERR_BUFFER_OVERFLOW)
235 {
236 rc = rStrDst.reserveNoThrow(RTPATH_MAX);
237 if (RT_SUCCESS(rc))
238 rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), rStrAppend.c_str());
239 }
240 rStrDst.jolt();
241 }
242 return rc;
243}
244
245
246/** @} */
247
248#endif /* !IPRT_INCLUDED_cpp_path_h */
249
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