package org.gnunet.seaspider;
import java.io.File;
-import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.FilenameFilter;
import org.gnunet.seaspider.parser.CParser;
import org.gnunet.seaspider.parser.ParseException;
public class Seaspider {
- static final boolean DEBUG = false;
-
+ private static final boolean DEBUG = true;
+ private static CParser parser = null;
+ private static boolean isFirstFile = true;
+ private static int successCount = 0;
+ private static int failureCount = 0;
+ private static FilenameFilter filter = new FilenameFilter() {
+ public boolean accept(File dir, String fileName) {
+ File file = new File(dir.getPath() + "/" + fileName);
+ if ((file.isDirectory() && !fileName.startsWith(".")) || (fileName.endsWith(".c") && !fileName.startsWith("test_")))
+ /* directories like .svn are of no interest */
+ return true;
+ return false;
+ }
+ };
+
+
+ private static void doParseFile(String filePath)
+ {
+ try {
+ if (isFirstFile) {
+ parser = new CParser(new FileInputStream(filePath));
+ isFirstFile = false;
+ }
+ else
+ parser.ReInit(new FileInputStream(filePath));
+ }
+ catch (FileNotFoundException e) {
+ /* This should never happen */
+ System.err.println("File not found!");
+ e.printStackTrace();
+ System.exit(1);
+ }
+ try {
+ System.out.println("Parsing file: " + filePath);
+ Node root = parser.TranslationUnit();
+ root.accept(new ExpressionExtractorVisitor(filePath));
+ System.out.println("File " + filePath + " parsed successfully.");
+ successCount++;
+ }
+ catch (ParseException e) {
+ System.out.println("Encountered errors during parsing file " + filePath);
+ failureCount++;
+ if (DEBUG)
+ e.printStackTrace();
+ } catch (TokenMgrError e) {
+ System.err.println("Encountered errors during parsing file " + filePath + ":" + e.getMessage());
+ failureCount++;
+ if (DEBUG)
+ e.printStackTrace();
+ }
+ }
+
+
+ private static void parseRecursively(String dirPath)
+ {
+ File dir = new File(dirPath);
+
+ if (!dir.isDirectory()) {
+ if (dirPath.endsWith(".c")) {
+ doParseFile(dirPath);
+ return;
+ } else /* Probably the Database file */
+ return;
+ }
+
+ System.out.println("Reading from: " + dirPath + " source directory...");
+ String[] dirContents = dir.list(filter);/* Only directories and .c files */
+ for (int i = 0; i < dirContents.length; i++) {
+ String fullPath = dirPath + "/" + dirContents[i];
+ File file = new File(fullPath);
+
+ if (file.isDirectory())
+ parseRecursively(fullPath);
+ else
+ doParseFile(fullPath); /* Path is for a file */
+ }
+ }
+
public static void main(String args[])
{
- CParser parser = null;
- boolean isFirstFile = true;
- int fileNotFoundCount = 0;
- int successCount = 0;
- int failureCount = 0;
+ String dbFilePath = null;
- if (args.length != 2)
+ if (args.length < 2)
{
System.err.println("Invoke seaspider with database filename and source path!");
System.exit(1);
}
System.out.println("Seaspider 0.0\n");
- System.out.println("Reading from " + args[1] + " source directory...");
- String gnunetSourcePath = args[1];
-
- /* Filtering out files */
- FileFilter filter = new FileFilter() {
- public boolean accept(File file) {
- return file.isDirectory();
- }
- };
- /* File filter to get only source files and no test cases */
- FileFilter sourceFilter = new FileFilter() {
- public boolean accept(File file) {
- String fileName = file.getName();
- return fileName.endsWith(".c") && ! fileName.startsWith("test_");
- }
- };
-
- /* Create the Expressions Database */
- ExpressionDatabaseHandler.createExpressionDatabase(args[0]);
- File[] dirArr = (new File(gnunetSourcePath)).listFiles(filter);
- for (int i = 0; i < dirArr.length; i++) {
- File dir = dirArr[i];
- File[] fileArr = dir.listFiles(sourceFilter);
- for (int j = 0; j < fileArr.length; j++) {
- try {
- if (isFirstFile) {
- parser = new CParser(new FileInputStream(fileArr[j].getPath()));
- isFirstFile = false;
- }
- else
- parser.ReInit(new FileInputStream(fileArr[j].getPath()));
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].endsWith(".db"))
+ dbFilePath = args[i];
+ else {
+ /* Should be a valid path for a file or a directory */
+ File file = new File(args[i]);
+ if (!file.exists()) {
+ System.err.println("\"" + args[i]+ "\" is an invalid file or directory location");
+ System.exit(1);
}
- catch (FileNotFoundException e) {
- fileNotFoundCount++;
- e.printStackTrace();
- }
- try {
- Node root = parser.TranslationUnit();
- root.accept(new ExpressionExtractorVisitor(fileArr[j].getName()));
- System.out.println("File " + dir + "/" + fileArr[j].getName() + " parsed successfully.");
- successCount++;
- }
- catch (ParseException e) {
- System.err.println("Encountered errors during parsing file " + fileArr[j].getName() + ":" + e.getMessage());
- failureCount++;
- if (DEBUG)
- e.printStackTrace();
- } catch (TokenMgrError e)
- {
- System.err.println("Encountered errors during parsing file " + fileArr[j].getName() + ":" + e.getMessage());
- failureCount++;
- if (DEBUG)
- e.printStackTrace();
- }
}
}
+ if (null == dbFilePath) {
+ System.err.println("Missing database file path");
+ System.exit(1);
+ }
+
+ /* Create the Expressions Database */
+ ExpressionDatabaseHandler.createExpressionDatabase(dbFilePath);
+
+ for (int i = 0; i < args.length; i++)
+ parseRecursively(args[i]);
/* We're done with the Expression Database, close it */
ExpressionDatabaseHandler.closeDatabase();
System.out.println(successCount + " parsed successfully.");
System.out.println("Failed to parse " + failureCount + " files.");
- System.out.println(fileNotFoundCount + " files not found.");
}
-
}