import java.util.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class SubFunctionSummary {
private static String[] fileFilter = new String[] { ".bas", ".frm", ".cls" };
private static String[] function = new String[] { " Function ", "End Function", "Fun_" };
private static String[] sub = new String[] { " Sub ", "End Sub" , "Sub_" };
// 不是以Sub_ 或者 Fun_开头的Sub 或者 Function
private static String[] specialSubFunction = new String[] {
"WriteIniString", "GetIniString" };
public static void main(String[] args) {
String path = "C:/Documents and Settings/Evan/桌面/Down.LieHuo.Net";///frmBD_ASL.frm";
process(path);
}
public static void process(String path) {
File directory = new File(path);
if (directory.isDirectory()) {
for (int i = 0; i < directory.listFiles().length; i++) {
process2(directory.listFiles()[i]);
}
} else if (directory.isFile()) {
process2(directory);
}
}
public static void process2(File file) {
String fileName = file.getName();
if (fileName.endsWith(fileFilter[0]) || fileName.endsWith(fileFilter[1])
|| fileName.endsWith(fileFilter[2])) {
process3(file);
} else {
return;
}
}
public static void process3(File file) {
Map<String, List<String>> subFunctionMap = new HashMap<String, List<String>>();
List<String> subFunctionList = null;
BufferedReader reader = null;
String srcLine = "";
String subFunctionName = null;
try {
reader = new BufferedReader(new FileReader(file));
while (srcLine != null) {
int iFunction = srcLine.indexOf(function[0]);
int iSub = srcLine.indexOf(sub[0]);
int iStart = 0;
int iEnd = 0;
// Function 或者 Sub 定义开始行
if (iFunction > 0 || iSub > 0) {
if (iFunction > 0) {
// 非注释行
if (srcLine.substring(0, iFunction).indexOf("'") <= 0) {
iStart = iFunction + function[0].length();
iEnd = srcLine.indexOf("(");
}
} else {
// 非注释行
if (srcLine.substring(0, iSub).indexOf("'") <= 0) {
iStart = iSub + sub[0].length();
iEnd = srcLine.indexOf("(");
}
}
if (iStart > 0 && iEnd > iStart) {
subFunctionName = srcLine.substring(iStart, iEnd);
subFunctionList = new ArrayList<String>();
}
} else {
// Function 或者 Sub 定义结束行
if (srcLine.startsWith(function[1])
|| srcLine.startsWith(sub[1])) {
if (subFunctionName != null && subFunctionList != null) {
subFunctionMap.put(subFunctionName, subFunctionList);
}
} else {
process4(srcLine, subFunctionList, subFunctionName);
}
}
srcLine = reader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
output("C:/Documents and Settings/Evan/桌面", "SubFunction.csv", subFunctionMap, file.getName());
}
public static void process4(String srcLine, List<String> subFunctionList, String subFunctionName) {
// 被调用的Sub 或者Function的名字
String subSubFunctionName = null;
int iFunction = srcLine.indexOf(function[2]);
int iSub = srcLine.indexOf(sub[2]);
int iStart = 0;
int iEnd = 0;
// 包含Fun_ 或者 Sub_ 的行
if (iFunction > 0 || iSub > 0) {
if (iFunction > 0) {
// 非注释行
if (srcLine.substring(0, iFunction).indexOf("'") <= 0) {
iStart = iFunction;
iEnd = srcLine.substring(iStart).indexOf(" ");
}
} else {
// 非注释行
if (srcLine.substring(0, iSub).indexOf("'") <= 0) {
iStart = iSub + sub[0].length();
iEnd = srcLine.substring(iStart).indexOf(" ");
}
}
if (iStart > 0 && iEnd > iStart) {
subSubFunctionName = srcLine.substring(iStart, iEnd);
// 不是在Functiong赋值的语句
if (!subFunctionName.equals(subSubFunctionName) && !subFunctionList.contains(subSubFunctionName)) {
subFunctionList.add(subSubFunctionName);
}
}
} else {
for (int i = 0; i < specialSubFunction.length; i++) {
if (srcLine.indexOf(specialSubFunction[i]) >= 0) {
subSubFunctionName = specialSubFunction[i];
// 不是在Functiong赋值的语句
if (!subFunctionName.equals(subSubFunctionName) && !subFunctionList.contains(subSubFunctionName)) {
subFunctionList.add(subSubFunctionName);
}
}
}
}
}
public static void output(String path, String fileName, Map<String, List<String>> subFunctionMap, String srcName) {
File file = new File(path + "/" + fileName);
BufferedWriter writer = null;
try {
if (!file.exists()) {
file.createNewFile();
}
writer = new BufferedWriter(new FileWriter(file, true));
writer.write(srcName + ",,\n");
for (Iterator<Entry<String, List<String>>> iterator = subFunctionMap.entrySet().iterator(); iterator.hasNext();) {
Entry<String, List<String>> subFunctionEntry = iterator.next();
String subFunctionName = subFunctionEntry.getKey();
List<String> subFunctionList = subFunctionEntry.getValue();
if (subFunctionList != null && !subFunctionList.isEmpty()) {
for (int i = 0; i < subFunctionList.size(); i++) {
if (i == 0) {
writer.write("," + subFunctionName + "," + subFunctionList.get(i) + "\n");
} else {
writer.write(",," + subFunctionList.get(i));
}
}
} else {
writer.write("," + subFunctionName + ",\n");
}
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}