1 | /* $Id: string.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - String Manipulation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/alloc.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/err.h>
|
---|
30 | #include "internal/string.h"
|
---|
31 |
|
---|
32 | #include <locale.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Init C runtime locale
|
---|
37 | * note: actually where is no need in this global var, use it only for
|
---|
38 | * auto run of setlocale() func.
|
---|
39 | */
|
---|
40 | /** @todo rewrite this to do setlocale() from some proper init function. */
|
---|
41 | static int g_RTLocaleInited = (setlocale(LC_CTYPE, "") != NULL);
|
---|
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 | */
|
---|
51 | RTR3DECL(void) RTStrFree(char *pszString)
|
---|
52 | {
|
---|
53 | if (pszString)
|
---|
54 | RTMemTmpFree(pszString);
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Allocates a new copy of the given UTF-8 string.
|
---|
60 | *
|
---|
61 | * @returns Pointer to the allocated UTF-8 string.
|
---|
62 | * @param pszString UTF-8 string to duplicate.
|
---|
63 | */
|
---|
64 | RTR3DECL(char *) RTStrDup(const char *pszString)
|
---|
65 | {
|
---|
66 | Assert(VALID_PTR(pszString));
|
---|
67 | size_t cch = strlen(pszString) + 1;
|
---|
68 | char *psz = (char *)RTMemAlloc(cch);
|
---|
69 | if (psz)
|
---|
70 | memcpy(psz, pszString, cch);
|
---|
71 | return psz;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Allocates a new copy of the given UTF-8 string.
|
---|
77 | *
|
---|
78 | * @returns iprt status code.
|
---|
79 | * @param ppszString Receives pointer of the allocated UTF-8 string.
|
---|
80 | * The returned pointer must be freed using RTStrFree().
|
---|
81 | * @param pszString UTF-8 string to duplicate.
|
---|
82 | */
|
---|
83 | RTR3DECL(int) RTStrDupEx(char **ppszString, const char *pszString)
|
---|
84 | {
|
---|
85 | Assert(VALID_PTR(ppszString));
|
---|
86 | Assert(VALID_PTR(pszString));
|
---|
87 |
|
---|
88 | size_t cch = strlen(pszString) + 1;
|
---|
89 | char *psz = (char *)RTMemAlloc(cch);
|
---|
90 | if (psz)
|
---|
91 | {
|
---|
92 | memcpy(psz, pszString, cch);
|
---|
93 | *ppszString = psz;
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | }
|
---|
96 | return VERR_NO_MEMORY;
|
---|
97 | }
|
---|
98 |
|
---|