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

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


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

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

char *magi_params_get(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(magi_params *params)
{
    if (params) {
        magi_params_free(params->next);
        free(params->next);
        free(params->item.name);
        free(params->item.data);
    }
}