This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias.c
66 lines (56 loc) · 1.6 KB
/
alias.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#define _XOPEN_SOURCE 500 // to use strdup
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "utils.h"
#include "cfuhash.h"
cfuhash_table_t *alias_table = NULL;
static void alias_delete(void)
{
if (alias_table != NULL) cfuhash_destroy(alias_table);
}
void alias_init(void)
{
if (alias_table != NULL) return;
alias_table = cfuhash_new();
if (alias_table == NULL) panic("Can't allocate alias table");
atexit(&alias_delete);
}
bool alias_add(const char *name, const char *value)
{
if (alias_table == NULL || name == NULL || value == NULL) return false;
void *old_data = cfuhash_put(alias_table, name, strdup(value));
if (old_data != NULL) free(old_data);
return true;
}
const char *alias_get(const char *name)
{
if (alias_table == NULL || name == NULL) return NULL;
return cfuhash_get(alias_table, name);
}
bool alias_in(const char *name)
{
if (alias_table == NULL || name == NULL) return false;
return cfuhash_exists(alias_table, name);
}
bool alias_del(const char *name)
{
if (alias_table == NULL || name == NULL) return false;
void *old_data = cfuhash_delete(alias_table, name);
if (old_data != NULL) free(old_data);
return true;
}
static int alias_foreach(void *key, size_t key_size, void *data, size_t data_size, void *arg)
{
UNUSED_VAR(key_size); UNUSED_VAR(data_size); UNUSED_VAR(arg);
printf("%s=%s\n", (char *)key, (char *)data);
return 0;
}
void alias_print_all()
{
if (alias_table == NULL) return;
cfuhash_foreach(alias_table, &alias_foreach, NULL);
}