comparison dwt/internal/Converter.d @ 9:ad2b69216039

moved org.eclipse.swt to dwt
author Frank Benoit <benoit@tionex.de>
date Sat, 05 Jan 2008 17:39:49 +0100
parents org/eclipse/swt/internal/Converter.d@94c5d794407f
children 63c023465156
comparison
equal deleted inserted replaced
8:a1f832ca7d17 9:ad2b69216039
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 *******************************************************************************/
11 module org.eclipse.swt.internal.Converter;
12
13
14 //import org.eclipse.swt.internal.gtk.OS;
15 import tango.stdc.stringz;
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 char[] defaultCodePage () {
52 return "UTF8";
53 }
54
55 public static wchar [] mbcsToWcs (char[] 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){
59 return EmptyCharArray;
60 }
61 wchar[] chars = ptr[ 0 .. items_written].dup;
62 g_free (ptr);
63 return chars;
64 }
65
66 /+ // only difference with String vs. char[] arg, so no diff in dwt
67 public static char [] wcsToMbcs (char[] 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 (char[] 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) {
83 return terminate ? NullByteArray : EmptyByteArray;
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 }