VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/* $Id: utf8-win.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - UTF8 helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 <Windows.h>
33
34#include <iprt/string.h>
35#include <iprt/alloc.h>
36#include <iprt/assert.h>
37#include <iprt/err.h>
38
39
40/**
41 * Allocates tmp buffer, translates pszString from UTF8 to current codepage.
42 *
43 * @returns iprt status code.
44 * @param ppszString Receives pointer of allocated native CP string.
45 * The returned pointer must be freed using RTStrFree().
46 * @param pszString UTF-8 string to convert.
47 */
48RTR3DECL(int) RTStrUtf8ToCurrentCP(char **ppszString, const char *pszString)
49{
50 Assert(ppszString);
51 Assert(pszString);
52
53 /*
54 * Check for zero length input string.
55 */
56 if (!*pszString)
57 {
58 *ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
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 = RTStrToUtf16(pszString, &pwszString);
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)RTMemTmpAlloc(cbResult);
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/**
118 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
119 *
120 * @returns iprt status code.
121 * @param ppszString Receives pointer of allocated UTF-8 string.
122 * The returned pointer must be freed using RTStrFree().
123 * @param pszString Native string to convert.
124 */
125RTR3DECL(int) RTStrCurrentCPToUtf8(char **ppszString, const char *pszString)
126{
127 Assert(ppszString);
128 Assert(pszString);
129 *ppszString = NULL;
130
131 /** @todo is there a quicker way? Currently: ACP -> UTF-16 -> UTF-8 */
132
133 size_t cch = strlen(pszString);
134 if (cch <= 0)
135 {
136 /* zero length string passed. */
137 *ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
138 if (*ppszString)
139 return VINF_SUCCESS;
140 return VERR_NO_TMP_MEMORY;
141 }
142
143 /*
144 * First calc result string length.
145 */
146 int rc;
147 int cwc = MultiByteToWideChar(CP_ACP, 0, pszString, -1, NULL, 0);
148 if (cwc > 0)
149 {
150 /*
151 * Alloc space for result buffer.
152 */
153 PRTUTF16 pwszString = (PRTUTF16)RTMemTmpAlloc(cwc * sizeof(RTUTF16));
154 if (pwszString)
155 {
156 /*
157 * Do the translation.
158 */
159 if (MultiByteToWideChar(CP_ACP, 0, pszString, -1, pwszString, cwc) > 0)
160 {
161 /*
162 * Now we got UTF-16, convert it to UTF-8
163 */
164 rc = RTUtf16ToUtf8(pwszString, ppszString);
165 RTMemTmpFree(pwszString);
166 return rc;
167 }
168 RTMemTmpFree(pwszString);
169 /* translation error */
170 int iLastErr = GetLastError();
171 AssertMsgFailed(("ACP to Unicode translation failed. lasterr=%d\n", iLastErr));
172 rc = RTErrConvertFromWin32(iLastErr);
173 }
174 else
175 rc = VERR_NO_TMP_MEMORY;
176 }
177 else
178 {
179 /* translation error */
180 int iLastErr = GetLastError();
181 AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
182 rc = RTErrConvertFromWin32(iLastErr);
183 }
184 return rc;
185}
186
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