]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/toolchain/ConfigReader.java
add .. support
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / toolchain / ConfigReader.java
CommitLineData
878ddf1f 1/** @file\r
2 ConfigReader class.\r
d2059d05 3 \r
4 ConfigReader is used to read tool chain config file with flat format. \r
5 \r
878ddf1f 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
2279b26c 17import org.tianocore.build.exception.GenBuildException;\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
d2059d05 26 \r
878ddf1f 27 ConfigReader is used to read tool chain config file with flat format. Comments\r
28 is line starting with character '#'.\r
d2059d05 29 \r
878ddf1f 30 @since GenBuild 1.0\r
31**/\r
32public class ConfigReader {\r
33\r
878ddf1f 34 /**\r
d2059d05 35 Parse specified tool chain definition file.\r
36 \r
37 @param filename The config file name with full path\r
ff225cbb 38\r
d2059d05 39 @return String[][] The definition array\r
878ddf1f 40 **/\r
2279b26c 41 public static synchronized String[][] parse(String filename) throws GenBuildException {\r
d2059d05 42 return parse(new File(filename));\r
878ddf1f 43 }\r
44\r
45 /**\r
d2059d05 46 Get all definitions in config file. the config file format is flat\r
47 with "A=B". If line started with '#' looks as comments. \r
48 \r
49 @param configFile The config file\r
50\r
51 @return String[][] The variables defined in the config file\r
52\r
53 @throws EdkException\r
54 Config file's format is not valid\r
878ddf1f 55 **/\r
2279b26c 56 public static synchronized String[][] parse(File configFile) throws GenBuildException {\r
a29c47e0 57 List<String> keyList = new ArrayList<String>(256);\r
58 List<String> valueList = new ArrayList<String>(256);\r
d2059d05 59 int lines = 0;\r
a29c47e0 60\r
878ddf1f 61 try {\r
d2059d05 62 FileReader reader = new FileReader(configFile);\r
878ddf1f 63 BufferedReader in = new BufferedReader(reader);\r
64 String str;\r
a29c47e0 65\r
878ddf1f 66 while ((str = in.readLine()) != null) {\r
d2059d05 67 ++lines;\r
878ddf1f 68 str = str.trim();\r
69 //\r
d2059d05 70 // skip empty line, comment (start with '#') \r
878ddf1f 71 //\r
d2059d05 72 if (str.length() == 0 || str.startsWith("#")) {\r
878ddf1f 73 continue;\r
74 }\r
d2059d05 75\r
76 //\r
77 // stop if the definition line is not in "name=value" form\r
78 // \r
79 int index;\r
80 if ((index = str.indexOf('=')) <= 0) {\r
2279b26c 81 throw new GenBuildException("ERROR Processing file [" + configFile.getAbsolutePath() \r
d2059d05 82 + "] (line " + lines + ").\n");\r
83 }\r
84\r
878ddf1f 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
2279b26c 92 throw new GenBuildException("ERROR Processing file [" + configFile.getAbsolutePath() \r
d2059d05 93 + "] (line " + lines + ").\n" + e.getMessage());\r
878ddf1f 94 }\r
a29c47e0 95\r
96 String[][] definitions = new String[2][keyList.size()];\r
97 definitions[0] = (String[])keyList.toArray(definitions[0]);\r
98 definitions[1] = (String[])valueList.toArray(definitions[1]);\r
99\r
100 return definitions;\r
878ddf1f 101 }\r
878ddf1f 102}\r
a29c47e0 103\r
104\r