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