]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/Common.java
Modify Critic
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Common.java
1 /** @file
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.io.*;
16 import java.util.regex.*;
17 import java.util.*;
18
19 public class Common {
20 public static Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");
21
22 public static String file2string(String filename) throws Exception {
23 BufferedReader rd = new BufferedReader(new FileReader(filename));
24 StringBuffer wholefile = new StringBuffer();
25 String line;
26 while ((line = rd.readLine()) != null) {
27 wholefile.append(line + "\n");
28 }
29 return wholefile.toString();
30 }
31
32 public static void string2file(String content, String filename) throws Exception {
33 ensureDir(filename);
34 PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
35 outfile.append(content);
36 outfile.flush();
37 outfile.close();
38 }
39
40 public static void ensureDir(String objFileWhole) {
41 File tempdir;
42 Matcher mtrseparate = ptnseparate.matcher(objFileWhole);
43 if (mtrseparate.find()) {
44 tempdir = new File(mtrseparate.group(1));
45 if (!tempdir.exists()) tempdir.mkdirs();
46 }
47 }
48
49 public static HashSet<String> dirScan(String path) { // use HashSet, persue speed rather than space
50 HashSet<String> filelist = new HashSet<String>();
51 String[] list = new File(path).list();
52 File test;
53
54 for (int i = 0 ; i < list.length ; i++) {
55 test = new File(path + File.separator + list[i]);
56 if (test.isDirectory()) {
57 dirScan(path + File.separator + list[i]);
58 } else {
59 filelist.add(path + File.separator + list[i]);
60 }
61 }
62
63 return filelist;
64 }
65
66 public static String replaceAll(String line, Pattern ptn, String des) {
67 Matcher mtr = ptn.matcher(line);
68 if (mtr.find()) {
69 return mtr.replaceAll(des);
70 }
71 return line;
72 }
73
74 public static String dirCopy_(String src) throws Exception {
75 Matcher mtrseparate = Common.ptnseparate.matcher(src);
76 if (mtrseparate.find()) {
77 dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));
78 }
79 return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);
80 }
81
82 public static void dirCopy(String src, String des) throws Exception {
83 String[] list = new File(src).list();
84 File test;
85
86 for (int i = 0 ; i < list.length ; i++) {
87 test = new File(src + File.separator + list[i]);
88 if (test.isDirectory()) {
89 dirCopy(src + File.separator + list[i], des + File.separator + list[i]);
90 } else {
91 ensureDir(des + File.separator + list[i]);
92 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);
93 }
94 }
95 }
96
97 public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo
98 String[] list = new File(path).list();
99 File test;
100
101 for (int i = 0 ; i < list.length ; i++) {
102 test = new File(path + File.separator + list[i]);
103 if (test.isDirectory()) {
104 toDoAll(path + File.separator + list[i], fda);
105 } else {
106 fda.toDo(path + File.separator + list[i]);
107 }
108 }
109 }
110
111 public static interface ForDoAll {
112 public void toDo(String filepath) throws Exception;
113 }
114 }