Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 26 | /* dummy context structure */ |
| 27 | struct ly_ctx; |
| 28 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 29 | /** |
| 30 | * @defgroup dict Dictionary |
| 31 | * @{ |
| 32 | * |
| 33 | * Publicly visible functions and values of the libyang dictionary. They provide |
| 34 | * access to the strings stored in the libyang context. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 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 | * |
| 42 | * @param[in] ctx libyang context handler |
Michal Vasko | 0f6b3e2 | 2018-09-07 12:18:12 +0200 | [diff] [blame] | 43 | * @param[in] value String to be stored in the dictionary. If NULL, function does nothing. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 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 |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 47 | * byte is added automatically. If \p len is 0, it is count automatically using strlen(). |
Michal Vasko | 0f6b3e2 | 2018-09-07 12:18:12 +0200 | [diff] [blame] | 48 | * @return pointer to the string stored in the dictionary, NULL if \p value was NULL. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 49 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 50 | const char *lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 51 | |
| 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 | * |
| 58 | * @param[in] ctx libyang context handler |
| 59 | * @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 |
Michal Vasko | 0f6b3e2 | 2018-09-07 12:18:12 +0200 | [diff] [blame] | 63 | * value address anymore. If NULL, function does nothing. |
| 64 | * @return pointer to the string stored in the dictionary, NULL if \p value was NULL. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 65 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 66 | const char *lydict_insert_zc(const struct ly_ctx *ctx, char *value); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 67 | |
| 68 | /** |
| 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 | * |
| 72 | * @param[in] ctx libyang context handler |
| 73 | * @param[in] value String to be freed. Note, that not only the string itself |
| 74 | * must match the stored value, but also the address is being compared and the |
Michal Vasko | 0f6b3e2 | 2018-09-07 12:18:12 +0200 | [diff] [blame] | 75 | * counter is decremented only if it matches. If NULL, function does nothing. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 76 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 77 | void lydict_remove(const struct ly_ctx *ctx, const char *value); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 78 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 79 | /** @} dict */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 80 | |
| 81 | #ifdef __cplusplus |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | #endif /* LY_DICT_H_ */ |