-
Notifications
You must be signed in to change notification settings - Fork 0
/
glibwrap.c
123 lines (105 loc) · 2.1 KB
/
glibwrap.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
/*
* glibwrap.c
*
* Created on: 14/07/2015
* Author: thborges
*/
#include "glibwrap.h"
void g_free(void *pointer) {
free(pointer);
}
void *g_memdup(const void *mem, unsigned int byte_size) {
void *result = g_new(unsigned char, byte_size);
return memcpy(result, mem, byte_size);
}
GList *g_new_list(void *data) {
GList *lst = g_new0(GList, 1);
lst->data = data;
return lst;
}
GList *g_list_append(GList *lst, void *data) {
if (lst == NULL)
return g_new_list(data);
GList *last = lst;
while(last->next)
last = last->next;
GList *newi = g_new(GList, 1);
newi->data = data;
newi->next = NULL;
newi->priour = last;
last->next = newi;
return lst;
}
GList *g_list_concat(GList *lst, GList *lst2) {
GList *last = lst;
while(last->next)
last = last->next;
last->next = lst2;
lst2->priour = last;
return last;
}
GList *g_list_prepend(GList *lst, void *data) {
GList *newi = g_new(GList, 1);
newi->next = lst;
newi->priour = NULL;
newi->data = data;
if (lst)
lst->priour = newi;
return newi;
}
GList *g_list_find(GList *lst, void *data) {
while(lst) {
if (lst->data == data) return lst;
lst = lst->next;
}
return NULL;
}
GList *g_list_remove(GList *lst, void *data) {
GList *link = g_list_find(lst, data);
if (link) {
if (link->next)
link->next->priour = link->priour;
if (link->priour)
link->priour->next = link->next;
else
lst = link->next;
}
return lst;
}
GList *g_list_remove_link(GList *lst, GList *link) {
if (link->next)
link->next->priour = link->priour;
if (link->priour)
link->priour->next = link->next;
else
lst = link->next;
return lst;
}
GList *g_list_delete_link(GList *lst, GList *link) {
lst = g_list_remove_link(lst, link);
g_free(link);
return lst;
}
void g_list_free_full(GList *lst, void (*destroy)(void *data)) {
while (lst) {
GList *aux = lst;
lst = lst->next;
destroy(aux->data);
g_free(aux);
}
}
void g_list_free(GList *lst) {
while (lst) {
GList *aux = lst;
lst = lst->next;
g_free(aux);
}
}
unsigned g_list_length(GList *lst) {
unsigned result = 0;
while (lst) {
result++;
lst = lst->next;
}
return result;
}