]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
17 | import org.tianocore.exception.EdkException;\r | |
18 | \r | |
19 | import java.io.BufferedReader;\r | |
20 | import java.io.File;\r | |
21 | import java.io.FileReader;\r | |
22 | import java.util.ArrayList;\r | |
23 | import java.util.List;\r | |
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 | |
32 | public class ConfigReader {\r | |
33 | \r | |
34 | private static String confPath = ".";\r | |
35 | \r | |
36 | \r | |
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 | |
49 | public static synchronized String[][] parse(String filename) throws EdkException {\r | |
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 | |
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 | |
68 | try {\r | |
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 | |
73 | \r | |
74 | while ((str = in.readLine()) != null) {\r | |
75 | str = str.trim();\r | |
76 | //\r | |
77 | // if str is empty line, comments (start with '#'),\r | |
78 | // without '=', or start with '='\r | |
79 | //\r | |
80 | int index;\r | |
81 | if (str.length() == 0 || str.startsWith("#") || \r | |
82 | (index = str.indexOf('=')) <= 0) {\r | |
83 | continue;\r | |
84 | }\r | |
85 | //\r | |
86 | // look as line "A = B"\r | |
87 | //\r | |
88 | keyList.add(str.substring(0, index).trim());\r | |
89 | valueList.add(str.substring(index + 1).trim());\r | |
90 | }\r | |
91 | } catch (Exception e) {\r | |
92 | throw new EdkException("ERROR Processing file [" + filename + "].\n" + e.getMessage());\r | |
93 | }\r | |
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 | |
100 | }\r | |
101 | \r | |
102 | public static synchronized ToolChainMap parseToolChainConfig(File ConfigFile) throws EdkException {\r | |
103 | ToolChainMap map = new ToolChainMap();\r | |
104 | \r | |
105 | try {\r | |
106 | FileReader reader = new FileReader(ConfigFile);\r | |
107 | BufferedReader in = new BufferedReader(reader);\r | |
108 | String str;\r | |
109 | \r | |
110 | while ((str = in.readLine()) != null) {\r | |
111 | str = str.trim();\r | |
112 | //\r | |
113 | // if str is empty line, comments (start with '#'),\r | |
114 | // without '=', or start with '='\r | |
115 | //\r | |
116 | int index;\r | |
117 | if (str.length() == 0 || str.startsWith("#") || \r | |
118 | (index = str.indexOf('=')) <= 0) {\r | |
119 | continue;\r | |
120 | }\r | |
121 | //\r | |
122 | // look as line "A = B"\r | |
123 | //\r | |
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 | |
127 | }\r | |
128 | } catch (Exception e) {\r | |
129 | throw new EdkException("ERROR Processing file [" + ConfigFile.getAbsolutePath() + "].\n" + e.getMessage());\r | |
130 | }\r | |
131 | \r | |
132 | return map;\r | |
133 | }\r | |
134 | }\r | |
135 | \r | |
136 | \r |