-
Notifications
You must be signed in to change notification settings - Fork 0
/
patt.c
245 lines (213 loc) · 4.9 KB
/
patt.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* See LICENSE for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <wctype.h>
#include <time.h>
#include "util.h"
#include "sspec.h"
#include "xlseq.h"
int
string_pattern_match(const wchar_t rune)
{
return 1;
}
void
string_pattern_run(union sample_space samples, int count)
{
int i;
long suffix;
size_t ind;
const char *cend = NULL;
char *endptr, *buf;
const char *walk;
for (walk = samples.ordered.last; *walk; walk++) {
ind = walk - samples.ordered.last;
if (!samples.ordered.middle[ind])
break;
else if (samples.ordered.middle[ind] != *walk) {
break;
}
cend = walk;
}
if (cend && *cend) {
cend++;
suffix = strtol(cend, &endptr, 10);
buf = calloc(sizeof(char), cend - samples.ordered.last + 1);
if (!buf) {
perror("buf allocation");
return;
}
strncpy(buf, samples.ordered.last, cend - samples.ordered.last);
for (int i = 0; i < count; i++) {
if (suffix) {
printf("%s%ld%s ", buf, ++suffix, endptr);
} else {
printf("%s%s ", buf, endptr);
}
}
free(buf);
} else {
for (i = 0; i < count; i++) {
printf("%s ", samples.samples[1 - i % 2]);
}
}
}
int
number_pattern_match(const wchar_t rune)
{
return iswdigit(rune) || rune == '.' || rune == '-';
}
void
number_pattern_run(struct full_sample samples, int count)
{
sspec_t *sq;
int i;
long isamp[samples.len], obuf[count];
long diff;
for (i = 0; i < samples.len; i++) {
isamp[i] = strtol(samples.samples[i], NULL, 10);
/* printf("%d: %ld\n", i, isamp[i]); */
}
/*
* As a special case, do a naive extrapolation over two elements if
* they are the only two provided, as sspec requires at least three
*/
if (samples.len == 2) {
diff = isamp[1] - isamp[0];
for (i = 1; i <= count; i++) {
printf("%ld ", isamp[1] + (i * diff));
}
return;
}
sq = sspec_analyze(isamp, samples.len);
if (!sq) {
fputs("xlseq: no valid arithmetic sequence determined\n",
stderr);
return;
}
sspec_continue(sq, obuf, count);
for (i = 0; i < count; i++) {
printf("%ld ", obuf[i]);
}
}
/* NOTE: not a normal matcher - called once and overrides any other matches */
int
date_pattern_match(const char *in)
{
int i, any = 0;
time_t ts;
struct tm tm;
const char *cmp;
ts = time(NULL);
tm = *localtime(&ts);
for (i = 0; i < LENGTH(datefmt); i++) {
cmp = strptime(in, datefmt[i], &tm);
if (cmp && *cmp == 0) {
any++;
break;
}
}
return any;
}
void
date_pattern_run(union sample_space samples, int count)
{
int i;
time_t ts, diff, start;
struct tm tm0, tm1, tmp;
int parsed0 = 0, parsed1 = 0;
const char *cmp;
ts = time(NULL);
tm0 = tm1 = *localtime(&ts);
for (i = 0; i < LENGTH(datefmt); i++) {
if (!parsed0) {
cmp = strptime(samples.ordered.middle, datefmt[i], &tm0);
if (cmp && *cmp == 0) {
parsed0++;
}
}
if (!parsed1) {
cmp = strptime(samples.ordered.last, datefmt[i], &tm1);
if (cmp && *cmp == 0) {
parsed1++;
}
}
}
if (!parsed0 || !parsed1) {
fprintf(stderr,
"xlseq: date_pattern_match didn't catch bad format(s)\ntm0:\t'%s'\ntm1:\t'%s'\n",
samples.ordered.last, samples.ordered.middle);
abort();
}
start = mktime(&tm1);
diff = start - mktime(&tm0);
for (i = 0; i < count; i++) {
start += diff;
tmp = *localtime(&start);
printf("%d-%02d-%02d %02d:%02d:%02d ", tmp.tm_year + 1900, tmp.tm_mon + 1, tmp.tm_mday, tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
}
}
int
buffered_pattern_match(const wchar_t rune, struct buffered_matcher_state *state,
const struct long_short *dataset, size_t datalen)
{
if (state->bufpos >= BUFSIZ - 1) {
return 0;
}
state->buf[state->bufpos++] = towlower(rune);
state->buf[state->bufpos] = 0;
for (int i = 0; i < datalen; i++) {
if (wcsncmp(state->buf, dataset[i].l, state->bufpos) == 0 ||
wcsncmp(state->buf, dataset[i].s, state->bufpos) == 0) {
return 1;
}
}
return 0;
}
void
buffered_pattern_run(union sample_space samples, int count,
const struct long_short *dataset, size_t datalen)
{
int i;
const wchar_t *out;
const char *work = samples.ordered.last;
const struct long_short *cur;
int uselong;
int rlen, rind = 0;
unsigned int dind = 0;
int arglen = strlen(samples.ordered.last);
wchar_t decode[arglen];
memset(decode, 0, sizeof(wchar_t) * arglen);
do {
rlen = mbtowc(decode + rind, work, arglen);
if (rlen < 0) {
fprintf(stderr, "xlseq: invalid text encoding\n");
return;
}
work += rlen;
rind++;
} while (*work);
decode[rind] = 0;
for (i = 0; i < datalen; i++) {
cur = &dataset[i];
if (wcscasecmp(cur->l, decode) == 0) {
uselong = 1;
break;
} else if (wcscasecmp(cur->s, decode) == 0) {
uselong = 0;
break;
}
}
dind = i;
if (count <= 0)
count = datalen - 1 - (cur - dataset);
for (i = 1; i <= count; i++) {
out = (uselong) ? dataset[(dind + i) % datalen].l :
dataset[(dind + i) % datalen].s;
printf("%ls ", out);
}
}