]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/Common.java
debug R8Lib.h
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Common.java
CommitLineData
8c4eeeb6 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
90503bad 13package org.tianocore.migration;\r
14\r
fed802b1 15import java.io.*;\r
16import java.util.regex.*;\r
17import java.util.*;\r
5ea254f6 18import java.lang.reflect.*;\r
90503bad 19\r
5ea254f6 20public final class Common {\r
21 public static final int BOTH = 0;\r
22 public static final int FILE = 1;\r
23 public static final int DIR = 2;\r
8c4eeeb6 24 \r
ac62aa9a 25 public static final String strseparate = "(.*)\\\\([^\\\\]*)";\r
5ea254f6 26 public static final Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");\r
27\r
28 //-------------------------------------regex------------------------------------------//\r
29 \r
30 public static final String replaceAll(String line, Pattern ptn, String des) {\r
31 Matcher mtr = ptn.matcher(line);\r
a817ecbf 32\r
5ea254f6 33 if (mtr.find()) {\r
34 return mtr.replaceAll(des);\r
35 }\r
a817ecbf 36 \r
5ea254f6 37 return line;\r
38 }\r
39\r
40 //-------------------------------------regex------------------------------------------//\r
41 \r
42 //-----------------------------------file&string---------------------------------------//\r
43 \r
44 public static final String file2string(String filename) throws Exception {\r
90503bad 45 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
46 StringBuffer wholefile = new StringBuffer();\r
47 String line;\r
48 while ((line = rd.readLine()) != null) {\r
49 wholefile.append(line + "\n");\r
50 }\r
51 return wholefile.toString();\r
52 }\r
53\r
5ea254f6 54 public static final void string2file(String content, String filename) throws Exception {\r
a756211f 55 ensureDir(filename);\r
56 PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r
57 outfile.append(content);\r
58 outfile.flush();\r
59 outfile.close();\r
60 }\r
61\r
5ea254f6 62 //-----------------------------------file&string---------------------------------------//\r
63\r
64 //--------------------------------------dir--------------------------------------------//\r
b678aaca 65 /*\r
66 public static final HashSet<String> walkDir(String path, int mode) throws Exception {\r
67 HashSet<String> pathlist = new HashSet<String>();\r
68 Common.toDoAll(path, Common.class.getMethod("walkDir", String.class), null, null, mode);\r
69 return pathlist;\r
70 }\r
71 */\r
5ea254f6 72 public static final void ensureDir(String objFileWhole) {\r
90503bad 73 File tempdir;\r
8c4eeeb6 74 Matcher mtrseparate = ptnseparate.matcher(objFileWhole);\r
90503bad 75 if (mtrseparate.find()) {\r
76 tempdir = new File(mtrseparate.group(1));\r
77 if (!tempdir.exists()) tempdir.mkdirs();\r
78 }\r
fed802b1 79 }\r
80 \r
5ea254f6 81 public static final void deleteDir(String objFileWhole) {\r
82 String[] list = new File(objFileWhole).list();\r
83 File temp;\r
fed802b1 84 for (int i = 0 ; i < list.length ; i++) {\r
5ea254f6 85 temp = new File(objFileWhole + File.separator + list[i]);\r
86 if (temp.isDirectory()) {\r
87 deleteDir(objFileWhole + File.separator + list[i]);\r
fed802b1 88 } else {\r
5ea254f6 89 temp.delete();\r
fed802b1 90 }\r
91 }\r
5ea254f6 92 new File(objFileWhole).delete();\r
a756211f 93 }\r
94 \r
5ea254f6 95 public static final String dirCopy_(String src) throws Exception {\r
8c4eeeb6 96 Matcher mtrseparate = Common.ptnseparate.matcher(src);\r
97 if (mtrseparate.find()) {\r
98 dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r
99 }\r
100 return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r
101 }\r
102 \r
5ea254f6 103 public static final void dirCopy(String src, String des) throws Exception {\r
8c4eeeb6 104 String[] list = new File(src).list();\r
105 File test;\r
106\r
107 for (int i = 0 ; i < list.length ; i++) {\r
108 test = new File(src + File.separator + list[i]);\r
109 if (test.isDirectory()) {\r
110 dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r
111 } else {\r
112 ensureDir(des + File.separator + list[i]);\r
113 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r
114 }\r
115 }\r
116 }\r
5ea254f6 117\r
118 //--------------------------------------dir--------------------------------------------//\r
119\r
120 //-------------------------------like python walk-----------------------------------------//\r
121 \r
122 public static final void toDoAll(String path, Method md, Object obj, Object[] args, int type) throws Exception {\r
123 String[] list = new File(path).list();\r
124 ArrayList<Object> _args = new ArrayList<Object>();\r
125 \r
126 _args.add(path);\r
127 if (args != null) {\r
128 for (int i = 0; i < args.length; i++) {\r
129 _args.add(args[i]);\r
130 }\r
131 }\r
132\r
133 if (type == DIR || type == BOTH) {\r
134 md.invoke(obj, _args.toArray());\r
135 }\r
136 for (int i = 0 ; i < list.length ; i++) {\r
137 if (new File(path + File.separator + list[i]).isDirectory()) {\r
138 toDoAll(path + File.separator + list[i], md, obj, args, type);\r
139 } else {\r
140 if (type == FILE || type == BOTH) {\r
141 _args.set(0, path + File.separator + list[i]);\r
142 md.invoke(obj, _args.toArray());\r
143 }\r
144 }\r
145 }\r
146 }\r
16b7eeef 147\r
148 public static final void toDoAll(Set<String> set, ForDoAll fda) throws Exception {\r
149 Iterator<String> di = set.iterator();\r
150 while (di.hasNext()) {\r
151 fda.run(di.next());\r
152 }\r
153 }\r
fed802b1 154 \r
16b7eeef 155 public static final void toDoAll(String path, ForDoAll fda, int type) throws Exception { // filter of file type can be done in toDo\r
fed802b1 156 String[] list = new File(path).list();\r
157 File test;\r
158\r
b678aaca 159 if (type == DIR || type == BOTH) {\r
71874f93 160 fda.run(path);\r
b678aaca 161 }\r
fed802b1 162 for (int i = 0 ; i < list.length ; i++) {\r
163 test = new File(path + File.separator + list[i]);\r
164 if (test.isDirectory()) {\r
71874f93 165 if (fda.filter(test)) {\r
166 toDoAll(path + File.separator + list[i], fda, type);\r
167 }\r
fed802b1 168 } else {\r
b678aaca 169 if (type == FILE || type == BOTH) {\r
71874f93 170 fda.run(path + File.separator + list[i]);\r
b678aaca 171 }\r
fed802b1 172 }\r
173 }\r
174 }\r
175 \r
176 public static interface ForDoAll {\r
7deec148 177 public void run(String filepath) throws Exception;\r
178 \r
71874f93 179 public boolean filter(File dir);\r
180 }\r
181 \r
182 public static abstract class Laplace {\r
2887f99b 183 public void transform(String src, String des) throws Exception {\r
71874f93 184 Common.string2file(operation(Common.file2string(src)), des);\r
185 }\r
7deec148 186 \r
2887f99b 187 public void run() {\r
188 \r
189 }\r
190 \r
71874f93 191 public abstract String operation(String wholeline);\r
2887f99b 192 \r
193 public abstract boolean recognize(String filename);\r
194 \r
195 public abstract String namechange(String oldname);\r
90503bad 196 }\r
7ab9a5e5 197 \r
198 public static interface Element {\r
199 \r
200// public int replace = 0;\r
201// public int type = 1;\r
202 \r
203 public String getReplace(String key);\r
204 \r
205// public void getType(String key);\r
206// \r
207// public void setReplace(int num);\r
208// \r
209// public void setType(int num);\r
210 \r
211 }\r
90503bad 212}\r