Commit | Line | Data |
---|---|---|
d0d4e0ed | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
d0d4e0ed | 3 | * |
0235b0db | 4 | * Copyright 2019 EfficiOS Inc. |
d0d4e0ed | 5 | */ |
0235b0db MJ |
6 | #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H |
7 | #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H | |
d0d4e0ed SM |
8 | |
9 | #include <babeltrace2/babeltrace.h> | |
10 | #include <glib.h> | |
4b3b8e4a | 11 | #include <stdbool.h> |
d0d4e0ed SM |
12 | |
13 | #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */ | |
14 | ||
40f17955 SM |
15 | #include <common/macros.h> |
16 | ||
d0d4e0ed SM |
17 | struct bt_param_validation_context; |
18 | struct bt_param_validation_value_descr; | |
19 | ||
20 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} } | |
21 | ||
22 | struct bt_param_validation_map_value_descr { | |
23 | const struct bt_param_validation_map_value_entry_descr *entries; | |
24 | }; | |
25 | ||
26 | #define BT_PARAM_VALIDATION_INFINITE UINT64_MAX | |
27 | ||
28 | struct bt_param_validation_array_value_descr { | |
29 | uint64_t min_length; | |
30 | uint64_t max_length; /* Use BT_PARAM_VALIDATION_INFINITE if there's no max. */ | |
31 | const struct bt_param_validation_value_descr *element_type; | |
32 | }; | |
33 | ||
34 | struct bt_param_validation_string_value_descr { | |
35 | /* NULL-terminated array of choices. Unused if NULL. */ | |
36 | const char **choices; | |
37 | }; | |
38 | ||
39 | enum bt_param_validation_status { | |
40 | BT_PARAM_VALIDATION_STATUS_OK = 0, | |
41 | BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR = -1, | |
42 | BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR = -2, | |
43 | }; | |
44 | ||
45 | typedef enum bt_param_validation_status | |
46 | (bt_param_validation_func)(const bt_value *value, | |
47 | struct bt_param_validation_context *); | |
48 | ||
49 | struct bt_param_validation_value_descr { | |
50 | bt_value_type type; | |
51 | ||
52 | /* Additional checks dependent on the type. */ | |
53 | union { | |
54 | struct bt_param_validation_array_value_descr array; | |
55 | struct bt_param_validation_map_value_descr map; | |
56 | struct bt_param_validation_string_value_descr string; | |
57 | }; | |
58 | ||
59 | /* | |
60 | * If set, call this function, which is responsible of validating the | |
61 | * value. The other fields are ignored. | |
62 | * | |
63 | * If validation fails, this function must call | |
64 | * `bt_param_validation_error` with the provided context | |
65 | * to set the error string. | |
66 | */ | |
67 | bt_param_validation_func *validation_func; | |
68 | }; | |
69 | ||
70 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true | |
71 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false | |
72 | ||
73 | struct bt_param_validation_map_value_entry_descr { | |
74 | const char *key; | |
75 | bool is_optional; | |
76 | ||
77 | const struct bt_param_validation_value_descr value_descr; | |
78 | }; | |
79 | ||
40f17955 | 80 | BT_HIDDEN |
d0d4e0ed SM |
81 | enum bt_param_validation_status bt_param_validation_validate( |
82 | const bt_value *params, | |
83 | const struct bt_param_validation_map_value_entry_descr *entries, | |
84 | gchar **error); | |
85 | ||
8b305066 | 86 | BT_HIDDEN __BT_ATTR_FORMAT_PRINTF(2, 3) |
d0d4e0ed SM |
87 | enum bt_param_validation_status bt_param_validation_error( |
88 | struct bt_param_validation_context *ctx, | |
89 | const char *format, ...); | |
90 | ||
91 | #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */ |