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