1.add code to support friendly output message.
[mirror_edk2.git] / Tools / Java / Source / ContextTool / org / tianocore / context / TargetFile.java
CommitLineData
987912ad 1/** @file\r
2 File is TargetFile class which is used to generate the new target.txt. \r
3 \r
4Copyright (c) 2006, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
c74c7a43 13package org.tianocore.context;\r
14\r
15import java.io.BufferedReader;\r
16import java.io.BufferedWriter;\r
17import java.io.File;\r
18import java.io.FileNotFoundException;\r
c74c7a43 19import java.io.FileReader;\r
20import java.io.FileWriter;\r
21import java.io.IOException;\r
c74c7a43 22\r
23public class TargetFile {\r
24\r
c81f5d44 25 \r
f337986f 26 \r
27 /** \r
28 * validate the filename\r
29 * @param String filename : the name of target file\r
30 * \r
31 * @return true or false\r
32 **/\r
33 public static boolean validateFilename(String filename) {\r
34 \r
35 String workspacePath = System.getenv("WORKSPACE");\r
36 \r
37 Fd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);\r
38 \r
39 if (Fd.exists() == true && Fd.canRead() == true)\r
40 return true;\r
41 else\r
42 return false;\r
43 }\r
44 \r
c74c7a43 45\r
46 /**\r
47 * create a empty temp file, which is located at the same directory with target file\r
48 * @param String filename : the name of target temp file\r
49 * @return true or false\r
50 **/\r
f337986f 51 public static boolean createTempFile(String filename) {\r
c74c7a43 52\r
53 String workspacePath = System.getenv("WORKSPACE");\r
54 \r
f337986f 55 TempFd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename + "tmp");\r
c74c7a43 56\r
57 if (TempFd.exists() == true) {\r
58 if (TempFd.delete() == false) {\r
c81f5d44 59 System.out.printf("%n%s%n", "target.txttmp has been existed, and failed in deletion!");\r
c74c7a43 60 return false;\r
61 }\r
62 }\r
63 try {\r
64 TempFd.createNewFile();\r
65 } catch (IOException e) {\r
c81f5d44 66 System.out.printf("%n%s%n", "Failed in creation of the temp file:target.txttmp!");\r
c74c7a43 67 return false;\r
68 }\r
69\r
70 return true;\r
71 }\r
72\r
73 /**\r
74 * read from target.txt and write to target.txttmp, del target.txt, rename\r
75 * @param no paremeter\r
76 * @return true or false\r
77 **/\r
f337986f 78 public static boolean readwriteFile() {\r
c74c7a43 79\r
80 if (Fd.canRead() != true)\r
81 return false;\r
82\r
83 BufferedReader br = null;\r
84 BufferedWriter bw = null;\r
85 String textLine = null;\r
86\r
87 try {\r
88 br = new BufferedReader(new FileReader(Fd));\r
89 } catch (FileNotFoundException e) {\r
90 System.out\r
c81f5d44 91 .println("\n# Creating BufferedReader fail!");\r
c74c7a43 92 return false;\r
93 }\r
94 try {\r
95 bw = new BufferedWriter(new FileWriter(TempFd));\r
96 } catch (IOException e) {\r
c81f5d44 97 System.out.println("\n# Creating the BufferedWriter fail!");\r
c74c7a43 98 return false;\r
99 }\r
100 \r
101 //\r
102 //TARGET_ARCH must be in front of TARGET!!! according to the target.txt\r
103 //\r
104 try {\r
105 while ((textLine = br.readLine()) != null) {\r
2c71f6e5 106 //\r
107 // the line is composed of Space\r
108 //\r
c74c7a43 109 if (textLine.trim().compareToIgnoreCase("") == 0) {\r
110 bw.write(textLine);\r
111 bw.newLine();\r
2c71f6e5 112 } \r
113 //\r
114 // the line starts with "#", and no "="\r
115 //\r
116 else if ((textLine.trim().charAt(0) == '#') && (textLine.indexOf("=") == -1)){\r
c74c7a43 117 bw.write(textLine);\r
118 bw.newLine();\r
119 } else {\r
2c71f6e5 120 //\r
d3f45848 121 //modify at the first time, and there should be "*ACTIVE_PLATFORM*=*" in the line\r
2c71f6e5 122 //\r
c74c7a43 123 if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {\r
2c71f6e5 124 if(pflag == true){\r
125 if(textLine.trim().charAt(0) == '#'){\r
f15e1359 126 if(ParseParameter.npflag == true) {\r
f337986f 127 bw.write(ParseParameter.curpstr);\r
f15e1359 128 }else{\r
129 bw.write(textLine);\r
2c71f6e5 130 }\r
f15e1359 131 bw.newLine();\r
132 pflag = false;\r
2c71f6e5 133 continue;\r
134 }\r
f15e1359 135 if(ParseParameter.npflag == true) {\r
f337986f 136 bw.write(ParseParameter.curpstr);\r
2c71f6e5 137 } else {\r
138 bw.write(textLine);\r
139 }\r
140 bw.newLine();\r
141 pflag = false;\r
c74c7a43 142 }\r
c74c7a43 143 } else if (textLine.indexOf("TARGET_ARCH") != -1) {\r
2c71f6e5 144 if(aflag == true){\r
145 if(textLine.trim().charAt(0) == '#'){\r
f15e1359 146 if(ParseParameter.naflag == true) {\r
f337986f 147 bw.write(ParseParameter.curastr);\r
f15e1359 148 }else{\r
149 bw.write(textLine);\r
2c71f6e5 150 }\r
f15e1359 151 bw.newLine();\r
152 aflag = false;\r
2c71f6e5 153 continue;\r
154 }\r
f15e1359 155 if(ParseParameter.naflag == true) {\r
f337986f 156 bw.write(ParseParameter.curastr);\r
2c71f6e5 157 } else {\r
158 bw.write(textLine);\r
159 }\r
160 bw.newLine();\r
161 aflag = false;\r
c74c7a43 162 }\r
c74c7a43 163 } else if (textLine.indexOf("TARGET") != -1) {\r
2c71f6e5 164 if(tflag == true){\r
165 if(textLine.trim().charAt(0) == '#'){\r
f15e1359 166 if(ParseParameter.ntflag == true) {\r
f337986f 167 bw.write(ParseParameter.curtstr);\r
f15e1359 168 }else{\r
169 bw.write(textLine);\r
2c71f6e5 170 }\r
f15e1359 171 bw.newLine();\r
172 tflag = false;\r
2c71f6e5 173 continue;\r
174 }\r
f15e1359 175 if(ParseParameter.ntflag == true) {\r
f337986f 176 bw.write(ParseParameter.curtstr);\r
2c71f6e5 177 } else {\r
178 bw.write(textLine);\r
179 }\r
180 bw.newLine();\r
181 tflag = false;\r
c74c7a43 182 }\r
c74c7a43 183 } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {\r
2c71f6e5 184 if(cflag == true){\r
185 if(textLine.trim().charAt(0) == '#'){\r
f15e1359 186 if(ParseParameter.ncflag == true) {\r
f337986f 187 bw.write(ParseParameter.curcstr);\r
f15e1359 188 }else{\r
189 bw.write(textLine);\r
2c71f6e5 190 }\r
f15e1359 191 bw.newLine();\r
192 cflag = false;\r
2c71f6e5 193 continue;\r
194 }\r
f15e1359 195 if(ParseParameter.ncflag == true) {\r
f337986f 196 bw.write(ParseParameter.curcstr);\r
2c71f6e5 197 } else {\r
198 bw.write(textLine);\r
199 }\r
200 bw.newLine();\r
201 cflag = false;\r
c74c7a43 202 }\r
c74c7a43 203 } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {\r
2c71f6e5 204 if(nflag == true){\r
205 if(textLine.trim().charAt(0) == '#'){\r
f15e1359 206 if(ParseParameter.nnflag == true) {\r
f337986f 207 bw.write(ParseParameter.curnstr);\r
f15e1359 208 }else{\r
209 bw.write(textLine);\r
2c71f6e5 210 }\r
f15e1359 211 bw.newLine();\r
212 nflag = false;\r
2c71f6e5 213 continue;\r
214 }\r
f15e1359 215 if(ParseParameter.nnflag == true) {\r
f337986f 216 bw.write(ParseParameter.curnstr);\r
2c71f6e5 217 } else {\r
218 bw.write(textLine);\r
219 }\r
220 bw.newLine();\r
221 nflag = false;\r
222 }\r
223 } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {\r
224 if(mflag == true){\r
225 if(textLine.trim().charAt(0) == '#'){\r
f15e1359 226 if(ParseParameter.nmflag == true) {\r
f337986f 227 bw.write(ParseParameter.curmstr);\r
f15e1359 228 }else{\r
229 bw.write(textLine);\r
2c71f6e5 230 }\r
f15e1359 231 bw.newLine();\r
232 mflag = false;\r
2c71f6e5 233 continue;\r
234 }\r
f15e1359 235 if(ParseParameter.nmflag == true) {\r
f337986f 236 bw.write(ParseParameter.curmstr);\r
2c71f6e5 237 } else {\r
238 bw.write(textLine);\r
239 }\r
240 bw.newLine();\r
241 mflag = false;\r
242 }\r
243 }else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {\r
244 if(meflag == true){\r
245 if(textLine.trim().charAt(0) == '#'){\r
f15e1359 246 if(ParseParameter.nmeflag == true) {\r
f337986f 247 bw.write(ParseParameter.curmestr);\r
f15e1359 248 }else{\r
249 bw.write(textLine);\r
2c71f6e5 250 }\r
f15e1359 251 bw.newLine();\r
252 meflag = false;\r
2c71f6e5 253 continue;\r
254 }\r
f15e1359 255 if(ParseParameter.nmeflag == true) {\r
f337986f 256 bw.write(ParseParameter.curmestr);\r
2c71f6e5 257 } else {\r
258 bw.write(textLine);\r
259 }\r
260 bw.newLine();\r
261 meflag = false;\r
c74c7a43 262 }\r
c74c7a43 263 }\r
264 }\r
265 }\r
2c71f6e5 266 //\r
267 //user maybe delete the line *ACTIVE_PLATFORM*=*\r
268 //\r
f15e1359 269 if( (pflag == true) && (ParseParameter.npflag == true) ){\r
f337986f 270 bw.write(ParseParameter.curpstr);\r
2c71f6e5 271 bw.newLine();\r
f15e1359 272 } else if ( (tflag == true) && (ParseParameter.ntflag == true) ){\r
f337986f 273 bw.write(ParseParameter.curtstr);\r
2c71f6e5 274 bw.newLine();\r
f15e1359 275 } else if ( (aflag == true) && (ParseParameter.naflag == true) ){\r
f337986f 276 bw.write(ParseParameter.curastr);\r
2c71f6e5 277 bw.newLine();\r
f15e1359 278 } else if ( (cflag == true) && (ParseParameter.ncflag == true) ){\r
f337986f 279 bw.write(ParseParameter.curcstr);\r
2c71f6e5 280 bw.newLine();\r
f15e1359 281 } else if ( (nflag == true) && (ParseParameter.nnflag == true) ){\r
f337986f 282 bw.write(ParseParameter.curnstr);\r
2c71f6e5 283 bw.newLine();\r
f15e1359 284 } else if ( (meflag == true) && (ParseParameter.nmeflag == true) ){\r
f337986f 285 bw.write(ParseParameter.curmestr);\r
2c71f6e5 286 bw.newLine();\r
f15e1359 287 } else if ( (mflag == true) && (ParseParameter.nmflag == true) ){\r
f337986f 288 bw.write(ParseParameter.curmstr);\r
2c71f6e5 289 bw.newLine();\r
290 }\r
c74c7a43 291 } catch (IOException e) {\r
c81f5d44 292 System.out.println("\n# Reading or Writing file fail!");\r
c74c7a43 293 return false;\r
294 }\r
295\r
296 try {\r
297 br.close();\r
298 bw.close();\r
299 } catch (IOException e) {\r
300 System.out\r
c81f5d44 301 .println("\n# Closing BufferedReader&BufferedWriter fail!");\r
c74c7a43 302 return false;\r
303 }\r
304\r
305 if (Fd.delete() == false) {\r
c81f5d44 306 System.out.println("\n# Deleting file fail!");\r
c74c7a43 307 return false;\r
308 }\r
309 if (TempFd.renameTo(Fd) == false) {\r
c81f5d44 310 System.out.println("\n# Renaming file failed!");\r
c74c7a43 311 return false;\r
312 }\r
313\r
314 return true;\r
315 }\r
f337986f 316 \r
317 /**\r
318 * read the file and output the lines which include setting\r
319 * @param File fd : the File of the target file\r
320 * @return String: the current setting\r
321 **/\r
322 public static boolean readFile() {\r
323 \r
324 BufferedReader br = null;\r
325 String textLine = null;\r
326\r
327 try {\r
328 br = new BufferedReader(new FileReader(Fd));\r
329 } catch (FileNotFoundException e) {\r
330 System.out\r
c81f5d44 331 .println("\n# Creating BufferedReader fail!");\r
f337986f 332 return false;\r
333 }\r
334 try {\r
335 while ((textLine = br.readLine()) != null) {\r
336 //\r
337 // the line is composed of Space\r
338 //\r
339 if (textLine.trim().compareToIgnoreCase("") == 0) {\r
340 continue;\r
341 } \r
342 //\r
343 // the line starts with "#"\r
344 //\r
345 else if ((textLine.trim().charAt(0) == '#')){\r
346 continue;\r
347 } else {\r
348 if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {\r
349 ParseParameter.curpstr = textLine;\r
f337986f 350 } else if (textLine.indexOf("TARGET_ARCH") != -1) {\r
351 ParseParameter.curastr = textLine;\r
f337986f 352 } else if (textLine.indexOf("TARGET") != -1) {\r
353 ParseParameter.curtstr = textLine;\r
f337986f 354 } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {\r
355 ParseParameter.curcstr = textLine;\r
f337986f 356 } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {\r
357 ParseParameter.curnstr = textLine;\r
f337986f 358 } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {\r
359 ParseParameter.curmstr = textLine;\r
f337986f 360 } else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {\r
361 ParseParameter.curmestr = textLine;\r
f337986f 362 }\r
363 }\r
364 }\r
365 } catch (IOException e) {\r
c81f5d44 366 System.out.println("\n# Reading file fail!");\r
f337986f 367 return false;\r
368 }\r
369\r
370 try {\r
371 br.close();\r
372 } catch (IOException e) {\r
373 System.out\r
c81f5d44 374 .println("\n# Closing BufferedReader fail!");\r
f337986f 375 return false;\r
376 }\r
377 return true;\r
378 }\r
379 \r
c74c7a43 380\r
c74c7a43 381 ///\r
382 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.\r
383 ///\r
384 private static File TempFd;\r
385 \r
386 ///\r
387 /// point to target.txt.\r
388 ///\r
389 private static File Fd;\r
2c71f6e5 390 \r
391 ///\r
392 /// when the flag is true, the corresponding str should be add at the end of file.\r
393 ///\r
394 private static boolean pflag = true;\r
395 private static boolean tflag = true;\r
396 private static boolean aflag = true;\r
397 private static boolean cflag = true;\r
398 private static boolean nflag = true;\r
399 private static boolean mflag = true;\r
400 private static boolean meflag = true;\r
c74c7a43 401\r
c74c7a43 402\r
c74c7a43 403}\r