VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win32/utf8-win32.cpp@ 799

Last change on this file since 799 was 1, checked in by vboxsync, 55 years ago

import

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