VirtualBox

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

Last change on this file since 85112 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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