| 1 | package org.tianocore.migration;\r |
| 2 | \r |
| 3 | import java.io.*;\r |
| 4 | import java.util.regex.*;\r |
| 5 | import java.util.*;\r |
| 6 | \r |
| 7 | public class Common {\r |
| 8 | public static String file2string(String filename) throws Exception {\r |
| 9 | BufferedReader rd = new BufferedReader(new FileReader(filename));\r |
| 10 | StringBuffer wholefile = new StringBuffer();\r |
| 11 | String line;\r |
| 12 | while ((line = rd.readLine()) != null) {\r |
| 13 | wholefile.append(line + "\n");\r |
| 14 | }\r |
| 15 | return wholefile.toString();\r |
| 16 | }\r |
| 17 | \r |
| 18 | public static void ensureDir(String objFileWhole) {\r |
| 19 | Pattern ptnseparate = Pattern.compile("(.*)\\\\[^\\\\]*");\r |
| 20 | Matcher mtrseparate;\r |
| 21 | File tempdir;\r |
| 22 | \r |
| 23 | mtrseparate = ptnseparate.matcher(objFileWhole);\r |
| 24 | if (mtrseparate.find()) {\r |
| 25 | tempdir = new File(mtrseparate.group(1));\r |
| 26 | if (!tempdir.exists()) tempdir.mkdirs();\r |
| 27 | }\r |
| 28 | }\r |
| 29 | \r |
| 30 | public static void string2file(String content, String filename) throws Exception {\r |
| 31 | ensureDir(filename);\r |
| 32 | PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r |
| 33 | outfile.append(content);\r |
| 34 | outfile.flush();\r |
| 35 | outfile.close();\r |
| 36 | }\r |
| 37 | \r |
| 38 | public static HashSet<String> dirScan(String path) { // use HashSet, persue speed rather than space\r |
| 39 | HashSet<String> filelist = new HashSet<String>();\r |
| 40 | String[] list = new File(path).list();\r |
| 41 | File test;\r |
| 42 | \r |
| 43 | for (int i = 0 ; i < list.length ; i++) {\r |
| 44 | test = new File(path + File.separator + list[i]);\r |
| 45 | if (test.isDirectory()) {\r |
| 46 | dirScan(path + File.separator + list[i]);\r |
| 47 | } else {\r |
| 48 | filelist.add(path + File.separator + list[i]);\r |
| 49 | }\r |
| 50 | }\r |
| 51 | \r |
| 52 | return filelist;\r |
| 53 | }\r |
| 54 | \r |
| 55 | public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo\r |
| 56 | String[] list = new File(path).list();\r |
| 57 | File test;\r |
| 58 | \r |
| 59 | for (int i = 0 ; i < list.length ; i++) {\r |
| 60 | test = new File(path + File.separator + list[i]);\r |
| 61 | if (test.isDirectory()) {\r |
| 62 | toDoAll(path + File.separator + list[i], fda);\r |
| 63 | } else {\r |
| 64 | fda.toDo(path + File.separator + list[i]);\r |
| 65 | }\r |
| 66 | }\r |
| 67 | }\r |
| 68 | \r |
| 69 | public static interface ForDoAll {\r |
| 70 | public void toDo(String filepath) throws Exception;\r |
| 71 | }\r |
| 72 | }\r |