]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/toolchain/ConfigReader.java
Fixed grammar in messages.
[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
136adffc 17import org.tianocore.exception.EdkException;\r
a29c47e0 18\r
878ddf1f 19import java.io.BufferedReader;\r
20import java.io.File;\r
21import java.io.FileReader;\r
a29c47e0 22import java.util.ArrayList;\r
23import java.util.List;\r
878ddf1f 24\r
25/**\r
26 \r
27 ConfigReader is used to read tool chain config file with flat format. Comments\r
28 is line starting with character '#'.\r
29 \r
30 @since GenBuild 1.0\r
31**/\r
32public class ConfigReader {\r
33\r
34 private static String confPath = ".";\r
35\r
a29c47e0 36\r
878ddf1f 37 /**\r
38 Public construct method. \r
39 **/\r
40 public ConfigReader () {\r
41 }\r
42\r
43 /**\r
44 Default filepath is ".".\r
45 \r
46 @param filename the config file name like "target.txt"\r
47 @return the variables defined in file\r
48 **/\r
a29c47e0 49 public static synchronized String[][] parse(String filename) throws EdkException {\r
878ddf1f 50 return parse(confPath, filename);\r
51 }\r
52\r
53 /**\r
54 Get all variables defined in config file. the config file format is flat\r
55 with "A=B". If line started with '#' looks as comments. \r
56 \r
57 @param confPath the path of config file\r
58 @param filename the file name of the config file\r
59 @return the variables defined in the config file\r
60 @throws BuildException\r
61 Config file's format is not valid\r
62 **/\r
a29c47e0 63 public static synchronized String[][] parse(String confPath, String filename) throws EdkException {\r
64 //Map<String, String> map = new TreeMap<String,String>(comparator);\r
65 List<String> keyList = new ArrayList<String>(256);\r
66 List<String> valueList = new ArrayList<String>(256);\r
67\r
878ddf1f 68 try {\r
878ddf1f 69 File file = new File(confPath + File.separatorChar + filename);\r
70 FileReader reader = new FileReader(file);\r
71 BufferedReader in = new BufferedReader(reader);\r
72 String str;\r
a29c47e0 73\r
878ddf1f 74 while ((str = in.readLine()) != null) {\r
75 str = str.trim();\r
76 //\r
a29c47e0 77 // if str is empty line, comments (start with '#'),\r
78 // without '=', or start with '='\r
878ddf1f 79 //\r
a29c47e0 80 int index;\r
81 if (str.length() == 0 || str.startsWith("#") || \r
82 (index = str.indexOf('=')) <= 0) {\r
878ddf1f 83 continue;\r
84 }\r
85 //\r
86 // look as line "A = B"\r
87 //\r
a29c47e0 88 keyList.add(str.substring(0, index).trim());\r
89 valueList.add(str.substring(index + 1).trim());\r
878ddf1f 90 }\r
878ddf1f 91 } catch (Exception e) {\r
a29c47e0 92 throw new EdkException("Process file [" + filename + "] error. \n" + e.getMessage());\r
878ddf1f 93 }\r
a29c47e0 94\r
95 String[][] definitions = new String[2][keyList.size()];\r
96 definitions[0] = (String[])keyList.toArray(definitions[0]);\r
97 definitions[1] = (String[])valueList.toArray(definitions[1]);\r
98\r
99 return definitions;\r
878ddf1f 100 }\r
101\r
a29c47e0 102 public static synchronized ToolChainMap parseToolChainConfig(File ConfigFile) throws EdkException {\r
103 ToolChainMap map = new ToolChainMap();\r
104 \r
878ddf1f 105 try {\r
a29c47e0 106 FileReader reader = new FileReader(ConfigFile);\r
878ddf1f 107 BufferedReader in = new BufferedReader(reader);\r
108 String str;\r
a29c47e0 109\r
878ddf1f 110 while ((str = in.readLine()) != null) {\r
111 str = str.trim();\r
112 //\r
a29c47e0 113 // if str is empty line, comments (start with '#'),\r
114 // without '=', or start with '='\r
878ddf1f 115 //\r
a29c47e0 116 int index;\r
117 if (str.length() == 0 || str.startsWith("#") || \r
118 (index = str.indexOf('=')) <= 0) {\r
878ddf1f 119 continue;\r
120 }\r
878ddf1f 121 //\r
a29c47e0 122 // look as line "A = B"\r
878ddf1f 123 //\r
a29c47e0 124 String key = str.substring(0, index).trim().toUpperCase();\r
125 String value = str.substring(index + 1).trim();\r
126 map.put(key, value);\r
878ddf1f 127 }\r
878ddf1f 128 } catch (Exception e) {\r
a29c47e0 129 throw new EdkException("Process file [" + ConfigFile.getAbsolutePath() + "] error. \n" + e.getMessage());\r
878ddf1f 130 }\r
a29c47e0 131\r
132 return map;\r
878ddf1f 133 }\r
134}\r
a29c47e0 135\r
136\r