Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 55f17f3c947b283fefb610de75a2fe37984227fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
 * Copyright (c) 2000, 2014 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
 *******************************************************************************/
package org.eclipse.swt.internal.win32;

import java.util.Arrays;

/**
 * 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>
 *
 * @jniclass flags=no_gen
 */
public class TCHAR {
	public char [] chars;
	public byte [] bytes;

public final static int sizeof = 2;

public TCHAR (int codePage, int length) {
	chars = new char [length];
}

public TCHAR (int codePage, char ch, boolean terminate) {
	this (codePage, terminate ? new char [] {ch, '\0'} : new char [] {ch}, false);
}

public TCHAR (int codePage, char [] chars, boolean terminate) {
	int charCount = chars.length;
	if (terminate) {
		if (charCount == 0 || (charCount > 0 && chars [charCount - 1] != 0)) {
			char [] newChars = new char [charCount + 1];
			System.arraycopy (chars, 0, newChars, 0, charCount);
			chars = newChars;
		}
	}
	this.chars = chars;
}

public TCHAR (int codePage, String string, boolean terminate) {
	this (codePage, getChars (string, terminate), false);
}

static char [] getChars (String string, boolean terminate) {
	int length = string.length ();
	char [] chars = new char [length + (terminate ? 1 : 0)];
	string.getChars (0, length, chars, 0);
	return chars;
}

public void clear() {
	Arrays.fill (chars, (char) 0);
}

public int length () {
	return chars.length;
}

public int strlen () {
	for (int i=0; i<chars.length; i++) {
		if (chars [i] == '\0') return i;
	}
	return chars.length;
}

public int tcharAt (int index) {
	return chars [index];
}

@Override
public String toString () {
	return toString (0, length ());
}

public String toString (int start, int length) {
	return new String (chars, start, length);
}

}

Back to the top