| 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> |
| 28 | #include <stdbool.h> |
| 29 | |
| 30 | #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */ |
| 31 | |
| 32 | #include <common/macros.h> |
| 33 | |
| 34 | struct bt_param_validation_context; |
| 35 | struct bt_param_validation_value_descr; |
| 36 | |
| 37 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} } |
| 38 | |
| 39 | struct bt_param_validation_map_value_descr { |
| 40 | const struct bt_param_validation_map_value_entry_descr *entries; |
| 41 | }; |
| 42 | |
| 43 | #define BT_PARAM_VALIDATION_INFINITE UINT64_MAX |
| 44 | |
| 45 | struct bt_param_validation_array_value_descr { |
| 46 | uint64_t min_length; |
| 47 | uint64_t max_length; /* Use BT_PARAM_VALIDATION_INFINITE if there's no max. */ |
| 48 | const struct bt_param_validation_value_descr *element_type; |
| 49 | }; |
| 50 | |
| 51 | struct bt_param_validation_string_value_descr { |
| 52 | /* NULL-terminated array of choices. Unused if NULL. */ |
| 53 | const char **choices; |
| 54 | }; |
| 55 | |
| 56 | enum bt_param_validation_status { |
| 57 | BT_PARAM_VALIDATION_STATUS_OK = 0, |
| 58 | BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR = -1, |
| 59 | BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR = -2, |
| 60 | }; |
| 61 | |
| 62 | typedef enum bt_param_validation_status |
| 63 | (bt_param_validation_func)(const bt_value *value, |
| 64 | struct bt_param_validation_context *); |
| 65 | |
| 66 | struct bt_param_validation_value_descr { |
| 67 | bt_value_type type; |
| 68 | |
| 69 | /* Additional checks dependent on the type. */ |
| 70 | union { |
| 71 | struct bt_param_validation_array_value_descr array; |
| 72 | struct bt_param_validation_map_value_descr map; |
| 73 | struct bt_param_validation_string_value_descr string; |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * If set, call this function, which is responsible of validating the |
| 78 | * value. The other fields are ignored. |
| 79 | * |
| 80 | * If validation fails, this function must call |
| 81 | * `bt_param_validation_error` with the provided context |
| 82 | * to set the error string. |
| 83 | */ |
| 84 | bt_param_validation_func *validation_func; |
| 85 | }; |
| 86 | |
| 87 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true |
| 88 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false |
| 89 | |
| 90 | struct bt_param_validation_map_value_entry_descr { |
| 91 | const char *key; |
| 92 | bool is_optional; |
| 93 | |
| 94 | const struct bt_param_validation_value_descr value_descr; |
| 95 | }; |
| 96 | |
| 97 | BT_HIDDEN |
| 98 | enum bt_param_validation_status bt_param_validation_validate( |
| 99 | const bt_value *params, |
| 100 | const struct bt_param_validation_map_value_entry_descr *entries, |
| 101 | gchar **error); |
| 102 | |
| 103 | BT_HIDDEN __BT_ATTR_FORMAT_PRINTF(2, 3) |
| 104 | enum bt_param_validation_status bt_param_validation_error( |
| 105 | struct bt_param_validation_context *ctx, |
| 106 | const char *format, ...); |
| 107 | |
| 108 | #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */ |