Commit | Line | Data |
---|---|---|
d0d4e0ed SM |
1 | #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H |
2 | #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H | |
3 | ||
4 | /* | |
5 | * Copyright 2019 EfficiOS Inc. | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include <babeltrace2/babeltrace.h> | |
27 | #include <glib.h> | |
4b3b8e4a | 28 | #include <stdbool.h> |
d0d4e0ed SM |
29 | |
30 | #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */ | |
31 | ||
32 | #ifdef __MINGW_PRINTF_FORMAT | |
33 | # define BT_PRINTF_FORMAT __MINGW_PRINTF_FORMAT | |
34 | #else | |
35 | # define BT_PRINTF_FORMAT printf | |
36 | #endif | |
37 | ||
38 | struct bt_param_validation_context; | |
39 | struct bt_param_validation_value_descr; | |
40 | ||
41 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} } | |
42 | ||
43 | struct bt_param_validation_map_value_descr { | |
44 | const struct bt_param_validation_map_value_entry_descr *entries; | |
45 | }; | |
46 | ||
47 | #define BT_PARAM_VALIDATION_INFINITE UINT64_MAX | |
48 | ||
49 | struct bt_param_validation_array_value_descr { | |
50 | uint64_t min_length; | |
51 | uint64_t max_length; /* Use BT_PARAM_VALIDATION_INFINITE if there's no max. */ | |
52 | const struct bt_param_validation_value_descr *element_type; | |
53 | }; | |
54 | ||
55 | struct bt_param_validation_string_value_descr { | |
56 | /* NULL-terminated array of choices. Unused if NULL. */ | |
57 | const char **choices; | |
58 | }; | |
59 | ||
60 | enum bt_param_validation_status { | |
61 | BT_PARAM_VALIDATION_STATUS_OK = 0, | |
62 | BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR = -1, | |
63 | BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR = -2, | |
64 | }; | |
65 | ||
66 | typedef enum bt_param_validation_status | |
67 | (bt_param_validation_func)(const bt_value *value, | |
68 | struct bt_param_validation_context *); | |
69 | ||
70 | struct bt_param_validation_value_descr { | |
71 | bt_value_type type; | |
72 | ||
73 | /* Additional checks dependent on the type. */ | |
74 | union { | |
75 | struct bt_param_validation_array_value_descr array; | |
76 | struct bt_param_validation_map_value_descr map; | |
77 | struct bt_param_validation_string_value_descr string; | |
78 | }; | |
79 | ||
80 | /* | |
81 | * If set, call this function, which is responsible of validating the | |
82 | * value. The other fields are ignored. | |
83 | * | |
84 | * If validation fails, this function must call | |
85 | * `bt_param_validation_error` with the provided context | |
86 | * to set the error string. | |
87 | */ | |
88 | bt_param_validation_func *validation_func; | |
89 | }; | |
90 | ||
91 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true | |
92 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false | |
93 | ||
94 | struct bt_param_validation_map_value_entry_descr { | |
95 | const char *key; | |
96 | bool is_optional; | |
97 | ||
98 | const struct bt_param_validation_value_descr value_descr; | |
99 | }; | |
100 | ||
101 | enum bt_param_validation_status bt_param_validation_validate( | |
102 | const bt_value *params, | |
103 | const struct bt_param_validation_map_value_entry_descr *entries, | |
104 | gchar **error); | |
105 | ||
106 | __attribute__((format(BT_PRINTF_FORMAT, 2, 3))) | |
107 | enum bt_param_validation_status bt_param_validation_error( | |
108 | struct bt_param_validation_context *ctx, | |
109 | const char *format, ...); | |
110 | ||
111 | #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */ |