25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.internal.Converter;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 extern(C) {
|
|
18 struct GError{};
|
|
19 char* g_utf16_to_utf8 ( wchar *str,
|
|
20 int len,
|
|
21 int *items_read,
|
|
22 int *items_written,
|
|
23 GError **error);
|
|
24 wchar* g_utf8_to_utf16 ( char *str,
|
|
25 int len,
|
|
26 int *items_read,
|
|
27 int *items_written,
|
|
28 GError **error);
|
|
29 void g_free (void* ptr );
|
|
30 }
|
|
31
|
|
32 /**
|
|
33 * This class implements the conversions between unicode characters
|
|
34 * and the <em>platform supported</em> representation for characters.
|
|
35 * <p>
|
|
36 * Note that, unicode characters which can not be found in the platform
|
|
37 * encoding will be converted to an arbitrary platform specific character.
|
|
38 * </p>
|
|
39 */
|
|
40 public final class Converter {
|
|
41 public static const char [] NullByteArray = "\0";
|
51
|
42 public static const char [] EmptyByteArray;
|
|
43 public static const wchar [] EmptyCharArray;
|
25
|
44
|
|
45 /**
|
|
46 * Returns the default code page for the platform where the
|
|
47 * application is currently running.
|
|
48 *
|
|
49 * @return the default code page
|
|
50 */
|
|
51 public static String defaultCodePage () {
|
|
52 return "UTF8";
|
|
53 }
|
|
54
|
|
55 public static wchar [] mbcsToWcs (String codePage, char [] buffer) {
|
|
56 int items_written;
|
|
57 wchar* ptr = g_utf8_to_utf16 (toStringz(buffer), buffer.length, null, &items_written, null);
|
|
58 if (!ptr){
|
51
|
59 return cast(wchar[])EmptyCharArray;
|
25
|
60 }
|
|
61 wchar[] chars = ptr[ 0 .. items_written].dup;
|
|
62 g_free (ptr);
|
|
63 return chars;
|
|
64 }
|
|
65
|
|
66 /+ // only difference with String vs. String arg, so no diff in org.eclipse.swt
|
|
67 public static char [] wcsToMbcs (String codePage, String str, bool terminate) {
|
|
68 int length = str.length ();
|
|
69 wchar [] buffer = new wchar [length];
|
|
70 string.getChars (0, length, buffer, 0);
|
|
71 return wcsToMbcs (codePage, buffer, terminate);
|
|
72 }
|
|
73 +/
|
|
74
|
|
75 public static char [] wcsToMbcs (String codePage, wchar [] buffer, bool terminate) {
|
|
76 int items_read, items_written;
|
|
77 /*
|
|
78 * Note that g_utf16_to_utf8() stops converting
|
|
79 * when it finds the first NULL.
|
|
80 */
|
|
81 char* ptr = g_utf16_to_utf8 (toString16z(buffer), buffer.length, & items_read, & items_written, null);
|
|
82 if (!ptr) {
|
51
|
83 return terminate ?cast(char[]) NullByteArray : cast(char[])EmptyByteArray;
|
25
|
84 }
|
|
85 char [] bytes = new char [items_written + (terminate ? 1 : 0)];
|
|
86 bytes[ 0 .. items_written ] = ptr[ 0 .. items_written ];
|
|
87 if( terminate ){
|
|
88 bytes[ items_written ] = 0;
|
|
89 }
|
|
90 g_free (ptr);
|
|
91 return bytes;
|
|
92 }
|
|
93
|
|
94 }
|