summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/TCHAR.java
blob: 53612097f62a19c3bf072bde0d38607db27c5888 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.eclipse.swt.internal.win32;

/*
 * Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
 * This file is made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

/**
 * 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 class TCHAR {
	
int codePage;
char [] chars;
byte [] bytes;
int byteCount;

public final static int sizeof = OS.IsUnicode ? 2 : 1;

public TCHAR (int codePage, int length) {
	this.codePage = codePage;
	if (OS.IsUnicode) {
		chars = new char [length];
	} else {
		bytes = new byte [byteCount = length];
	}
}

public TCHAR (int codePage, char ch, boolean terminate) {
	this (codePage, String.valueOf (ch), terminate);
}

public TCHAR (int codePage, String string, boolean terminate) {
	this.codePage = codePage;
	int charCount = string.length ();
	char [] chars = new char [charCount + (terminate ? 1 : 0)];
	string.getChars (0, charCount, chars, 0);
	if (OS.IsUnicode) {
		this.chars = chars;
	} else {
		int cp = codePage != 0 ? codePage : OS.CP_ACP;
		bytes = new byte [byteCount = charCount * 2 + (terminate ? 1 : 0)];
		byteCount = OS.WideCharToMultiByte (cp, 0, chars, charCount, bytes, byteCount, null, null);
		if (terminate) byteCount++;
	}
}

public int length () {
	if (OS.IsUnicode) {
		return chars.length;
	} else {
		return byteCount;
	}
}

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

public int tcharAt (int index) {
	if (OS.IsUnicode) {
		return chars [index];
	} else {
		int ch = bytes [index] & 0xFF;
		if (OS.IsDBCSLeadByte ((byte) ch)) {
			ch = ch << 8 | (bytes [index + 1] & 0xFF);
		}
		return ch;
	}
}

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

public String toString (int start, int length) {
	if (OS.IsUnicode) {
		return new String (chars, start, length);
	} else {
		byte [] bytes = this.bytes;
		if (start != 0) {
			bytes = new byte [length];
			System.arraycopy (this.bytes, start, bytes, 0, length);
		}
		char [] chars = new char [length];
		int cp = codePage != 0 ? codePage : OS.CP_ACP;
		int charCount = OS.MultiByteToWideChar (cp, OS.MB_PRECOMPOSED, bytes, length, chars, length);
		return new String (chars, 0, charCount);
	}
}

}