Mercurial > projects > dwt-linux
annotate dwt/internal/Compatibility.d @ 238:380bad9f6852
reverted char[] to String
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:42:55 +0200 |
parents | 0f12f6bb9739 |
children | ce446666f5a2 |
rev | line source |
---|---|
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
1 /******************************************************************************* |
0 | 2 * Copyright (c) 2000, 2006 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
0 | 12 *******************************************************************************/ |
10
63c023465156
moved from org.eclipse.swt to dwt
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
13 module dwt.internal.Compatibility; |
0 | 14 |
238 | 15 import dwt.dwthelper.utils; |
16 | |
0 | 17 /+ |
18 import java.io.*; | |
19 import java.text.MessageFormat; | |
20 import java.util.MissingResourceException; | |
21 import java.util.ResourceBundle; | |
22 import java.util.zip.InflaterInputStream; | |
23 +/ | |
24 | |
71 | 25 import dwt.DWT; |
12 | 26 public import dwt.dwthelper.FileInputStream; |
27 public import dwt.dwthelper.FileOutputStream; | |
28 public import dwt.dwthelper.InflaterInputStream; | |
1 | 29 |
0 | 30 import Math = tango.math.Math; |
31 import Unicode = tango.text.Unicode; | |
32 import tango.sys.Process; | |
33 | |
34 /** | |
35 * This class is a placeholder for utility methods commonly | |
36 * used on J2SE platforms but not supported on some J2ME | |
37 * profiles. | |
38 * <p> | |
39 * It is part of our effort to provide support for both J2SE | |
40 * and J2ME platforms. | |
41 * </p> | |
42 * <p> | |
43 * IMPORTANT: some of the methods have been modified from their | |
44 * J2SE parents. Refer to the description of each method for | |
45 * specific changes. | |
46 * </p> | |
47 * <ul> | |
48 * <li>Exceptions thrown may differ since J2ME's set of | |
49 * exceptions is a subset of J2SE's one. | |
50 * </li> | |
51 * <li>The range of the mathematic functions is subject to | |
52 * change. | |
53 * </li> | |
54 * </ul> | |
55 */ | |
56 public final class Compatibility { | |
57 | |
58 /** | |
59 * Returns the PI constant as a double. | |
60 */ | |
61 public static const real PI = Math.PI; | |
62 | |
63 static const real toRadians = PI / 180; | |
64 | |
65 /** | |
66 * Answers the length of the side adjacent to the given angle | |
67 * of a right triangle. In other words, it returns the integer | |
68 * conversion of length * cos (angle). | |
69 * <p> | |
70 * IMPORTANT: the j2me version has an additional restriction on | |
71 * the argument. length must be between -32767 and 32767 (inclusive). | |
72 * </p> | |
73 * | |
74 * @param angle the angle in degrees | |
75 * @param length the length of the triangle's hypotenuse | |
76 * @return the integer conversion of length * cos (angle) | |
77 */ | |
78 public static int cos(int angle, int length) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
79 return cast(int)(Math.cos(angle * toRadians) * length); |
0 | 80 } |
81 | |
82 /** | |
83 * Answers the length of the side opposite to the given angle | |
84 * of a right triangle. In other words, it returns the integer | |
85 * conversion of length * sin (angle). | |
86 * <p> | |
87 * IMPORTANT: the j2me version has an additional restriction on | |
88 * the argument. length must be between -32767 and 32767 (inclusive). | |
89 * </p> | |
90 * | |
91 * @param angle the angle in degrees | |
92 * @param length the length of the triangle's hypotenuse | |
93 * @return the integer conversion of length * sin (angle) | |
94 */ | |
95 public static int sin(int angle, int length) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
96 return cast(int)(Math.sin(angle * toRadians) * length); |
0 | 97 } |
98 | |
99 /** | |
100 * Answers the most negative (i.e. closest to negative infinity) | |
101 * integer value which is greater than the number obtained by dividing | |
102 * the first argument p by the second argument q. | |
103 * | |
104 * @param p numerator | |
105 * @param q denominator (must be different from zero) | |
106 * @return the ceiling of the rational number p / q. | |
107 */ | |
108 public static int ceil(int p, int q) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
109 return cast(int)Math.ceil(cast(float)p / q); |
0 | 110 } |
111 | |
112 /** | |
113 * Answers the most positive (i.e. closest to positive infinity) | |
114 * integer value which is less than the number obtained by dividing | |
115 * the first argument p by the second argument q. | |
116 * | |
117 * @param p numerator | |
118 * @param q denominator (must be different from zero) | |
119 * @return the floor of the rational number p / q. | |
120 */ | |
121 public static int floor(int p, int q) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
122 return cast(int)Math.floor(cast(double)p / q); |
0 | 123 } |
124 | |
125 /** | |
126 * Answers the result of rounding to the closest integer the number obtained | |
127 * by dividing the first argument p by the second argument q. | |
128 * <p> | |
129 * IMPORTANT: the j2me version has an additional restriction on | |
130 * the arguments. p must be within the range 0 - 32767 (inclusive). | |
131 * q must be within the range 1 - 32767 (inclusive). | |
132 * </p> | |
133 * | |
134 * @param p numerator | |
135 * @param q denominator (must be different from zero) | |
136 * @return the closest integer to the rational number p / q | |
137 */ | |
138 public static int round(int p, int q) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
139 return cast(int)Math.round(cast(float)p / q); |
0 | 140 } |
141 | |
142 /** | |
143 * Returns 2 raised to the power of the argument. | |
144 * | |
145 * @param n an int value between 0 and 30 (inclusive) | |
146 * @return 2 raised to the power of the argument | |
147 * | |
148 * @exception IllegalArgumentException <ul> | |
149 * <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li> | |
150 * </ul> | |
151 */ | |
152 public static int pow2(int n) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
153 if (n >= 1 && n <= 30) |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
154 return 2 << (n - 1); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
155 else if (n != 0) { |
71 | 156 DWT.error(DWT.ERROR_INVALID_RANGE); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
157 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
158 return 1; |
0 | 159 } |
4 | 160 |
0 | 161 /** |
162 * Open a file if such things are supported. | |
163 * | |
164 * @param filename the name of the file to open | |
165 * @return a stream on the file if it could be opened. | |
166 * @exception IOException | |
167 */ | |
238 | 168 public static InputStream newFileInputStream(String filename) { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
169 return new FileInputStream(filename); |
0 | 170 } |
171 | |
172 /** | |
173 * Open a file if such things are supported. | |
174 * | |
175 * @param filename the name of the file to open | |
176 * @return a stream on the file if it could be opened. | |
177 * @exception IOException | |
178 */ | |
238 | 179 public static OutputStream newFileOutputStream(String filename) { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
180 return new FileOutputStream(filename); |
0 | 181 } |
182 | |
183 /** | |
184 * Create an InflaterInputStream if such things are supported. | |
185 * | |
186 * @param stream the input stream | |
187 * @return a inflater stream or <code>null</code> | |
188 * @exception IOException | |
189 * | |
190 * @since 3.3 | |
191 */ | |
12 | 192 public static InflaterInputStream newInflaterInputStream(InputStream stream) { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
193 return new InflaterInputStream(stream); |
0 | 194 } |
4 | 195 |
0 | 196 /** |
197 * Answers whether the character is a letter. | |
198 * | |
199 * @param c the character | |
200 * @return true when the character is a letter | |
201 */ | |
202 public static bool isLetter(dchar c) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
203 return Unicode.isLetter(c); |
0 | 204 } |
205 | |
206 /** | |
207 * Answers whether the character is a letter or a digit. | |
208 * | |
209 * @param c the character | |
210 * @return true when the character is a letter or a digit | |
211 */ | |
212 public static bool isLetterOrDigit(dchar c) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
213 return Unicode.isLetterOrDigit(c); |
0 | 214 } |
215 | |
216 /** | |
217 * Answers whether the character is a Unicode space character. | |
218 * | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
219 * @param c the character |
0 | 220 * @return true when the character is a Unicode space character |
221 */ | |
222 public static bool isSpaceChar(dchar c) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
223 return Unicode.isSpace(c); |
0 | 224 } |
225 | |
226 /** | |
227 * Answers whether the character is a whitespace character. | |
228 * | |
229 * @param c the character to test | |
230 * @return true if the character is whitespace | |
231 */ | |
232 public static bool isWhitespace(dchar c) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
233 return Unicode.isWhitespace(c); |
0 | 234 } |
235 | |
236 /** | |
237 * Execute a program in a separate platform process if the | |
238 * underlying platform support this. | |
239 * <p> | |
240 * The new process inherits the environment of the caller. | |
241 * </p> | |
242 * | |
243 * @param prog the name of the program to execute | |
244 * | |
245 * @exception ProcessException | |
246 * if the program cannot be executed | |
247 */ | |
238 | 248 public static void exec(String prog) { |
0 | 249 auto proc = new Process( prog ); |
250 proc.execute; | |
251 } | |
252 | |
253 /** | |
254 * Execute progArray[0] in a separate platform process if the | |
255 * underlying platform support this. | |
256 * <p> | |
257 * The new process inherits the environment of the caller. | |
258 * <p> | |
259 * | |
260 * @param progArray array containing the program to execute and its arguments | |
261 * | |
262 * @exception ProcessException | |
263 * if the program cannot be executed | |
264 */ | |
238 | 265 public static void exec(String[] progArray) { |
0 | 266 auto proc = new Process( progArray ); |
267 proc.execute; | |
268 } | |
269 /++ PORTING_LEFT | |
270 private static ResourceBundle msgs = null; | |
271 | |
272 /** | |
273 * Returns the NLS'ed message for the given argument. This is only being | |
71 | 274 * called from DWT. |
0 | 275 * |
276 * @param key the key to look up | |
277 * @return the message for the given key | |
278 * | |
71 | 279 * @see DWT#getMessage(String) |
0 | 280 */ |
281 public static String getMessage(String key) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
282 String answer = key; |
0 | 283 |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
284 if (key == null) { |
71 | 285 DWT.error (DWT.ERROR_NULL_ARGUMENT); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
286 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
287 if (msgs == null) { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
288 try { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
289 msgs = ResourceBundle.getBundle("dwt.internal.SWTMessages"); //$NON-NLS-1$ |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
290 } catch (MissingResourceException ex) { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
291 answer = key + " (no resource bundle)"; //$NON-NLS-1$ |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
292 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
293 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
294 if (msgs != null) { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
295 try { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
296 answer = msgs.getString(key); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
297 } catch (MissingResourceException ex2) {} |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
298 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
299 return answer; |
0 | 300 } |
301 | |
302 public static String getMessage(String key, Object[] args) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
303 String answer = key; |
0 | 304 |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
305 if (key == null || args == null) { |
71 | 306 DWT.error (DWT.ERROR_NULL_ARGUMENT); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
307 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
308 if (msgs == null) { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
309 try { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
310 msgs = ResourceBundle.getBundle("dwt.internal.SWTMessages"); //$NON-NLS-1$ |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
311 } catch (MissingResourceException ex) { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
312 answer = key + " (no resource bundle)"; //$NON-NLS-1$ |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
313 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
314 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
315 if (msgs != null) { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
316 try { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
317 MessageFormat formatter = new MessageFormat(""); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
318 formatter.applyPattern(msgs.getString(key)); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
319 answer = formatter.format(args); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
320 } catch (MissingResourceException ex2) {} |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
321 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
322 return answer; |
0 | 323 } |
46 | 324 ++/ |
325 | |
0 | 326 |
327 /** | |
328 * Interrupt the current thread. | |
329 * <p> | |
330 * Note that this is not available on CLDC. | |
331 * </p> | |
332 */ | |
333 public static void interrupt() { | |
46 | 334 //PORTING_FIXME: how to implement?? |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
335 //Thread.currentThread().interrupt(); |
0 | 336 } |
46 | 337 |
0 | 338 /** |
339 * Compares two instances of class String ignoring the case of the | |
340 * characters and answers if they are equal. | |
341 * | |
342 * @param s1 string | |
343 * @param s2 string | |
344 * @return true if the two instances of class String are equal | |
345 */ | |
238 | 346 public static bool equalsIgnoreCase(String s1, String s2) { |
347 String s1b = new char[ s1.length ]; | |
348 String s2b = new char[ s1.length ]; | |
0 | 349 scope(exit){ |
350 delete s1b; | |
351 delete s2b; | |
352 } | |
238 | 353 String s1c = Unicode.toFold( s1, s1b ); |
354 String s2c = Unicode.toFold( s2, s2b ); | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
46
diff
changeset
|
355 return s1c == s2c; |
0 | 356 } |
357 | |
358 } |