linkedlist.c
2.97 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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* Copyright (C) 2008 Banco do Brasil S.A.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
*/
/**
* @brief Handle linked lists.
*/
#include <lib3270.h>
#include <lib3270/log.h>
#include <linkedlist.h>
#include <string.h>
#include <errno.h>
/*---[ Implement ]------------------------------------------------------------------------------------------------------------*/
void * lib3270_linked_list_append_node(struct lib3270_linked_list_head *head, size_t szBlock, void *userdata) {
struct lib3270_linked_list_node * node = lib3270_malloc(szBlock);
memset(node,0,szBlock);
node->userdata = userdata;
if(head->last) {
head->last->next = node;
node->prev = head->last;
} else {
head->first = node;
}
head->last = node;
/*
#ifdef DEBUG
{
struct lib3270_linked_list_node * dCurrent;
debug("%s: head=%p first=%p last=%p", __FUNCTION__, head, head->first, head->last);
for(dCurrent = head->first; dCurrent; dCurrent = dCurrent->next)
{
debug("node=%p prev=%p next=%p",dCurrent,dCurrent->prev,dCurrent->next);
}
}
#endif // DEBUG
*/
return (void *) node;
}
int lib3270_linked_list_delete_node(struct lib3270_linked_list_head *head, const void *node) {
struct lib3270_linked_list_node * current;
for(current = head->first; current; current = current->next) {
if(current == node) {
if(current->prev)
current->prev->next = current->next;
else
head->first = current->next;
if(current->next)
current->next->prev = current->prev;
else
head->last = current->prev;
lib3270_free(current);
/*
#ifdef DEBUG
{
struct lib3270_linked_list_node * dCurrent;
debug("%s: head=%p first=%p last=%p", __FUNCTION__, head, head->first, head->last);
for(dCurrent = head->first; dCurrent; dCurrent = dCurrent->next)
{
debug("node=%p prev=%p next=%p",dCurrent,dCurrent->prev,dCurrent->next);
}
}
#endif // DEBUG
*/
return 0;
}
}
return errno = ENOENT;
}
void lib3270_linked_list_free(struct lib3270_linked_list_head *head) {
struct lib3270_linked_list_node * node = head->first;
while(node) {
void * ptr = (void *) node;
node = node->next;
lib3270_free(ptr);
}
head->first = head->last = NULL;
}