blob: d334fb2cd4033f3ded0ca28611fa851fb16e2280 [file] [log] [blame]
Radek Krejciee627582015-04-20 17:39:59 +02001/**
2 * @file dict.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang dictionary
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejciee627582015-04-20 17:39:59 +020013 */
14
15#ifndef LY_DICT_H_
16#define LY_DICT_H_
17
Radek Krejcida04f4a2015-05-21 12:54:09 +020018#include <stdint.h>
19
Radek Krejci41912fe2015-10-22 10:22:12 +020020#ifdef __cplusplus
21extern "C" {
22#endif
23
Radek Krejcida04f4a2015-05-21 12:54:09 +020024/*
25 * structure definition from context.h
26 */
27struct ly_ctx;
28
29/**
Radek Krejci41912fe2015-10-22 10:22:12 +020030 * @defgroup dict Dictionary
31 * @{
Radek Krejcida04f4a2015-05-21 12:54:09 +020032 *
Radek Krejci41912fe2015-10-22 10:22:12 +020033 * Publicly visible functions and values of the libyang dictionary. They provide
34 * access to the strings stored in the libyang context.
Radek Krejcida04f4a2015-05-21 12:54:09 +020035 */
Radek Krejcida04f4a2015-05-21 12:54:09 +020036
Radek Krejci4ea08382015-04-21 09:41:40 +020037/**
38 * @brief Insert string into dictionary. If the string is already present,
39 * only a reference counter is incremented and no memory allocation is
40 * performed.
41 *
Radek Krejcida04f4a2015-05-21 12:54:09 +020042 * @param[in] ctx libyang context handler
Radek Krejci4ea08382015-04-21 09:41:40 +020043 * @param[in] value String to be stored in the dictionary.
44 * @param[in] len Number of bytes to store. The value is not required to be
45 * NULL terminated string, the len parameter says number of bytes stored in
46 * dictionary. The specified number of bytes is duplicated and terminating NULL
47 * byte is added automatically.
48 * @return pointer to the string stored in the dictionary
49 */
Radek Krejcice7fb782015-05-29 16:52:34 +020050const char *lydict_insert(struct ly_ctx *ctx, const char *value, size_t len);
Radek Krejci4ea08382015-04-21 09:41:40 +020051
52/**
53 * @brief Insert string into dictionary - zerocopy version. If the string is
54 * already present, only a reference counter is incremented and no memory
55 * allocation is performed. This insert function variant avoids duplication of
56 * specified value - it is inserted into the dictionary directly.
57 *
Radek Krejcida04f4a2015-05-21 12:54:09 +020058 * @param[in] ctx libyang context handler
Radek Krejci4ea08382015-04-21 09:41:40 +020059 * @param[in] value NULL-terminated string to be stored in the dictionary. If
60 * the string is not present in dictionary, the pointer is directly used by the
61 * dictionary. Otherwise, the reference counter is incremented and the value is
62 * freed. So, after calling the function, caller is supposed to not use the
63 * value address anymore.
64 * @return pointer to the string stored in the dictionary
65 */
Radek Krejcice7fb782015-05-29 16:52:34 +020066const char *lydict_insert_zc(struct ly_ctx *ctx, char *value);
Radek Krejci4ea08382015-04-21 09:41:40 +020067
Radek Krejci4ea08382015-04-21 09:41:40 +020068/**
69 * @brief Remove specified string from the dictionary. It decrement reference
70 * counter for the string and if it is zero, the string itself is freed.
71 *
Radek Krejcida04f4a2015-05-21 12:54:09 +020072 * @param[in] ctx libyang context handler
73 * @param[in] value String to be freed. Note, that not only the string itself
Radek Krejci4ea08382015-04-21 09:41:40 +020074 * must match the stored value, but also the address is being compared and the
75 * counter is decremented only if it matches.
76 */
Radek Krejcida04f4a2015-05-21 12:54:09 +020077void lydict_remove(struct ly_ctx *ctx, const char *value);
Radek Krejciee627582015-04-20 17:39:59 +020078
Radek Krejci41912fe2015-10-22 10:22:12 +020079/**@} dict */
80
81#ifdef __cplusplus
82}
83#endif
84
Radek Krejciee627582015-04-20 17:39:59 +020085#endif /* LY_DICT_H_ */