Fix EDKT191.
[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
90503bad 18\r
19public class Common {\r
8c4eeeb6 20 public static Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");\r
21 \r
fed802b1 22 public static String file2string(String filename) throws Exception {\r
90503bad 23 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
24 StringBuffer wholefile = new StringBuffer();\r
25 String line;\r
26 while ((line = rd.readLine()) != null) {\r
27 wholefile.append(line + "\n");\r
28 }\r
29 return wholefile.toString();\r
30 }\r
31\r
32 public static void ensureDir(String objFileWhole) {\r
90503bad 33 File tempdir;\r
8c4eeeb6 34 Matcher mtrseparate = ptnseparate.matcher(objFileWhole);\r
90503bad 35 if (mtrseparate.find()) {\r
36 tempdir = new File(mtrseparate.group(1));\r
37 if (!tempdir.exists()) tempdir.mkdirs();\r
38 }\r
fed802b1 39 }\r
40 \r
41 public static void string2file(String content, String filename) throws Exception {\r
42 ensureDir(filename);\r
43 PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r
44 outfile.append(content);\r
45 outfile.flush();\r
46 outfile.close();\r
47 }\r
48 \r
49 public static HashSet<String> dirScan(String path) { // use HashSet, persue speed rather than space\r
50 HashSet<String> filelist = new HashSet<String>();\r
51 String[] list = new File(path).list();\r
52 File test;\r
53\r
54 for (int i = 0 ; i < list.length ; i++) {\r
55 test = new File(path + File.separator + list[i]);\r
56 if (test.isDirectory()) {\r
57 dirScan(path + File.separator + list[i]);\r
58 } else {\r
59 filelist.add(path + File.separator + list[i]);\r
60 }\r
61 }\r
90503bad 62 \r
fed802b1 63 return filelist;\r
64 }\r
8c4eeeb6 65\r
66 public static String dirCopy_(String src) throws Exception {\r
67 Matcher mtrseparate = Common.ptnseparate.matcher(src);\r
68 if (mtrseparate.find()) {\r
69 dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r
70 }\r
71 return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r
72 }\r
73 \r
74 public static void dirCopy(String src, String des) throws Exception {\r
75 String[] list = new File(src).list();\r
76 File test;\r
77\r
78 for (int i = 0 ; i < list.length ; i++) {\r
79 test = new File(src + File.separator + list[i]);\r
80 if (test.isDirectory()) {\r
81 dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r
82 } else {\r
83 ensureDir(des + File.separator + list[i]);\r
84 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r
85 }\r
86 }\r
87 }\r
fed802b1 88 \r
89 public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo\r
90 String[] list = new File(path).list();\r
91 File test;\r
92\r
93 for (int i = 0 ; i < list.length ; i++) {\r
94 test = new File(path + File.separator + list[i]);\r
95 if (test.isDirectory()) {\r
96 toDoAll(path + File.separator + list[i], fda);\r
97 } else {\r
98 fda.toDo(path + File.separator + list[i]);\r
99 }\r
100 }\r
101 }\r
102 \r
103 public static interface ForDoAll {\r
104 public void toDo(String filepath) throws Exception;\r
90503bad 105 }\r
106}\r