forked from OpenGridForum/drmaav2-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drmaa2-dict.c
138 lines (124 loc) · 3.25 KB
/
drmaa2-dict.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "drmaa2-dict.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
drmaa2_dict drmaa2_dict_create(const drmaa2_dict_entryfree callback)
{
drmaa2_dict d = (drmaa2_dict)malloc(sizeof(drmaa2_dict_s));
d->free_entry = callback;
d->head = NULL;
return d;
}
void drmaa2_dict_free(drmaa2_dict * dRef)
{
drmaa2_dict d = *dRef;
if (d == NULL)
{
return;
}
drmaa2_dict_item head = d->head;
drmaa2_dict_item tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
if (d->free_entry != NULL)
d->free_entry((char **)&(tmp->key), (char **)&(tmp->value));
free(tmp);
}
free(d);
*dRef = NULL;
}
drmaa2_string_list drmaa2_dict_list(const drmaa2_dict d)
{
// TODO implement
return NULL;
}
drmaa2_bool drmaa2_dict_has(const drmaa2_dict d, const char * key)
{
drmaa2_dict_item current_item = d->head;
while (current_item != NULL)
{
if (!strcmp(current_item->key, key))
{
// found
return DRMAA2_TRUE;
}
current_item = current_item->next;
}
return DRMAA2_FALSE;
}
const char * drmaa2_dict_get(const drmaa2_dict d, const char * key)
{
drmaa2_dict_item current_item = d->head;
while (current_item != NULL)
{
if (!strcmp(current_item->key, key))
{
// found -> return
return current_item->value;
}
current_item = current_item->next;
}
return NULL;
}
drmaa2_error drmaa2_dict_del(drmaa2_dict d, const char * key)
{
drmaa2_dict_item current_item = d->head;
drmaa2_dict_item prev = NULL;
while (current_item != NULL)
{
if (!strcmp(current_item->key, key))
{
// found -> delete
if (prev)
{
prev->next = current_item->next;
}
else
{
d->head = current_item->next;
}
if (d->free_entry != NULL) d->free_entry((char **)&(current_item->key), (char **)&(current_item->value));
free(current_item);
return DRMAA2_SUCCESS;
}
prev = current_item;
current_item = current_item->next;
}
return DRMAA2_INVALID_ARGUMENT;
}
drmaa2_error drmaa2_dict_set(drmaa2_dict d, const char * key, const char * val)
{
if (d->head == NULL)
{
//empty dict
drmaa2_dict_item new = (drmaa2_dict_item)malloc(sizeof(drmaa2_dict_item_s));
new->key = key;
new->value = val;
new->next = NULL;
d->head = new;
return DRMAA2_SUCCESS;
}
drmaa2_dict_item current_item = d->head;
drmaa2_dict_item prev = NULL;
while (current_item != NULL)
{
if (!strcmp(current_item->key, key))
{
// found -> replace
if (d->free_entry != NULL) d->free_entry(NULL, (char **)&(current_item->value));
current_item->value = val;
return DRMAA2_SUCCESS;
}
prev = current_item;
current_item = current_item->next;
}
drmaa2_dict_item new = (drmaa2_dict_item)malloc(sizeof(drmaa2_dict_item_s));
new->key = key;
new->value = val;
new->next = NULL;
prev->next = new;
return DRMAA2_FALSE;
}