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";
42     public static const char  [] EmptyByteArray;
43     public static const wchar [] EmptyCharArray;
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), cast(int)/*64bit*/buffer.length,
58                                   null, &items_written, null);
59     if (!ptr){
60         return cast(wchar[])EmptyCharArray;
61     }
62     wchar[] chars = ptr[ 0 .. items_written].dup;
63     g_free (ptr);
64     return chars;
65 }
66 
67 /+ // only difference with String vs. String arg, so no diff in org.eclipse.swt
68 public static char [] wcsToMbcs (String codePage, String str, bool terminate) {
69     int length = str.length ();
70     wchar [] buffer = new wchar [length];
71     string.getChars (0, length, buffer, 0);
72     return wcsToMbcs (codePage, buffer, terminate);
73 }
74 +/
75 
76 public static char [] wcsToMbcs (String codePage, wchar [] buffer, bool terminate) {
77     int items_read, items_written;
78     /*
79     * Note that g_utf16_to_utf8()  stops converting
80     * when it finds the first NULL.
81     */
82     char* ptr = g_utf16_to_utf8 (toString16z(buffer), cast(int)/*64bit*/buffer.length,
83                                  & items_read, & items_written, null);
84     if (!ptr) {
85         return terminate ?cast(char[]) NullByteArray : cast(char[])EmptyByteArray;
86     }
87     char [] bytes = new char [items_written + (terminate ? 1 : 0)];
88     bytes[ 0 .. items_written ] = ptr[ 0 .. items_written ];
89     if( terminate ){
90         bytes[ items_written ] = 0;
91     }
92     g_free (ptr);
93     return bytes;
94 }
95 
96 }