VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/stringalloc.cpp@ 33982

Last change on this file since 33982 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1/* $Id: stringalloc.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
2/** @file
3 * IPRT - String Manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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 <iprt/string.h>
32#include "internal/iprt.h"
33
34#ifndef IN_RING0
35# include <iprt/alloca.h>
36#endif
37#include <iprt/assert.h>
38#include <iprt/mem.h>
39#include <iprt/err.h>
40#include "internal/string.h"
41
42
43
44RTDECL(char *) RTStrAllocTag(size_t cb, const char *pszTag)
45{
46 char *psz = (char *)RTMemAllocTag(RT_MAX(cb, 1), pszTag);
47 if (psz)
48 *psz = '\0';
49 return psz;
50}
51RT_EXPORT_SYMBOL(RTStrAllocTag);
52
53
54RTDECL(int) RTStrAllocExTag(char **ppsz, size_t cb, const char *pszTag)
55{
56 char *psz = *ppsz = (char *)RTMemAllocTag(RT_MAX(cb, 1), pszTag);
57 if (psz)
58 {
59 *psz = '\0';
60 return VINF_SUCCESS;
61 }
62 return VERR_NO_STR_MEMORY;
63}
64RT_EXPORT_SYMBOL(RTStrAllocTag);
65
66
67RTDECL(int) RTStrReallocTag(char **ppsz, size_t cbNew, const char *pszTag)
68{
69 char *pszOld = *ppsz;
70 if (!cbNew)
71 {
72 RTMemFree(pszOld);
73 *ppsz = NULL;
74 }
75 else if (pszOld)
76 {
77 char *pszNew = (char *)RTMemReallocTag(pszOld, cbNew, pszTag);
78 if (!pszNew)
79 return VERR_NO_STR_MEMORY;
80 pszNew[cbNew - 1] = '\0';
81 *ppsz = pszNew;
82 }
83 else
84 {
85 char *pszNew = (char *)RTMemAllocTag(cbNew, pszTag);
86 if (!pszNew)
87 return VERR_NO_STR_MEMORY;
88 pszNew[0] = '\0';
89 pszNew[cbNew - 1] = '\0';
90 *ppsz = pszNew;
91 }
92 return VINF_SUCCESS;
93}
94RT_EXPORT_SYMBOL(RTStrReallocTag);
95
96RTDECL(void) RTStrFree(char *pszString)
97{
98 if (pszString)
99 RTMemTmpFree(pszString);
100}
101RT_EXPORT_SYMBOL(RTStrFree);
102
103
104RTDECL(char *) RTStrDupTag(const char *pszString, const char *pszTag)
105{
106 AssertPtr(pszString);
107 size_t cch = strlen(pszString) + 1;
108 char *psz = (char *)RTMemAllocTag(cch, pszTag);
109 if (psz)
110 memcpy(psz, pszString, cch);
111 return psz;
112}
113RT_EXPORT_SYMBOL(RTStrDupTag);
114
115
116RTDECL(int) RTStrDupExTag(char **ppszString, const char *pszString, const char *pszTag)
117{
118 AssertPtr(ppszString);
119 AssertPtr(pszString);
120
121 size_t cch = strlen(pszString) + 1;
122 char *psz = (char *)RTMemAllocTag(cch, pszTag);
123 if (psz)
124 {
125 memcpy(psz, pszString, cch);
126 *ppszString = psz;
127 return VINF_SUCCESS;
128 }
129 return VERR_NO_MEMORY;
130}
131RT_EXPORT_SYMBOL(RTStrDupExTag);
132
133
134RTDECL(char *) RTStrDupNTag(const char *pszString, size_t cchMax, const char *pszTag)
135{
136 AssertPtr(pszString);
137 char const *pszEnd = RTStrEnd(pszString, cchMax);
138 size_t cch = pszEnd ? (uintptr_t)pszEnd - (uintptr_t)pszString : cchMax;
139 char *pszDst = (char *)RTMemAllocTag(cch + 1, pszTag);
140 if (pszDst)
141 {
142 memcpy(pszDst, pszString, cch);
143 pszDst[cch] = '\0';
144 }
145 return pszDst;
146}
147RT_EXPORT_SYMBOL(RTStrDupNTag);
148
149
150RTDECL(int) RTStrAAppendTag(char **ppsz, const char *pszAppend, const char *pszTag)
151{
152 if (!pszAppend)
153 return VINF_SUCCESS;
154 return RTStrAAppendNTag(ppsz, pszAppend, RTSTR_MAX, pszTag);
155}
156
157
158RTDECL(int) RTStrAAppendNTag(char **ppsz, const char *pszAppend, size_t cchAppend, const char *pszTag)
159{
160 if (!cchAppend)
161 return VINF_SUCCESS;
162 if (cchAppend == RTSTR_MAX)
163 cchAppend = strlen(pszAppend);
164 else
165 Assert(cchAppend == RTStrNLen(pszAppend, cchAppend));
166
167 size_t const cchOrg = *ppsz ? strlen(*ppsz) : 0;
168 char *pszNew = (char *)RTMemReallocTag(*ppsz, cchOrg + cchAppend + 1, pszTag);
169 if (!pszNew)
170 return VERR_NO_STR_MEMORY;
171
172 memcpy(&pszNew[cchOrg], pszAppend, cchAppend);
173 pszNew[cchOrg + cchAppend] = '\0';
174
175 *ppsz = pszNew;
176 return VINF_SUCCESS;
177}
178
179
180#ifndef IN_RING0
181
182/* XXX Currently not needed anywhere. alloca() induces some linker problems for ring 0 code
183 * with newer versions of VCC */
184
185RTDECL(int) RTStrAAppendExNVTag(char **ppsz, size_t cPairs, va_list va, const char *pszTag)
186{
187 AssertPtr(ppsz);
188 if (!cPairs)
189 return VINF_SUCCESS;
190
191 /*
192 * Determine the length of each string and calc the new total.
193 */
194 struct RTStrAAppendExNVStruct
195 {
196 const char *psz;
197 size_t cch;
198 } *paPairs = (struct RTStrAAppendExNVStruct *)alloca(cPairs * sizeof(*paPairs));
199 AssertReturn(paPairs, VERR_NO_STR_MEMORY);
200
201 size_t cchOrg = *ppsz ? strlen(*ppsz) : 0;
202 size_t cchNewTotal = cchOrg;
203 for (size_t i = 0; i < cPairs; i++)
204 {
205 const char *psz = va_arg(va, const char *);
206 size_t cch = va_arg(va, size_t);
207 AssertPtrNull(psz);
208 Assert(cch == RTSTR_MAX || cch == RTStrNLen(psz, cch));
209
210 if (cch == RTSTR_MAX)
211 cch = psz ? strlen(psz) : 0;
212 cchNewTotal += cch;
213
214 paPairs[i].cch = cch;
215 paPairs[i].psz = psz;
216 }
217 cchNewTotal++; /* '\0' */
218
219 /*
220 * Try reallocate the string.
221 */
222 char *pszNew = (char *)RTMemReallocTag(*ppsz, cchNewTotal, pszTag);
223 if (!pszNew)
224 return VERR_NO_STR_MEMORY;
225
226 /*
227 * Do the appending.
228 */
229 size_t off = cchOrg;
230 for (size_t i = 0; i < cPairs; i++)
231 {
232 memcpy(&pszNew[off], paPairs[i].psz, paPairs[i].cch);
233 off += paPairs[i].cch;
234 }
235 Assert(off + 1 == cchNewTotal);
236 pszNew[off] = '\0';
237
238 /* done */
239 *ppsz = pszNew;
240 return VINF_SUCCESS;
241}
242RT_EXPORT_SYMBOL(RTStrAAppendExNVTag);
243
244#endif
245
246
247RTDECL(int) RTStrATruncateTag(char **ppsz, size_t cchNew, const char *pszTag)
248{
249 char *pszOld = *ppsz;
250 if (!cchNew)
251 {
252 if (pszOld && *pszOld)
253 {
254 *pszOld = '\0';
255 char *pszNew = (char *)RTMemReallocTag(pszOld, 1, pszTag);
256 if (pszNew)
257 *ppsz = pszNew;
258 }
259 }
260 else
261 {
262 AssertPtrReturn(pszOld, VERR_OUT_OF_RANGE);
263 AssertPtrReturn(cchNew < ~(size_t)64, VERR_OUT_OF_RANGE);
264 char *pszZero = RTStrEnd(pszOld, cchNew + 63);
265 AssertReturn(!pszZero || (size_t)(pszZero - pszOld) >= cchNew, VERR_OUT_OF_RANGE);
266 pszOld[cchNew] = '\0';
267 if (!pszZero)
268 {
269 char *pszNew = (char *)RTMemReallocTag(pszOld, cchNew + 1, pszTag);
270 if (pszNew)
271 *ppsz = pszNew;
272 }
273 }
274 return VINF_SUCCESS;
275}
276RT_EXPORT_SYMBOL(RTStrATruncateTag);
277
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