1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.github.maven_nar.cpptasks;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.*;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.taskdefs.Execute;
29 import org.apache.tools.ant.taskdefs.LogStreamHandler;
30 import org.apache.tools.ant.types.Commandline;
31 import org.apache.tools.ant.types.Environment;
32 import org.apache.tools.ant.util.StringUtils;
33
34
35
36
37
38
39
40 public class CUtil {
41
42
43
44
45 public static final class StringArrayBuilder {
46 private final String[] _value;
47
48 public StringArrayBuilder(final String value) {
49
50 final StringTokenizer tokens = new StringTokenizer(value, ", ");
51 final Vector vallist = new Vector();
52 while (tokens.hasMoreTokens()) {
53 final String val = tokens.nextToken().trim();
54 if (val.length() == 0) {
55 continue;
56 }
57 vallist.addElement(val);
58 }
59 this._value = new String[vallist.size()];
60 vallist.copyInto(this._value);
61 }
62
63 public String[] getValue() {
64 return this._value;
65 }
66 }
67
68 public final static int FILETIME_EPSILON = 500;
69
70
71
72
73 public static void addAll(final Vector dest, final Object[] src) {
74 if (src == null) {
75 return;
76 }
77 for (final Object element : src) {
78 dest.addElement(element);
79 }
80 }
81
82
83
84
85
86
87
88 public static int checkDirectoryArray(final String[] names) {
89 int count = 0;
90 for (int i = 0; i < names.length; i++) {
91 if (names[i] != null) {
92 final File dir = new File(names[i]);
93 if (dir.exists() && dir.isDirectory()) {
94 count++;
95 } else {
96 names[i] = null;
97 }
98 }
99 }
100 return count;
101 }
102
103
104
105
106 public static String getBasename(final File file) {
107 final String path = file.getPath();
108
109 String basename = file.getName();
110 final int pos = basename.lastIndexOf('.');
111 if (pos != -1) {
112 basename = basename.substring(0, pos);
113 }
114 return basename;
115 }
116
117
118
119
120
121
122
123
124
125 public static File getExecutableLocation(final String exeName) {
126
127
128
129 final File currentDir = new File(System.getProperty("user.dir"));
130 if (new File(currentDir, exeName).exists()) {
131 return currentDir;
132 }
133 final File[] envPath = CUtil.getPathFromEnvironment("PATH", File.pathSeparator);
134 for (final File element : envPath) {
135 if (new File(element, exeName).exists()) {
136 return element;
137 }
138 }
139 return null;
140 }
141
142
143
144
145 public static String getParentPath(final String path) {
146 final int pos = path.lastIndexOf(File.separator);
147 if (pos <= 0) {
148 return null;
149 }
150 return path.substring(0, pos);
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164 public static File[] getPathFromEnvironment(final String envVariable, final String delim) {
165
166 if (System.getProperty("os.name").equals("OS/400")) {
167 return new File[] {};
168 }
169 final Vector osEnv = Execute.getProcEnvironment();
170 final String match = envVariable.concat("=");
171 for (final Enumeration e = osEnv.elements(); e.hasMoreElements();) {
172 final String entry = ((String) e.nextElement()).trim();
173 if (entry.length() > match.length()) {
174 final String entryFrag = entry.substring(0, match.length());
175 if (entryFrag.equalsIgnoreCase(match)) {
176 final String path = entry.substring(match.length());
177 return parsePath(path, delim);
178 }
179 }
180 }
181 final File[] noPath = new File[0];
182 return noPath;
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public static String getRelativePath(final String base, final File targetFile) {
198 try {
199
200
201
202 String canonicalBase = base;
203 if (base.charAt(base.length() - 1) != File.separatorChar) {
204 canonicalBase = base + File.separatorChar;
205 }
206
207
208
209 String canonicalTarget;
210 if (System.getProperty("os.name").equals("OS/400")) {
211 canonicalTarget = targetFile.getPath();
212 } else {
213 canonicalTarget = targetFile.getCanonicalPath();
214 }
215 if (canonicalBase.startsWith(canonicalTarget + File.separatorChar)) {
216 canonicalTarget = canonicalTarget + File.separator;
217 }
218 if (canonicalTarget.equals(canonicalBase)) {
219 return ".";
220 }
221
222
223
224 if (substringMatch(canonicalBase, 0, 2, "\\\\")) {
225
226
227
228 final int endPrefix = canonicalBase.indexOf('\\', 2);
229 final String prefix1 = canonicalBase.substring(0, endPrefix);
230 final String prefix2 = canonicalTarget.substring(0, endPrefix);
231 if (!prefix1.equals(prefix2)) {
232 return canonicalTarget;
233 }
234 } else {
235 if (substringMatch(canonicalBase, 1, 3, ":\\")) {
236 final int endPrefix = 2;
237 final String prefix1 = canonicalBase.substring(0, endPrefix);
238 final String prefix2 = canonicalTarget.substring(0, endPrefix);
239 if (!prefix1.equals(prefix2)) {
240 return canonicalTarget;
241 }
242 } else {
243 if (canonicalBase.charAt(0) == '/') {
244 if (canonicalTarget.charAt(0) != '/') {
245 return canonicalTarget;
246 }
247 }
248 }
249 }
250 final char separator = File.separatorChar;
251 int lastCommonSeparator = -1;
252 int minLength = canonicalBase.length();
253 if (canonicalTarget.length() < minLength) {
254 minLength = canonicalTarget.length();
255 }
256
257
258
259 for (int i = 0; i < minLength; i++) {
260 if (canonicalTarget.charAt(i) == canonicalBase.charAt(i)) {
261 if (canonicalTarget.charAt(i) == separator) {
262 lastCommonSeparator = i;
263 }
264 } else {
265 break;
266 }
267 }
268 final StringBuffer relativePath = new StringBuffer(50);
269
270
271
272
273 for (int i = lastCommonSeparator + 1; i < canonicalBase.length(); i++) {
274 if (canonicalBase.charAt(i) == separator) {
275 if (relativePath.length() > 0) {
276 relativePath.append(separator);
277 }
278 relativePath.append("..");
279 }
280 }
281 if (canonicalTarget.length() > lastCommonSeparator + 1) {
282 if (relativePath.length() > 0) {
283 relativePath.append(separator);
284 }
285 relativePath.append(canonicalTarget.substring(lastCommonSeparator + 1));
286 }
287 return relativePath.toString();
288 } catch (final IOException ex) {
289 }
290 return targetFile.toString();
291 }
292
293 public static boolean isActive(final Project p, final String ifCond, final String unlessCond) throws BuildException {
294 if (ifCond != null) {
295 final String ifValue = p.getProperty(ifCond);
296 if (ifValue == null) {
297 return false;
298 } else {
299 if (ifValue.equals("false") || ifValue.equals("no")) {
300 throw new BuildException("if condition \"" + ifCond + "\" has suspicious value \"" + ifValue);
301 }
302 }
303 }
304 if (unlessCond != null) {
305 final String unlessValue = p.getProperty(unlessCond);
306 if (unlessValue != null) {
307 if (unlessValue.equals("false") || unlessValue.equals("no")) {
308 throw new BuildException("unless condition \"" + unlessCond + "\" has suspicious value \"" + unlessValue);
309 }
310 return false;
311 }
312 }
313 return true;
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328 public static boolean isSignificantlyAfter(final long time1, final long time2) {
329 return time1 > time2 + FILETIME_EPSILON;
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344 public static boolean isSignificantlyBefore(final long time1, final long time2) {
345 return time1 + FILETIME_EPSILON < time2;
346 }
347
348
349
350
351
352
353
354
355
356
357 public static boolean isSystemPath(final File source) {
358 final String lcPath = source.getAbsolutePath().toLowerCase(java.util.Locale.US);
359 return lcPath.contains("platformsdk") || lcPath.contains("windows kits") || lcPath.contains("microsoft")
360 || Objects.equals(lcPath, "/usr/include")
361 || Objects.equals(lcPath, "/usr/lib") || Objects.equals(lcPath, "/usr/local/include")
362 || Objects.equals(lcPath, "/usr/local/lib");
363 }
364
365
366
367
368
369
370
371
372
373 public static File[] parsePath(final String path, final String delim) {
374 final Vector libpaths = new Vector();
375 int delimPos = 0;
376 for (int startPos = 0; startPos < path.length(); startPos = delimPos + delim.length()) {
377 delimPos = path.indexOf(delim, startPos);
378 if (delimPos < 0) {
379 delimPos = path.length();
380 }
381
382
383
384 if (delimPos > startPos) {
385 final String dirName = path.substring(startPos, delimPos);
386 final File dir = new File(dirName);
387 if (dir.exists() && dir.isDirectory()) {
388 libpaths.addElement(dir);
389 }
390 }
391 }
392 final File[] paths = new File[libpaths.size()];
393 libpaths.copyInto(paths);
394 return paths;
395 }
396
397
398
399
400
401 public static int runCommand(final CCTask task, final File workingDir, final String[] cmdline,
402 final boolean newEnvironment, final Environment env) throws BuildException {
403 try {
404 task.log(Commandline.toString(cmdline), task.getCommandLogLevel());
405 final Execute exe = new Execute(new LogStreamHandler(task, Project.MSG_INFO, Project.MSG_ERR));
406 if (System.getProperty("os.name").equals("OS/390")) {
407 exe.setVMLauncher(false);
408 }
409 exe.setAntRun(task.getProject());
410 exe.setCommandline(cmdline);
411 exe.setWorkingDirectory(workingDir);
412 if (env != null) {
413 final String[] environment = env.getVariables();
414 if (environment != null) {
415 for (final String element : environment) {
416 task.log("Setting environment variable: " + element, Project.MSG_VERBOSE);
417 }
418 }
419 exe.setEnvironment(environment);
420 }
421 exe.setNewenvironment(newEnvironment);
422 return exe.execute();
423 } catch (final java.io.IOException exc) {
424 throw new BuildException("Could not launch " + cmdline[0] + ": " + exc, task.getLocation());
425 }
426 }
427
428
429
430
431 public static boolean sameList(final Object[] a, final Object[] b) {
432 if (a == null || b == null || a.length != b.length) {
433 return false;
434 }
435 for (int i = 0; i < a.length; i++) {
436 if (!a[i].equals(b[i])) {
437 return false;
438 }
439 }
440 return true;
441 }
442
443
444
445
446 public static boolean sameList(final Vector v, final Object[] a) {
447 if (v == null || a == null || v.size() != a.length) {
448 return false;
449 }
450 for (int i = 0; i < a.length; i++) {
451 final Object o = a[i];
452 if (!o.equals(v.elementAt(i))) {
453 return false;
454 }
455 }
456 return true;
457 }
458
459
460
461
462
463 public static boolean sameSet(final Object[] a, final Vector b) {
464 if (a == null || b == null || a.length != b.size()) {
465 return false;
466 }
467 if (a.length == 0) {
468 return true;
469 }
470
471 final Hashtable t = new Hashtable();
472 for (final Object element : a) {
473 t.put(element, element);
474 }
475 for (int i = 0; i < b.size(); i++) {
476 final Object o = b.elementAt(i);
477 if (t.remove(o) == null) {
478 return false;
479 }
480 }
481 return t.size() == 0;
482 }
483
484 private static boolean
485 substringMatch(final String src, final int beginIndex, final int endIndex, final String target) {
486 if (src.length() < endIndex) {
487 return false;
488 }
489 return src.substring(beginIndex, endIndex).equals(target);
490 }
491
492
493
494
495 public static String[] toArray(final Vector src) {
496 final String[] retval = new String[src.size()];
497 src.copyInto(retval);
498 return retval;
499 }
500
501 public static String toUnixPath(final String path) {
502 if (File.separatorChar != '/' && path.indexOf(File.separatorChar) != -1) {
503 return StringUtils.replace(path, File.separator, "/");
504 }
505 return path;
506 }
507
508 public static String toWindowsPath(final String path) {
509 if (File.separatorChar != '\\' && path.indexOf(File.separatorChar) != -1) {
510 return StringUtils.replace(path, File.separator, "\\");
511 }
512 return path;
513 }
514
515
516
517
518
519
520
521
522
523
524 public static String xmlAttribEncode(final String attrValue) {
525 final StringBuffer buf = new StringBuffer(attrValue);
526 int quotePos;
527
528 for (quotePos = -1; (quotePos = buf.indexOf("\"", quotePos + 1)) >= 0;) {
529 buf.deleteCharAt(quotePos);
530 buf.insert(quotePos, """);
531 quotePos += 5;
532 }
533
534 for (quotePos = -1; (quotePos = buf.indexOf("<", quotePos + 1)) >= 0;) {
535 buf.deleteCharAt(quotePos);
536 buf.insert(quotePos, "<");
537 quotePos += 3;
538 }
539
540 for (quotePos = -1; (quotePos = buf.indexOf(">", quotePos + 1)) >= 0;) {
541 buf.deleteCharAt(quotePos);
542 buf.insert(quotePos, ">");
543 quotePos += 3;
544 }
545
546 return buf.toString();
547 }
548
549 }