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
     1.1 --- a/Makefile	Thu Oct 20 17:29:23 2011 +0200
     1.2 +++ b/Makefile	Fri Oct 21 15:09:26 2011 +0200
     1.3 @@ -1,5 +1,5 @@
     1.4  CC = gcc
     1.5 -CARG = -Wall -std=gnu99
     1.6 +CARG = -Wall -std=gnu99 -O
     1.7  BUILDDIR = build/
     1.8  OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ')
     1.9  BIN = ${BUILDDIR}cline
     2.1 --- a/bfile_heuristics.c	Thu Oct 20 17:29:23 2011 +0200
     2.2 +++ b/bfile_heuristics.c	Fri Oct 21 15:09:26 2011 +0200
     2.3 @@ -32,20 +32,23 @@
     2.4        def->bcount++;
     2.5      }
     2.6  
     2.7 -    switch (def->level) {
     2.8 -    case BFILE_LOW_ACCURACY:
     2.9 -      if (def->tcount > 15 || next_char == EOF) {
    2.10 -        ret = (1.0*def->bcount)/def->tcount > 0.32;
    2.11 -      }
    2.12 -      break;
    2.13 -    case BFILE_HIGH_ACCURACY:
    2.14 -      if (def->tcount > 500 || next_char == EOF) {
    2.15 -        ret = (1.0*def->bcount)/def->tcount > 0.1;
    2.16 -      }
    2.17 -      break;
    2.18 -    default: /* BFILE_MEDIUM_ACCURACY */
    2.19 -      if (def->tcount > 100 || next_char == EOF) {
    2.20 -        ret = (1.0*def->bcount)/def->tcount > 0.1;
    2.21 +    if (def->tcount > 1) { /* empty files are text files */
    2.22 +      switch (def->level) {
    2.23 +      case BFILE_LOW_ACCURACY:
    2.24 +        if (def->tcount > 15 || next_char == EOF) {
    2.25 +          ret = (1.0*def->bcount)/def->tcount > 0.32;
    2.26 +        }
    2.27 +        break;
    2.28 +      case BFILE_HIGH_ACCURACY:
    2.29 +        if (def->tcount > 500 || next_char == EOF) {
    2.30 +          ret = (1.0*def->bcount)/def->tcount > 0.1;
    2.31 +        }
    2.32 +        break;
    2.33 +      default: /* BFILE_MEDIUM_ACCURACY */
    2.34 +        if (def->tcount > 100 || next_char == EOF) {
    2.35 +          ret = (1.0*def->bcount)/def->tcount > 0.1;
    2.36 +        }
    2.37 +        break;
    2.38        }
    2.39      }
    2.40    }
     3.1 --- a/cline.c	Thu Oct 20 17:29:23 2011 +0200
     3.2 +++ b/cline.c	Fri Oct 21 15:09:26 2011 +0200
     3.3 @@ -153,17 +153,8 @@
     3.4      finder = strtok(NULL, ",");
     3.5    }
     3.6  
     3.7 -  /* Open directory */
     3.8 -  DIR *dir = opendir(directory);
     3.9 -  if (dir == NULL) {
    3.10 -    perror("Operation failed");
    3.11 -    destroy_settings_t(settings);
    3.12 -    return 1;
    3.13 -  }
    3.14 -
    3.15    /* Scan directory */
    3.16 -  int lines = scanDirectory(dir, 0, directory, settings);
    3.17 -  closedir(dir);
    3.18 +  int lines = scanDirectory((scanner_t){directory, 0}, settings);
    3.19    destroy_settings_t(settings);
    3.20  
    3.21    /* Print double line and line count */
     4.1 --- a/scanner.c	Thu Oct 20 17:29:23 2011 +0200
     4.2 +++ b/scanner.c	Fri Oct 21 15:09:26 2011 +0200
     4.3 @@ -9,37 +9,50 @@
     4.4  #include "scanner.h"
     4.5  #include "suffix_fnc.h"
     4.6  #include "bfile_heuristics.h"
     4.7 +#include <sys/stat.h>
     4.8  
     4.9 -int scanDirectory(DIR *dir, const int spaces,
    4.10 -                  char* currdir, settings_t* settings) {
    4.11 -  DIR *subdir;
    4.12 +int scanDirectory(scanner_t scanner, settings_t* settings) {
    4.13 +
    4.14 +  DIR *dirf;
    4.15    struct dirent *entry;
    4.16    int lines, a;
    4.17    int lineSum = 0;
    4.18    bool bfile;
    4.19 +  struct stat statbuf;
    4.20  
    4.21 -  while ((entry = readdir(dir)) != NULL) {
    4.22 +  if ((dirf = opendir(scanner.dir)) == NULL) {
    4.23 +    printf(scanner.dir);
    4.24 +    perror("  Directory access failed");
    4.25 +    return 0;
    4.26 +  }
    4.27 +
    4.28 +  while ((entry = readdir(dirf)) != NULL) {
    4.29      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    4.30 -      /* Print occurence */
    4.31 -      char entryname[strlen(entry->d_name)+spaces];
    4.32 -      for (int t = 0 ; t < spaces ; t++) {
    4.33 +      /* Construct tree view and absolute pathname strings */
    4.34 +      char entryname[strlen(entry->d_name)+scanner.spaces];
    4.35 +      for (int t = 0 ; t < scanner.spaces ; t++) {
    4.36          entryname[t]=' ';
    4.37        }
    4.38 -      entryname[spaces] = 0;
    4.39 +      entryname[scanner.spaces] = 0;
    4.40        strcat(entryname, entry->d_name);
    4.41    
    4.42 -      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
    4.43 -      strcpy(filename, currdir);
    4.44 +      char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
    4.45 +      strcpy(filename, scanner.dir);
    4.46        strncat(filename, &settings->fileSeparator, 1);
    4.47        strcat(filename, entry->d_name);
    4.48  
    4.49        /* Check for subdirectory */
    4.50 -      if ((subdir = opendir(filename)) != NULL) {
    4.51 -        printf("%-60s\n", entryname);
    4.52 -        if (settings->recursive) {
    4.53 -          lineSum += scanDirectory(subdir, spaces+1, filename, settings);
    4.54 +      if (stat(filename, &statbuf) == 0) {
    4.55 +        if (!(statbuf.st_mode & S_IFREG)) {
    4.56 +          printf("%-60s\n", entryname);
    4.57 +          if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
    4.58 +            lineSum += scanDirectory(
    4.59 +                (scanner_t) {filename, scanner.spaces+1}, settings);
    4.60 +          }
    4.61 +          continue;
    4.62          }
    4.63 -        closedir(subdir);
    4.64 +      } else {
    4.65 +        perror("  Error in stat call");
    4.66          continue;
    4.67        }
    4.68  
    4.69 @@ -50,6 +63,7 @@
    4.70        if (testSuffix(filename, settings)) {
    4.71          FILE *file = fopen(filename, "r");
    4.72          if (file == NULL) {
    4.73 +          printf(entryname);
    4.74            perror("  File acces failed");
    4.75            continue;
    4.76          }
    4.77 @@ -82,5 +96,8 @@
    4.78        }
    4.79      }
    4.80    }
    4.81 +
    4.82 +  closedir(dirf);
    4.83 +
    4.84    return lineSum;
    4.85  }
     5.1 --- a/scanner.h	Thu Oct 20 17:29:23 2011 +0200
     5.2 +++ b/scanner.h	Fri Oct 21 15:09:26 2011 +0200
     5.3 @@ -11,12 +11,16 @@
     5.4  #include "stdinc.h"
     5.5  #include "settings.h"
     5.6  
     5.7 +typedef struct {
     5.8 +  char *dir;
     5.9 +  int spaces;
    5.10 +} scanner_t;
    5.11 +
    5.12  #ifdef _cplusplus
    5.13  extern "C" {
    5.14  #endif
    5.15  
    5.16 -int scanDirectory(DIR *dir, const int spaces,
    5.17 -                  char* currdir, settings_t* settings);
    5.18 +int scanDirectory(scanner_t scanner, settings_t* settings);
    5.19  
    5.20  #ifdef _cplusplus
    5.21  }

mercurial