2020-07-18 09:08:47 +00:00
|
|
|
/*
|
|
|
|
* libifupdown/dict.c
|
|
|
|
* Purpose: wrapping linked lists to provide a naive dictionary
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
2020-07-28 20:41:27 +00:00
|
|
|
* Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
|
2020-07-18 09:08:47 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
2020-07-28 21:25:01 +00:00
|
|
|
#include <stdbool.h>
|
2020-07-18 09:08:47 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "libifupdown/dict.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
lif_dict_init(struct lif_dict *dict)
|
|
|
|
{
|
|
|
|
memset(dict, 0, sizeof *dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lif_dict_fini(struct lif_dict *dict)
|
|
|
|
{
|
|
|
|
struct lif_node *iter, *iter_next;
|
|
|
|
|
|
|
|
LIF_DICT_FOREACH_SAFE(iter, iter_next, dict)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
|
|
|
|
lif_dict_delete_entry(dict, entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lif_dict_entry *
|
|
|
|
lif_dict_add(struct lif_dict *dict, const char *key, void *data)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = calloc(1, sizeof *entry);
|
|
|
|
|
|
|
|
entry->key = strdup(key);
|
|
|
|
entry->data = data;
|
|
|
|
|
2020-07-19 00:48:49 +00:00
|
|
|
lif_node_insert_tail(&entry->node, entry, &dict->list);
|
2020-07-18 09:08:47 +00:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:44:22 +00:00
|
|
|
struct lif_dict_entry *
|
|
|
|
lif_dict_add_once(struct lif_dict *dict, const char *key, void *data,
|
2020-07-28 21:57:51 +00:00
|
|
|
lif_dict_cmp_t compar)
|
2020-07-28 20:44:22 +00:00
|
|
|
{
|
|
|
|
struct lif_list *existing = lif_dict_find_all(dict, key);
|
2020-07-28 21:25:01 +00:00
|
|
|
if (existing != NULL)
|
2020-07-28 20:44:22 +00:00
|
|
|
{
|
2020-07-28 21:25:01 +00:00
|
|
|
bool found = false;
|
2020-07-28 20:44:22 +00:00
|
|
|
struct lif_node *iter;
|
|
|
|
LIF_LIST_FOREACH(iter, existing->head)
|
|
|
|
{
|
|
|
|
if (!compar(data, iter->data))
|
2020-07-28 21:25:01 +00:00
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-28 20:44:22 +00:00
|
|
|
}
|
2020-07-28 21:25:01 +00:00
|
|
|
|
2020-07-28 21:53:48 +00:00
|
|
|
lif_list_free_nodes(existing);
|
|
|
|
|
2020-07-28 21:25:01 +00:00
|
|
|
if (found)
|
|
|
|
return NULL;
|
2020-07-28 20:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct lif_dict_entry *entry = calloc(1, sizeof *entry);
|
|
|
|
|
|
|
|
entry->key = strdup(key);
|
|
|
|
entry->data = data;
|
|
|
|
|
|
|
|
lif_node_insert_tail(&entry->node, entry, &dict->list);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2020-07-18 09:08:47 +00:00
|
|
|
struct lif_dict_entry *
|
|
|
|
lif_dict_find(struct lif_dict *dict, const char *key)
|
|
|
|
{
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, dict)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
|
|
|
|
if (!strcmp(entry->key, key))
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:41:27 +00:00
|
|
|
struct lif_list *
|
|
|
|
lif_dict_find_all(struct lif_dict *dict, const char *key)
|
|
|
|
{
|
|
|
|
struct lif_list *entries = calloc(1, sizeof(struct lif_list));
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
|
|
|
LIF_LIST_FOREACH(iter, dict->list.head)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
if (!strcmp(entry->key, key))
|
|
|
|
{
|
|
|
|
struct lif_node *new = calloc(1, sizeof(struct lif_node));
|
|
|
|
lif_node_insert_tail(new, entry->data, entries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entries->length == 0)
|
|
|
|
{
|
|
|
|
free(entries);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-07-18 09:08:47 +00:00
|
|
|
void
|
|
|
|
lif_dict_delete(struct lif_dict *dict, const char *key)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = lif_dict_find(dict, key);
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lif_dict_delete_entry(dict, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lif_dict_delete_entry(struct lif_dict *dict, struct lif_dict_entry *entry)
|
|
|
|
{
|
|
|
|
lif_node_delete(&entry->node, &dict->list);
|
|
|
|
|
|
|
|
free(entry->key);
|
|
|
|
free(entry);
|
|
|
|
}
|