blob: bc8cac1fe00222e1da5865a0cb687a4ff41eadad [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/**
Radek Krejci8678fa42020-08-18 16:07:28 +020032 * @page howtoContextDict Context Dictionary
33 *
34 * Context includes dictionary to store strings more effectively. The most of strings repeats quite often in schema
35 * as well as data trees. Therefore, instead of allocating those strings each time they appear, libyang stores them
36 * as records in the dictionary. The basic API to the context dictionary is public, so even a caller application can
37 * use the dictionary.
38 *
39 * To insert a string into the dictionary, caller can use ::lydict_insert() (adding a constant string) or
40 * ::lydict_insert_zc() (for dynamically allocated strings that won't be used by the caller after its insertion into
41 * the dictionary). Both functions provide the pointer to the inserted string in the dictionary record.
42 *
43 * To remove (reference of the) string from the context dictionary, ::lydict_remove() is supposed to be used.
44 *
45 * \note Incorrect usage of the dictionary can break libyang functionality.
46 *
47 * \note API for this group of functions is described in the [Dictionary module](@ref dict).
48 *
49 * Functions List
50 * --------------
51 * - ::lydict_insert()
52 * - ::lydict_insert_zc()
53 * - ::lydict_remove()
54 */
55
56/**
Radek Krejci5aeea3a2018-09-05 13:29:36 +020057 * @defgroup dict Dictionary
58 * @{
59 *
60 * Publicly visible functions and values of the libyang dictionary. They provide
Radek Krejci8678fa42020-08-18 16:07:28 +020061 * access to the strings stored in the libyang context. More detailed information can be found at
62 * @ref howtoContextDict page.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020063 */
64
65/**
66 * @brief Insert string into dictionary. If the string is already present,
67 * only a reference counter is incremented and no memory allocation is
68 * performed.
69 *
70 * @param[in] ctx libyang context handler
Michal Vasko0f6b3e22018-09-07 12:18:12 +020071 * @param[in] value String to be stored in the dictionary. If NULL, function does nothing.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020072 * @param[in] len Number of bytes to store. The value is not required to be
73 * NULL terminated string, the len parameter says number of bytes stored in
74 * dictionary. The specified number of bytes is duplicated and terminating NULL
Radek Krejciaaf6d402018-09-20 15:14:47 +020075 * byte is added automatically. If \p len is 0, it is count automatically using strlen().
Radek Krejci011e4aa2020-09-04 15:22:31 +020076 * @param[out] str_p Optional parameter to get pointer to the string corresponding to the @p value and stored in dictionary.
77 * @return LY_SUCCESS in case of successful insertion into dictionary, note that the function does not return LY_EEXIST.
78 * @return LY_EINVAL in case of invalid input parameters.
79 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020080 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010081LIBYANG_API_DECL LY_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 +020082
83/**
84 * @brief Insert string into dictionary - zerocopy version. If the string is
85 * already present, only a reference counter is incremented and no memory
86 * allocation is performed. This insert function variant avoids duplication of
87 * specified value - it is inserted into the dictionary directly.
88 *
89 * @param[in] ctx libyang context handler
90 * @param[in] value NULL-terminated string to be stored in the dictionary. If
91 * the string is not present in dictionary, the pointer is directly used by the
92 * dictionary. Otherwise, the reference counter is incremented and the value is
93 * freed. So, after calling the function, caller is supposed to not use the
Michal Vasko0f6b3e22018-09-07 12:18:12 +020094 * value address anymore. If NULL, function does nothing.
Radek Krejci011e4aa2020-09-04 15:22:31 +020095 * @param[out] str_p Optional parameter to get pointer to the string corresponding to the @p value and stored in dictionary.
96 * @return LY_SUCCESS in case of successful insertion into dictionary, note that the function does not return LY_EEXIST.
97 * @return LY_EINVAL in case of invalid input parameters.
98 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020099 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100100LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200101
102/**
103 * @brief Remove specified string from the dictionary. It decrement reference
104 * counter for the string and if it is zero, the string itself is freed.
105 *
106 * @param[in] ctx libyang context handler
107 * @param[in] value String to be freed. Note, that not only the string itself
108 * must match the stored value, but also the address is being compared and the
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200109 * counter is decremented only if it matches. If NULL, function does nothing.
Michal Vaskoe180ed02021-02-05 16:31:20 +0100110 * @return LY_SUCCESS if the value was found and removed (or refcount decreased).
111 * @return LY_ENOTFOUND if the value was not found.
112 * @return LY_ERR on other errors.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200113 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100114LIBYANG_API_DECL LY_ERR lydict_remove(const struct ly_ctx *ctx, const char *value);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200115
Michal Vasko60752e82024-09-09 15:20:09 +0200116/**
117 * @brief Duplicate string in dictionary. Only a reference counter is incremented.
118 *
119 * @param[in] ctx libyang context handler
120 * @param[in] value NULL-terminated string to be duplicated in the dictionary (reference counter is incremented).
121 * @param[out] str_p Optional parameter to get pointer to the string corresponding to the @p value and stored in dictionary.
122 * @return LY_SUCCESS in case the string already exists in the dictionary.
123 * @return LY_ENOTFOUND in case the string was not found.
124 * @return LY_ERR on other errors
125 */
126LIBYANG_API_DECL LY_ERR lydict_dup(const struct ly_ctx *ctx, const char *value, const char **str_p);
127
Michal Vasko52927e22020-03-16 17:26:14 +0100128/** @} dict */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200129
130#ifdef __cplusplus
131}
132#endif
133
134#endif /* LY_DICT_H_ */