-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
136 lines (107 loc) · 2.51 KB
/
Copy pathlist.c
File metadata and controls
136 lines (107 loc) · 2.51 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "list.h"
LISTNODE *AddNode(LIST *list, void *data, unsigned long sz)
{
LISTNODE *node;
if (!(list))
return NULL;
if (!(list->head))
{ /*uninitialized list! set up head & tail nodes.*/
list->head = malloc(sizeof(LISTNODE));
if (!(list->head))
return NULL;
list->tail = list->head; /*initial degenerate case, head==tail*/
node = list->tail;
}
else
{
list->tail->next = malloc(sizeof(LISTNODE)); /*make room for the next one*/
if (!(list->tail->next))
return NULL;
list->tail = list->tail->next; /*tail must always point to the last node*/
node = list->tail; /*tail is always the last node by definition*/
}
memset(node, 0x00, sizeof(LISTNODE));
node->data = malloc(sz);
memset(node->data, 0x00, sz);
node->sz = sz;
memcpy(node->data, data, sz);
return node;
}
void DeleteNode(LIST *list, LISTNODE *node)
{
LISTNODE *del;
if (!(list))
return;
if (node == list->head)
{
list->head = node->next;
free(node->data);
free(node);
return;
}
del = list->head;
while (del != NULL && del->next != node)
del = del->next;
if (!(del))
return;
if (del->next == list->tail)
list->tail = del;
del->next = del->next->next;
free(node->data);
free(node);
}
LISTNODE *FindNodeByRef(LIST *list, void *data)
{
LISTNODE *node;
if (!(list))
return NULL;
node = list->head;
while ((node))
{
if (node->data == data)
return node;
node = node->next;
}
return NULL;
}
LISTNODE *FindNodeByValue(LIST *list, void *data, unsigned long sz)
{
LISTNODE *node;
if (!(list))
return NULL;
node = list->head;
while ((node))
{
if (!memcmp(node->data, data, (sz > node->sz) ? node->sz : sz))
return node;
node = node->next;
}
return NULL;
}
/*frees all nodes in a list, including their data*/
void FreeNodes(LIST *list, int FreeParameterAsWell)
{
LISTNODE *node, *previous;
if (!(list))
return;
node = list->head;
while (node)
{
free(node->data);
previous = node;
node = node->next;
free(previous);
}
if (FreeParameterAsWell)
free(list);
return;
}
void FreeList(LIST *list)
{
FreeNodes(list, 0);
return;
}