| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 EfficiOS Inc. |
| 5 | */ |
| 6 | #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H |
| 7 | #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H |
| 8 | |
| 9 | #include <babeltrace2/babeltrace.h> |
| 10 | #include <glib.h> |
| 11 | #include <stdbool.h> |
| 12 | |
| 13 | #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */ |
| 14 | |
| 15 | #include <common/macros.h> |
| 16 | |
| 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 | |
| 80 | BT_HIDDEN |
| 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 | |
| 86 | BT_HIDDEN __BT_ATTR_FORMAT_PRINTF(2, 3) |
| 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 */ |