Added support for macro/property in tools_def.txt. Now you can define a property...
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / toolchain / ConfigReader.java
1 /** @file
2 ConfigReader class.
3
4 ConfigReader is used to read tool chain config file with flat format.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 **/
15 package org.tianocore.build.toolchain;
16
17 import org.apache.tools.ant.Project;
18
19 import org.tianocore.build.exception.GenBuildException;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28
29 ConfigReader is used to read tool chain config file with flat format. Comments
30 is line starting with character '#'.
31
32 @since GenBuild 1.0
33 **/
34 public class ConfigReader {
35
36 /**
37 Parse specified tool chain definition file.
38
39 @param filename The config file name with full path
40
41 @return String[][] The definition array
42 **/
43 public static synchronized String[][] parse(Project prj, String filename) throws GenBuildException {
44 return parse(prj, new File(filename));
45 }
46
47 /**
48 Get all definitions in config file. the config file format is flat
49 with "A=B". If line started with '#' looks as comments.
50
51 @param configFile The config file
52
53 @return String[][] The variables defined in the config file
54
55 @throws GenBuildException
56 Config file's format is not valid
57 **/
58 public static synchronized String[][] parse(Project prj, File configFile) throws GenBuildException {
59 List<String> keyList = new ArrayList<String>(256);
60 List<String> valueList = new ArrayList<String>(256);
61 int lines = 0;
62
63 try {
64 FileReader reader = new FileReader(configFile);
65 BufferedReader in = new BufferedReader(reader);
66 String str;
67
68 while ((str = in.readLine()) != null) {
69 ++lines;
70 str = str.trim();
71 //
72 // skip empty line, comment (start with '#')
73 //
74 if (str.length() == 0 || str.startsWith("#")) {
75 continue;
76 }
77
78 //
79 // stop if the definition line is not in "name=value" form
80 //
81 int index;
82 if ((index = str.indexOf('=')) <= 0) {
83 throw new GenBuildException("ERROR Processing file ["
84 + configFile.getAbsolutePath()
85 + "] (line " + lines + ").\n");
86 }
87
88 //
89 // look as line "A = B"
90 //
91 keyList.add(str.substring(0, index).trim());
92 if (prj != null) {
93 valueList.add(prj.replaceProperties(str.substring(index + 1).trim()));
94 } else {
95 valueList.add(str.substring(index + 1).trim());
96 }
97 }
98 } catch (Exception ex) {
99 GenBuildException e = new GenBuildException("ERROR Processing file ["
100 + configFile.getAbsolutePath()
101 + "] (line " + lines + ").\n" + ex.getMessage());
102 e.setStackTrace(ex.getStackTrace());
103 throw e;
104 }
105
106 String[][] definitions = new String[2][keyList.size()];
107 definitions[0] = (String[])keyList.toArray(definitions[0]);
108 definitions[1] = (String[])valueList.toArray(definitions[1]);
109
110 return definitions;
111 }
112 }
113
114