1 /*
2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 #include <windows.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28
29
30 /*
31 * Convert UTF-8 to a platform string
32 */
33 int
convertUft8ToPlatformString(char * utf8_str,int utf8_len,char * platform_str,int platform_len)34 convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {
35 LANGID langID;
36 LCID localeID;
37 TCHAR strCodePage[7]; // ANSI code page id
38 UINT codePage;
39 int wlen, plen;
40 WCHAR* wstr;
41
42 /*
43 * Get the code page for this locale
44 */
45 langID = LANGIDFROMLCID(GetUserDefaultLCID());
46 localeID = MAKELCID(langID, SORT_DEFAULT);
47 if (GetLocaleInfo(localeID, LOCALE_IDEFAULTANSICODEPAGE,
48 strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 ) {
49 codePage = atoi(strCodePage);
50 } else {
51 codePage = GetACP();
52 }
53
54 /*
55 * To convert the string to platform encoding we must first convert
56 * to unicode, and then convert to the platform encoding
57 */
58 plen = -1;
59 wlen = MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, NULL, 0);
60 if (wlen > 0) {
61 wstr = (WCHAR*)malloc(wlen * sizeof(WCHAR));
62 if (wstr != NULL) {
63 if (MultiByteToWideChar(CP_UTF8,
64 0,
65 utf8_str,
66 utf8_len,
67 wstr, wlen) > 0) {
68 plen = WideCharToMultiByte(codePage,
69 0,
70 wstr,
71 wlen,
72 platform_str,
73 platform_len,
74 NULL,
75 NULL);
76 if (plen >= 0) {
77 platform_str[plen] = '\0';
78 }
79 free(wstr);
80 }
81 }
82 }
83 return plen;
84 }
85