1 | /** @file
|
---|
2 | * IPRT - C++ path utilities.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2017-2019 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_cpp_path_h
|
---|
27 | #define ___iprt_cpp_path_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/errcore.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/cpp/ministring.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /** @defgroup grp_rt_cpp_path C++ Path Utilities
|
---|
39 | * @ingroup grp_rt_cpp
|
---|
40 | * @{
|
---|
41 | */
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * RTPathAbs() wrapper for working directly on a RTCString instance.
|
---|
45 | *
|
---|
46 | * @returns IPRT status code.
|
---|
47 | * @param rStrAbs Reference to the destiation string.
|
---|
48 | * @param pszRelative The relative source string.
|
---|
49 | */
|
---|
50 | DECLINLINE(int) RTPathAbsCxx(RTCString &rStrAbs, const char *pszRelative)
|
---|
51 | {
|
---|
52 | Assert(rStrAbs.c_str() != pszRelative);
|
---|
53 | int rc = rStrAbs.reserveNoThrow(RTPATH_MAX);
|
---|
54 | if (RT_SUCCESS(rc))
|
---|
55 | {
|
---|
56 | char *pszDst = rStrAbs.mutableRaw();
|
---|
57 | rc = RTPathAbs(pszRelative, pszDst, rStrAbs.capacity());
|
---|
58 | if (RT_FAILURE(rc))
|
---|
59 | *pszDst = '\0';
|
---|
60 | rStrAbs.jolt();
|
---|
61 | }
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * RTPathAbs() wrapper for working directly on a RTCString instance.
|
---|
68 | *
|
---|
69 | * @returns IPRT status code.
|
---|
70 | * @param rStrAbs Reference to the destiation string.
|
---|
71 | * @param rStrRelative Reference to the relative source string.
|
---|
72 | */
|
---|
73 | DECLINLINE(int) RTPathAbsCxx(RTCString &rStrAbs, RTCString const &rStrRelative)
|
---|
74 | {
|
---|
75 | return RTPathAbsCxx(rStrAbs, rStrRelative.c_str());
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * RTPathAppPrivateNoArch() wrapper for working directly on a RTCString instance.
|
---|
81 | *
|
---|
82 | * @returns IPRT status code.
|
---|
83 | * @param rStrDst Reference to the destiation string.
|
---|
84 | */
|
---|
85 | DECLINLINE(int) RTPathAppPrivateNoArchCxx(RTCString &rStrDst)
|
---|
86 | {
|
---|
87 | int rc = rStrDst.reserveNoThrow(RTPATH_MAX);
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | {
|
---|
90 | char *pszDst = rStrDst.mutableRaw();
|
---|
91 | rc = RTPathAppPrivateNoArch(pszDst, rStrDst.capacity());
|
---|
92 | if (RT_FAILURE(rc))
|
---|
93 | *pszDst = '\0';
|
---|
94 | rStrDst.jolt();
|
---|
95 | }
|
---|
96 | return rc;
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * RTPathAppend() wrapper for working directly on a RTCString instance.
|
---|
103 | *
|
---|
104 | * @returns IPRT status code.
|
---|
105 | * @param rStrDst Reference to the destiation string.
|
---|
106 | * @param pszAppend One or more components to append to the path already
|
---|
107 | * present in @a rStrDst.
|
---|
108 | */
|
---|
109 | DECLINLINE(int) RTPathAppendCxx(RTCString &rStrDst, const char *pszAppend)
|
---|
110 | {
|
---|
111 | Assert(rStrDst.c_str() != pszAppend);
|
---|
112 | size_t cbEstimate = rStrDst.length() + 1 + strlen(pszAppend) + 1;
|
---|
113 | int rc;
|
---|
114 | if (rStrDst.capacity() >= cbEstimate)
|
---|
115 | rc = VINF_SUCCESS;
|
---|
116 | else
|
---|
117 | rc = rStrDst.reserveNoThrow(RT_ALIGN_Z(cbEstimate, 8));
|
---|
118 | if (RT_SUCCESS(rc))
|
---|
119 | {
|
---|
120 | rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), pszAppend);
|
---|
121 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
122 | {
|
---|
123 | rc = rStrDst.reserveNoThrow(RTPATH_MAX);
|
---|
124 | if (RT_SUCCESS(rc))
|
---|
125 | rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), pszAppend);
|
---|
126 | }
|
---|
127 | rStrDst.jolt();
|
---|
128 | }
|
---|
129 | return rc;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * RTPathAppend() wrapper for working directly on a RTCString instance.
|
---|
135 | *
|
---|
136 | * @returns IPRT status code.
|
---|
137 | * @param rStrDst Reference to the destiation string.
|
---|
138 | * @param rStrAppend One or more components to append to the path already
|
---|
139 | * present in @a rStrDst.
|
---|
140 | */
|
---|
141 | DECLINLINE(int) RTPathAppendCxx(RTCString &rStrDst, RTCString const &rStrAppend)
|
---|
142 | {
|
---|
143 | Assert(&rStrDst != &rStrAppend);
|
---|
144 | size_t cbEstimate = rStrDst.length() + 1 + rStrAppend.length() + 1;
|
---|
145 | int rc;
|
---|
146 | if (rStrDst.capacity() >= cbEstimate)
|
---|
147 | rc = VINF_SUCCESS;
|
---|
148 | else
|
---|
149 | rc = rStrDst.reserveNoThrow(RT_ALIGN_Z(cbEstimate, 8));
|
---|
150 | if (RT_SUCCESS(rc))
|
---|
151 | {
|
---|
152 | rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), rStrAppend.c_str());
|
---|
153 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
154 | {
|
---|
155 | rc = rStrDst.reserveNoThrow(RTPATH_MAX);
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | rc = RTPathAppend(rStrDst.mutableRaw(), rStrDst.capacity(), rStrAppend.c_str());
|
---|
158 | }
|
---|
159 | rStrDst.jolt();
|
---|
160 | }
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /** @} */
|
---|
166 |
|
---|
167 | #endif
|
---|
168 |
|
---|