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.borland;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.Reader;
28 import java.util.Vector;
29
30 import com.github.maven_nar.cpptasks.CUtil;
31 import com.github.maven_nar.cpptasks.types.LibraryTypeEnum;
32
33
34
35
36
37
38 public final class BorlandProcessor {
39 public static void addWarningSwitch(final Vector<String> args, final int level) {
40 switch (level) {
41 case 0:
42 args.addElement("-w-");
43 break;
44 case 5:
45 args.addElement("-w!");
46 break;
47 default:
48 args.addElement("-w");
49 break;
50 }
51 }
52
53 public static void getDefineSwitch(final StringBuffer buffer, final String define, final String value) {
54 buffer.append("-D");
55 buffer.append(define);
56 if (value != null && value.length() > 0) {
57 buffer.append('=');
58 buffer.append(value);
59 }
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public static File[] getEnvironmentPath(final String toolName, final char switchChar,
75 final String[] defaultRelativePath) {
76 if (toolName == null) {
77 throw new NullPointerException("toolName");
78 }
79 if (defaultRelativePath == null) {
80 throw new NullPointerException("defaultRelativePath");
81 }
82 String[] path = defaultRelativePath;
83 File exeDir = CUtil.getExecutableLocation(toolName + ".exe");
84 if (exeDir != null) {
85 final File cfgFile = new File(exeDir, toolName + ".cfg");
86 if (cfgFile.exists()) {
87 try {
88 final Reader reader = new BufferedReader(new FileReader(cfgFile));
89 final BorlandCfgParser cfgParser = new BorlandCfgParser(switchChar);
90 path = cfgParser.parsePath(reader);
91 reader.close();
92 } catch (final IOException ex) {
93
94
95
96 }
97 }
98 } else {
99
100
101
102
103 exeDir = new File(System.getProperty("user.dir"));
104 }
105 int nonExistant = 0;
106 File[] resourcePath = new File[path.length];
107 for (int i = 0; i < path.length; i++) {
108 resourcePath[i] = new File(path[i]);
109 if (!resourcePath[i].isAbsolute()) {
110 resourcePath[i] = new File(exeDir, path[i]);
111 }
112
113
114
115 if (!(resourcePath[i].exists() && resourcePath[i].isDirectory())) {
116 resourcePath[i] = null;
117 nonExistant++;
118 }
119 }
120
121
122
123
124 if (nonExistant > 0) {
125 final File[] culled = new File[resourcePath.length - nonExistant];
126 int index = 0;
127 for (final File element : resourcePath) {
128 if (element != null) {
129 culled[index++] = element;
130 }
131 }
132 resourcePath = culled;
133 }
134 return resourcePath;
135 }
136
137 public static String getIncludeDirSwitch(final String includeOption, final String includeDir) {
138 final StringBuffer buf = new StringBuffer(includeOption);
139 quoteFile(buf, includeDir);
140 return buf.toString();
141 }
142
143 public static String[] getLibraryPatterns(final String[] libnames, final LibraryTypeEnum libType) {
144 final StringBuffer buf = new StringBuffer();
145 final String[] patterns = new String[libnames.length];
146 for (int i = 0; i < libnames.length; i++) {
147 buf.setLength(0);
148 buf.append(libnames[i]);
149 buf.append(".lib");
150 patterns[i] = buf.toString();
151 }
152 return patterns;
153 }
154
155 public static String[] getOutputFileSwitch(final String outFile) {
156 return new String[0];
157 }
158
159 public static void getUndefineSwitch(final StringBuffer buffer, final String define) {
160 buffer.append("-U");
161 buffer.append(define);
162 }
163
164 public static boolean isCaseSensitive() {
165 return false;
166 }
167
168
169
170
171
172
173
174
175
176
177 public static String[] prepareResponseFile(final File outputFile, final String[] args, final String continuation)
178 throws IOException {
179 final String baseName = outputFile.getName();
180 final File commandFile = new File(outputFile.getParent(), baseName + ".rsp");
181 final FileWriter writer = new FileWriter(commandFile);
182 for (int i = 1; i < args.length - 1; i++) {
183 writer.write(args[i]);
184
185
186
187
188 if (args[i].endsWith(",") || args[i + 1].startsWith(",")) {
189 writer.write(' ');
190 } else {
191
192
193
194 writer.write(continuation);
195 }
196 }
197
198
199
200 if (args.length > 1) {
201 writer.write(args[args.length - 1]);
202 }
203 writer.close();
204 final String[] execArgs = new String[2];
205 execArgs[0] = args[0];
206
207
208 execArgs[1] = commandFile.toString();
209 return execArgs;
210 }
211
212 public static void quoteFile(final StringBuffer buf, final String outPath) {
213 if (outPath.charAt(0) != '\"'
214 && (outPath.indexOf(' ') >= 0 || outPath.indexOf('-') >= 0 || outPath.indexOf('/') >= 0)) {
215 buf.append('\"');
216 buf.append(outPath);
217 buf.append('\"');
218 } else {
219 buf.append(outPath);
220 }
221 }
222
223 private BorlandProcessor() {
224 }
225 }