1 package org.gnunet.seaspider;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FilenameFilter;
8 import org.gnunet.seaspider.parser.CParser;
9 import org.gnunet.seaspider.parser.ParseException;
10 import org.gnunet.seaspider.parser.TokenMgrError;
11 import org.gnunet.seaspider.parser.nodes.Node;
13 public class Seaspider {
15 private static final boolean DEBUG = true;
16 private static CParser parser = null;
17 private static boolean isFirstFile = true;
18 private static int successCount = 0;
19 private static int failureCount = 0;
20 private static FilenameFilter filter = new FilenameFilter() {
21 public boolean accept(File dir, String fileName) {
22 File file = new File(dir.getPath() + "/" + fileName);
23 if ((file.isDirectory() && !fileName.startsWith(".")) || (fileName.endsWith(".c") && !fileName.startsWith("test_")))
24 /* directories like .svn are of no interest */
31 private static void doParseFile(String filePath)
35 parser = new CParser(new FileInputStream(filePath));
39 parser.ReInit(new FileInputStream(filePath));
41 catch (FileNotFoundException e) {
42 /* This should never happen */
43 System.err.println("File not found!");
48 System.out.println("Parsing file: " + filePath);
49 Node root = parser.TranslationUnit();
50 root.accept(new ExpressionExtractorVisitor(filePath));
51 System.out.println("File " + filePath + " parsed successfully.");
54 catch (ParseException e) {
55 System.out.println("Encountered errors during parsing file " + filePath);
59 } catch (TokenMgrError e) {
60 System.err.println("Encountered errors during parsing file " + filePath + ":" + e.getMessage());
68 private static void parseRecursively(String path)
70 File file = new File(path);
72 if (!file.isDirectory()) {
73 if (path.endsWith(".db"))
80 /* A source directory */
81 System.out.println("Reading from: " + path + " source directory...");
82 String[] dirContents = file.list(filter);/* Only directories and .c files */
83 for (int i = 0; i < dirContents.length; i++) {
84 String fullPath = path + "/" + dirContents[i];
85 parseRecursively(fullPath);
89 public static void main(String args[])
91 String dbFilePath = null;
95 System.err.println("Invoke seaspider with database filename and source path!");
98 System.out.println("Seaspider 0.0\n");
101 for (int i = 0; i < args.length; i++) {
102 if (args[i].endsWith(".db"))
103 dbFilePath = args[i];
105 /* Should be a valid path for a file or a directory */
106 File file = new File(args[i]);
107 if (!file.exists()) {
108 System.err.println("\"" + args[i] + "\" is an invalid file or directory location");
110 } else if (!file.isDirectory() && !args[i].endsWith(".c")) {
111 System.err.println("\"" + args[i] + "\" only source files can be parsed");
115 if (null == dbFilePath) {
116 System.err.println("Missing database file path");
120 /* Create the Expressions Database */
121 ExpressionDatabaseHandler.createExpressionDatabase(dbFilePath);
123 for (int i = 0; i < args.length; i++)
124 parseRecursively(args[i]);
126 /* We're done with the Expression Database, close it */
127 ExpressionDatabaseHandler.closeDatabase();
129 System.out.println(successCount + " parsed successfully.");
130 System.out.println("Failed to parse " + failureCount + " files.");