forked from ernestrc/logd
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog.c
91 lines (71 loc) · 1.41 KB
/
log.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "util.h"
log_t* log_create()
{
log_t* l;
if ((l = calloc(1, sizeof(log_t))) == NULL) {
perror("calloc");
return NULL;
}
log_init(l);
return l;
}
void log_init(log_t* l)
{
DEBUG_ASSERT(l != NULL);
l->props = NULL;
// remains unchanged
// l->is_safe = false;
}
const char* log_get(log_t* l, const char* key)
{
DEBUG_ASSERT(l != NULL);
DEBUG_ASSERT(key != NULL);
for (prop_t* next = l->props; next != NULL; next = next->next) {
DEBUG_ASSERT(next->key != NULL);
if (strcmp(key, next->key) == 0)
return next->value;
}
return NULL;
}
prop_t* log_remove(log_t* l, const char* key)
{
DEBUG_ASSERT(l != NULL);
DEBUG_ASSERT(key != NULL);
prop_t* ret = NULL;
for (prop_t** next = &l->props; *next != NULL; next = &((*next)->next)) {
DEBUG_ASSERT((*next)->key != NULL);
if (strcmp(key, (*next)->key) == 0) {
ret = (*next);
(*next) = (*next)->next;
break;
}
}
return ret;
}
void log_set(log_t* l, prop_t* prop, const char* key, const char* value)
{
DEBUG_ASSERT(l != NULL);
DEBUG_ASSERT(prop != NULL);
prop->key = key;
prop->value = value;
prop->next = l->props;
l->props = prop;
}
int log_size(log_t* l)
{
DEBUG_ASSERT(l != NULL);
int ret = 0;
for (prop_t* next = l->props; next != NULL; next = next->next)
ret++;
return ret;
}
void log_free(log_t* l)
{
if (l)
free(l);
}