bfile_heuristics.c

changeset 22
4508da679ffb
parent 21
91e0890464b0
child 23
778388400f7b
equal deleted inserted replaced
21:91e0890464b0 22:4508da679ffb
4 * Created on: 20.10.2011 4 * Created on: 20.10.2011
5 * Author: Mike 5 * Author: Mike
6 */ 6 */
7 7
8 #include "bfile_heuristics.h" 8 #include "bfile_heuristics.h"
9 #include <ctype.h>
9 10
10 bfile_heuristics_t *new_bfile_heuristics_t() { 11 bfile_heuristics_t *new_bfile_heuristics_t() {
11 bfile_heuristics_t *ret = malloc(sizeof(bfile_heuristics_t)); 12 bfile_heuristics_t *ret = malloc(sizeof(bfile_heuristics_t));
12 ret->level = BFILE_MEDIUM_ACCURACY; 13 ret->level = BFILE_MEDIUM_ACCURACY;
13 /* TODO: check why this fails */ 14 bfile_reset(ret);
14 /* ret->ccount = calloc(256, sizeof(int)); */
15 return ret; 15 return ret;
16 } 16 }
17 17
18 void destroy_bfile_heuristics_t(bfile_heuristics_t *def) { 18 void destroy_bfile_heuristics_t(bfile_heuristics_t *def) {
19 free(def->ccount);
20 free(def); 19 free(def);
20 }
21
22 void bfile_reset(bfile_heuristics_t *def) {
23 def->bcount = 0;
24 def->tcount = 0;
21 } 25 }
22 26
23 bool bfile_check(bfile_heuristics_t *def, int next_char) { 27 bool bfile_check(bfile_heuristics_t *def, int next_char) {
24 bool ret = false; 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 switch (def->level) {
36 case BFILE_LOW_ACCURACY:
37 if (def->tcount > 15 || next_char == EOF) {
38 ret = (1.0*def->bcount)/def->tcount > 0.32;
39 }
40 break;
41 case BFILE_HIGH_ACCURACY:
42 if (def->tcount > 500 || next_char == EOF) {
43 ret = (1.0*def->bcount)/def->tcount > 0.1;
44 }
45 break;
46 default: /* BFILE_MEDIUM_ACCURACY */
47 if (def->tcount > 100 || next_char == EOF) {
48 ret = (1.0*def->bcount)/def->tcount > 0.1;
49 }
50 }
51 }
25 52
26 return ret; 53 return ret;
27 } 54 }

mercurial