-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbzip-table-lines2.c
210 lines (165 loc) · 5.96 KB
/
bzip-table-lines2.c
1
2
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bzlib.h>
#include <stdio.h>
#include <stdlib.h>
//#include "micro-bunzip.h"
#include "fcntl.h"
#include "errno.h"
#define BUF_SIZE 4096 * 100
/**
* Read a bzip2 file from stdin and print
* 1) The block size
* 2) The starting offset (in BITS) of each block of compressed data
*
* This does not completely uncompress the data, so does not do CRC checks,
* (gaining 60% or so speedup), bzip2 --test can be used to verify files
* first if desired.
*/
int debug() {return 0;}
// the user define process function, is not defined, user has to define it to link
int process_line(const char * buffer); // null terminated
int
bunzip_one(FILE *f) {
int bzError=0;
BZFILE *bzf=0;
char buf[BUF_SIZE]={0};
bzf = BZ2_bzReadOpen(&bzError, f, 0, 0, NULL, 0);
if (bzError != BZ_OK) {
fprintf(stderr, "E: BZ2_bzReadOpen: %d\n", bzError);
return -1;
}
int blockcount =0;
int linecount =0;
int remainder =0;
char buffer2[(BUF_SIZE * 2)+1]={0};
char buffer3[(BUF_SIZE * 2)+1]={0};
char buffer_last[(BUF_SIZE * 2)+1]={0};
//memset(buf,0,sizeof(buf));
// memset(buffer3,0,sizeof(buffer3));
// memset(buffer2,0,sizeof(buffer2));
while (bzError == BZ_OK) {
int nread = BZ2_bzRead(&bzError, bzf, buf, sizeof buf);
if (bzError == BZ_OK || bzError == BZ_STREAM_END) {
const char * pc;
const char * lc;
lc=pc=buf;
printf("got count %d\n",nread); // debug
//printf("got buffer to check \"%s\"\n",buf); // debug
///size_t nwritten = fwrite(buf, 1, nread, stdout);
// now we look for newlines
while (
(*pc)
&& (pc < (buf + BUF_SIZE))
)
{
if ((*pc=='\r') ||(*pc=='\n'))
{
/* if (debug()) { */
/* printf("Check this remainder:%d\n",remainder); // */
// printf("Check this size :%d\n",pc-lc); //
/* } */
/* // now we first process the remainder from the previous block */
//memset(buffer3,'0',strlen(buffer3));
strncpy(buffer3,lc,(pc-lc));
// printf("new size :%d\n",strlen(buffer3)); //
;
/* if (debug()) */
/* printf("going to add : \"%s\" to remainder \"%s\" \n",buffer3,buffer2); // */
if (remainder) {
/* if (debug()) */
/* printf("before adding, remainder: \"%s\"\n",buffer2); // debug */
/* if (pc-lc>0) { */
//int len=strlen(buffer2);
printf("remainder :%d\n",remainder);
// printf("new len :%d\n",len);
// int len2=strlen(buffer3);
// printf("new len2 :%d\n",len2);
// int lenc=len+len2;
strcpy(buffer2 +(remainder ),buffer3);
remainder=0;
/* } */
//len=strlen(buffer2);
// printf("Check this curr :%.*s\n",80,buffer2);
// printf("new len3 :%d %d\n",len, lenc);
} else {
strcpy(buffer2,buffer3);
}
// printf("Check this curr :%.*s\n",40,buffer2);
// printf("Check this curr2 :%.*s\n",40,buffer3);
/* if (debug()) */
// printf("after adding \"%.*s\"\n",50, buffer2); // debug */
/* // process a line */
// for valgrind.
// printf("processline(%.*s)\n",200,buffer2);
//printf("C(%.*s,%d)\n",100,pc,*pc);
int ok = process_line(buffer2);
// printf ("Process line returned %d\n",ok);
if(ok==0) {
}else {
printf("error last buffer \"%s\"\n",buffer_last); // debug */
printf("current buffer \"%s\"\n",buffer2); // debug */
return (233);
}
strcpy(buffer_last, buffer2);
memset(buffer2,0,sizeof(buffer2));
memset(buffer3,0,sizeof(buffer3));
//buffer2[0]=0;
//buffer3[0]=0;
lc=pc;
linecount ++;
} // end of line
/* char buf[2]={0,0}; */
/* buf[0]=*pc; */
/* // printf(">%s",buf); // */
pc++;
}// end of block
if ((pc-lc) > 0) {
/* memset(buffer2,0,sizeof(buffer2)); */
/* // printf("leftover:\"%d\"\n",pc-lc); */
strncpy(buffer2,lc,pc-lc );
//printf("Check remainder:\"%s\"\n",buffer2);
// printf("Check remainder :%.*s\n",40,buffer2);
remainder=strlen(buffer2); // leftover
// printf("leftover check:\"%d\"\n",remainder);
// if (length) { */
/* remainder += length; */
/* //printf("leftover check2:\"%d\"\n",length); */
/* } */
}
blockcount++;
}
}
if (bzError != BZ_STREAM_END) {
fprintf(stderr, "E: bzip error after read: %d\n", bzError);
return -1;
}
BZ2_bzReadClose(&bzError, bzf);
return 0;
}
int main(int argc, const char ** argv) {
FILE *f;
printf("argc %d\n",argc);
if (argc>=2) {
printf("argv %s\n",argv[1]);
} else {
printf("no file\n");
return 0;
}
printf("going to open %s\n",argv[1]);
f=fopen(argv[1], "rb");
if (f == NULL)
{
printf("error %d\n",errno);
perror(argv[1]);
return 233;
}
else {
// printf("file opened with fd %d\n",fd);
}
if (bunzip_one(f) == -1)
return -1;
fclose(f);
return 0;
}