-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictionary.c
66 lines (58 loc) · 1.31 KB
/
dictionary.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
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
static entry *lookup_entry(dictionary d, char *key) {
struct entry *e;
if (d==NULL) return NULL;
e=d;
while (e!=NULL) {
if (strcmp(e->key, key)==0) {
return e;
}
e=e->next;
}
return NULL;
}
char *lookup_dictionary(dictionary d, char *key) {
struct entry *e;
if (d==NULL) return NULL;
e=lookup_entry(d, key);
if (e==NULL) return NULL;
else return e->value;
}
dictionary add_entry(dictionary d, char *key, char *value) {
struct entry *e;
e=lookup_entry(d, key);
if (e!=NULL) {
e->value=strdup(value);
return d;
}
e=(entry *)malloc(sizeof(entry));
e->key=strdup(key);
e->value=strdup(value);
e->next=d;
return e;
}
void free_dictionary(dictionary d) {
struct entry *nd=NULL;
while (d!=NULL) {
nd=d->next;
free(d->key);
free(d->value);
free(d);
d=nd;
}
}
static char key_value[1024];
char *get_current(dictionary d) {
if (d==NULL) return NULL;
strcpy(key_value, d->key);
strcat(key_value, "=\"");
strcat(key_value, d->value);
strcat(key_value, "\"");
return key_value;
}
dictionary next_entry(dictionary d) {
if (d==NULL) return NULL;
return d->next;
}