Thu, 01 Dec 2011 17:04:30 +0100
Added line buffer (and warning message - there is no regexp parser, though)
universe@20 | 1 | /* |
universe@20 | 2 | * bfile_heuristics.c |
universe@20 | 3 | * |
universe@20 | 4 | * Created on: 20.10.2011 |
universe@20 | 5 | * Author: Mike |
universe@20 | 6 | */ |
universe@20 | 7 | |
universe@20 | 8 | #include "bfile_heuristics.h" |
universe@22 | 9 | #include <ctype.h> |
universe@20 | 10 | |
universe@21 | 11 | bfile_heuristics_t *new_bfile_heuristics_t() { |
universe@21 | 12 | bfile_heuristics_t *ret = malloc(sizeof(bfile_heuristics_t)); |
universe@21 | 13 | ret->level = BFILE_MEDIUM_ACCURACY; |
universe@22 | 14 | bfile_reset(ret); |
universe@21 | 15 | return ret; |
universe@20 | 16 | } |
universe@20 | 17 | |
universe@21 | 18 | void destroy_bfile_heuristics_t(bfile_heuristics_t *def) { |
universe@21 | 19 | free(def); |
universe@20 | 20 | } |
universe@21 | 21 | |
universe@22 | 22 | void bfile_reset(bfile_heuristics_t *def) { |
universe@22 | 23 | def->bcount = 0; |
universe@22 | 24 | def->tcount = 0; |
universe@22 | 25 | } |
universe@22 | 26 | |
universe@21 | 27 | bool bfile_check(bfile_heuristics_t *def, int next_char) { |
universe@21 | 28 | bool ret = false; |
universe@22 | 29 | if (def->level != BFILE_IGNORE) { |
universe@22 | 30 | def->tcount++; |
universe@22 | 31 | if (!isprint(next_char) && !isspace(next_char)) { |
universe@22 | 32 | def->bcount++; |
universe@22 | 33 | } |
universe@22 | 34 | |
universe@23 | 35 | if (def->tcount > 1) { /* empty files are text files */ |
universe@23 | 36 | switch (def->level) { |
universe@23 | 37 | case BFILE_LOW_ACCURACY: |
universe@23 | 38 | if (def->tcount > 15 || next_char == EOF) { |
universe@23 | 39 | ret = (1.0*def->bcount)/def->tcount > 0.32; |
universe@23 | 40 | } |
universe@23 | 41 | break; |
universe@23 | 42 | case BFILE_HIGH_ACCURACY: |
universe@23 | 43 | if (def->tcount > 500 || next_char == EOF) { |
universe@23 | 44 | ret = (1.0*def->bcount)/def->tcount > 0.1; |
universe@23 | 45 | } |
universe@23 | 46 | break; |
universe@23 | 47 | default: /* BFILE_MEDIUM_ACCURACY */ |
universe@23 | 48 | if (def->tcount > 100 || next_char == EOF) { |
universe@23 | 49 | ret = (1.0*def->bcount)/def->tcount > 0.1; |
universe@23 | 50 | } |
universe@23 | 51 | break; |
universe@22 | 52 | } |
universe@22 | 53 | } |
universe@22 | 54 | } |
universe@21 | 55 | |
universe@21 | 56 | return ret; |
universe@21 | 57 | } |