提取C++库函数的代码(java)

 

闲来无事,写了一个提取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();
    }


}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值