encapsulated scanner arguments + enabled optimizer + empty file is no bfile

Fri, 21 Oct 2011 15:09:26 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Oct 2011 15:09:26 +0200
changeset 23
778388400f7b
parent 22
4508da679ffb
child 24
3963e8800a12
child 25
802c5382f499

encapsulated scanner arguments + enabled optimizer + empty file is no bfile

Makefile file | annotate | diff | comparison | revisions
bfile_heuristics.c file | annotate | diff | comparison | revisions
cline.c file | annotate | diff | comparison | revisions
scanner.c file | annotate | diff | comparison | revisions
scanner.h file | annotate | diff | comparison | revisions
--- a/Makefile	Thu Oct 20 17:29:23 2011 +0200
+++ b/Makefile	Fri Oct 21 15:09:26 2011 +0200
@@ -1,5 +1,5 @@
 CC = gcc
-CARG = -Wall -std=gnu99
+CARG = -Wall -std=gnu99 -O
 BUILDDIR = build/
 OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ')
 BIN = ${BUILDDIR}cline
--- a/bfile_heuristics.c	Thu Oct 20 17:29:23 2011 +0200
+++ b/bfile_heuristics.c	Fri Oct 21 15:09:26 2011 +0200
@@ -32,20 +32,23 @@
       def->bcount++;
     }
 
-    switch (def->level) {
-    case BFILE_LOW_ACCURACY:
-      if (def->tcount > 15 || next_char == EOF) {
-        ret = (1.0*def->bcount)/def->tcount > 0.32;
-      }
-      break;
-    case BFILE_HIGH_ACCURACY:
-      if (def->tcount > 500 || next_char == EOF) {
-        ret = (1.0*def->bcount)/def->tcount > 0.1;
-      }
-      break;
-    default: /* BFILE_MEDIUM_ACCURACY */
-      if (def->tcount > 100 || next_char == EOF) {
-        ret = (1.0*def->bcount)/def->tcount > 0.1;
+    if (def->tcount > 1) { /* empty files are text files */
+      switch (def->level) {
+      case BFILE_LOW_ACCURACY:
+        if (def->tcount > 15 || next_char == EOF) {
+          ret = (1.0*def->bcount)/def->tcount > 0.32;
+        }
+        break;
+      case BFILE_HIGH_ACCURACY:
+        if (def->tcount > 500 || next_char == EOF) {
+          ret = (1.0*def->bcount)/def->tcount > 0.1;
+        }
+        break;
+      default: /* BFILE_MEDIUM_ACCURACY */
+        if (def->tcount > 100 || next_char == EOF) {
+          ret = (1.0*def->bcount)/def->tcount > 0.1;
+        }
+        break;
       }
     }
   }
--- a/cline.c	Thu Oct 20 17:29:23 2011 +0200
+++ b/cline.c	Fri Oct 21 15:09:26 2011 +0200
@@ -153,17 +153,8 @@
     finder = strtok(NULL, ",");
   }
 
-  /* Open directory */
-  DIR *dir = opendir(directory);
-  if (dir == NULL) {
-    perror("Operation failed");
-    destroy_settings_t(settings);
-    return 1;
-  }
-
   /* Scan directory */
-  int lines = scanDirectory(dir, 0, directory, settings);
-  closedir(dir);
+  int lines = scanDirectory((scanner_t){directory, 0}, settings);
   destroy_settings_t(settings);
 
   /* Print double line and line count */
--- a/scanner.c	Thu Oct 20 17:29:23 2011 +0200
+++ b/scanner.c	Fri Oct 21 15:09:26 2011 +0200
@@ -9,37 +9,50 @@
 #include "scanner.h"
 #include "suffix_fnc.h"
 #include "bfile_heuristics.h"
+#include <sys/stat.h>
 
-int scanDirectory(DIR *dir, const int spaces,
-                  char* currdir, settings_t* settings) {
-  DIR *subdir;
+int scanDirectory(scanner_t scanner, settings_t* settings) {
+
+  DIR *dirf;
   struct dirent *entry;
   int lines, a;
   int lineSum = 0;
   bool bfile;
+  struct stat statbuf;
 
-  while ((entry = readdir(dir)) != NULL) {
+  if ((dirf = opendir(scanner.dir)) == NULL) {
+    printf(scanner.dir);
+    perror("  Directory access failed");
+    return 0;
+  }
+
+  while ((entry = readdir(dirf)) != NULL) {
     if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
-      /* Print occurence */
-      char entryname[strlen(entry->d_name)+spaces];
-      for (int t = 0 ; t < spaces ; t++) {
+      /* 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[spaces] = 0;
+      entryname[scanner.spaces] = 0;
       strcat(entryname, entry->d_name);
   
-      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
-      strcpy(filename, currdir);
+      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 ((subdir = opendir(filename)) != NULL) {
-        printf("%-60s\n", entryname);
-        if (settings->recursive) {
-          lineSum += scanDirectory(subdir, spaces+1, filename, settings);
+      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;
         }
-        closedir(subdir);
+      } else {
+        perror("  Error in stat call");
         continue;
       }
 
@@ -50,6 +63,7 @@
       if (testSuffix(filename, settings)) {
         FILE *file = fopen(filename, "r");
         if (file == NULL) {
+          printf(entryname);
           perror("  File acces failed");
           continue;
         }
@@ -82,5 +96,8 @@
       }
     }
   }
+
+  closedir(dirf);
+
   return lineSum;
 }
--- a/scanner.h	Thu Oct 20 17:29:23 2011 +0200
+++ b/scanner.h	Fri Oct 21 15:09:26 2011 +0200
@@ -11,12 +11,16 @@
 #include "stdinc.h"
 #include "settings.h"
 
+typedef struct {
+  char *dir;
+  int spaces;
+} scanner_t;
+
 #ifdef _cplusplus
 extern "C" {
 #endif
 
-int scanDirectory(DIR *dir, const int spaces,
-                  char* currdir, settings_t* settings);
+int scanDirectory(scanner_t scanner, settings_t* settings);
 
 #ifdef _cplusplus
 }

mercurial