b0282412 |
1 | /** @file\r |
2 | \r |
3 | Copyright (c) 2006, Intel Corporation\r |
4 | All rights reserved. This program and the accompanying materials\r |
5 | are licensed and made available under the terms and conditions of the BSD License\r |
6 | which accompanies this distribution. The full text of the license may be found at\r |
7 | http://opensource.org/licenses/bsd-license.php\r |
8 | \r |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
11 | \r |
12 | **/\r |
0dc8c589 |
13 | package org.tianocore.migration;\r |
14 | \r |
15 | import java.io.*;\r |
16 | import java.util.*;\r |
eee63a7b |
17 | import java.util.regex.*;\r |
0dc8c589 |
18 | \r |
7bcb8d17 |
19 | public final class Database {\r |
27e0221a |
20 | private static final Database INSTANCE = Database.init();\r |
21 | \r |
22 | Database(String path) {\r |
23 | DatabasePath = path;\r |
4f60c26f |
24 | \r |
27e0221a |
25 | try {\r |
26 | importDBLib("Library.csv");\r |
27 | importDBGuid("Guid.csv", "Guid");\r |
28 | importDBGuid("Ppi.csv", "Ppi");\r |
29 | importDBGuid("Protocol.csv", "Protocol");\r |
30 | importDBMacro("Macro.csv");\r |
31 | importListR8Only();\r |
32 | } catch (Exception e) {\r |
33 | System.out.println(e.getMessage());\r |
34 | }\r |
35 | }\r |
36 | \r |
37 | public String DatabasePath;\r |
38 | public Set<String> error = new HashSet<String>();\r |
39 | public Set<String> r8only = new HashSet<String>();\r |
40 | \r |
41 | private Map<String,Guid> hashguid = new HashMap<String,Guid>();\r |
42 | private Map<String,Func> hashfunc = new HashMap<String,Func>();\r |
43 | private Map<String,Macro> hashmacro = new HashMap<String,Macro>();\r |
44 | \r |
45 | //-------------------------------------import------------------------------------------//\r |
46 | \r |
47 | private void importDBLib(String filename) throws Exception {\r |
48 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r |
49 | String line;\r |
50 | String[] linecontext;\r |
51 | Func lf;\r |
52 | \r |
53 | if (rd.ready()) {\r |
54 | System.out.println("Found " + filename + ", Importing Library Database.");\r |
55 | while ((line = rd.readLine()) != null) {\r |
56 | if (line.length() != 0) {\r |
57 | linecontext = line.split(",");\r |
58 | lf = new Func(linecontext);\r |
59 | hashfunc.put(lf.r8funcname,lf);\r |
60 | }\r |
61 | }\r |
62 | }\r |
63 | }\r |
64 | \r |
65 | private void importDBGuid(String filename, String type) throws Exception {\r |
66 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r |
67 | String line;\r |
68 | String[] linecontext;\r |
69 | Guid gu;\r |
70 | \r |
71 | if (rd.ready()) {\r |
72 | System.out.println("Found " + filename + ", Importing " + type + " Database.");\r |
73 | while ((line = rd.readLine()) != null) {\r |
74 | if (line.length() != 0) {\r |
75 | linecontext = line.split(",");\r |
76 | gu = new Guid(linecontext, type);\r |
77 | hashguid.put(gu.r8name,gu);\r |
78 | }\r |
79 | }\r |
80 | }\r |
81 | }\r |
82 | \r |
83 | private void importDBMacro(String filename) throws Exception {\r |
84 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r |
85 | String line;\r |
86 | String[] linecontext;\r |
87 | Macro mc;\r |
88 | \r |
89 | if (rd.ready()) {\r |
90 | System.out.println("Found " + filename + ", Importing Macro Database.");\r |
91 | while ((line = rd.readLine()) != null) {\r |
92 | if (line.length() != 0) {\r |
93 | linecontext = line.split(",");\r |
94 | mc = new Macro(linecontext);\r |
95 | hashmacro.put(mc.r8name,mc);\r |
96 | }\r |
97 | }\r |
98 | }\r |
99 | }\r |
eee63a7b |
100 | \r |
27e0221a |
101 | private void importListR8Only() throws Exception {\r |
102 | Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern.DOTALL);\r |
103 | String wholeline = Common.file2string(DatabasePath + File.separator + "R8Lib.c");\r |
104 | System.out.println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");\r |
105 | Matcher mtrr8only = ptnr8only.matcher(wholeline);\r |
106 | while (mtrr8only.find()) {\r |
107 | r8only.add(mtrr8only.group(2));\r |
108 | }\r |
109 | }\r |
110 | \r |
111 | //-------------------------------------import------------------------------------------//\r |
eee63a7b |
112 | \r |
27e0221a |
113 | //-------------------------------------get------------------------------------------//\r |
eee63a7b |
114 | \r |
27e0221a |
115 | public String getR9Lib(String r8funcname) {\r |
116 | String temp = null;\r |
117 | if (hashfunc.containsKey(r8funcname)) {\r |
118 | temp = hashfunc.get(r8funcname).r9libname;\r |
119 | }\r |
120 | return temp;\r |
121 | }\r |
122 | \r |
123 | public String getR9Func(String r8funcname) {\r |
124 | String temp = null;\r |
125 | if (hashfunc.containsKey(r8funcname)) {\r |
126 | temp = hashfunc.get(r8funcname).r9funcname;\r |
127 | }\r |
128 | return temp;\r |
129 | }\r |
130 | \r |
131 | public String getR9Macro(String r8macro) {\r |
132 | return hashmacro.get(r8macro).r9name; // the verification job of if the macro exists in the database is done when registering it\r |
133 | }\r |
7bcb8d17 |
134 | \r |
27e0221a |
135 | public String getR9Guidname(String r8Guid) {\r |
136 | String temp = null;\r |
137 | try {\r |
138 | temp = hashguid.get(r8Guid).r9name;\r |
139 | } catch (NullPointerException e) {\r |
140 | error.add("getR9Guidname :" + r8Guid);\r |
141 | }\r |
142 | return temp;\r |
143 | }\r |
7bcb8d17 |
144 | \r |
27e0221a |
145 | public String getGuidType(String r8Guid) {\r |
146 | String temp = null;\r |
147 | try {\r |
148 | temp = hashguid.get(r8Guid).type;\r |
149 | } catch (NullPointerException e) {\r |
150 | error.add("getR9Guidname :" + r8Guid);\r |
151 | }\r |
152 | return temp;\r |
153 | }\r |
7bcb8d17 |
154 | \r |
27e0221a |
155 | //-------------------------------------get------------------------------------------//\r |
eee63a7b |
156 | \r |
27e0221a |
157 | //-------------------------------------has------------------------------------------//\r |
eee63a7b |
158 | \r |
27e0221a |
159 | public boolean hasFunc(String r8lib) {\r |
160 | return hashfunc.containsKey(r8lib);\r |
161 | }\r |
eee63a7b |
162 | \r |
27e0221a |
163 | public boolean hasGuid(String r8guid) {\r |
164 | return hashguid.containsKey(r8guid);\r |
165 | }\r |
eee63a7b |
166 | \r |
27e0221a |
167 | public boolean hasMacro(String r8macro) {\r |
168 | return hashmacro.containsKey(r8macro);\r |
169 | }\r |
170 | \r |
171 | //-------------------------------------has------------------------------------------//\r |
172 | \r |
173 | //-------------------------------------init------------------------------------------//\r |
4f60c26f |
174 | \r |
27e0221a |
175 | private static final Database init() {\r |
176 | if (System.getenv("WORKSPACE") == null) {\r |
177 | return new Database("C:" + File.separator + "tianocore" + File.separator + "edk2" + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");\r |
178 | } else {\r |
179 | return new Database(System.getenv("WORKSPACE") + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");\r |
180 | }\r |
181 | }\r |
182 | \r |
183 | public static final Database getInstance() {\r |
184 | return INSTANCE;\r |
185 | }\r |
0dc8c589 |
186 | }\r |