summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/toolchain/ConfigReader.java
blob: 4275f8eb78f4229c6bb33b63e33f0251f8752d5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** @file
  ConfigReader class.
  
  ConfigReader is used to read tool chain config file with flat format. 
  
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
package org.tianocore.build.toolchain;

import org.apache.tools.ant.Project;

import org.tianocore.build.exception.GenBuildException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
  
  ConfigReader is used to read tool chain config file with flat format. Comments
  is line starting with character '#'.
  
  @since GenBuild 1.0
**/
public class ConfigReader {

    /**
      Parse specified tool chain definition file.
    
      @param    filename    The config file name with full path

      @return String[][]    The definition array
    **/
    public static synchronized String[][] parse(Project prj, String filename) throws GenBuildException {
        return parse(prj, new File(filename));
    }

    /**
      Get all definitions in config file. the config file format is flat
      with "A=B". If line started with '#' looks as comments. 
    
      @param    configFile      The config file

      @return   String[][]      The variables defined in the config file

      @throws   GenBuildException
                Config file's format is not valid
    **/
    public static synchronized String[][] parse(Project prj, File configFile) throws GenBuildException {
        List<String> keyList = new ArrayList<String>(256);
        List<String> valueList = new ArrayList<String>(256);
        int lines = 0;

        try {
            FileReader reader = new FileReader(configFile);
            BufferedReader in = new BufferedReader(reader);
            String str;

            while ((str = in.readLine()) != null) {
                ++lines;
                str = str.trim();
                //
                // skip empty line, comment (start with '#') 
                //
                if (str.length() == 0 || str.startsWith("#")) {
                    continue;
                }

                //
                // stop if the definition line is not in "name=value" form
                // 
                int index;
                if ((index = str.indexOf('=')) <= 0) {
                    throw new GenBuildException("ERROR Processing file [" 
                        + configFile.getAbsolutePath() 
                        + "] (line " + lines + ").\n");
                }

                //
                // look as line "A = B"
                //
                keyList.add(str.substring(0, index).trim());
                if (prj != null) {
                    valueList.add(prj.replaceProperties(str.substring(index + 1).trim()));
                } else {
                    valueList.add(str.substring(index + 1).trim());
                }
            }
        } catch (Exception ex) {
            GenBuildException e = new GenBuildException("ERROR Processing file [" 
                + configFile.getAbsolutePath() 
                + "] (line " + lines + ").\n" + ex.getMessage());
            e.setStackTrace(ex.getStackTrace());
            throw e;
        }

        String[][] definitions = new String[2][keyList.size()];
        definitions[0] = (String[])keyList.toArray(definitions[0]);
        definitions[1] = (String[])valueList.toArray(definitions[1]);

        return definitions;
    }
}