-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutbetween.c
More file actions
101 lines (94 loc) · 2.31 KB
/
Copy pathcutbetween.c
File metadata and controls
101 lines (94 loc) · 2.31 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
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
const char * const INFO =
"\n"
"Cut a section of input between two given strings.\n"
"\n"
"Usage:\n"
" cutbetween <string1> <string2>\n"
"\n";
int * kmp_buf = 0;
unsigned kmp_matched = 0;
int kmp_print = 0;
void kmp_start(const char * needle, int delayprint) {
unsigned k, i;
kmp_print = delayprint;
if (kmp_buf) free(kmp_buf);
kmp_buf = malloc(sizeof(unsigned)*strlen(needle));
if (!kmp_buf) return;
kmp_buf[0] = 0;
for (i = 1; needle[i]; i++) {
kmp_buf[i] = 0;
k = i;
while (k >= 1) {
k = kmp_buf[k-1];
if (needle[i] == needle[k]) {
kmp_buf[i] = k+1;
break;
}
}
}
kmp_matched = 0;
}
void kmp_free(const char * needle) {
unsigned i;
if (kmp_buf) {
if (kmp_print && kmp_matched) {
for (i = 0; i < kmp_matched; i++) putc(needle[i], stdout);
}
free(kmp_buf);
}
kmp_buf = 0;
}
int kmp_next(const char * needle, char next) {
unsigned k, i;
if (needle[kmp_matched] == next) {
kmp_matched++;
if (needle[kmp_matched] == 0) {
kmp_matched = 0;
return 1;
}
return 0;
}
while (kmp_matched > 0) {
k = kmp_matched;
kmp_matched = kmp_buf[kmp_matched-1];
if (kmp_print) {
for (i = 0; i < k-kmp_matched; i++) putc(needle[i], stdout);
}
if (needle[kmp_matched] == next) {
kmp_matched++;
return 0;
}
}
if (kmp_print) putc(next, stdout);
return 0;
}
int main(int argc, char ** argv) {
if (argc != 3) {
fprintf(stderr, "%s", INFO);
return 2;
}
int c;
if (argv[1][0]) {
kmp_start(argv[1], 0);
while ((c = getc(stdin)) >= 0) {
if (kmp_next(argv[1], c)) break;
}
kmp_free(argv[1]);
if (c < 0) return 1;
}
if (argv[2][0]) {
kmp_start(argv[2], 1);
while ((c = getc(stdin)) >= 0) {
if (kmp_next(argv[2], c)) break;
}
if (c >= 0) kmp_print = 0;
kmp_free(argv[2]);
while ((c = getc(stdin)) >= 0);
} else {
while ((c = getc(stdin)) >= 0) putc(c, stdout);
}
return 0;
}