Mercurial > projects > dwt-mac
comparison dwt/internal/Library.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | e831403a80a9 f565d3a95c0a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 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 * Port to the D Programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 * Jacob Carlborg <jacob.carlborg@gmail.com> | |
14 *******************************************************************************/ | |
15 module dwt.internal.Library; | |
16 | |
17 import tango.util.Convert; | |
18 | |
19 import dwt.dwthelper.utils; | |
20 | |
21 //do it here, so it can be evaluated at compile time | |
22 //this saves a static ctor. | |
23 private int buildSWT_VERSION (int major, int minor) | |
24 { | |
25 return major * 1000 + minor; | |
26 } | |
27 | |
28 public class Library | |
29 { | |
30 | |
31 /* DWT Version - Mmmm (M=major, mmm=minor) */ | |
32 | |
33 /** | |
34 * DWT Major version number (must be >= 0) | |
35 */ | |
36 static const int MAJOR_VERSION = 3; | |
37 | |
38 /** | |
39 * DWT Minor version number (must be in the range 0..999) | |
40 */ | |
41 static const int MINOR_VERSION = 440; | |
42 | |
43 /** | |
44 * DWT revision number (must be >= 0) | |
45 */ | |
46 static const int REVISION = 0; | |
47 | |
48 /** | |
49 * The JAVA and DWT versions | |
50 */ | |
51 //public static const int JAVA_VERSION; | |
52 public static const int SWT_VERSION = .buildSWT_VERSION(MAJOR_VERSION, | |
53 MINOR_VERSION); | |
54 | |
55 version (darwin) | |
56 static const String SEPARATOR = "\n"; | |
57 | |
58 else | |
59 static assert(false, "Only Mac OS X supported for this port"); | |
60 | |
61 static int parseVersion (String aVersion) | |
62 { | |
63 if (aVersion == null) | |
64 return 0; | |
65 int major = 0, minor = 0, micro = 0; | |
66 int length = aVersion.length, index = 0, start = 0; | |
67 bool isDigit (char c) | |
68 { | |
69 return c >= '0' && c <= '9'; | |
70 } | |
71 while (index < length && isDigit(aVersion[index])) | |
72 index++; | |
73 try | |
74 { | |
75 if (start < length) | |
76 major = to!(int)(aVersion[start .. index]); | |
77 } | |
78 catch (ConversionException e) | |
79 { | |
80 } | |
81 start = ++index; | |
82 while (index < length && isDigit(aVersion[index])) | |
83 index++; | |
84 try | |
85 { | |
86 if (start < length) | |
87 minor = to!(int)(aVersion[start .. index]); | |
88 } | |
89 catch (ConversionException e) | |
90 { | |
91 } | |
92 start = ++index; | |
93 while (index < length && isDigit(aVersion[index])) | |
94 index++; | |
95 try | |
96 { | |
97 if (start < length) | |
98 micro = to!(int)(aVersion[start .. index]); | |
99 } | |
100 catch (ConversionException e) | |
101 { | |
102 } | |
103 return buildJAVA_VERSION(major, minor, micro); | |
104 } | |
105 | |
106 /** | |
107 * Returns the Java version number as an integer. | |
108 * | |
109 * @param major | |
110 * @param minor | |
111 * @param micro | |
112 * @return the version | |
113 */ | |
114 public static int buildJAVA_VERSION (int major, int minor, int micro) | |
115 { | |
116 return (major << 16) + (minor << 8) + micro; | |
117 } | |
118 | |
119 /** | |
120 * Returns the DWT version number as an integer. | |
121 * | |
122 * @param major | |
123 * @param minor | |
124 * @return the version | |
125 */ | |
126 public static int buildSWT_VERSION (int major, int minor) | |
127 { | |
128 return .buildSWT_VERSION(major, minor); | |
129 } | |
130 /+ PORTING_LEFT | |
131 static bool extract (String fileName, String mappedName) { | |
132 FileOutputStream os = null; | |
133 InputStream is = null; | |
134 File file = new File(fileName); | |
135 try { | |
136 if (!file.exists ()) { | |
137 is = Library.class.getResourceAsStream ("/" + mappedName); //$NON-NLS-1$ | |
138 if (is !is null) { | |
139 int read; | |
140 byte [] buffer = new byte [4096]; | |
141 os = new FileOutputStream (fileName); | |
142 while ((read = is.read (buffer)) !is -1) { | |
143 os.write(buffer, 0, read); | |
144 } | |
145 os.close (); | |
146 is.close (); | |
147 if (!Platform.PLATFORM.equals ("win32")) { //$NON-NLS-1$ | |
148 try { | |
149 Runtime.getRuntime ().exec (new String []{"chmod", "755", fileName}).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$ | |
150 } catch (Throwable e) {} | |
151 } | |
152 if (load (fileName)) return true; | |
153 } | |
154 } | |
155 } catch (Throwable e) { | |
156 try { | |
157 if (os !is null) os.close (); | |
158 } catch (IOException e1) {} | |
159 try { | |
160 if (is !is null) is.close (); | |
161 } catch (IOException e1) {} | |
162 } | |
163 if (file.exists ()) file.delete (); | |
164 return false; | |
165 } | |
166 | |
167 static bool load (String libName) { | |
168 try { | |
169 if (libName.indexOf (SEPARATOR) !is -1) { | |
170 System.load (libName); | |
171 } else { | |
172 System.loadLibrary (libName); | |
173 } | |
174 return true; | |
175 } catch (UnsatisfiedLinkError e) {} | |
176 return false; | |
177 } | |
178 | |
179 /** | |
180 * Loads the shared library that matches the version of the | |
181 * Java code which is currently running. DWT shared libraries | |
182 * follow an encoding scheme where the major, minor and revision | |
183 * numbers are embedded in the library name and this along with | |
184 * <code>name</code> is used to load the library. If this fails, | |
185 * <code>name</code> is used in another attempt to load the library, | |
186 * this time ignoring the DWT version encoding scheme. | |
187 * | |
188 * @param name the name of the library to load | |
189 */ | |
190 public static void loadLibrary (String name) { | |
191 loadLibrary (name, true); | |
192 } | |
193 | |
194 /** | |
195 * Loads the shared library that matches the version of the | |
196 * Java code which is currently running. DWT shared libraries | |
197 * follow an encoding scheme where the major, minor and revision | |
198 * numbers are embedded in the library name and this along with | |
199 * <code>name</code> is used to load the library. If this fails, | |
200 * <code>name</code> is used in another attempt to load the library, | |
201 * this time ignoring the DWT version encoding scheme. | |
202 * | |
203 * @param name the name of the library to load | |
204 * @param mapName true if the name should be mapped, false otherwise | |
205 */ | |
206 public static void loadLibrary (String name, bool mapName) { | |
207 String prop = System.getProperty ("sun.arch.data.model"); //$NON-NLS-1$ | |
208 if (prop is null) prop = System.getProperty ("com.ibm.vm.bitmode"); //$NON-NLS-1$ | |
209 if (prop !is null) { | |
210 if ("32".equals (prop)) { //$NON-NLS-1$ | |
211 if (0x1FFFFFFFFL is (int /*long*/)0x1FFFFFFFFL) { | |
212 throw new UnsatisfiedLinkError ("Cannot load 64-bit DWT libraries on 32-bit JVM"); //$NON-NLS-1$ | |
213 } | |
214 } | |
215 if ("64".equals (prop)) { //$NON-NLS-1$ | |
216 if (0x1FFFFFFFFL !is (int /*long*/)0x1FFFFFFFFL) { | |
217 throw new UnsatisfiedLinkError ("Cannot load 32-bit DWT libraries on 64-bit JVM"); //$NON-NLS-1$ | |
218 } | |
219 } | |
220 } | |
221 | |
222 /* Compute the library name and mapped name */ | |
223 String libName1, libName2, mappedName1, mappedName2; | |
224 if (mapName) { | |
225 String version = System.getProperty ("swt.version"); //$NON-NLS-1$ | |
226 if (version is null) { | |
227 version = "" + MAJOR_VERSION; //$NON-NLS-1$ | |
228 /* Force 3 digits in minor version number */ | |
229 if (MINOR_VERSION < 10) { | |
230 version += "00"; //$NON-NLS-1$ | |
231 } else { | |
232 if (MINOR_VERSION < 100) version += "0"; //$NON-NLS-1$ | |
233 } | |
234 version += MINOR_VERSION; | |
235 /* No "r" until first revision */ | |
236 if (REVISION > 0) version += "r" + REVISION; //$NON-NLS-1$ | |
237 } | |
238 libName1 = name + "-" + Platform.PLATFORM + "-" + version; //$NON-NLS-1$ //$NON-NLS-2$ | |
239 libName2 = name + "-" + Platform.PLATFORM; //$NON-NLS-1$ | |
240 mappedName1 = System.mapLibraryName (libName1); | |
241 mappedName2 = System.mapLibraryName (libName2); | |
242 } else { | |
243 libName1 = libName2 = mappedName1 = mappedName2 = name; | |
244 } | |
245 | |
246 /* Try loading library from swt library path */ | |
247 String path = System.getProperty ("swt.library.path"); //$NON-NLS-1$ | |
248 if (path !is null) { | |
249 path = new File (path).getAbsolutePath (); | |
250 if (load (path + SEPARATOR + mappedName1)) return; | |
251 if (mapName && load (path + SEPARATOR + mappedName2)) return; | |
252 } | |
253 | |
254 /* Try loading library from java library path */ | |
255 if (load (libName1)) return; | |
256 if (mapName && load (libName2)) return; | |
257 | |
258 /* Try loading library from the tmp directory if swt library path is not specified */ | |
259 if (path is null) { | |
260 path = System.getProperty ("java.io.tmpdir"); //$NON-NLS-1$ | |
261 path = new File (path).getAbsolutePath (); | |
262 if (load (path + SEPARATOR + mappedName1)) return; | |
263 if (mapName && load (path + SEPARATOR + mappedName2)) return; | |
264 } | |
265 | |
266 /* Try extracting and loading library from jar */ | |
267 if (path !is null) { | |
268 if (extract (path + SEPARATOR + mappedName1, mappedName1)) return; | |
269 if (mapName && extract (path + SEPARATOR + mappedName2, mappedName2)) return; | |
270 } | |
271 | |
272 /* Failed to find the library */ | |
273 throw new UnsatisfiedLinkError ("no " + libName1 + " or " + libName2 + " in swt.library.path, java.library.path or the jar file"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
274 } | |
275 +/ | |
276 } |