1 11 package org.eclipse.swt.internal.win32; 12 13 14 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 string, boolean terminate) { 65 this (codePage, getChars (string, terminate), false); 66 } 67 68 static char [] getChars (String 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 toString () { 110 return toString (0, length ()); 111 } 112 113 public String toString (int start, int length) { 114 if (OS.IsUnicode) { 115 return new String (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 (chars, 0, charCount); 126 } 127 } 128 129 } 130 131 | Popular Tags |