forked from DC-SWAT/DreamShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
148 lines (93 loc) · 2.55 KB
/
Copy pathlist.c
File metadata and controls
148 lines (93 loc) · 2.55 KB
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
140
141
142
143
144
145
146
147
148
/****************************
* DreamShell ##version## *
* list.c *
* DreamShell list manager *
* Created by SWAT *
* http://www.dc-swat.ru *
***************************/
#include <kos.h>
#include <stdlib.h>
#include "list.h"
Item_list_t *listMake() {
Item_list_t *l;
l = (Item_list_t *) calloc(1, sizeof(Item_list_t));
if(l == NULL)
return NULL;
SLIST_INIT(l);
return l;
}
void listDestroy(Item_list_t *lst, listFreeItemFunc *ifree) {
Item_t *c, *n;
c = SLIST_FIRST(lst);
while(c) {
n = SLIST_NEXT(c, list);
if(ifree != NULL)
ifree(c->data);
free(c);
c = n;
}
SLIST_INIT(lst);
free(lst);
}
static uint32 listLastId = 0;
uint32 listGetLastId(Item_list_t *lst) {
return listLastId;
}
Item_t *listAddItem(Item_list_t *lst, ListItemType type, const char *name, void *data, uint32 size) {
Item_t *i = NULL;
i = (Item_t *) calloc(1, sizeof(Item_t));
if(i == NULL)
return NULL;
i->name = name;
i->type = type;
i->id = ++listLastId;
i->data = data;
i->size = size;
//printf("List added item with id=%d\n", i->id);
SLIST_INSERT_HEAD(lst, i, list);
return i;
}
void listRemoveItem(Item_list_t *lst, Item_t *i, listFreeItemFunc *ifree) {
SLIST_REMOVE(lst, i, Item, list);
if(ifree != NULL)
ifree(i->data);
free(i);
}
Item_t *listGetItemByName(Item_list_t *lst, const char *name) {
Item_t *i;
SLIST_FOREACH(i, lst, list) {
if(!strcasecmp(name, i->name))
return i;
}
return NULL;
}
Item_t *listGetItemByNameAndType(Item_list_t *lst, const char *name, ListItemType type) {
Item_t *i;
SLIST_FOREACH(i, lst, list) {
if(i->type == type && !strcmp(name, i->name))
return i;
}
return NULL;
}
Item_t *listGetItemByType(Item_list_t *lst, ListItemType type) {
Item_t *i;
SLIST_FOREACH(i, lst, list) {
if(i->type == type)
return i;
}
return NULL;
}
Item_t *listGetItemById(Item_list_t *lst, uint32 id) {
Item_t *i;
SLIST_FOREACH(i, lst, list) {
if(id == i->id)
return i;
}
return NULL;
}
Item_t *listGetItemFirst(Item_list_t *lst) {
return SLIST_FIRST(lst);
}
Item_t *listGetItemNext(Item_t *i) {
return SLIST_NEXT(i, list);
}