forked from cjdelisle/cjdns
/
List.c
139 lines (126 loc) · 3.69 KB
/
List.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
139
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "memory/Allocator.h"
#include "benc/List.h"
int32_t List_size(const List* list)
{
if (list != NULL) {
struct List_Item* item = *list;
int32_t i;
for (i = 0; item != NULL; i++) {
item = item->next;
}
return i;
}
return -1;
}
static List* addObject(List* list, Object* item, const struct Allocator* allocator)
{
if (list == NULL) {
List* newList = allocator->calloc(sizeof(List), 1, allocator);
return addObject(newList, item, allocator);
}
struct List_Item* entry = allocator->malloc(sizeof(struct List_Item), allocator);
entry->next = *list;
entry->elem = item;
*list = entry;
return list;
}
/** @see Object.h */
List* List_addInt(List* list, int64_t toAdd, const struct Allocator* allocator)
{
Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
.type = Object_INTEGER,
.as.number = toAdd
});
return addObject(list, obj, allocator);
}
/** @see Object.h */
List* List_addString(List* list, String* toAdd, const struct Allocator* allocator)
{
Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
.type = Object_STRING,
.as.string = toAdd
});
return addObject(list, obj, allocator);
}
/** @see Object.h */
List* List_addDict(List* list, Dict* toAdd, const struct Allocator* allocator)
{
Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
.type = Object_DICT,
.as.dictionary = toAdd
});
return addObject(list, obj, allocator);
}
/** @see Object.h */
List* List_addList(List* list, List* toAdd, const struct Allocator* allocator)
{
Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
.type = Object_LIST,
.as.list = toAdd
});
return addObject(list, obj, allocator);
}
static Object* getObject(const List* list, uint32_t index)
{
if (list != NULL && *list != NULL) {
struct List_Item* entry = *list;
uint32_t i;
for (i = 0; entry != NULL; i++) {
if (i == index) {
return entry->elem;
}
entry = entry->next;
}
}
return NULL;
}
/** @see Object.h */
int64_t* List_getInt(const List* list, uint32_t index)
{
Object* o = getObject(list, index);
if (o != NULL && o->type == Object_INTEGER) {
return &(o->as.number);
}
return NULL;
}
/** @see Object.h */
String* List_getString(const List* list, uint32_t index)
{
Object* o = getObject(list, index);
if (o != NULL && o->type == Object_STRING) {
return o->as.string;
}
return NULL;
}
/** @see Object.h */
Dict* List_getDict(const List* list, uint32_t index)
{
Object* o = getObject(list, index);
if (o != NULL && o->type == Object_DICT) {
return o->as.dictionary;
}
return NULL;
}
/** @see Object.h */
List* List_getList(const List* list, uint32_t index)
{
Object* o = getObject(list, index);
if (o != NULL && o->type == Object_LIST) {
return o->as.list;
}
return NULL;
}