root/scrcnt.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. i_bf_mmhcheck
  2. c_chk
  3. main

   1 #include <stdio.h>
   2 #include <unistd.h>
   3 #include <stddef.h>
   4 #include <stdint.h>
   5 #include <string.h>
   6 #include <ctype.h>
   7 #include "lang.h"
   8 #include "nav.h"
   9 #include "tally.h"
  10 #include "hash.h"
  11 #include "magic.h"
  12 #include "linuxism.h"
  13 
  14 #define I_BF_MAX 0xFFFF
  15 #define I_BF_MAXARR (I_BF_MAX + 1) / 8
  16 #define I_BF_MMHKEY 0xFA0BCC2D
  17 
  18 const static char *nothing = "";
  19 static char ignorelist[I_BF_MAXARR] = {};
  20 static bool ignable = false;
  21 static bool dbgenable = false;
  22 
  23 #define I_BF_GET(idx) (ignorelist[idx / 8] & (1 << (idx % 8)))
  24 #define I_BF_SET(idx) ignorelist[idx / 8] |= 1 << (idx % 8)
  25 
  26 bool i_bf_mmhcheck(const char *str) {
  27     uint64_t hash = mmh2a(str, strlen(str), I_BF_MMHKEY);
  28     if (I_BF_GET( hash        & 0xFFFF) &&
  29         I_BF_GET((hash >> 16) & 0xFFFF) &&
  30         I_BF_GET((hash >> 32) & 0xFFFF) &&
  31         I_BF_GET((hash >> 48) & 0xFFFF)) return true;
  32 
  33     return false;
  34 }
  35 
  36 void c_chk(const char *path, bool dir) {
  37     if (dir) return;
  38     if (access(path, F_OK) != 0) return;
  39     if (ignable && i_bf_mmhcheck(get_bname(path))) return;
  40 
  41     const char *lang = NULL;
  42     int depth = 0;
  43 
  44     if (!get_ext(path, depth)) {
  45         lang = unsure_guess(path);
  46         if (!lang) lang = get_lang("");
  47     } else {
  48         do {
  49             const char *ext = get_ext(path, depth);
  50             if (!ext) break;
  51             if (ignable && i_bf_mmhcheck(ext)) return;
  52             if (!(lang = unsure_override(path, ext)))
  53                 lang = get_lang(ext);
  54 
  55             depth++;
  56         } while (!lang);
  57 
  58         if (!lang) return;
  59     }
  60 
  61     if (dbgenable) {
  62         fprintf(stderr, "Detected %s as %s\n", path, lang);
  63     }
  64 
  65     add_m_tally(lang, get_file_sz(path));
  66 }
  67 
  68 int main(int argc, char *argv[]) {
  69     if (argc < 2) return 1;
  70 
  71     if (argc > 2) {
  72         /* TODO: Tidy this to use flags instead so we can do ignore AND debug */
  73         if (strcmp("--ignore", argv[2]) == 0) {
  74             int c = EOF;
  75             char buffer[PATH_MAX] = {};
  76             size_t bufu = 0;
  77             do {
  78                 c = getchar();
  79 
  80                 if ((c == '\n' || c == '\r' || c == EOF || bufu >= PATH_MAX) &&
  81                     bufu > 0) {
  82                     ignable = true;
  83                     uint64_t hash = mmh2a(&buffer, bufu, I_BF_MMHKEY);
  84                     I_BF_SET( hash        & 0xFFFF);
  85                     I_BF_SET((hash >> 16) & 0xFFFF);
  86                     I_BF_SET((hash >> 32) & 0xFFFF);
  87                     I_BF_SET((hash >> 48) & 0xFFFF);
  88                     memset(&buffer, 0, PATH_MAX);
  89                     bufu = 0;
  90                     continue;
  91                 }
  92 
  93                 if (isprint(c)) {
  94                     buffer[bufu] = (char)c;
  95                     bufu++;
  96                 }
  97             } while (c != EOF);
  98         } else if (strcmp("--debug", argv[2]) == 0) {
  99             dbgenable = true;
 100         }
 101     }
 102 
 103     clear_m_tally();
 104     do_tree(argv[1], c_chk);
 105     printf("%s", get_m_tally());
 106 
 107     return 0;
 108 }

/* [previous][next][first][last][top][bottom][index][help] */