VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/utf8-win.cpp@ 77641

Last change on this file since 77641 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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