forked from ulthiel/polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
260 lines (168 loc) · 4.68 KB
/
parse.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// parse.c
// includes
#include <string.h>
#include "parse.h"
#include "util.h"
// variables
char * Star[STAR_NUMBER];
// prototypes
static bool match_rec (char string[], const char pattern[], char * star[]);
// functions
// match()
bool match(char string[], const char pattern[]) {
ASSERT(string!=NULL);
ASSERT(pattern!=NULL);
ASSERT(strstr(pattern,"**")==NULL);
return match_rec(string,pattern,Star);
}
// match_rec()
static bool match_rec(char string[], const char pattern[], char * star[]) {
int c;
ASSERT(string!=NULL);
ASSERT(pattern!=NULL);
ASSERT(star!=NULL);
// iterative matches
while ((c=*pattern++) != '*') {
if (FALSE) {
} else if (c == '\0') { // end of pattern
while (*string == ' ') string++; // skip trailing spaces
return *string == '\0';
} else if (c == ' ') { // spaces
if (*string++ != ' ') return FALSE; // mismatch
while (*string == ' ') string++; // skip trailing spaces
} else { // normal character
if (*string++ != c) return FALSE; // mismatch
}
}
// recursive wildcard match
ASSERT(c=='*');
while (*string == ' ') string++; // skip leading spaces
*star++ = string; // remember beginning of star
while ((c=*string++) != '\0') { // reject empty-string match
if (c != ' ' && match_rec(string,pattern,star)) { // shortest match
ASSERT(string>star[-1]);
*string = '\0'; // truncate star
return TRUE;
}
}
return FALSE;
}
// parse_is_ok()
bool parse_is_ok(const parse_t * parse) {
if (parse == NULL) return FALSE;
if (parse->string == NULL) return FALSE;
if (parse->pos < 0 || parse->pos > (int) strlen(parse->string)) return FALSE;
if (parse->keyword_nb < 0 || parse->keyword_nb >= KEYWORD_NUMBER) return FALSE;
return TRUE;
}
// parse_open()
void parse_open(parse_t * parse, const char string[]) {
ASSERT(parse!=NULL);
ASSERT(string!=NULL);
parse->string = string;
parse->pos = 0;
parse->keyword_nb = 0;
}
// parse_close()
void parse_close(parse_t * parse) {
int i;
ASSERT(parse_is_ok(parse));
parse->string = NULL;
parse->pos = 0;
for (i = 0; i < parse->keyword_nb; i++) {
my_string_clear(&parse->keyword[i]);
}
parse->keyword_nb = 0;
}
// parse_add_keyword()
void parse_add_keyword(parse_t * parse, const char keyword[]) {
const char * * string;
ASSERT(parse_is_ok(parse));
ASSERT(keyword!=NULL);
if (parse->keyword_nb < KEYWORD_NUMBER) {
string = &parse->keyword[parse->keyword_nb];
parse->keyword_nb++;
*string = NULL;
my_string_set(string,keyword);
}
}
// parse_get_word()
bool parse_get_word(parse_t * parse, char string[], int size) {
int pos;
int c;
ASSERT(parse!=NULL);
ASSERT(string!=NULL);
ASSERT(size>=256);
// skip blanks
for (; parse->string[parse->pos] == ' '; parse->pos++)
;
ASSERT(parse->string[parse->pos]!=' ');
// copy word
pos = 0;
while (TRUE) {
c = parse->string[parse->pos];
if (c == ' ' || pos >= size-1) c = '\0';
string[pos] = c;
if (c == '\0') break;
parse->pos++;
pos++;
}
ASSERT(strchr(string,' ')==NULL);
return pos > 0; // non-empty word?
}
// parse_get_string()
bool parse_get_string(parse_t * parse, char string[], int size) {
int pos;
parse_t parse_2[1];
char word[StringSize];
int i;
int c;
ASSERT(parse!=NULL);
ASSERT(string!=NULL);
ASSERT(size>=256);
// skip blanks
for (; parse->string[parse->pos] == ' '; parse->pos++)
;
ASSERT(parse->string[parse->pos]!=' ');
// copy string
pos = 0;
while (TRUE) {
parse_open(parse_2,&parse->string[parse->pos]);
if (!parse_get_word(parse_2,word,StringSize)) {
string[pos] = '\0';
parse_close(parse_2);
goto finished;
}
for (i = 0; i < parse->keyword_nb; i++) {
if (my_string_equal(parse->keyword[i],word)) {
string[pos] = '\0';
parse_close(parse_2);
goto finished;
}
}
parse_close(parse_2);
// copy spaces
while (TRUE) {
c = parse->string[parse->pos];
if (c != ' ') break;
if (pos >= size-1) c = '\0';
string[pos] = c;
if (c == '\0') break;
parse->pos++;
pos++;
}
// copy non spaces
while (TRUE) {
c = parse->string[parse->pos];
if (c == ' ' || pos >= size-1) c = '\0';
string[pos] = c;
if (c == '\0') break;
parse->pos++;
pos++;
}
string[pos] = '\0';
}
finished: ;
return pos > 0; // non-empty string?
}
// end of parse.cpp