提取C++库函数的代码(java)
来源:互联网 发布:如何注册淘宝企业店铺 编辑:程序博客网 时间:2024/05/16 14:03
闲来无事,写了一个提取C++库函数的代码,没有仔细测试。
package edu.kevin.regular;
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class SrcAnalyze ...{
private String srcDir;
private HashSet allAPI;
private HashSet inAPI;
private Pattern pt;
public SrcAnalyze(String dir) ...{
srcDir = dir;
allAPI = new HashSet(1000);
inAPI = new HashSet(1000);
//抽取C++的库函数调用
pt = Pattern.compile("(/b+)(/w+)/([^/)]*/)(/s*(/{)|[^/{])");
}
public void extract() ...{
Date start = new Date();
long startTime = start.getTime();
extDir(srcDir);
for (Object hs : inAPI) ...{
if (allAPI.contains(hs)) ...{
allAPI.remove(hs);
}
}
Date end = new Date();
long endTime = end.getTime();
//计算程序运行时间
System.out.println("Time: " + (endTime - startTime) + "ms");
}
//遍历指定目录(包括子文件夹)
private void extDir(String strPath) ...{
File dir = new File(strPath);
File[] files = dir.listFiles();
if (files == null)
return;
for (File fl : files) ...{
if (fl.isDirectory()) ...{
extDir(fl.getAbsolutePath());
} else ...{
String strFileName = fl.getAbsolutePath();
if (strFileName.matches("^.+/.([cChH]|(cpp))$"))
readFile(strFileName);
}
}
}
private void readFile(String filename) ...{
try ...{
BufferedReader in = new BufferedReader(new FileReader(filename));
String s = null;
StringBuffer tmp = new StringBuffer("");
//删除所有的注释(单行的双行的)
while ((s = in.readLine()) != null) ...{
if (!(s.matches("^/s*//*.+$"))) ...{
// get rid of comment like "//..."(Single Line)
tmp.append(s.replaceAll(""[^"]*"", "").replaceAll(
"//.+$", ""));
} else ...{
// get rid of comment like "/*...*/"(Multi-Lines)
if (!(s.matches("^.+/*/$"))) ...{
while ((s = in.readLine()) != null) ...{
if (s.matches("^.+/*/$"))
break;
}
}
}
}
in.close();
//抽取库函数调用
extMethod(tmp);
} catch (IOException e) ...{
System.out.println(e.toString());
}
}
//抽取库函数调用
private void extMethod(StringBuffer src) ...{
int i = 0, j = 0;
String tmp;
// internal API
Matcher m = pt.matcher(src);
while (m.find()) ...{
tmp = m.group(2);
if (!tmp.equals("if") && !tmp.equals("for") && !tmp.equals("while")
&& !tmp.equals("switch") && !tmp.equals("return")) ...{
allAPI.add(tmp);
if (m.group(4) != null)
inAPI.add(tmp);
}
}
}
public void print() ...{
System.out.println("+++++++++++++++++++++++++++++++++++++++");
Object[] allAPI_List = allAPI.toArray();
Arrays.sort(allAPI_List);
for (Object all : allAPI_List)
System.out.println(all);
System.out.println("+++++++++++++++++++++++++++++++++++++++");
}
public static void main(String[] args) ...{
SrcAnalyze test = new SrcAnalyze("D:/Test");
test.extract();
test.print();
}
}
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class SrcAnalyze ...{
private String srcDir;
private HashSet allAPI;
private HashSet inAPI;
private Pattern pt;
public SrcAnalyze(String dir) ...{
srcDir = dir;
allAPI = new HashSet(1000);
inAPI = new HashSet(1000);
//抽取C++的库函数调用
pt = Pattern.compile("(/b+)(/w+)/([^/)]*/)(/s*(/{)|[^/{])");
}
public void extract() ...{
Date start = new Date();
long startTime = start.getTime();
extDir(srcDir);
for (Object hs : inAPI) ...{
if (allAPI.contains(hs)) ...{
allAPI.remove(hs);
}
}
Date end = new Date();
long endTime = end.getTime();
//计算程序运行时间
System.out.println("Time: " + (endTime - startTime) + "ms");
}
//遍历指定目录(包括子文件夹)
private void extDir(String strPath) ...{
File dir = new File(strPath);
File[] files = dir.listFiles();
if (files == null)
return;
for (File fl : files) ...{
if (fl.isDirectory()) ...{
extDir(fl.getAbsolutePath());
} else ...{
String strFileName = fl.getAbsolutePath();
if (strFileName.matches("^.+/.([cChH]|(cpp))$"))
readFile(strFileName);
}
}
}
private void readFile(String filename) ...{
try ...{
BufferedReader in = new BufferedReader(new FileReader(filename));
String s = null;
StringBuffer tmp = new StringBuffer("");
//删除所有的注释(单行的双行的)
while ((s = in.readLine()) != null) ...{
if (!(s.matches("^/s*//*.+$"))) ...{
// get rid of comment like "//..."(Single Line)
tmp.append(s.replaceAll(""[^"]*"", "").replaceAll(
"//.+$", ""));
} else ...{
// get rid of comment like "/*...*/"(Multi-Lines)
if (!(s.matches("^.+/*/$"))) ...{
while ((s = in.readLine()) != null) ...{
if (s.matches("^.+/*/$"))
break;
}
}
}
}
in.close();
//抽取库函数调用
extMethod(tmp);
} catch (IOException e) ...{
System.out.println(e.toString());
}
}
//抽取库函数调用
private void extMethod(StringBuffer src) ...{
int i = 0, j = 0;
String tmp;
// internal API
Matcher m = pt.matcher(src);
while (m.find()) ...{
tmp = m.group(2);
if (!tmp.equals("if") && !tmp.equals("for") && !tmp.equals("while")
&& !tmp.equals("switch") && !tmp.equals("return")) ...{
allAPI.add(tmp);
if (m.group(4) != null)
inAPI.add(tmp);
}
}
}
public void print() ...{
System.out.println("+++++++++++++++++++++++++++++++++++++++");
Object[] allAPI_List = allAPI.toArray();
Arrays.sort(allAPI_List);
for (Object all : allAPI_List)
System.out.println(all);
System.out.println("+++++++++++++++++++++++++++++++++++++++");
}
public static void main(String[] args) ...{
SrcAnalyze test = new SrcAnalyze("D:/Test");
test.extract();
test.print();
}
}
- 提取C++库函数的代码(java)
- 库函数的实现代码
- java提取html正文的图片代码
- c库函数的实现
- c的库函数getline
- C的库函数
- C的库函数
- C的库函数
- 常用的C库函数
- C库函数的手册
- Java中调用C库函数
- Java调用C/C++生成的动态库函数
- java调用C/C++写的库函数 dll
- java调用C/C++写的库函数 dll
- 通过库函数API和C代码中嵌入汇编代码剖析系统调用的工作机制
- Java发送HTTP请求和提取数据的Java代码
- C语言的常用库函数
- C语言的常用库函数
- Mac OS X 重设管理员密码
- 搞定诡秘进程,删除顽固文件
- Liva运行时截图[00原创]
- 分析:ERP实施项目中体系管理部门的角色[转]
- 网站设计常用技巧(上)
- 提取C++库函数的代码(java)
- The end, the new beginning
- 网站设计常用技巧(下)
- 用C#实时获取CPU利用率
- Mac机装双操作系统
- 关于MFC中View类中GetDocument的出错解决方案
- 每天用电脑4小时-得癌症
- SQL Server六步改善安全规划全攻略(转载来的,感觉写的不错)
- 在suse linux 下 使用脚本安装 sybase 全过程