1 | /* $Id: utf8-win.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - UTF8 helpers.
|
---|
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 | #define LOG_GROUP RTLOGGROUP_UTF8
|
---|
32 | #include <Windows.h>
|
---|
33 |
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | RTR3DECL(int) RTStrUtf8ToCurrentCPTag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
41 | {
|
---|
42 | Assert(ppszString);
|
---|
43 | Assert(pszString);
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * Check for zero length input string.
|
---|
47 | */
|
---|
48 | if (!*pszString)
|
---|
49 | {
|
---|
50 | *ppszString = (char *)RTMemTmpAllocZTag(sizeof(char), pszTag);
|
---|
51 | if (*ppszString)
|
---|
52 | return VINF_SUCCESS;
|
---|
53 | return VERR_NO_TMP_MEMORY;
|
---|
54 | }
|
---|
55 |
|
---|
56 | *ppszString = NULL;
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * Convert to wide char first.
|
---|
60 | */
|
---|
61 | PRTUTF16 pwszString = NULL;
|
---|
62 | int rc = RTStrToUtf16(pszString, &pwszString);
|
---|
63 | if (RT_FAILURE(rc))
|
---|
64 | return rc;
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * First calc result string length.
|
---|
68 | */
|
---|
69 | int cbResult = WideCharToMultiByte(CP_ACP, 0, pwszString, -1, NULL, 0, NULL, NULL);
|
---|
70 | if (cbResult > 0)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Alloc space for result buffer.
|
---|
74 | */
|
---|
75 | LPSTR lpString = (LPSTR)RTMemTmpAllocTag(cbResult, pszTag);
|
---|
76 | if (lpString)
|
---|
77 | {
|
---|
78 | /*
|
---|
79 | * Do the translation.
|
---|
80 | */
|
---|
81 | if (WideCharToMultiByte(CP_ACP, 0, pwszString, -1, lpString, cbResult, NULL, NULL) > 0)
|
---|
82 | {
|
---|
83 | /* ok */
|
---|
84 | *ppszString = lpString;
|
---|
85 | RTMemTmpFree(pwszString);
|
---|
86 | return VINF_SUCCESS;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* translation error */
|
---|
90 | int iLastErr = GetLastError();
|
---|
91 | AssertMsgFailed(("Unicode to ACP translation failed. lasterr=%d\n", iLastErr));
|
---|
92 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
93 | }
|
---|
94 | else
|
---|
95 | rc = VERR_NO_TMP_MEMORY;
|
---|
96 | RTMemTmpFree(lpString);
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | /* translation error */
|
---|
101 | int iLastErr = GetLastError();
|
---|
102 | AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
|
---|
103 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
104 | }
|
---|
105 | RTMemTmpFree(pwszString);
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | RTR3DECL(int) RTStrCurrentCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
111 | {
|
---|
112 | Assert(ppszString);
|
---|
113 | Assert(pszString);
|
---|
114 | *ppszString = NULL;
|
---|
115 |
|
---|
116 | /** @todo is there a quicker way? Currently: ACP -> UTF-16 -> UTF-8 */
|
---|
117 |
|
---|
118 | size_t cch = strlen(pszString);
|
---|
119 | if (cch <= 0)
|
---|
120 | {
|
---|
121 | /* zero length string passed. */
|
---|
122 | *ppszString = (char *)RTMemTmpAllocZTag(sizeof(char), pszTag);
|
---|
123 | if (*ppszString)
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | return VERR_NO_TMP_MEMORY;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * First calc result string length.
|
---|
130 | */
|
---|
131 | int rc;
|
---|
132 | int cwc = MultiByteToWideChar(CP_ACP, 0, pszString, -1, NULL, 0);
|
---|
133 | if (cwc > 0)
|
---|
134 | {
|
---|
135 | /*
|
---|
136 | * Alloc space for result buffer.
|
---|
137 | */
|
---|
138 | PRTUTF16 pwszString = (PRTUTF16)RTMemTmpAlloc(cwc * sizeof(RTUTF16));
|
---|
139 | if (pwszString)
|
---|
140 | {
|
---|
141 | /*
|
---|
142 | * Do the translation.
|
---|
143 | */
|
---|
144 | if (MultiByteToWideChar(CP_ACP, 0, pszString, -1, pwszString, cwc) > 0)
|
---|
145 | {
|
---|
146 | /*
|
---|
147 | * Now we got UTF-16, convert it to UTF-8
|
---|
148 | */
|
---|
149 | rc = RTUtf16ToUtf8(pwszString, ppszString);
|
---|
150 | RTMemTmpFree(pwszString);
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 | RTMemTmpFree(pwszString);
|
---|
154 | /* translation error */
|
---|
155 | int iLastErr = GetLastError();
|
---|
156 | AssertMsgFailed(("ACP to Unicode translation failed. lasterr=%d\n", iLastErr));
|
---|
157 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
158 | }
|
---|
159 | else
|
---|
160 | rc = VERR_NO_TMP_MEMORY;
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | /* translation error */
|
---|
165 | int iLastErr = GetLastError();
|
---|
166 | AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
|
---|
167 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
168 | }
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 |
|
---|