blob: a2e643d11a2aa9f58bdf831b3213b1d3907c4e19 [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
22#include "libyang.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/**
29 * @defgroup dict Dictionary
30 * @{
31 *
32 * Publicly visible functions and values of the libyang dictionary. They provide
33 * access to the strings stored in the libyang context.
34 */
35
36/**
37 * @brief Insert string into dictionary. If the string is already present,
38 * only a reference counter is incremented and no memory allocation is
39 * performed.
40 *
41 * @param[in] ctx libyang context handler
42 * @param[in] value String to be stored in the dictionary.
43 * @param[in] len Number of bytes to store. The value is not required to be
44 * NULL terminated string, the len parameter says number of bytes stored in
45 * dictionary. The specified number of bytes is duplicated and terminating NULL
46 * byte is added automatically.
47 * @return pointer to the string stored in the dictionary
48 */
49const char *lydict_insert(struct ly_ctx *ctx, const char *value, size_t len);
50
51/**
52 * @brief Insert string into dictionary - zerocopy version. If the string is
53 * already present, only a reference counter is incremented and no memory
54 * allocation is performed. This insert function variant avoids duplication of
55 * specified value - it is inserted into the dictionary directly.
56 *
57 * @param[in] ctx libyang context handler
58 * @param[in] value NULL-terminated string to be stored in the dictionary. If
59 * the string is not present in dictionary, the pointer is directly used by the
60 * dictionary. Otherwise, the reference counter is incremented and the value is
61 * freed. So, after calling the function, caller is supposed to not use the
62 * value address anymore.
63 * @return pointer to the string stored in the dictionary
64 */
65const char *lydict_insert_zc(struct ly_ctx *ctx, char *value);
66
67/**
68 * @brief Remove specified string from the dictionary. It decrement reference
69 * counter for the string and if it is zero, the string itself is freed.
70 *
71 * @param[in] ctx libyang context handler
72 * @param[in] value String to be freed. Note, that not only the string itself
73 * must match the stored value, but also the address is being compared and the
74 * counter is decremented only if it matches.
75 */
76void lydict_remove(struct ly_ctx *ctx, const char *value);
77
78/**@} dict */
79
80#ifdef __cplusplus
81}
82#endif
83
84#endif /* LY_DICT_H_ */