878ddf1f |
1 | /** @file\r |
2 | ConfigReader class.\r |
ff225cbb |
3 | \r |
4 | ConfigReader is used to read tool chain config file with flat format.\r |
5 | \r |
878ddf1f |
6 | Copyright (c) 2006, Intel Corporation\r |
7 | All rights reserved. This program and the accompanying materials\r |
8 | are licensed and made available under the terms and conditions of the BSD License\r |
9 | which accompanies this distribution. The full text of the license may be found at\r |
10 | http://opensource.org/licenses/bsd-license.php\r |
11 | \r |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
14 | **/\r |
15 | package org.tianocore.build.toolchain;\r |
16 | \r |
ff225cbb |
17 | import org.tianocore.common.exception.EdkException;\r |
a29c47e0 |
18 | \r |
878ddf1f |
19 | import java.io.BufferedReader;\r |
20 | import java.io.File;\r |
21 | import java.io.FileReader;\r |
a29c47e0 |
22 | import java.util.ArrayList;\r |
23 | import java.util.List;\r |
878ddf1f |
24 | \r |
25 | /**\r |
ff225cbb |
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 |
ff225cbb |
29 | \r |
878ddf1f |
30 | @since GenBuild 1.0\r |
31 | **/\r |
32 | public class ConfigReader {\r |
33 | \r |
34 | private static String confPath = ".";\r |
35 | \r |
a29c47e0 |
36 | \r |
878ddf1f |
37 | /**\r |
ff225cbb |
38 | Public construct method.\r |
878ddf1f |
39 | **/\r |
40 | public ConfigReader () {\r |
41 | }\r |
42 | \r |
43 | /**\r |
44 | Default filepath is ".".\r |
ff225cbb |
45 | \r |
878ddf1f |
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 |
ff225cbb |
55 | with "A=B". If line started with '#' looks as comments.\r |
56 | \r |
878ddf1f |
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 |
ff225cbb |
81 | if (str.length() == 0 || str.startsWith("#") ||\r |
a29c47e0 |
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 |
391dbbb1 |
92 | throw new EdkException("ERROR Processing file [" + filename + "].\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 |
ff225cbb |
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 |
ff225cbb |
117 | if (str.length() == 0 || str.startsWith("#") ||\r |
a29c47e0 |
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 |
391dbbb1 |
129 | throw new EdkException("ERROR Processing file [" + ConfigFile.getAbsolutePath() + "].\n" + e.getMessage());\r |
878ddf1f |
130 | }\r |
a29c47e0 |
131 | \r |
132 | return map;\r |
878ddf1f |
133 | }\r |
134 | }\r |
a29c47e0 |
135 | \r |
136 | \r |