fa1fc239f4ffd26d6ff1637176aa77503fd85a6f
[mirror_edk2.git] / Tools / Java / 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.build.exception.GenBuildException;
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 /**
35 Parse specified tool chain definition file.
36
37 @param filename The config file name with full path
38
39 @return String[][] The definition array
40 **/
41 public static synchronized String[][] parse(String filename) throws GenBuildException {
42 return parse(new File(filename));
43 }
44
45 /**
46 Get all definitions in config file. the config file format is flat
47 with "A=B". If line started with '#' looks as comments.
48
49 @param configFile The config file
50
51 @return String[][] The variables defined in the config file
52
53 @throws GenBuildException
54 Config file's format is not valid
55 **/
56 public static synchronized String[][] parse(File configFile) throws GenBuildException {
57 List<String> keyList = new ArrayList<String>(256);
58 List<String> valueList = new ArrayList<String>(256);
59 int lines = 0;
60
61 try {
62 FileReader reader = new FileReader(configFile);
63 BufferedReader in = new BufferedReader(reader);
64 String str;
65
66 while ((str = in.readLine()) != null) {
67 ++lines;
68 str = str.trim();
69 //
70 // skip empty line, comment (start with '#')
71 //
72 if (str.length() == 0 || str.startsWith("#")) {
73 continue;
74 }
75
76 //
77 // stop if the definition line is not in "name=value" form
78 //
79 int index;
80 if ((index = str.indexOf('=')) <= 0) {
81 throw new GenBuildException("ERROR Processing file ["
82 + configFile.getAbsolutePath()
83 + "] (line " + lines + ").\n");
84 }
85
86 //
87 // look as line "A = B"
88 //
89 keyList.add(str.substring(0, index).trim());
90 valueList.add(str.substring(index + 1).trim());
91 }
92 } catch (Exception ex) {
93 GenBuildException e = new GenBuildException("ERROR Processing file ["
94 + configFile.getAbsolutePath()
95 + "] (line " + lines + ").\n" + ex.getMessage());
96 e.setStackTrace(ex.getStackTrace());
97 throw e;
98 }
99
100 String[][] definitions = new String[2][keyList.size()];
101 definitions[0] = (String[])keyList.toArray(definitions[0]);
102 definitions[1] = (String[])valueList.toArray(definitions[1]);
103
104 return definitions;
105 }
106 }
107
108