src/scanner.c

changeset 34
fa9bda32de17
parent 30
d642fdb6745e
child 36
a7ff583e153f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/scanner.c	Fri Dec 28 15:44:28 2012 +0100
@@ -0,0 +1,150 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
+ * Copyright 2011 Mike Becker. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ *
+ * scanner.c
+ *
+ *  Created on: 23.05.2011
+ *      Author: Mike
+ */
+
+
+#include "scanner.h"
+#include "suffix_fnc.h"
+#include "bfile_heuristics.h"
+#include "regex_parser.h"
+#include <sys/stat.h>
+
+int scanDirectory(scanner_t scanner, settings_t* settings) {
+
+  DIR *dirf;
+  struct dirent *entry;
+  int lines, a;
+  int lineSum = 0;
+  bool bfile;
+  struct stat statbuf;
+
+  if ((dirf = opendir(scanner.dir)) == NULL) {
+    printf("%s", scanner.dir);
+    perror("  Directory access failed");
+    return 0;
+  }
+
+  while ((entry = readdir(dirf)) != NULL) {
+    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
+      /* Construct tree view and absolute pathname strings */
+      char entryname[strlen(entry->d_name)+scanner.spaces];
+      for (int t = 0 ; t < scanner.spaces ; t++) {
+        entryname[t]=' ';
+      }
+      entryname[scanner.spaces] = 0;
+      strcat(entryname, entry->d_name);
+  
+      char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
+      strcpy(filename, scanner.dir);
+      strncat(filename, &settings->fileSeparator, 1);
+      strcat(filename, entry->d_name);
+
+      /* Check for subdirectory */
+      if (stat(filename, &statbuf) == 0) {
+        if (!(statbuf.st_mode & S_IFREG)) {
+          printf("%-60s\n", entryname);
+          if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
+            lineSum += scanDirectory(
+                (scanner_t) {filename, scanner.spaces+1}, settings);
+          }
+          continue;
+        }
+      } else {
+        perror("  Error in stat call");
+        continue;
+      }
+
+      if ((settings->includeSuffixes->count == 0
+          || testSuffix(filename, settings->includeSuffixes))
+          && !testSuffix(filename, settings->excludeSuffixes)) {
+        /* Count lines */
+        lines = 0;
+        bfile = false;
+        bfile_reset(settings->bfileHeuristics);
+        char line_buffer[REGEX_MAX_LINELENGTH];
+        int line_buffer_offset = 0;
+
+        FILE *file = fopen(filename, "r");
+        if (file == NULL) {
+          printf("%s", entryname);
+          perror("  File acces failed");
+          continue;
+        }
+
+        do {
+          a = fgetc(file);
+
+          bfile = bfile_check(settings->bfileHeuristics, a);
+
+          if (a == 10 || a == EOF) {
+            line_buffer[line_buffer_offset] = 0;
+            if (regex_parser_do(settings->regex, line_buffer) == 0) {
+              /* Only subtract lines when matching has finished */
+              if (!regex_parser_matching(settings->regex)) {
+                lines -= settings->regex->matched_lines;
+              }
+            }
+
+            line_buffer_offset = 0;
+            lines++;
+          } else {
+            if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
+              line_buffer[line_buffer_offset] = a;
+              line_buffer_offset++;
+            } else {
+              line_buffer[line_buffer_offset-1] = 0;
+              settings->confusing_lnlen = true;
+            }
+          }
+        } while (!bfile && a != EOF);
+        fclose(file);
+
+        /* Print and sum line count */
+        if (bfile) {
+          if (!settings->matchesOnly) {
+            printf("%-60s%19s\n", entryname, "binary");
+          }
+        } else {
+          lineSum += lines;
+          printf("%-60s%13d lines\n", entryname, lines);
+        }
+      } else {
+        if (!settings->matchesOnly) {
+          /* Print hint */
+          printf("%-60s%19s\n", entryname, "no match");
+        }
+      }
+    }
+  }
+
+  closedir(dirf);
+
+  return lineSum;
+}

mercurial