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