1 | /* $Id: string.cpp 8245 2008-04-21 17:24:28Z 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 <iprt/alloc.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include "internal/string.h"
|
---|
40 |
|
---|
41 | #include <locale.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Init C runtime locale
|
---|
46 | * note: actually where is no need in this global var, use it only for
|
---|
47 | * auto run of setlocale() func.
|
---|
48 | */
|
---|
49 | /** @todo rewrite this to do setlocale() from some proper init function. */
|
---|
50 | static int g_RTLocaleInited = (setlocale(LC_CTYPE, "") != NULL);
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Free string allocated by any of the non-UCS-2 string functions.
|
---|
55 | *
|
---|
56 | * @returns iprt status code.
|
---|
57 | * @param pszString Pointer to buffer with string to free.
|
---|
58 | * NULL is accepted.
|
---|
59 | */
|
---|
60 | RTR3DECL(void) RTStrFree(char *pszString)
|
---|
61 | {
|
---|
62 | if (pszString)
|
---|
63 | RTMemTmpFree(pszString);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Allocates a new copy of the given UTF-8 string.
|
---|
69 | *
|
---|
70 | * @returns Pointer to the allocated UTF-8 string.
|
---|
71 | * @param pszString UTF-8 string to duplicate.
|
---|
72 | */
|
---|
73 | RTR3DECL(char *) RTStrDup(const char *pszString)
|
---|
74 | {
|
---|
75 | Assert(VALID_PTR(pszString));
|
---|
76 | size_t cch = strlen(pszString) + 1;
|
---|
77 | char *psz = (char *)RTMemAlloc(cch);
|
---|
78 | if (psz)
|
---|
79 | memcpy(psz, pszString, cch);
|
---|
80 | return psz;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Allocates a new copy of the given UTF-8 string.
|
---|
86 | *
|
---|
87 | * @returns iprt status code.
|
---|
88 | * @param ppszString Receives pointer of the allocated UTF-8 string.
|
---|
89 | * The returned pointer must be freed using RTStrFree().
|
---|
90 | * @param pszString UTF-8 string to duplicate.
|
---|
91 | */
|
---|
92 | RTR3DECL(int) RTStrDupEx(char **ppszString, const char *pszString)
|
---|
93 | {
|
---|
94 | Assert(VALID_PTR(ppszString));
|
---|
95 | Assert(VALID_PTR(pszString));
|
---|
96 |
|
---|
97 | size_t cch = strlen(pszString) + 1;
|
---|
98 | char *psz = (char *)RTMemAlloc(cch);
|
---|
99 | if (psz)
|
---|
100 | {
|
---|
101 | memcpy(psz, pszString, cch);
|
---|
102 | *ppszString = psz;
|
---|
103 | return VINF_SUCCESS;
|
---|
104 | }
|
---|
105 | return VERR_NO_MEMORY;
|
---|
106 | }
|
---|
107 |
|
---|