-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_chunk.c
More file actions
226 lines (212 loc) · 6.14 KB
/
Copy pathget_chunk.c
File metadata and controls
226 lines (212 loc) · 6.14 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
* Represents the end-of-line terminator type of the input
*/
typedef enum EolType { EOL_UNKNOWN, EOL_CR, EOL_LF, EOL_CRLF } EolType;
/**
* @brief Get all eol in this buffer, and save the index
* (relative index in this buffer) of eol in eol_idx
* when eol is cr or lf
*
* @param eol_idx a list to save the index of eol in this buffer
* @param len the length of this buffer
* @param fstr buffer of data
* @param qc quote char
* @param xc escape char
* @param eol end of line char
*
*/
void get_eol_cr_or_lf(int *eol_idx, int len, char *fstr, int qc, int xc,
int eol) {
int in_quote = 0;
int last_was_esc = 0;
int ch = 0;
int i = 0;
int res_len = 0;
for (i = 0; i < len && fstr[i] != '\0'; i++) {
ch = fstr[i];
if (in_quote) {
if (!last_was_esc) {
if (ch == qc)
in_quote = 0;
else if (ch == xc)
last_was_esc = 1;
} else
last_was_esc = 0;
} else if (ch == eol) {
eol_idx[res_len] = i;
res_len++;
} else if (ch == qc) {
in_quote = 1;
}
}
}
/**
* @brief Get all eol in this buffer, and save the index
* (relative index in this buffer) of eol(last char) in eol_idx
* when eol is crlf
*
* @param eol_idx a list to save the relative index of eol in this buffer
* @param len the length of this buffer
* @param fstr buffer of data
* @param qc quote char
* @param xc escape char
*
*/
void get_eol_crlf(int *eol_idx, int len, char *fstr, int qc, int xc) {
int in_quote = 0;
int last_was_esc = 0;
int ch = 0;
int lastch = 0;
int i = 0;
int res_len = 0;
for (i = 0; i < len && fstr[i] != '\0'; i++) {
lastch = ch;
ch = fstr[i];
if (in_quote) {
if (!last_was_esc) {
if (ch == qc)
in_quote = 0;
else if (ch == xc)
last_was_esc = 1;
} else
last_was_esc = 0;
} else if (ch == '\n' && lastch == '\r') {
eol_idx[res_len] = i;
res_len++;
} else if (ch == qc) {
in_quote = 1;
}
}
}
/**
* @brief print the index of every chunk, for parrent process to get the index form
* pipe stdout
*
* @param argc
* @param argv
* @return int
*/
int main(int argc, char *argv[]) {
int i = 0;
char *fpath;
char qc = '\'';
char xc = '\\';
EolType eol_type = EOL_LF;
int bs = 32768;
int cs = 64 * 1024;
while (i < argc) {
if (strcmp(argv[i], "-f") == 0) {
fpath = (char *)calloc(strlen(argv[i + 1]), sizeof(char));
strcpy(fpath, argv[i + 1]);
i += 2;
} else if (strcmp(argv[i], "-q") == 0) { //quote char
qc = argv[i + 1][0];
i += 2;
} else if (strcmp(argv[i], "-x") == 0) { //escape char
xc = argv[i + 1][0];
i += 2;
} else if (strcmp(argv[i], "-eol") == 0) { //eol
if (strcmp(argv[i + 1], "CR") == 0) {
eol_type = EOL_CR;
} else if (strcmp(argv[i + 1], "LF") == 0) {
eol_type = EOL_LF;
} else if (strcmp(argv[i + 1], "CRLF") == 0) {
eol_type = EOL_CRLF;
} else {
fprintf(stderr, "eol shoud be one of CR, LF, CRLF\n");
exit(-1);
}
i += 2;
} else if (strcmp(argv[i], "-bs") == 0) { //read buffer size
bs = atoi(argv[i + 1]);
i += 2;
} else if (strcmp(argv[i], "-cs") == 0) { //chunk size
cs = atoi(argv[i + 1]);
i += 2;
} else
i++;
}
FILE *fp;
if ((fp = fopen(fpath, "r")) == NULL) {
fprintf(stderr, "cannot op file %s\n", fpath);
exit(-1);
}
fseek(fp, 0, SEEK_END);
int filesize = ftell(fp);
rewind(fp);
char *buffer;
buffer = (char *)calloc(bs, sizeof(char));
int cur_buffer = 0;
int *eol_idx;
eol_idx = (int *)calloc(bs, sizeof(int));
int chunk_st = 0; // start index(absolute index in the file) of current chunk
int cur_chunk = 0; // the size of current chunk
int numread = 0;
int read_time = 0;
while (1) {
if (feof(fp)) break;
numread = fread(buffer + cur_buffer, sizeof(char), bs - cur_buffer, fp);
read_time++;
memset(eol_idx, -1, bs * sizeof(int));
int eol_cnt = -1;
switch (eol_type) {
case EOL_CR:
get_eol_cr_or_lf(eol_idx, numread + cur_buffer, buffer, qc, xc, '\r');
break;
case EOL_LF:
get_eol_cr_or_lf(eol_idx, numread + cur_buffer, buffer, qc, xc, '\n');
break;
case EOL_CRLF:
get_eol_crlf(eol_idx, numread + cur_buffer, buffer, qc, xc);
break;
default:
get_eol_cr_or_lf(eol_idx, numread + cur_buffer, buffer, qc, xc, '\n');
break;
}
// do not found a eol in this buffer, so the element in eol_idx is -1
if (eol_idx[0] == -1) {
fprintf(stderr,
"line too long in file %s\nplease set buffer size larger\n",
fpath);
exit(-1);
}
for (int i = 0; i < bs && eol_idx[i] >= 0; i++) {
eol_cnt++;
int next_line = i == 0 ? eol_idx[0] + 1 : eol_idx[i] - eol_idx[i - 1];
// maybe chunk_size < line_len
if (next_line > cs) {
fprintf(stderr,
"line too long for chunk size\nplease set chunk size larger\n");
exit(-1);
}
// current chunk size add net line size is larger than the chunk size
// we see current chunk as a full chunk
// next line belongs to the next chunk
if (cur_chunk + next_line > cs) {
chunk_st += cur_chunk; // the end of this chunk
printf("%d\n", chunk_st);
cur_chunk = next_line;
} else // this chunk is not full, add next line to current chunk
{
cur_chunk += next_line;
}
}
// move the data after the last eol to the start of the buffer
// make sure a line would not be splitted to different buffers
// and every start of a buffer is a start of a line
memmove(buffer, buffer + eol_idx[eol_cnt] + 1, bs - eol_idx[eol_cnt] - 1);
cur_buffer = bs - eol_idx[eol_cnt] - 1;
}
if (filesize - chunk_st > cs) { // some data last after the last chunk
printf("%d\n", cur_chunk + chunk_st);
printf("%d\n", filesize);
} else { // may have a last line without a eol, is not included in cur_chunk
printf("%d\n", filesize);
}
fclose(fp);
return 1;
}