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
|
#include <inttypes.h>
#include <stdio.h>
#include <parserutils/parserutils.h>
#include <parserutils/charset/utf8.h>
#include <parserutils/input/inputstream.h>
#include "utils/utils.h"
#include "testutils.h"
#ifdef __riscos
const char * const __dynamic_da_name = "InputStream";
int __dynamic_da_max_size = 128*1024*1024;
#endif
int main(int argc, char **argv)
{
parserutils_inputstream *stream;
FILE *fp;
size_t len;
#define CHUNK_SIZE (4096)
uint8_t buf[CHUNK_SIZE];
const uint8_t *c;
size_t clen;
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
assert(parserutils_inputstream_create("UTF-8", 1, NULL,
&stream) == PARSERUTILS_OK);
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Failed opening %s\n", argv[1]);
return 1;
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
while (len >= CHUNK_SIZE) {
size_t read = fread(buf, 1, CHUNK_SIZE, fp);
assert(read == CHUNK_SIZE);
assert(parserutils_inputstream_append(stream,
buf, CHUNK_SIZE) == PARSERUTILS_OK);
len -= CHUNK_SIZE;
while (parserutils_inputstream_peek(stream, 0, &c, &clen) !=
PARSERUTILS_NEEDDATA) {
parserutils_inputstream_advance(stream, clen);
if (*c == 'a') {
assert(parserutils_inputstream_insert(stream,
(const uint8_t *) "hello!!!",
SLEN("hello!!!")) == PARSERUTILS_OK);
}
}
}
if (len > 0) {
size_t read = fread(buf, 1, len, fp);
assert(read == len);
assert(parserutils_inputstream_append(stream,
buf, len) == PARSERUTILS_OK);
len = 0;
}
fclose(fp);
assert(parserutils_inputstream_insert(stream,
(const uint8_t *) "hello!!!",
SLEN("hello!!!")) == PARSERUTILS_OK);
assert(parserutils_inputstream_append(stream, NULL, 0) ==
PARSERUTILS_OK);
while (parserutils_inputstream_peek(stream, 0, &c, &clen) !=
PARSERUTILS_EOF) {
parserutils_inputstream_advance(stream, clen);
}
parserutils_inputstream_destroy(stream);
printf("PASS\n");
return 0;
}
|