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