Fixed grammar in messages.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / toolchain / ConfigReader.java
1 /** @file
2 ConfigReader class.
3
4 ConfigReader is used to read tool chain config file with flat format.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 **/
15 package org.tianocore.build.toolchain;
16
17 import org.tianocore.exception.EdkException;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26
27 ConfigReader is used to read tool chain config file with flat format. Comments
28 is line starting with character '#'.
29
30 @since GenBuild 1.0
31 **/
32 public class ConfigReader {
33
34 private static String confPath = ".";
35
36
37 /**
38 Public construct method.
39 **/
40 public ConfigReader () {
41 }
42
43 /**
44 Default filepath is ".".
45
46 @param filename the config file name like "target.txt"
47 @return the variables defined in file
48 **/
49 public static synchronized String[][] parse(String filename) throws EdkException {
50 return parse(confPath, filename);
51 }
52
53 /**
54 Get all variables defined in config file. the config file format is flat
55 with "A=B". If line started with '#' looks as comments.
56
57 @param confPath the path of config file
58 @param filename the file name of the config file
59 @return the variables defined in the config file
60 @throws BuildException
61 Config file's format is not valid
62 **/
63 public static synchronized String[][] parse(String confPath, String filename) throws EdkException {
64 //Map<String, String> map = new TreeMap<String,String>(comparator);
65 List<String> keyList = new ArrayList<String>(256);
66 List<String> valueList = new ArrayList<String>(256);
67
68 try {
69 File file = new File(confPath + File.separatorChar + filename);
70 FileReader reader = new FileReader(file);
71 BufferedReader in = new BufferedReader(reader);
72 String str;
73
74 while ((str = in.readLine()) != null) {
75 str = str.trim();
76 //
77 // if str is empty line, comments (start with '#'),
78 // without '=', or start with '='
79 //
80 int index;
81 if (str.length() == 0 || str.startsWith("#") ||
82 (index = str.indexOf('=')) <= 0) {
83 continue;
84 }
85 //
86 // look as line "A = B"
87 //
88 keyList.add(str.substring(0, index).trim());
89 valueList.add(str.substring(index + 1).trim());
90 }
91 } catch (Exception e) {
92 throw new EdkException("ERROR Processing file [" + filename + "].\n" + e.getMessage());
93 }
94
95 String[][] definitions = new String[2][keyList.size()];
96 definitions[0] = (String[])keyList.toArray(definitions[0]);
97 definitions[1] = (String[])valueList.toArray(definitions[1]);
98
99 return definitions;
100 }
101
102 public static synchronized ToolChainMap parseToolChainConfig(File ConfigFile) throws EdkException {
103 ToolChainMap map = new ToolChainMap();
104
105 try {
106 FileReader reader = new FileReader(ConfigFile);
107 BufferedReader in = new BufferedReader(reader);
108 String str;
109
110 while ((str = in.readLine()) != null) {
111 str = str.trim();
112 //
113 // if str is empty line, comments (start with '#'),
114 // without '=', or start with '='
115 //
116 int index;
117 if (str.length() == 0 || str.startsWith("#") ||
118 (index = str.indexOf('=')) <= 0) {
119 continue;
120 }
121 //
122 // look as line "A = B"
123 //
124 String key = str.substring(0, index).trim().toUpperCase();
125 String value = str.substring(index + 1).trim();
126 map.put(key, value);
127 }
128 } catch (Exception e) {
129 throw new EdkException("ERROR Processing file [" + ConfigFile.getAbsolutePath() + "].\n" + e.getMessage());
130 }
131
132 return map;
133 }
134 }
135
136