13 char * Star[STAR_NUMBER];
17 static bool match_rec (char string[], const char pattern[], char * star[]);
23 bool match(char string[], const char pattern[]) {
26 ASSERT(pattern!=NULL);
28 ASSERT(strstr(pattern,"**")==NULL);
30 return match_rec(string,pattern,Star);
35 static bool match_rec(char string[], const char pattern[], char * star[]) {
40 ASSERT(pattern!=NULL);
45 while ((c=*pattern++) != '*') {
47 } else if (c == '\0') { // end of pattern
48 while (*string == ' ') string++; // skip trailing spaces
49 return *string == '\0';
50 } else if (c == ' ') { // spaces
51 if (*string++ != ' ') return FALSE; // mismatch
52 while (*string == ' ') string++; // skip trailing spaces
53 } else { // normal character
54 if (*string++ != c) return FALSE; // mismatch
58 // recursive wildcard match
62 while (*string == ' ') string++; // skip leading spaces
63 *star++ = string; // remember beginning of star
65 while ((c=*string++) != '\0') { // reject empty-string match
66 if (c != ' ' && match_rec(string,pattern,star)) { // shortest match
67 ASSERT(string>star[-1]);
68 *string = '\0'; // truncate star
78 bool parse_is_ok(const parse_t * parse) {
80 if (parse == NULL) return FALSE;
81 if (parse->string == NULL) return FALSE;
82 if (parse->pos < 0 || parse->pos > (int) strlen(parse->string)) return FALSE;
83 if (parse->keyword_nb < 0 || parse->keyword_nb >= KEYWORD_NUMBER) return FALSE;
90 void parse_open(parse_t * parse, const char string[]) {
95 parse->string = string;
97 parse->keyword_nb = 0;
102 void parse_close(parse_t * parse) {
106 ASSERT(parse_is_ok(parse));
108 parse->string = NULL;
111 for (i = 0; i < parse->keyword_nb; i++) {
112 my_string_clear(&parse->keyword[i]);
115 parse->keyword_nb = 0;
118 // parse_add_keyword()
120 void parse_add_keyword(parse_t * parse, const char keyword[]) {
122 const char * * string;
124 ASSERT(parse_is_ok(parse));
125 ASSERT(keyword!=NULL);
127 if (parse->keyword_nb < KEYWORD_NUMBER) {
129 string = &parse->keyword[parse->keyword_nb];
133 my_string_set(string,keyword);
139 bool parse_get_word(parse_t * parse, char string[], int size) {
145 ASSERT(string!=NULL);
150 for (; parse->string[parse->pos] == ' '; parse->pos++)
153 ASSERT(parse->string[parse->pos]!=' ');
161 c = parse->string[parse->pos];
162 if (c == ' ' || pos >= size-1) c = '\0';
165 if (c == '\0') break;
171 ASSERT(strchr(string,' ')==NULL);
173 return pos > 0; // non-empty word?
176 // parse_get_string()
178 bool parse_get_string(parse_t * parse, char string[], int size) {
182 char word[StringSize];
187 ASSERT(string!=NULL);
192 for (; parse->string[parse->pos] == ' '; parse->pos++)
195 ASSERT(parse->string[parse->pos]!=' ');
203 parse_open(parse_2,&parse->string[parse->pos]);
205 if (!parse_get_word(parse_2,word,StringSize)) {
207 parse_close(parse_2);
211 for (i = 0; i < parse->keyword_nb; i++) {
212 if (my_string_equal(parse->keyword[i],word)) {
214 parse_close(parse_2);
219 parse_close(parse_2);
225 c = parse->string[parse->pos];
228 if (pos >= size-1) c = '\0';
231 if (c == '\0') break;
241 c = parse->string[parse->pos];
242 if (c == ' ' || pos >= size-1) c = '\0';
245 if (c == '\0') break;
256 return pos > 0; // non-empty string?