提取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();
    }


}

 
原创粉丝点击