VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1/* $Id: stringalloc.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - String Manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 *) RTStrAlloc(size_t cb)
43{
44 char *psz = (char *)RTMemAlloc(RT_MAX(cb, 1));
45 if (psz)
46 *psz = '\0';
47 return psz;
48}
49RT_EXPORT_SYMBOL(RTStrAlloc);
50
51
52RTDECL(int) RTStrAllocEx(char **ppsz, size_t cb)
53{
54 char *psz = *ppsz = (char *)RTMemAlloc(RT_MAX(cb, 1));
55 if (psz)
56 {
57 *psz = '\0';
58 return VINF_SUCCESS;
59 }
60 return VERR_NO_STR_MEMORY;
61}
62RT_EXPORT_SYMBOL(RTStrAlloc);
63
64
65RTDECL(int) RTStrRealloc(char **ppsz, size_t cbNew)
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 *)RTMemRealloc(pszOld, cbNew);
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 *)RTMemAlloc(cbNew);
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}
92
93RTDECL(void) RTStrFree(char *pszString)
94{
95 if (pszString)
96 RTMemTmpFree(pszString);
97}
98RT_EXPORT_SYMBOL(RTStrFree);
99
100
101RTDECL(char *) RTStrDup(const char *pszString)
102{
103 AssertPtr(pszString);
104 size_t cch = strlen(pszString) + 1;
105 char *psz = (char *)RTMemAlloc(cch);
106 if (psz)
107 memcpy(psz, pszString, cch);
108 return psz;
109}
110RT_EXPORT_SYMBOL(RTStrDup);
111
112
113RTDECL(int) RTStrDupEx(char **ppszString, const char *pszString)
114{
115 AssertPtr(ppszString);
116 AssertPtr(pszString);
117
118 size_t cch = strlen(pszString) + 1;
119 char *psz = (char *)RTMemAlloc(cch);
120 if (psz)
121 {
122 memcpy(psz, pszString, cch);
123 *ppszString = psz;
124 return VINF_SUCCESS;
125 }
126 return VERR_NO_MEMORY;
127}
128RT_EXPORT_SYMBOL(RTStrDupEx);
129
130
131RTDECL(char *) RTStrDupN(const char *pszString, size_t cchMax)
132{
133 AssertPtr(pszString);
134 char *pszEnd = (char *)memchr(pszString, '\0', cchMax);
135 size_t cch = pszEnd ? (uintptr_t)pszEnd - (uintptr_t)pszString : cchMax;
136 char *pszDst = (char *)RTMemAlloc(cch + 1);
137 if (pszDst)
138 {
139 memcpy(pszDst, pszString, cch);
140 pszDst[cch] = '\0';
141 }
142 return pszDst;
143}
144RT_EXPORT_SYMBOL(RTStrDupN);
145
146
147RTDECL(int) RTStrAAppend(char **ppsz, const char *pszAppend)
148{
149 if (!pszAppend)
150 return VINF_SUCCESS;
151 return RTStrAAppendN(ppsz, pszAppend, RTSTR_MAX);
152}
153
154
155RTDECL(int) RTStrAAppendN(char **ppsz, const char *pszAppend, size_t cchAppend)
156{
157 if (!cchAppend)
158 return VINF_SUCCESS;
159 if (cchAppend == RTSTR_MAX)
160 cchAppend = strlen(pszAppend);
161 else
162 Assert(cchAppend == RTStrNLen(pszAppend, cchAppend));
163
164 size_t const cchOrg = *ppsz ? strlen(*ppsz) : 0;
165 char *pszNew = (char *)RTMemRealloc(*ppsz, cchOrg + cchAppend + 1);
166 if (!pszNew)
167 return VERR_NO_STR_MEMORY;
168
169 memcpy(&pszNew[cchOrg], pszAppend, cchAppend);
170 pszNew[cchOrg + cchAppend] = '\0';
171
172 *ppsz = pszNew;
173 return VINF_SUCCESS;
174}
175
176
177RTDECL(int) RTStrAAppendExNV(char **ppsz, size_t cPairs, va_list va)
178{
179 AssertPtr(ppsz);
180 if (!cPairs)
181 return VINF_SUCCESS;
182
183 /*
184 * Determin the length of each string and calc the new total.
185 */
186 struct RTStrAAppendExNVStruct
187 {
188 const char *psz;
189 size_t cch;
190 } *paPairs = (struct RTStrAAppendExNVStruct *)alloca(cPairs * sizeof(*paPairs));
191 AssertReturn(paPairs, VERR_NO_STR_MEMORY);
192
193 size_t cchOrg = *ppsz ? strlen(*ppsz) : 0;
194 size_t cchNewTotal = cchOrg;
195 for (size_t i = 0; i < cPairs; i++)
196 {
197 const char *psz = va_arg(va, const char *);
198 size_t cch = va_arg(va, size_t);
199 AssertPtrNull(psz);
200 Assert(cch == RTSTR_MAX || cch == RTStrNLen(psz, cch));
201
202 if (cch == RTSTR_MAX)
203 cch = psz ? strlen(psz) : 0;
204 cchNewTotal += cch;
205
206 paPairs[i].cch = cch;
207 paPairs[i].psz = psz;
208 }
209 cchNewTotal++; /* '\0' */
210
211 /*
212 * Try reallocate the string.
213 */
214 char *pszNew = (char *)RTMemRealloc(*ppsz, cchNewTotal);
215 if (!pszNew)
216 return VERR_NO_STR_MEMORY;
217
218 /*
219 * Do the appending.
220 */
221 size_t off = cchOrg;
222 for (size_t i = 0; i < cPairs; i++)
223 {
224 memcpy(&pszNew[off], paPairs[i].psz, paPairs[i].cch);
225 off += paPairs[i].cch;
226 }
227 Assert(off + 1 == cchNewTotal);
228 pszNew[off] = '\0';
229
230 /* done */
231 *ppsz = pszNew;
232 return VINF_SUCCESS;
233}
234RT_EXPORT_SYMBOL(RTStrAAppendExNV);
235
236
237RTDECL(int) RTStrAAppendExN(char **ppsz, size_t cPairs, ...)
238{
239 va_list va;
240 va_start(va, cPairs);
241 int rc = RTStrAAppendExNV(ppsz, cPairs, va);
242 va_end(va);
243 return rc;
244}
245RT_EXPORT_SYMBOL(RTStrAAppendExN);
246
247
248RTDECL(int) RTStrATruncate(char **ppsz, size_t cchNew)
249{
250 char *pszOld = *ppsz;
251 if (!cchNew)
252 {
253 if (pszOld && *pszOld)
254 {
255 *pszOld = '\0';
256 char *pszNew = (char *)RTMemRealloc(pszOld, 1);
257 if (pszNew)
258 *ppsz = pszNew;
259 }
260 }
261 else
262 {
263 AssertPtrReturn(pszOld, VERR_OUT_OF_RANGE);
264 char *pszZero = (char *)memchr(pszOld, '\0', 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 *)RTMemRealloc(pszOld, cchNew + 1);
270 if (pszNew)
271 *ppsz = pszNew;
272 }
273 }
274 return VINF_SUCCESS;
275}
276RT_EXPORT_SYMBOL(RTStrATruncate);
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