scanner.c

Thu, 20 Oct 2011 15:21:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 20 Oct 2011 15:21:53 +0200
changeset 21
91e0890464b0
parent 20
43725438ac50
child 22
4508da679ffb
permissions
-rw-r--r--

implemented bfile heuristics option + TODO: implement algorithm

/*
 * scanner.c
 *
 *  Created on: 23.05.2011
 *      Author: Mike
 */


#include "scanner.h"
#include "suffix_fnc.h"
#include "bfile_heuristics.h"

int scanDirectory(DIR *dir, const int spaces,
                  char* currdir, settings_t* settings) {
  DIR *subdir;
  struct dirent *entry;
  int lines, a;
  int lineSum = 0;
  bool bfile;

  while ((entry = readdir(dir)) != 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++) {
        entryname[t]=' ';
      }
      entryname[spaces] = 0;
      strcat(entryname, entry->d_name);
  
      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
      strcpy(filename, currdir);
      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);
        }
        closedir(subdir);
        continue;
      }

      // Count lines
      lines = 0;
      bfile = false;
      if (testSuffix(filename, settings)) {
        FILE *file = fopen(filename, "r");
        if (file == NULL) {
          perror("  File acces failed");
          continue;
        }

        do {
          a = fgetc(file);

          bfile = bfile_check(settings->bfileHeuristics, a);

          if (a == 10) {
            lines++;
          }
        } while (!bfile && a != EOF);
        fclose(file);

        // Print line count
        if (bfile) {
          printf("%-60s%19s\n", entryname, "binary");
        } else {
          printf("%-60s%13d lines\n", entryname, lines);
        }
        lineSum += lines;
      } else {
        if (!settings->matchesOnly) {
          // Print hint
          printf("%-60s%19s\n", entryname, "no match");
        }
      }
    }
  }
  return lineSum;
}

mercurial