Fix EDKT191.
[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 ensureDir(String objFileWhole) {
33 File tempdir;
34 Matcher mtrseparate = ptnseparate.matcher(objFileWhole);
35 if (mtrseparate.find()) {
36 tempdir = new File(mtrseparate.group(1));
37 if (!tempdir.exists()) tempdir.mkdirs();
38 }
39 }
40
41 public static void string2file(String content, String filename) throws Exception {
42 ensureDir(filename);
43 PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
44 outfile.append(content);
45 outfile.flush();
46 outfile.close();
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 dirCopy_(String src) throws Exception {
67 Matcher mtrseparate = Common.ptnseparate.matcher(src);
68 if (mtrseparate.find()) {
69 dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));
70 }
71 return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);
72 }
73
74 public static void dirCopy(String src, String des) throws Exception {
75 String[] list = new File(src).list();
76 File test;
77
78 for (int i = 0 ; i < list.length ; i++) {
79 test = new File(src + File.separator + list[i]);
80 if (test.isDirectory()) {
81 dirCopy(src + File.separator + list[i], des + File.separator + list[i]);
82 } else {
83 ensureDir(des + File.separator + list[i]);
84 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);
85 }
86 }
87 }
88
89 public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo
90 String[] list = new File(path).list();
91 File test;
92
93 for (int i = 0 ; i < list.length ; i++) {
94 test = new File(path + File.separator + list[i]);
95 if (test.isDirectory()) {
96 toDoAll(path + File.separator + list[i], fda);
97 } else {
98 fda.toDo(path + File.separator + list[i]);
99 }
100 }
101 }
102
103 public static interface ForDoAll {
104 public void toDo(String filepath) throws Exception;
105 }
106 }