globset: Introduce globset for event handling
[src/app-framework-binder.git] / src / tests / globset / test-globset.c
1 /*
2  * Copyright (C) 2018 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include "globset.h"
25
26 int main()
27 {
28         int rc;
29         char buffer[1024], *str;
30         const struct globset_handler *gh;
31         struct globset *set;
32
33         setvbuf(stdout, NULL, _IOLBF, 0);
34         set = globset_create();
35         while (fgets(buffer, sizeof buffer, stdin)) {
36                 str = strchr(buffer,'\n');
37                 if (str) *str = 0;
38                 errno = 0;
39                 switch (buffer[0]) {
40                 case '+':
41                         rc = globset_add(set, &buffer[1], NULL, NULL);
42                         printf("add [%s]: %d, %m\n", &buffer[1], rc);
43                         break;
44                 case '-':
45                         rc = globset_del(set, &buffer[1], NULL);
46                         printf("del [%s]: %d, %m\n", &buffer[1], rc);
47                         break;
48                 case '?':
49                         gh = globset_search(set, &buffer[1]);
50                         printf("search [%s]: %s%s\n", &buffer[1], gh ? "found " : "NOT FOUND", gh ? gh->pattern : "");
51                         break;
52                 default:
53                         gh = globset_match(set, buffer);
54                         printf("match [%s]: %s%s\n", buffer, gh ? "found by " : "NOT FOUND", gh ? gh->pattern : "");
55                         break;
56                 }
57         }
58         globset_destroy(set);
59         return 0;
60 }