blob: 69bf7d313481c60f5cd224373f436e2bdb026e6c [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file dict.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang dictionary
5 *
6 * Copyright (c) 2015-2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_DICT_H_
16#define LY_DICT_H_
17
18#include <stddef.h>
19#include <stdint.h>
20#include <string.h>
21
Radek Krejci011e4aa2020-09-04 15:22:31 +020022#include "log.h"
23
Radek Krejci5aeea3a2018-09-05 13:29:36 +020024#ifdef __cplusplus
25extern "C" {
26#endif
27
Radek Krejciad573502018-09-07 15:26:55 +020028/* dummy context structure */
29struct ly_ctx;
30
Radek Krejci5aeea3a2018-09-05 13:29:36 +020031/**
32 * @defgroup dict Dictionary
33 * @{
34 *
35 * Publicly visible functions and values of the libyang dictionary. They provide
36 * access to the strings stored in the libyang context.
37 */
38
39/**
40 * @brief Insert string into dictionary. If the string is already present,
41 * only a reference counter is incremented and no memory allocation is
42 * performed.
43 *
44 * @param[in] ctx libyang context handler
Michal Vasko0f6b3e22018-09-07 12:18:12 +020045 * @param[in] value String to be stored in the dictionary. If NULL, function does nothing.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020046 * @param[in] len Number of bytes to store. The value is not required to be
47 * NULL terminated string, the len parameter says number of bytes stored in
48 * dictionary. The specified number of bytes is duplicated and terminating NULL
Radek Krejciaaf6d402018-09-20 15:14:47 +020049 * byte is added automatically. If \p len is 0, it is count automatically using strlen().
Radek Krejci011e4aa2020-09-04 15:22:31 +020050 * @param[out] str_p Optional parameter to get pointer to the string corresponding to the @p value and stored in dictionary.
51 * @return LY_SUCCESS in case of successful insertion into dictionary, note that the function does not return LY_EEXIST.
52 * @return LY_EINVAL in case of invalid input parameters.
53 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020054 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020055LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020056
57/**
58 * @brief Insert string into dictionary - zerocopy version. If the string is
59 * already present, only a reference counter is incremented and no memory
60 * allocation is performed. This insert function variant avoids duplication of
61 * specified value - it is inserted into the dictionary directly.
62 *
63 * @param[in] ctx libyang context handler
64 * @param[in] value NULL-terminated string to be stored in the dictionary. If
65 * the string is not present in dictionary, the pointer is directly used by the
66 * dictionary. Otherwise, the reference counter is incremented and the value is
67 * freed. So, after calling the function, caller is supposed to not use the
Michal Vasko0f6b3e22018-09-07 12:18:12 +020068 * value address anymore. If NULL, function does nothing.
Radek Krejci011e4aa2020-09-04 15:22:31 +020069 * @param[out] str_p Optional parameter to get pointer to the string corresponding to the @p value and stored in dictionary.
70 * @return LY_SUCCESS in case of successful insertion into dictionary, note that the function does not return LY_EEXIST.
71 * @return LY_EINVAL in case of invalid input parameters.
72 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020073 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020074LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020075
76/**
77 * @brief Remove specified string from the dictionary. It decrement reference
78 * counter for the string and if it is zero, the string itself is freed.
79 *
80 * @param[in] ctx libyang context handler
81 * @param[in] value String to be freed. Note, that not only the string itself
82 * must match the stored value, but also the address is being compared and the
Michal Vasko0f6b3e22018-09-07 12:18:12 +020083 * counter is decremented only if it matches. If NULL, function does nothing.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020084 */
Michal Vasko52927e22020-03-16 17:26:14 +010085void lydict_remove(const struct ly_ctx *ctx, const char *value);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020086
Michal Vasko52927e22020-03-16 17:26:14 +010087/** @} dict */
Radek Krejci5aeea3a2018-09-05 13:29:36 +020088
89#ifdef __cplusplus
90}
91#endif
92
93#endif /* LY_DICT_H_ */