blob: ad5e55b549c9e69b18bf277fb1a54281245085c1 (
plain)
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
|
#!/usr/bin/tcc -run -O2
#include <stdio.h>
#include <string.h>
int main(int argc, char const* argv[])
{
static const char *beginning;
static const char *ending;
int beginning_found = 0, beginning_length = 0;
int ending_found = 0, ending_length = 0;
// TODO: getopt to enable this
int multimatching = 0;
FILE *fp;
int read;
char buf[4096];
int i;
if (argc != 4) {
puts("Usage: partextract <file> <start token> <end token>");
puts("");
puts("partextract outputs everything between the start and end token");
puts("(including the tokens). Only the first match will be output");
puts("unless multimatching is enabled and overlapping tokens will be ignored.");
return 1;
}
beginning = argv[2];
ending = argv[3];
beginning_length = strlen(beginning);
ending_length = strlen(ending);
fp = fopen(argv[1], "rb");
if (fp == NULL) {
perror(argv[1]);
return 1;
}
while (!feof(fp)) {
read = fread(buf, sizeof(char), sizeof(buf), fp);
for (i = 0; i < read; i++) {
// find the starting token
if (beginning_found != beginning_length && ending_found != ending_length) {
if (buf[i] == beginning[beginning_found]) {
beginning_found++;
if (beginning_found == beginning_length) {
// we don't allow tokens to overlap
ending_found = 0;
// We only start outputting if we had the complete start token
// This makes sure the token is in the output too
// and jumps to the next char so the fputc below won't run for
// the last char of the token
fputs(beginning, stdout);
continue;
}
} else if (beginning_found != beginning_length) {
beginning_found = 0;
}
}
// output until we hit the ending token
if (beginning_found == beginning_length && ending_found != ending_length) {
fputc(buf[i], stdout);
if (buf[i] == ending[ending_found]) {
ending_found++;
if (multimatching && ending_found == ending_length) {
beginning_found = 0;
ending_found = 0;
}
} else if (ending_found != ending_length) {
ending_found = 0;
}
}
}
}
return 0;
}
|