view dwt/internal/Converter.d @ 10:63c023465156

moved from org.eclipse.swt to dwt
author Frank Benoit <benoit@tionex.de>
date Sat, 05 Jan 2008 17:58:37 +0100
parents ad2b69216039
children 8cec8f536af3
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.internal.Converter;


//import dwt.internal.gtk.OS;
import tango.stdc.stringz;

extern(C) {
    struct GError{};
    char*     g_utf16_to_utf8     ( wchar  *str,
                    int             len,
                    int             *items_read,
                    int             *items_written,
                    GError          **error);
    wchar* g_utf8_to_utf16     ( char      *str,
                    int              len,
                    int             *items_read,
                    int             *items_written,
                    GError          **error);
    void g_free (void* ptr );
}

/**
 * This class implements the conversions between unicode characters
 * and the <em>platform supported</em> representation for characters.
 * <p>
 * Note that, unicode characters which can not be found in the platform
 * encoding will be converted to an arbitrary platform specific character.
 * </p>
 */
public final class Converter {
	public static const char  [] NullByteArray = "\0";
	public static const char  [] EmptyByteArray = "";
	public static const wchar [] EmptyCharArray = "";

/**
 * Returns the default code page for the platform where the
 * application is currently running.
 *
 * @return the default code page
 */
public static char[] defaultCodePage () {
	return "UTF8";
}

public static wchar [] mbcsToWcs (char[] codePage, char [] buffer) {
	int items_written;
	wchar* ptr = g_utf8_to_utf16 (toStringz(buffer), buffer.length, null, &items_written, null);
	if (!ptr){
        return EmptyCharArray;
    }
    wchar[] chars = ptr[ 0 .. items_written].dup;
	g_free (ptr);
	return chars;
}

/+ // only difference with String vs. char[] arg, so no diff in dwt
public static char [] wcsToMbcs (char[] codePage, String str, bool terminate) {
	int length = str.length ();
	wchar [] buffer = new wchar [length];
	string.getChars (0, length, buffer, 0);
	return wcsToMbcs (codePage, buffer, terminate);
}
+/

public static char [] wcsToMbcs (char[] codePage, wchar [] buffer, bool terminate) {
	int items_read, items_written;
	/*
	* Note that g_utf16_to_utf8()  stops converting
	* when it finds the first NULL.
	*/
	char* ptr = g_utf16_to_utf8 (toString16z(buffer), buffer.length, & items_read, & items_written, null);
	if (!ptr) {
        return terminate ? NullByteArray : EmptyByteArray;
    }
	char [] bytes = new char [items_written + (terminate ? 1 : 0)];
    bytes[ 0 .. items_written ] = ptr[ 0 .. items_written ];
    if( terminate ){
        bytes[ items_written ] = 0;
    }
	g_free (ptr);
	return bytes;
}

}