Repositories » TXT0
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Refactored raw2t.c and parser.c.

CRC initialization and clean-up was moved to parser.c, in stead of
doing it in raw2t.c and passing the CRC 'model' to the function
parse().

Changeset e9a56fa025a5

Parent 043cfd6a1a21

by Rup Palchowdhury

Changes to 3 files · Browse files at e9a56fa025a5 Showing diff from parent 043cfd6a1a21 Diff from another changeset...

Change 1 of 4 Show Entire File parser.c Stacked
 
3
4
5
6
7
8
9
10
11
12
13
 
 
 
14
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
 
24
25
26
27
28
29
30
 
31
32
33
 
34
35
36
 
53
54
55
56
57
 
 
58
59
 
60
61
 
62
63
64
 
101
102
103
104
105
 
 
106
107
 
108
109
 
110
111
 
112
113
 
114
115
 
116
117
118
 
119
120
121
 
3
4
5
 
6
7
8
9
10
11
 
12
13
14
15
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
53
54
55
 
56
 
 
57
58
59
60
61
62
63
64
 
81
82
83
 
 
84
85
86
 
87
88
 
89
90
91
92
 
129
130
131
 
 
132
133
134
 
135
136
 
137
138
 
139
140
 
141
142
 
143
144
145
146
147
148
149
150
@@ -3,16 +3,45 @@
 #include <string.h>  #include <stdint.h>  #include <malloc/malloc.h> -#include <kak/klog.h>  #include <kak/klist.h>  #include "crc.h"  #include "tokenizer.h"  #include "tfile.h"  #include "parser.h"   -/* TODO: parser() assumes a TREC-type TFile */ +static char *str = "width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680 name=\"CRC-32/POSIX\""; +static char crc_s[128]; +static model_t crc_m;   -TFile* parse(FILE *fp, model_t *crc_model) +static void __initcrc() +{ + int ret; + strcpy(crc_s, str); + crc_m.name = NULL; + ret = read_model(&crc_m, crc_s); + if (ret == 2) { + fprintf(stderr, "crc: out of memory -- aborting\n"); + exit(1); + } + else if (ret == 1) { + fprintf(stderr, "crc: %s: -- unusable model\n", + crc_m.name == NULL ? "<no name>" : crc_m.name); + exit(1); + } + crc_table_wordwise(&crc_m); +} + +static void __deinitcrc() +{ + free(crc_m.name); + crc_m.name = NULL; +} + +/* TODO: + parser() assumes a TREC-type TFile + use sizeof() var in stead of type +*/ +TFile* parse(FILE *fp)  {   Tokenizer *toktxt, *tokid;   Token token; @@ -24,13 +53,12 @@
  int n, ntxtbuf;   char *septxt = " ;,.:`'\"?!(){}[]<>~^&*_-+=#$%@|\\/";   char *sepid = " <>"; - /* Log *log; */   - /* log = newlog("raw2t"); */ - lowmem = 1024; + __initcrc();   reset(&mem, 3);   toktxt = newtokenizer(septxt, LOWERCASE, &mem);   tokid = newtokenizer(sepid, KEEPCASE, &mem); + lowmem = 1024;   n_term = 0;   bytesread = 0;   tfile = newTFile(TREC); @@ -53,12 +81,12 @@
  n_term = 0;     /* fill remaining parts of TDoc->TSubHeader */ - crc = crc_wordwise(crc_model, 0, NULL, 0); - crc = crc_wordwise(crc_model, crc, (unsigned char *)tdoc->txt, + crc = crc_wordwise(&crc_m, 0, NULL, 0); + crc = crc_wordwise(&crc_m, crc, (unsigned char *)tdoc->txt,   tdoc->h->n_txt); - crc = crc_wordwise(crc_model, crc, (unsigned char *)tdoc->id, + crc = crc_wordwise(&crc_m, crc, (unsigned char *)tdoc->id,   tdoc->h->n_id); - crc = crc_wordwise(crc_model, crc, (unsigned char *)tdoc->rsrc[0], + crc = crc_wordwise(&crc_m, crc, (unsigned char *)tdoc->rsrc[0],   tdoc->h->n_rsrc[0]);   tdoc->h->crc = crc;   @@ -101,21 +129,22 @@
    tfile->h->b += THEADER_SIZE;   - crc = crc_wordwise(crc_model, 0, NULL, 0); - crc = crc_wordwise(crc_model, crc, (unsigned char *)&tfile->h->ver, + crc = crc_wordwise(&crc_m, 0, NULL, 0); + crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->ver,   sizeof(uint32_t)); - crc = crc_wordwise(crc_model, crc, (unsigned char *)&tfile->h->type, + crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->type,   sizeof(uint32_t)); - crc = crc_wordwise(crc_model, crc, (unsigned char *)&tfile->h->b, + crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->b,   sizeof(uint32_t)); - crc = crc_wordwise(crc_model, crc, (unsigned char *)&tfile->h->n, + crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->n,   sizeof(uint32_t)); - crc = crc_wordwise(crc_model, crc, (unsigned char *)&tfile->h->r, + crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->r,   sizeof(uint32_t)); - crc = crc_wordwise(crc_model, crc, (unsigned char *)&tfile->h->bo, + crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->bo,   sizeof(uint32_t));     tfile->h->crc = crc; + __deinitcrc();   /*   fprintf(stderr, "%16s: %u\n", "bytes read", bytesread);   fprintf(stderr, "%16s: %u\n", "TFile size", tfile->h->b);
Change 1 of 1 Show Entire File parser.h Stacked
 
1
2
 
 
1
 
2
@@ -1,2 +1,2 @@
 #define BUFSIZE 10240 -TFile* parse(FILE*, model_t*); +TFile* parse(FILE*);
Change 1 of 1 Show Entire File raw2t.c Stacked
 
24
25
26
27
28
29
 
 
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
49
50
51
52
53
54
55
56
57
 
24
25
26
 
 
 
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
33
34
 
 
35
36
37
@@ -24,34 +24,14 @@
 int main(int argc, char *argv[])  {   TFile *tfile; - char crc_model_str[121]; - model_t crc_model; - int ret; + + /* read list of common words */   - /* init crc computation */ - - strcpy(crc_model_str, "width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680 name=\"CRC-32/POSIX\""); - crc_model.name = NULL; - ret = read_model(&crc_model, crc_model_str); - if (ret == 2) { - fputs("ABORT: crc: out of memory -- aborting\n", stderr); - exit(0); - } - else if (ret == 1) { - fprintf(stderr, "ABORT: crc: %s: -- unusable model\n", - crc_model.name == NULL ? "<no name>" : crc_model.name); - exit(0); - } - - crc_table_wordwise(&crc_model); - - tfile = parse(stdin, &crc_model); + tfile = parse(stdin);   writeTFile(tfile, stdout);     /* FIX: call segfaults */   /* freeTFile(tfile); */ - free(crc_model.name); - crc_model.name = NULL;     return 0;  }