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 |
13 | package org.tianocore.migration;\r |
14 | \r |
fed802b1 |
15 | import java.io.*;\r |
16 | import java.util.regex.*;\r |
17 | import java.util.*;\r |
90503bad |
18 | \r |
19 | public 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 |
a756211f |
32 | public static void string2file(String content, String filename) throws Exception {\r |
33 | ensureDir(filename);\r |
34 | PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r |
35 | outfile.append(content);\r |
36 | outfile.flush();\r |
37 | outfile.close();\r |
38 | }\r |
39 | \r |
90503bad |
40 | public static void ensureDir(String objFileWhole) {\r |
90503bad |
41 | File tempdir;\r |
8c4eeeb6 |
42 | Matcher mtrseparate = ptnseparate.matcher(objFileWhole);\r |
90503bad |
43 | if (mtrseparate.find()) {\r |
44 | tempdir = new File(mtrseparate.group(1));\r |
45 | if (!tempdir.exists()) tempdir.mkdirs();\r |
46 | }\r |
fed802b1 |
47 | }\r |
48 | \r |
fed802b1 |
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 |
a756211f |
66 | public static String replaceAll(String line, Pattern ptn, String des) {\r |
67 | Matcher mtr = ptn.matcher(line);\r |
68 | if (mtr.find()) {\r |
69 | return mtr.replaceAll(des);\r |
70 | }\r |
71 | return line;\r |
72 | }\r |
73 | \r |
8c4eeeb6 |
74 | public static String dirCopy_(String src) throws Exception {\r |
75 | Matcher mtrseparate = Common.ptnseparate.matcher(src);\r |
76 | if (mtrseparate.find()) {\r |
77 | dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r |
78 | }\r |
79 | return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r |
80 | }\r |
81 | \r |
82 | public static void dirCopy(String src, String des) throws Exception {\r |
83 | String[] list = new File(src).list();\r |
84 | File test;\r |
85 | \r |
86 | for (int i = 0 ; i < list.length ; i++) {\r |
87 | test = new File(src + File.separator + list[i]);\r |
88 | if (test.isDirectory()) {\r |
89 | dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r |
90 | } else {\r |
91 | ensureDir(des + File.separator + list[i]);\r |
92 | string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r |
93 | }\r |
94 | }\r |
95 | }\r |
fed802b1 |
96 | \r |
97 | public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo\r |
98 | String[] list = new File(path).list();\r |
99 | File test;\r |
100 | \r |
101 | for (int i = 0 ; i < list.length ; i++) {\r |
102 | test = new File(path + File.separator + list[i]);\r |
103 | if (test.isDirectory()) {\r |
104 | toDoAll(path + File.separator + list[i], fda);\r |
105 | } else {\r |
106 | fda.toDo(path + File.separator + list[i]);\r |
107 | }\r |
108 | }\r |
109 | }\r |
110 | \r |
111 | public static interface ForDoAll {\r |
112 | public void toDo(String filepath) throws Exception;\r |
90503bad |
113 | }\r |
114 | }\r |