aboutsummaryrefslogtreecommitdiff
path: root/src/param.c
blob: adc2b8b8b077e2c5352defdbda56b1cba9ea64e2 (plain)
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
#include "param.h"

#include <stdlib.h>
#include <string.h>


void magi_params_add(struct magi_params     **params,
                     const struct magi_param *newitem)
{
    struct magi_params *node = malloc(sizeof(*node));
    if (node) {
        node->next = *params;
        node->item = *newitem;
        *params    = node;
    }
}

void magi_params_set(struct magi_params     **params,
                     const struct magi_param *newitem)
{
    if (!*params) {
        magi_params_add(params, newitem);
    } else if (!strcmp((*params)->item.name, newitem->name)) {
        free((*params)->item.name);
        free((*params)->item.data);
        (*params)->item.name = newitem->name;
        (*params)->item.data = newitem->data;
    } else {
        magi_params_set(&(*params)->next, newitem);
    }
}

char *magi_params_get(const struct magi_params *params, const char *name)
{
    if (!params || !name) {
        return 0;
    } else if (!strcmp(params->item.name, name)) {
        return params->item.data;
    } else {
        return magi_params_get(params->next, name);
    }
}

void magi_params_free(struct magi_params *params)
{
    if (params) {
        magi_params_free(params->next);
        free(params->next);
        free(params->item.name);
        free(params->item.data);
    }
}