VirtualBox

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

Last change on this file since 8170 was 8170, checked in by vboxsync, 16 years ago

Rebranding: replacing more innotek strings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/* $Id: utf8-win.cpp 8170 2008-04-18 17:52:25Z vboxsync $ */
2/** @file
3 * Incredibly Portable Runtime - UTF8 helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_UTF8
36#include <Windows.h>
37
38#include <iprt/string.h>
39#include <iprt/alloc.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42
43
44/**
45 * Allocates tmp buffer, translates pszString from UTF8 to current codepage.
46 *
47 * @returns iprt status code.
48 * @param ppszString Receives pointer of allocated native CP string.
49 * The returned pointer must be freed using RTStrFree().
50 * @param pszString UTF-8 string to convert.
51 */
52RTR3DECL(int) RTStrUtf8ToCurrentCP(char **ppszString, const char *pszString)
53{
54 Assert(ppszString);
55 Assert(pszString);
56
57 /*
58 * Check for zero length input string.
59 */
60 if (!*pszString)
61 {
62 *ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
63 if (*ppszString)
64 return VINF_SUCCESS;
65 return VERR_NO_TMP_MEMORY;
66 }
67
68 *ppszString = NULL;
69
70 /*
71 * Convert to wide char first.
72 */
73 PRTUTF16 pwszString = NULL;
74 int rc = RTStrToUtf16(pszString, &pwszString);
75 if (RT_FAILURE(rc))
76 return rc;
77
78 /*
79 * First calc result string length.
80 */
81 int cbResult = WideCharToMultiByte(CP_ACP, 0, pwszString, -1, NULL, 0, NULL, NULL);
82 if (cbResult > 0)
83 {
84 /*
85 * Alloc space for result buffer.
86 */
87 LPSTR lpString = (LPSTR)RTMemTmpAlloc(cbResult);
88 if (lpString)
89 {
90 /*
91 * Do the translation.
92 */
93 if (WideCharToMultiByte(CP_ACP, 0, pwszString, -1, lpString, cbResult, NULL, NULL) > 0)
94 {
95 /* ok */
96 *ppszString = lpString;
97 RTMemTmpFree(pwszString);
98 return VINF_SUCCESS;
99 }
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 else
107 rc = VERR_NO_TMP_MEMORY;
108 RTMemTmpFree(lpString);
109 }
110 else
111 {
112 /* translation error */
113 int iLastErr = GetLastError();
114 AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
115 rc = RTErrConvertFromWin32(iLastErr);
116 }
117 RTMemTmpFree(pwszString);
118 return rc;
119}
120
121/**
122 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
123 *
124 * @returns iprt status code.
125 * @param ppszString Receives pointer of allocated UTF-8 string.
126 * The returned pointer must be freed using RTStrFree().
127 * @param pszString Native string to convert.
128 */
129RTR3DECL(int) RTStrCurrentCPToUtf8(char **ppszString, const char *pszString)
130{
131 Assert(ppszString);
132 Assert(pszString);
133 *ppszString = NULL;
134
135 /** @todo is there a quicker way? Currently: ACP -> UTF-16 -> UTF-8 */
136
137 size_t cch = strlen(pszString);
138 if (cch <= 0)
139 {
140 /* zero length string passed. */
141 *ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
142 if (*ppszString)
143 return VINF_SUCCESS;
144 return VERR_NO_TMP_MEMORY;
145 }
146
147 /*
148 * First calc result string length.
149 */
150 int rc;
151 int cwc = MultiByteToWideChar(CP_ACP, 0, pszString, -1, NULL, 0);
152 if (cwc > 0)
153 {
154 /*
155 * Alloc space for result buffer.
156 */
157 PRTUTF16 pwszString = (PRTUTF16)RTMemTmpAlloc(cwc * sizeof(RTUTF16));
158 if (pwszString)
159 {
160 /*
161 * Do the translation.
162 */
163 if (MultiByteToWideChar(CP_ACP, 0, pszString, -1, pwszString, cwc) > 0)
164 {
165 /*
166 * Now we got UTF-16, convert it to UTF-8
167 */
168 rc = RTUtf16ToUtf8(pwszString, ppszString);
169 RTMemTmpFree(pwszString);
170 return rc;
171 }
172 RTMemTmpFree(pwszString);
173 /* translation error */
174 int iLastErr = GetLastError();
175 AssertMsgFailed(("ACP to Unicode translation failed. lasterr=%d\n", iLastErr));
176 rc = RTErrConvertFromWin32(iLastErr);
177 }
178 else
179 rc = VERR_NO_TMP_MEMORY;
180 }
181 else
182 {
183 /* translation error */
184 int iLastErr = GetLastError();
185 AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
186 rc = RTErrConvertFromWin32(iLastErr);
187 }
188 return rc;
189}
190
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