]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/toolchain/ConfigReader.java
f48735966e80c609ad8bf848cf6c4b5636ff5fcc
[mirror_edk2.git] / Tools / 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 java.io.BufferedReader;
18 import java.io.File;
19 import java.io.FileReader;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.Vector;
25
26 import org.apache.tools.ant.BuildException;
27
28 /**
29
30 ConfigReader is used to read tool chain config file with flat format. Comments
31 is line starting with character '#'.
32
33 @since GenBuild 1.0
34 **/
35 public class ConfigReader {
36
37 private static String confPath = ".";
38
39 /**
40 Public construct method.
41 **/
42 public ConfigReader () {
43 }
44
45 /**
46 Default filepath is ".".
47
48 @param filename the config file name like "target.txt"
49 @return the variables defined in file
50 **/
51 public static synchronized String[][] parse(String filename) {
52 return parse(confPath, filename);
53 }
54
55 /**
56 Get all variables defined in config file. the config file format is flat
57 with "A=B". If line started with '#' looks as comments.
58
59 @param confPath the path of config file
60 @param filename the file name of the config file
61 @return the variables defined in the config file
62 @throws BuildException
63 Config file's format is not valid
64 **/
65 public static synchronized String[][] parse(String confPath, String filename) throws BuildException {
66 try {
67 Map<String, String> map = new HashMap<String, String>(20);
68 File file = new File(confPath + File.separatorChar + filename);
69 FileReader reader = new FileReader(file);
70 BufferedReader in = new BufferedReader(reader);
71 String str;
72 while ((str = in.readLine()) != null) {
73 str = str.trim();
74 //
75 // if str is empty line or comments (start with '#')
76 //
77 if (str.equalsIgnoreCase("") || str.startsWith("#")) {
78 continue;
79 }
80 //
81 // if str without '=' or start with '='
82 //
83 if (str.indexOf('=') <= 0) {
84 continue;
85 }
86 //
87 // look as line "A = B"
88 //
89 int index = str.indexOf('=');
90 String key = str.substring(0, index).trim();
91 String value = str.substring(index + 1).trim();
92 //
93 // if key is existed, then update
94 //
95 if (map.containsKey(key)) {
96 map.remove(key);
97 }
98 map.put(key, value);
99 }
100 Set keyset = map.keySet();
101 Iterator iter = keyset.iterator();
102 String[][] result = new String[map.size()][2];
103 int i = 0;
104 while (iter.hasNext()) {
105 String key = (String) iter.next();
106 result[i][0] = key;
107 result[i++][1] = (String) map.get(key);
108 }
109 return result;
110 } catch (Exception e) {
111 throw new BuildException("Processor file [" + filename + "] error. \n" + e.getMessage());
112 }
113 }
114
115 /**
116 Parse global flags table. The format is like such(global flag name, value,
117 vendor_arch_cmd, [add flags], [sub flags]):
118
119 <pre>
120 # EFI_DEBUG
121 EFI_DEBUG YES MSFT_IA32_ASM ADD.["/Zi", "/DEBUG"]
122 EFI_DEBUG YES MSFT_IA32_CC ADD.["/Zi", "/Gm", "/D EFI_DEBUG"] SUB.["/nologo", "/WX"]
123 EFI_DEBUG YES MSFT_IA32_LINK ADD.["/DEBUG"]
124 EFI_DEBUG YES MSFT_NT32_CC ADD.["/DEBUG"]
125 </pre>
126
127 @param confPath the file path of config file
128 @param filename the file name of config file
129 @return the value list
130 @throws BuildException
131 Config file is not valid
132 **/
133 public static synchronized String[][] parseTable(String confPath,
134 String filename) throws BuildException {
135 try {
136 Vector<String[]> vector = new Vector<String[]>(20);
137 File file = new File(confPath + File.separatorChar + filename);
138 FileReader reader = new FileReader(file);
139 BufferedReader in = new BufferedReader(reader);
140 String str;
141 while ((str = in.readLine()) != null) {
142 str = str.trim();
143 //
144 // if str is empty line or comments (start with '#')
145 //
146 if (str.equalsIgnoreCase("") || str.startsWith("#")) {
147 continue;
148 }
149 String[] item = new String[5];
150 for(int i=0; i < item.length; i++){
151 item[i] = "";
152 }
153 //
154 // EFI_DEBUG YES MSFT_IA32_ASM ADD.["/Zi", "/DEBUG"]
155 // FLAGS: EFI_DEBUG
156 //
157 int index = str.indexOf(" ");
158 item[0] = str.substring(0, index);
159 str = str.substring(index + 1).trim();
160 //
161 // Setting: YES
162 //
163 index = str.indexOf(" ");
164 item[1] = str.substring(0, index);
165 str = str.substring(index + 1).trim();
166 //
167 // Vendor_Arch_Commandtype: MSFT_IA32_ASM
168 //
169 index = str.indexOf(" ");
170 item[2] = str.substring(0, index);
171 str = str.substring(index + 1).trim();
172 //
173 // Add or/and Sub
174 //
175 if (str.startsWith("ADD.")) {
176 index = str.indexOf("]");
177 if ( index > 0){
178 item[3] = str.substring(5, index);
179 str = str.substring(index + 1).trim();
180 }
181 }
182 else if(str.startsWith("SUB.")){
183 index = str.indexOf("]");
184 if ( index > 0){
185 item[4] = str.substring(5, index);
186 str = str.substring(index + 1).trim();
187 }
188 }
189 else {
190 throw new BuildException("File [" + filename + "] never conform to Global Flags Table format.");
191 }
192
193 if (str.startsWith("ADD.")) {
194 index = str.indexOf("]");
195 if ( index > 0){
196 item[3] = str.substring(5, index);
197 str = str.substring(index + 1).trim();
198 }
199 }
200 else if(str.startsWith("SUB.")){
201 index = str.indexOf("]");
202 if ( index > 0){
203 item[4] = str.substring(5, index);
204 str = str.substring(index + 1).trim();
205 }
206 }
207 vector.addElement(item);
208 }
209 String[][] result = new String[vector.size()][5];
210 for(int i=0; i < vector.size(); i++){
211 result[i] = (String[])vector.get(i);
212 }
213 return result;
214 } catch (Exception e) {
215 throw new BuildException("Processor file [" + filename + "] error. \n" + e.getMessage());
216 }
217 }
218 }