|
| 1 | +/** |
| 2 | + * @file utils/dictionary.c |
| 3 | + * @author Martin Pulec <[email protected]> |
| 4 | + * |
| 5 | + * this file implementents simple key/value dictionary in string |
| 6 | + * |
| 7 | + * Currenttly simple linked listi is used with linear lookup/insertion. |
| 8 | + */ |
| 9 | +/* |
| 10 | + * Copyright (c) 2026 CESNET, zájmové sdružení právnických osob |
| 11 | + * All rights reserved. |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without |
| 14 | + * modification, is permitted provided that the following conditions |
| 15 | + * are met: |
| 16 | + * |
| 17 | + * 1. Redistributions of source code must retain the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer. |
| 19 | + * |
| 20 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | + * notice, this list of conditions and the following disclaimer in the |
| 22 | + * documentation and/or other materials provided with the distribution. |
| 23 | + * |
| 24 | + * 3. Neither the name of CESNET nor the names of its contributors may be |
| 25 | + * used to endorse or promote products derived from this software without |
| 26 | + * specific prior written permission. |
| 27 | + * |
| 28 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS |
| 29 | + * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, |
| 30 | + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 31 | + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 32 | + * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 33 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 34 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 35 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 36 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 37 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 38 | + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 39 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 40 | + */ |
| 41 | + |
| 42 | +#include "dictionary.h" |
| 43 | + |
| 44 | +#include <assert.h> // for assert |
| 45 | +#include <stdlib.h> // for calloc, free |
| 46 | +#include <string.h> // for strdup, strchr |
| 47 | +#include <stdint.h> // for uint32_t |
| 48 | + |
| 49 | +#include "utils//macros.h" // for to_fourcc |
| 50 | + |
| 51 | +#define MAGIC to_fourcc('d', 'i', 'c', 't') |
| 52 | + |
| 53 | +struct item { |
| 54 | + char *key; |
| 55 | + char *val; |
| 56 | + struct item *next; |
| 57 | +}; |
| 58 | + |
| 59 | +struct dictionary { |
| 60 | + uint32_t magic; |
| 61 | + struct item *items; |
| 62 | + |
| 63 | + struct item *iterator; |
| 64 | +}; |
| 65 | + |
| 66 | +struct dictionary * |
| 67 | +dictionary_init() |
| 68 | +{ |
| 69 | + struct dictionary *dictionary = calloc(1, sizeof *dictionary); |
| 70 | + dictionary->magic = MAGIC; |
| 71 | + return dictionary; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * insert specified element |
| 76 | + */ |
| 77 | +void |
| 78 | +dictionary_insert(struct dictionary *dictionary, const char *key, |
| 79 | + const char *val) |
| 80 | +{ |
| 81 | + struct item **cur_p = &dictionary->items; |
| 82 | + while (*cur_p != NULL) { |
| 83 | + struct item *cur = *cur_p; |
| 84 | + if (strcmp(cur->key, key) == 0) { // replace |
| 85 | + free(cur->val); |
| 86 | + cur->val = strdup(val); |
| 87 | + return; |
| 88 | + } |
| 89 | + if (strcmp(cur->key, key) > 0) { // prepend |
| 90 | + struct item *new = calloc(1, sizeof(struct item)); |
| 91 | + new->key = strdup(key); |
| 92 | + new->val = strdup(val); |
| 93 | + new->next = cur; |
| 94 | + *cur_p = new; |
| 95 | + return; |
| 96 | + } |
| 97 | + cur_p = &(*cur_p)->next; |
| 98 | + } |
| 99 | + // append as last item |
| 100 | + struct item *new = calloc(1, sizeof(struct item)); |
| 101 | + new->key = strdup(key); |
| 102 | + new->val = strdup(val); |
| 103 | + *cur_p = new; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * insert an element in format key=vale |
| 108 | + */ |
| 109 | +void |
| 110 | +dictionary_insert2(struct dictionary *dictionary, const char *key_val) |
| 111 | +{ |
| 112 | + assert(strchr(key_val, '=') != NULL); |
| 113 | + char *key = strdup(key_val); |
| 114 | + char *val = strchr(key, '='); |
| 115 | + *val = '\0'; |
| 116 | + val += 1; |
| 117 | + dictionary_insert(dictionary, key, val); |
| 118 | + free(key); |
| 119 | +} |
| 120 | + |
| 121 | +const char * |
| 122 | +dictionary_lookup(struct dictionary *dictionary, const char *key) |
| 123 | +{ |
| 124 | + struct item *cur = dictionary->items; |
| 125 | + while (cur != NULL) { |
| 126 | + if (strcmp(cur->key, key) == 0) { |
| 127 | + return cur->key; |
| 128 | + } |
| 129 | + if (strcmp(cur->key, key) > 0) { |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + }; |
| 133 | + return NULL; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * create an iterator and return key of first element |
| 138 | + * |
| 139 | + * @note |
| 140 | + * As the iterator state is stored internally, creating the |
| 141 | + * iterator (calling this fn) invalidates any previous. |
| 142 | + */ |
| 143 | +const char * |
| 144 | +dictionary_first(struct dictionary *dictionary, const char **key) |
| 145 | +{ |
| 146 | + if (dictionary->items == NULL) { |
| 147 | + return NULL; |
| 148 | + } |
| 149 | + dictionary->iterator = dictionary->items; |
| 150 | + *key = dictionary->iterator->key; |
| 151 | + return dictionary->iterator->val; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * get next element |
| 156 | + * |
| 157 | + * this must be called after dictionary_first() |
| 158 | + * |
| 159 | + * When dictionary_next() returns nullptr, no more elements are |
| 160 | + * available and the internal iterator is invalidated. |
| 161 | + */ |
| 162 | +const char * |
| 163 | +dictionary_next(struct dictionary *dictionary, const char **key) |
| 164 | +{ |
| 165 | + if (dictionary->iterator->next == NULL) { |
| 166 | + return NULL; |
| 167 | + } |
| 168 | + dictionary->iterator = dictionary->iterator->next; |
| 169 | + *key = dictionary->iterator->key; |
| 170 | + return dictionary->iterator->val; |
| 171 | +} |
| 172 | + |
| 173 | +void |
| 174 | +dictionary_destroy(struct dictionary *dictionary) |
| 175 | +{ |
| 176 | + if (dictionary == NULL) { |
| 177 | + return; |
| 178 | + } |
| 179 | + assert(dictionary->magic == MAGIC); |
| 180 | + struct item *cur = dictionary->items; |
| 181 | + while (cur != NULL) { |
| 182 | + struct item *next = cur->next; |
| 183 | + free(cur->key); |
| 184 | + free(cur->val); |
| 185 | + free(cur); |
| 186 | + cur = next; |
| 187 | + }; |
| 188 | + free(dictionary); |
| 189 | +} |
0 commit comments