1 #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
2 #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
5 * Copyright 2019 EfficiOS Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include <babeltrace2/babeltrace.h>
30 #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */
32 #include <common/macros.h>
34 #ifdef __MINGW_PRINTF_FORMAT
35 # define BT_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
37 # define BT_PRINTF_FORMAT printf
40 struct bt_param_validation_context
;
41 struct bt_param_validation_value_descr
;
43 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} }
45 struct bt_param_validation_map_value_descr
{
46 const struct bt_param_validation_map_value_entry_descr
*entries
;
49 #define BT_PARAM_VALIDATION_INFINITE UINT64_MAX
51 struct bt_param_validation_array_value_descr
{
53 uint64_t max_length
; /* Use BT_PARAM_VALIDATION_INFINITE if there's no max. */
54 const struct bt_param_validation_value_descr
*element_type
;
57 struct bt_param_validation_string_value_descr
{
58 /* NULL-terminated array of choices. Unused if NULL. */
62 enum bt_param_validation_status
{
63 BT_PARAM_VALIDATION_STATUS_OK
= 0,
64 BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
= -1,
65 BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
= -2,
68 typedef enum bt_param_validation_status
69 (bt_param_validation_func
)(const bt_value
*value
,
70 struct bt_param_validation_context
*);
72 struct bt_param_validation_value_descr
{
75 /* Additional checks dependent on the type. */
77 struct bt_param_validation_array_value_descr array
;
78 struct bt_param_validation_map_value_descr map
;
79 struct bt_param_validation_string_value_descr string
;
83 * If set, call this function, which is responsible of validating the
84 * value. The other fields are ignored.
86 * If validation fails, this function must call
87 * `bt_param_validation_error` with the provided context
88 * to set the error string.
90 bt_param_validation_func
*validation_func
;
93 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true
94 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false
96 struct bt_param_validation_map_value_entry_descr
{
100 const struct bt_param_validation_value_descr value_descr
;
104 enum bt_param_validation_status
bt_param_validation_validate(
105 const bt_value
*params
,
106 const struct bt_param_validation_map_value_entry_descr
*entries
,
110 __attribute__((format(BT_PRINTF_FORMAT
, 2, 3)))
111 enum bt_param_validation_status
bt_param_validation_error(
112 struct bt_param_validation_context
*ctx
,
113 const char *format
, ...);
115 #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */