VirtualBox

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

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

IPRT: Added RTStrDupN.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/* $Id: string.cpp 22736 2009-09-03 11:32:59Z vboxsync $ */
2/** @file
3 * IPRT - String Manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/string.h>
36#include "internal/iprt.h"
37
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/err.h>
41#include "internal/string.h"
42
43
44/**
45 * Free string allocated by any of the non-UCS-2 string functions.
46 *
47 * @returns iprt status code.
48 * @param pszString Pointer to buffer with string to free.
49 * NULL is accepted.
50 */
51RTDECL(void) RTStrFree(char *pszString)
52{
53 if (pszString)
54 RTMemTmpFree(pszString);
55}
56RT_EXPORT_SYMBOL(RTStrFree);
57
58
59/**
60 * Allocates a new copy of the given UTF-8 string.
61 *
62 * @returns Pointer to the allocated UTF-8 string.
63 * @param pszString UTF-8 string to duplicate.
64 */
65RTDECL(char *) RTStrDup(const char *pszString)
66{
67 AssertPtr(pszString);
68 size_t cch = strlen(pszString) + 1;
69 char *psz = (char *)RTMemAlloc(cch);
70 if (psz)
71 memcpy(psz, pszString, cch);
72 return psz;
73}
74RT_EXPORT_SYMBOL(RTStrDup);
75
76
77/**
78 * Allocates a new copy of the given UTF-8 string.
79 *
80 * @returns iprt status code.
81 * @param ppszString Receives pointer of the allocated UTF-8 string.
82 * The returned pointer must be freed using RTStrFree().
83 * @param pszString UTF-8 string to duplicate.
84 */
85RTDECL(int) RTStrDupEx(char **ppszString, const char *pszString)
86{
87 AssertPtr(ppszString);
88 AssertPtr(pszString);
89
90 size_t cch = strlen(pszString) + 1;
91 char *psz = (char *)RTMemAlloc(cch);
92 if (psz)
93 {
94 memcpy(psz, pszString, cch);
95 *ppszString = psz;
96 return VINF_SUCCESS;
97 }
98 return VERR_NO_MEMORY;
99}
100RT_EXPORT_SYMBOL(RTStrDupEx);
101
102
103/**
104 * Allocates a new copy of the given UTF-8 substring.
105 *
106 * @returns Pointer to the allocated UTF-8 substring.
107 * @param pszString UTF-8 string to duplicate.
108 * @param cchMax The max number of chars to duplicate, not counting
109 * the terminator.
110 */
111RTDECL(char *) RTStrDupN(const char *pszString, size_t cchMax)
112{
113 AssertPtr(pszString);
114 char *pszEnd = (char *)memchr(pszString, '\0', cchMax);
115 size_t cch = pszEnd ? (uintptr_t)pszEnd - (uintptr_t)pszString : cchMax;
116 char *pszDst = (char *)RTMemAlloc(cch);
117 if (pszDst)
118 {
119 memcpy(pszDst, pszString, cch);
120 pszDst[cch] = '\0';
121 }
122 return pszDst;
123}
124RT_EXPORT_SYMBOL(RTStrDupN);
125
126
127
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