1 | /* $Id: utf8-win32.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - UTF8 helpers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP RTLOGGROUP_UTF8
|
---|
23 | #include <Windows.h>
|
---|
24 |
|
---|
25 | #include <iprt/string.h>
|
---|
26 | #include <iprt/alloc.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/err.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Allocates tmp buffer, translates pszString from UTF8 to current codepage.
|
---|
33 | *
|
---|
34 | * @returns iprt status code.
|
---|
35 | * @param ppszString Receives pointer of allocated native CP string.
|
---|
36 | * The returned pointer must be freed using RTStrFree().
|
---|
37 | * @param pszString UTF-8 string to convert.
|
---|
38 | */
|
---|
39 | RTR3DECL(int) RTStrUtf8ToCurrentCP(char **ppszString, const char *pszString)
|
---|
40 | {
|
---|
41 | Assert(ppszString);
|
---|
42 | Assert(pszString);
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Check for zero length input string.
|
---|
46 | */
|
---|
47 | if (!*pszString)
|
---|
48 | {
|
---|
49 | *ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
|
---|
50 | if (*ppszString)
|
---|
51 | return VINF_SUCCESS;
|
---|
52 | return VERR_NO_TMP_MEMORY;
|
---|
53 | }
|
---|
54 |
|
---|
55 | *ppszString = NULL;
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Convert to wide char first.
|
---|
59 | */
|
---|
60 | PRTUCS2 pucszString = NULL;
|
---|
61 | int rc = RTStrUtf8ToUcs2(&pucszString, pszString);
|
---|
62 | if (RT_FAILURE(rc))
|
---|
63 | return rc;
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * First calc result string length.
|
---|
67 | */
|
---|
68 | int cbResult = WideCharToMultiByte(CP_ACP, 0, pucszString, -1, NULL, 0, NULL, NULL);
|
---|
69 | if (cbResult > 0)
|
---|
70 | {
|
---|
71 | /*
|
---|
72 | * Alloc space for result buffer.
|
---|
73 | */
|
---|
74 | LPSTR lpString = (LPSTR)RTMemTmpAlloc(cbResult);
|
---|
75 | if (lpString)
|
---|
76 | {
|
---|
77 | /*
|
---|
78 | * Do the translation.
|
---|
79 | */
|
---|
80 | if (WideCharToMultiByte(CP_ACP, 0, pucszString, -1, lpString, cbResult, NULL, NULL) > 0)
|
---|
81 | {
|
---|
82 | /* ok */
|
---|
83 | *ppszString = lpString;
|
---|
84 | RTMemTmpFree(pucszString);
|
---|
85 | return VINF_SUCCESS;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /* translation error */
|
---|
89 | int iLastErr = GetLastError();
|
---|
90 | AssertMsgFailed(("Unicode to ACP translation failed. lasterr=%d\n", iLastErr));
|
---|
91 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
92 | }
|
---|
93 | else
|
---|
94 | rc = VERR_NO_TMP_MEMORY;
|
---|
95 | RTMemTmpFree(lpString);
|
---|
96 | }
|
---|
97 | else
|
---|
98 | {
|
---|
99 | /* translation error */
|
---|
100 | int iLastErr = GetLastError();
|
---|
101 | AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
|
---|
102 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
103 | }
|
---|
104 | RTMemTmpFree(pucszString);
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
|
---|
110 | *
|
---|
111 | * @returns iprt status code.
|
---|
112 | * @param ppszString Receives pointer of allocated UTF-8 string.
|
---|
113 | * The returned pointer must be freed using RTStrFree().
|
---|
114 | * @param pszString Native string to convert.
|
---|
115 | */
|
---|
116 | RTR3DECL(int) RTStrCurrentCPToUtf8(char **ppszString, const char *pszString)
|
---|
117 | {
|
---|
118 | Assert(ppszString);
|
---|
119 | Assert(pszString);
|
---|
120 | *ppszString = NULL;
|
---|
121 |
|
---|
122 | /** @todo is there a quicker way? Currently: ACP -> UCS-2 -> UTF-8 */
|
---|
123 |
|
---|
124 | size_t cch = strlen(pszString);
|
---|
125 | if (cch <= 0)
|
---|
126 | {
|
---|
127 | /* zero length string passed. */
|
---|
128 | *ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
|
---|
129 | if (*ppszString)
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | return VERR_NO_TMP_MEMORY;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * First calc result string length.
|
---|
136 | */
|
---|
137 | int rc;
|
---|
138 | int cuc = MultiByteToWideChar(CP_ACP, 0, pszString, -1, NULL, 0);
|
---|
139 | if (cuc > 0)
|
---|
140 | {
|
---|
141 | /*
|
---|
142 | * Alloc space for result buffer.
|
---|
143 | */
|
---|
144 | PRTUCS2 pucszString = (PRTUCS2)RTMemTmpAlloc(cuc * sizeof(RTUCS2));
|
---|
145 | if (pucszString)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * Do the translation.
|
---|
149 | */
|
---|
150 | if (MultiByteToWideChar(CP_ACP, 0, pszString, -1, pucszString, cuc) > 0)
|
---|
151 | {
|
---|
152 | /*
|
---|
153 | * Now we got UCS-2. Convert to UTF-8
|
---|
154 | */
|
---|
155 | rc = RTStrUcs2ToUtf8(ppszString, pucszString);
|
---|
156 | RTMemTmpFree(pucszString);
|
---|
157 | return rc;
|
---|
158 | }
|
---|
159 | RTMemTmpFree(pucszString);
|
---|
160 | /* translation error */
|
---|
161 | int iLastErr = GetLastError();
|
---|
162 | AssertMsgFailed(("ACP to Unicode translation failed. lasterr=%d\n", iLastErr));
|
---|
163 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
164 | }
|
---|
165 | else
|
---|
166 | rc = VERR_NO_TMP_MEMORY;
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | /* translation error */
|
---|
171 | int iLastErr = GetLastError();
|
---|
172 | AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
|
---|
173 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
174 | }
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|