1 | /*
|
---|
2 | cl_list.c
|
---|
3 | Implementation of a linked list of card-terminals
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 Carlos Prados <cprados@yahoo.com>
|
---|
7 |
|
---|
8 | This version is modified by doz21 to work in a special manner ;)
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 2 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include "ct_list.h"
|
---|
27 |
|
---|
28 | /*
|
---|
29 | * Exported functions definition
|
---|
30 | */
|
---|
31 |
|
---|
32 | extern CT_List *
|
---|
33 | CT_List_New (void)
|
---|
34 | {
|
---|
35 | CT_List *aux;
|
---|
36 |
|
---|
37 | aux = (CT_List *) malloc (sizeof (CT_List));
|
---|
38 | if (aux != NULL)
|
---|
39 | {
|
---|
40 | aux->first = NULL;
|
---|
41 | aux->last = NULL;
|
---|
42 | aux->elements = 0;
|
---|
43 | }
|
---|
44 | return (aux);
|
---|
45 | }
|
---|
46 |
|
---|
47 | extern bool
|
---|
48 | CT_List_AddCardTerminal (CT_List * list, CardTerminal * ct, unsigned short ctn)
|
---|
49 | {
|
---|
50 | struct CT_List_Node *node;
|
---|
51 |
|
---|
52 | if (list == NULL)
|
---|
53 | return FALSE;
|
---|
54 |
|
---|
55 | node = (struct CT_List_Node *) malloc (sizeof (struct CT_List_Node));
|
---|
56 | if (node != NULL)
|
---|
57 | {
|
---|
58 | node->ct = ct;
|
---|
59 | node->ctn = ctn;
|
---|
60 | node->next = NULL;
|
---|
61 | if (list->first == NULL)
|
---|
62 | {
|
---|
63 | list->first = node;
|
---|
64 | list->last = node;
|
---|
65 | }
|
---|
66 | else
|
---|
67 | {
|
---|
68 | list->last->next = node;
|
---|
69 | list->last = node;
|
---|
70 | }
|
---|
71 | list->elements++;
|
---|
72 | }
|
---|
73 |
|
---|
74 | return (node != NULL);
|
---|
75 | }
|
---|
76 |
|
---|
77 | extern CardTerminal *
|
---|
78 | CT_List_GetCardTerminal (CT_List * list, unsigned short ctn)
|
---|
79 | {
|
---|
80 | struct CT_List_Node *node;
|
---|
81 | CardTerminal *ct = NULL;
|
---|
82 |
|
---|
83 | if (list == NULL)
|
---|
84 | return NULL;
|
---|
85 |
|
---|
86 | for (node = list->first; (node != NULL) && (ct == NULL); node = node->next)
|
---|
87 | if (node->ctn == ctn)
|
---|
88 | ct = node->ct;
|
---|
89 |
|
---|
90 | return ct;
|
---|
91 | }
|
---|
92 |
|
---|
93 | extern int
|
---|
94 | CT_List_GetNumberOfElements (CT_List * list)
|
---|
95 | {
|
---|
96 | if (list == NULL)
|
---|
97 | return 0;
|
---|
98 |
|
---|
99 | return list->elements;
|
---|
100 | }
|
---|
101 |
|
---|
102 | extern bool
|
---|
103 | CT_List_RemoveCardTerminal (CT_List * list, unsigned short ctn)
|
---|
104 | {
|
---|
105 | struct CT_List_Node *current, *previous;
|
---|
106 | bool found;
|
---|
107 |
|
---|
108 | if (list == NULL)
|
---|
109 | return FALSE;
|
---|
110 |
|
---|
111 | previous = NULL;
|
---|
112 | current = list->first;
|
---|
113 | found = FALSE;
|
---|
114 |
|
---|
115 | while ((current != NULL) && (!found))
|
---|
116 | {
|
---|
117 | if (current->ctn == ctn)
|
---|
118 | found = TRUE;
|
---|
119 | else
|
---|
120 | {
|
---|
121 | previous = current;
|
---|
122 | current = current->next;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (found)
|
---|
127 | {
|
---|
128 | if (current == list->first)
|
---|
129 | list->first = current->next;
|
---|
130 | else
|
---|
131 | previous->next = current->next;
|
---|
132 |
|
---|
133 | if (current == list->last)
|
---|
134 | list->last = previous;
|
---|
135 |
|
---|
136 | CardTerminal_Delete (current->ct);
|
---|
137 | free (current);
|
---|
138 | list->elements--;
|
---|
139 | }
|
---|
140 |
|
---|
141 | return found;
|
---|
142 | }
|
---|
143 |
|
---|
144 | extern void
|
---|
145 | CT_List_Delete (CT_List * list)
|
---|
146 | {
|
---|
147 | struct CT_List_Node *node;
|
---|
148 |
|
---|
149 | if (list == NULL)
|
---|
150 | return;
|
---|
151 |
|
---|
152 | while (list->first != NULL)
|
---|
153 | {
|
---|
154 | node = list->first;
|
---|
155 | list->first = list->first->next;
|
---|
156 | CardTerminal_Delete (node->ct);
|
---|
157 | free (node);
|
---|
158 | }
|
---|
159 | free (list);
|
---|
160 | }
|
---|