summaryrefslogtreecommitdiff
path: root/Tools/Source/MigrationTools/org/tianocore/migration/Common.java
blob: 5b4d33adb8299e8fa0633a90d5b1250a3d5a2503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/** @file
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.migration;

import java.io.*;
import java.util.regex.*;
import java.util.*;
import java.lang.reflect.*;

public final class Common {
	public static final int BOTH = 0;
	public static final int FILE = 1;
	public static final int DIR = 2;
	
	public static final String strseparate = "(.*)\\\\([^\\\\]*)";
	public static final Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");

	//-------------------------------------regex------------------------------------------//
	
	public static final String replaceAll(String line, Pattern ptn, String des) {
		Matcher mtr = ptn.matcher(line);
		if (mtr.find()) {
			 return mtr.replaceAll(des);
		}
		return line;
	}

	//-------------------------------------regex------------------------------------------//
	
	//-----------------------------------file&string---------------------------------------//
	
	public static final String file2string(String filename) throws Exception {
		BufferedReader rd = new BufferedReader(new FileReader(filename));
		StringBuffer wholefile = new StringBuffer();
		String line;
		while ((line = rd.readLine()) != null) {
			wholefile.append(line + "\n");
		}
		return wholefile.toString();
	}

	public static final void string2file(String content, String filename) throws Exception {
		ensureDir(filename);
		PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
		outfile.append(content);
		outfile.flush();
		outfile.close();
	}

	//-----------------------------------file&string---------------------------------------//

	//--------------------------------------dir--------------------------------------------//
	/*
	public static final HashSet<String> walkDir(String path, int mode) throws Exception {
		HashSet<String> pathlist = new HashSet<String>();
		Common.toDoAll(path, Common.class.getMethod("walkDir", String.class), null, null, mode);
		return pathlist;
	}
	*/
	public static final void ensureDir(String objFileWhole) {
		File tempdir;
		Matcher mtrseparate = ptnseparate.matcher(objFileWhole);
		if (mtrseparate.find()) {
			tempdir = new File(mtrseparate.group(1));
			if (!tempdir.exists()) tempdir.mkdirs();
		}
	}
	
	public static final void deleteDir(String objFileWhole) {
		String[] list = new File(objFileWhole).list();
		File temp;
		for (int i = 0 ; i < list.length ; i++) {
			temp = new File(objFileWhole + File.separator + list[i]);
			if (temp.isDirectory()) {
				deleteDir(objFileWhole + File.separator + list[i]);
			} else {
				temp.delete();
			}
		}
		new File(objFileWhole).delete();
	}
	
	public static final String dirCopy_(String src) throws Exception {
		Matcher mtrseparate = Common.ptnseparate.matcher(src);
		if (mtrseparate.find()) {
			dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));
		}
		return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);
	}
	
	public static final void dirCopy(String src, String des) throws Exception {
		String[] list = new File(src).list();
		File test;

		for (int i = 0 ; i < list.length ; i++) {
			test = new File(src + File.separator + list[i]);
			if (test.isDirectory()) {
				dirCopy(src + File.separator + list[i], des + File.separator + list[i]);
			} else {
				ensureDir(des + File.separator + list[i]);
				string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);
			}
		}
	}

	//--------------------------------------dir--------------------------------------------//

	//-------------------------------like python walk-----------------------------------------//
	
	public static final void toDoAll(String path, Method md, Object obj, Object[] args, int type) throws Exception {
		String[] list = new File(path).list();
		ArrayList<Object> _args = new ArrayList<Object>();
		
		_args.add(path);
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				_args.add(args[i]);
			}
		}

		if (type == DIR || type == BOTH) {
			md.invoke(obj, _args.toArray());
		}
		for (int i = 0 ; i < list.length ; i++) {
			if (new File(path + File.separator + list[i]).isDirectory()) {
				toDoAll(path + File.separator + list[i], md, obj, args, type);
			} else {
				if (type == FILE || type == BOTH) {
					_args.set(0, path + File.separator + list[i]);
					md.invoke(obj, _args.toArray());
				}
			}
		}
	}
	
	public static void toDoAll(String path, ForDoAll fda, int type) throws Exception { // filter of file type can be done in toDo
		String[] list = new File(path).list();
		File test;

		if (type == DIR || type == BOTH) {
			fda.toDo(path);
		}
		for (int i = 0 ; i < list.length ; i++) {
			test = new File(path + File.separator + list[i]);
			if (test.isDirectory()) {
				toDoAll(path + File.separator + list[i], fda, type);
			} else {
				if (type == FILE || type == BOTH) {
					fda.toDo(path + File.separator + list[i]);
				}
			}
		}
	}
	
	public static interface ForDoAll {
		public void toDo(String filepath) throws Exception;
	}
	/*
	// this PathIterator is based on HashSet, an thread implementation is required.
	private final class PathIterator implements ForDoAll{
		PathIterator(String path) throws Exception {
			startpath = path;
			Common.toDoAll(startpath, this, mode);
		}
		PathIterator(String path, int md) throws Exception {
			startpath = path;
			mode = md;
			Common.toDoAll(startpath, this, mode);
		}
		private String startpath;
		private int mode = Common.BOTH;
		private HashSet<String> pathlist = new HashSet<String>();
		private Iterator<String> it = pathlist.iterator();
		
		public final void toDo(String path) throws Exception {
			pathlist.add(path);
		}
		
		public final String next() {
			return it.next();
		}
		
		public final boolean hasNext() {
			return it.hasNext();
		}
		
		public final String toString() {
			return pathlist.toString();
		}
	}
	
	public final PathIterator getPathIterator(String path, int md) throws Exception {
		return new PathIterator(path, md);
	}
	*/
}