Mercurial > projects > dwt-linux
annotate dwt/internal/Library.d @ 10:63c023465156
moved from org.eclipse.swt to dwt
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 05 Jan 2008 17:58:37 +0100 |
parents | ad2b69216039 |
children | 8f049b136add |
rev | line source |
---|---|
0 | 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 *******************************************************************************/ | |
10
63c023465156
moved from org.eclipse.swt to dwt
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
11 module dwt.internal.Library; |
0 | 12 |
13 import tango.util.Convert; | |
14 | |
15 public class Library { | |
16 | |
17 /* SWT Version - Mmmm (M=major, mmm=minor) */ | |
18 | |
19 /** | |
20 * SWT Major version number (must be >= 0) | |
21 */ | |
22 static const int MAJOR_VERSION = 3; | |
23 | |
24 /** | |
25 * SWT Minor version number (must be in the range 0..999) | |
26 */ | |
27 static const int MINOR_VERSION = 346; | |
28 | |
29 /** | |
30 * SWT revision number (must be >= 0) | |
31 */ | |
32 static const int REVISION = 0; | |
33 | |
34 /** | |
35 * The JAVA and SWT versions | |
36 */ | |
37 //public static const int JAVA_VERSION; | |
38 public static const int SWT_VERSION; | |
39 | |
40 version( linux ){ | |
41 static const char[] SEPARATOR = "\n"; | |
42 } | |
43 else { | |
44 static assert( false, "only linux supported for this port" ); | |
45 } | |
46 | |
47 | |
48 static this(){ | |
49 //SEPARATOR = System.getProperty("file.separator"); | |
50 //JAVA_VERSION = parseVersion(System.getProperty("java.version")); | |
51 SWT_VERSION = buildSWT_VERSION(MAJOR_VERSION, MINOR_VERSION); | |
52 } | |
53 | |
54 static int parseVersion(char[] aVersion) { | |
55 if (aVersion == null) return 0; | |
56 int major = 0, minor = 0, micro = 0; | |
57 int length = aVersion.length, index = 0, start = 0; | |
58 bool isDigit( char c ){ | |
59 return c >= '0' && c <= '9'; | |
60 } | |
61 while (index < length && isDigit(aVersion[index])) index++; | |
62 try { | |
63 if (start < length) major = to!(int)( aVersion[start .. index] ); | |
64 } catch (ConversionException e) {} | |
65 start = ++index; | |
66 while (index < length && isDigit(aVersion[index])) index++; | |
67 try { | |
68 if (start < length) minor = to!(int)(aVersion[start .. index]); | |
69 } catch (ConversionException e) {} | |
70 start = ++index; | |
71 while (index < length && isDigit(aVersion[index])) index++; | |
72 try { | |
73 if (start < length) micro = to!(int)(aVersion[start .. index]); | |
74 } catch (ConversionException e) {} | |
75 return buildJAVA_VERSION(major, minor, micro); | |
76 } | |
77 | |
78 /** | |
79 * Returns the Java version number as an integer. | |
80 * | |
81 * @param major | |
82 * @param minor | |
83 * @param micro | |
84 * @return the version | |
85 */ | |
86 public static int buildJAVA_VERSION (int major, int minor, int micro) { | |
87 return (major << 16) + (minor << 8) + micro; | |
88 } | |
89 | |
90 /** | |
91 * Returns the SWT version number as an integer. | |
92 * | |
93 * @param major | |
94 * @param minor | |
95 * @return the version | |
96 */ | |
97 public static int buildSWT_VERSION (int major, int minor) { | |
98 return major * 1000 + minor; | |
99 } | |
100 /+ PORTING_LEFT | |
101 static bool extract (char[] fileName, char[] mappedName) { | |
102 FileOutputStream os = null; | |
103 InputStream is = null; | |
104 File file = new File(fileName); | |
105 try { | |
106 if (!file.exists ()) { | |
107 is = Library.class.getResourceAsStream ("/" + mappedName); //$NON-NLS-1$ | |
108 if (is != null) { | |
109 int read; | |
110 byte [] buffer = new byte [4096]; | |
111 os = new FileOutputStream (fileName); | |
112 while ((read = is.read (buffer)) != -1) { | |
113 os.write(buffer, 0, read); | |
114 } | |
115 os.close (); | |
116 is.close (); | |
117 if (!Platform.PLATFORM.equals ("win32")) { //$NON-NLS-1$ | |
118 try { | |
119 Runtime.getRuntime ().exec (new String []{"chmod", "755", fileName}).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$ | |
120 } catch (Throwable e) {} | |
121 } | |
122 if (load (fileName)) return true; | |
123 } | |
124 } | |
125 } catch (Throwable e) { | |
126 try { | |
127 if (os != null) os.close (); | |
128 } catch (IOException e1) {} | |
129 try { | |
130 if (is != null) is.close (); | |
131 } catch (IOException e1) {} | |
132 } | |
133 if (file.exists ()) file.delete (); | |
134 return false; | |
135 } | |
136 | |
137 static bool load (char[] libName) { | |
138 try { | |
139 if (libName.indexOf (SEPARATOR) != -1) { | |
140 System.load (libName); | |
141 } else { | |
142 System.loadLibrary (libName); | |
143 } | |
144 return true; | |
145 } catch (UnsatisfiedLinkError e) {} | |
146 return false; | |
147 } | |
148 | |
149 /** | |
150 * Loads the shared library that matches the version of the | |
151 * Java code which is currently running. SWT shared libraries | |
152 * follow an encoding scheme where the major, minor and revision | |
153 * numbers are embedded in the library name and this along with | |
154 * <code>name</code> is used to load the library. If this fails, | |
155 * <code>name</code> is used in another attempt to load the library, | |
156 * this time ignoring the SWT version encoding scheme. | |
157 * | |
158 * @param name the name of the library to load | |
159 */ | |
160 public static void loadLibrary (char[] name) { | |
161 loadLibrary (name, true); | |
162 } | |
163 | |
164 /** | |
165 * Loads the shared library that matches the version of the | |
166 * Java code which is currently running. SWT shared libraries | |
167 * follow an encoding scheme where the major, minor and revision | |
168 * numbers are embedded in the library name and this along with | |
169 * <code>name</code> is used to load the library. If this fails, | |
170 * <code>name</code> is used in another attempt to load the library, | |
171 * this time ignoring the SWT version encoding scheme. | |
172 * | |
173 * @param name the name of the library to load | |
174 * @param mapName true if the name should be mapped, false otherwise | |
175 */ | |
176 public static void loadLibrary (char[] name, boolean mapName) { | |
177 | |
178 /* Compute the library name and mapped name */ | |
179 String libName1, libName2, mappedName1, mappedName2; | |
180 if (mapName) { | |
181 String version = System.getProperty ("swt.version"); //$NON-NLS-1$ | |
182 if (version == null) { | |
183 version = "" + MAJOR_VERSION; //$NON-NLS-1$ | |
184 /* Force 3 digits in minor version number */ | |
185 if (MINOR_VERSION < 10) { | |
186 version += "00"; //$NON-NLS-1$ | |
187 } else { | |
188 if (MINOR_VERSION < 100) version += "0"; //$NON-NLS-1$ | |
189 } | |
190 version += MINOR_VERSION; | |
191 /* No "r" until first revision */ | |
192 if (REVISION > 0) version += "r" + REVISION; //$NON-NLS-1$ | |
193 } | |
194 libName1 = name + "-" + Platform.PLATFORM + "-" + version; //$NON-NLS-1$ //$NON-NLS-2$ | |
195 libName2 = name + "-" + Platform.PLATFORM; //$NON-NLS-1$ | |
196 mappedName1 = System.mapLibraryName (libName1); | |
197 mappedName2 = System.mapLibraryName (libName2); | |
198 } else { | |
199 libName1 = libName2 = mappedName1 = mappedName2 = name; | |
200 } | |
201 | |
202 /* Try loading library from swt library path */ | |
203 String path = System.getProperty ("swt.library.path"); //$NON-NLS-1$ | |
204 if (path != null) { | |
205 path = new File (path).getAbsolutePath (); | |
206 if (load (path + SEPARATOR + mappedName1)) return; | |
207 if (mapName && load (path + SEPARATOR + mappedName2)) return; | |
208 } | |
209 | |
210 /* Try loading library from java library path */ | |
211 if (load (libName1)) return; | |
212 if (mapName && load (libName2)) return; | |
213 | |
214 /* Try loading library from the tmp directory if swt library path is not specified */ | |
215 if (path == null) { | |
216 path = System.getProperty ("java.io.tmpdir"); //$NON-NLS-1$ | |
217 path = new File (path).getAbsolutePath (); | |
218 if (load (path + SEPARATOR + mappedName1)) return; | |
219 if (mapName && load (path + SEPARATOR + mappedName2)) return; | |
220 } | |
221 | |
222 /* Try extracting and loading library from jar */ | |
223 if (path != null) { | |
224 if (extract (path + SEPARATOR + mappedName1, mappedName1)) return; | |
225 if (mapName && extract (path + SEPARATOR + mappedName2, mappedName2)) return; | |
226 } | |
227 | |
228 /* Failed to find the library */ | |
229 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$ | |
230 } | |
231 +/ | |
232 } |