VirtualBox

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

Last change on this file since 31376 was 31157, checked in by vboxsync, 15 years ago

iprt,++: Tag allocation in all builds with a string, defaulting to FILE.

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