KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > win32 > TCHAR


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.swt.internal.win32;
12
13
14 /**
15  * This class implements the conversions between unicode characters
16  * and the <em>platform supported</em> representation for characters.
17  * <p>
18  * Note that unicode characters which can not be found in the platform
19  * encoding will be converted to an arbitrary platform specific character.
20  * </p>
21  */

22
23 public class TCHAR {
24     int codePage;
25     public char [] chars;
26     public byte [] bytes;
27     int byteCount;
28
29 public final static int sizeof = OS.IsUnicode ? 2 : 1;
30
31 public TCHAR (int codePage, int length) {
32     this.codePage = codePage;
33     if (OS.IsUnicode) {
34         chars = new char [length];
35     } else {
36         bytes = new byte [byteCount = length];
37     }
38 }
39
40 public TCHAR (int codePage, char ch, boolean terminate) {
41     this (codePage, terminate ? new char [] {ch, '\0'} : new char [] {ch}, false);
42 }
43
44 public TCHAR (int codePage, char [] chars, boolean terminate) {
45     this.codePage = codePage;
46     int charCount = chars.length;
47     if (OS.IsUnicode) {
48         if (terminate) {
49             if (charCount == 0 || (charCount > 0 && chars [charCount - 1] != 0)) {
50                 char [] newChars = new char [charCount + 1];
51                 System.arraycopy (chars, 0, newChars, 0, charCount);
52                 chars = newChars;
53             }
54         }
55         this.chars = chars;
56     } else {
57         int cp = codePage != 0 ? codePage : OS.CP_ACP;
58         bytes = new byte [byteCount = charCount * 2 + (terminate ? 1 : 0)];
59         byteCount = OS.WideCharToMultiByte (cp, 0, chars, charCount, bytes, byteCount, null, null);
60         if (terminate) byteCount++;
61     }
62 }
63
64 public TCHAR (int codePage, String JavaDoc string, boolean terminate) {
65     this (codePage, getChars (string, terminate), false);
66 }
67
68 static char [] getChars (String JavaDoc string, boolean terminate) {
69     int length = string.length ();
70     char [] chars = new char [length + (terminate ? 1 : 0)];
71     string.getChars (0, length, chars, 0);
72     return chars;
73 }
74
75 public int length () {
76     if (OS.IsUnicode) {
77         return chars.length;
78     } else {
79         return byteCount;
80     }
81 }
82
83 public int strlen () {
84     if (OS.IsUnicode) {
85         for (int i=0; i<chars.length; i++) {
86             if (chars [i] == '\0') return i;
87         }
88         return chars.length;
89     } else {
90         for (int i=0; i<byteCount; i++) {
91             if (bytes [i] == '\0') return i;
92         }
93         return byteCount;
94     }
95 }
96
97 public int tcharAt (int index) {
98     if (OS.IsUnicode) {
99         return chars [index];
100     } else {
101         int ch = bytes [index] & 0xFF;
102         if (OS.IsDBCSLeadByte ((byte) ch)) {
103             ch = ch << 8 | (bytes [index + 1] & 0xFF);
104         }
105         return ch;
106     }
107 }
108
109 public String JavaDoc toString () {
110     return toString (0, length ());
111 }
112
113 public String JavaDoc toString (int start, int length) {
114     if (OS.IsUnicode) {
115         return new String JavaDoc (chars, start, length);
116     } else {
117         byte [] bytes = this.bytes;
118         if (start != 0) {
119             bytes = new byte [length];
120             System.arraycopy (this.bytes, start, bytes, 0, length);
121         }
122         char [] chars = new char [length];
123         int cp = codePage != 0 ? codePage : OS.CP_ACP;
124         int charCount = OS.MultiByteToWideChar (cp, OS.MB_PRECOMPOSED, bytes, length, chars, length);
125         return new String JavaDoc (chars, 0, charCount);
126     }
127 }
128
129 }
130
131
Popular Tags