forked from 4ZM/mfterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.c
105 lines (87 loc) · 2.53 KB
/
dictionary.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
/**
* Copyright (C) 2011 Anders Sundman <[email protected]>
*
* This file is part of mfterm.
*
* mfterm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mfterm 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 General Public License
* along with mfterm. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dictionary.h"
static key_list_t* key_list = NULL;
key_list_t* kl_add(key_list_t** list, const uint8_t* key);
void kl_clear(key_list_t** list);
key_list_t* kl_make_node(const uint8_t* key);
int key_cmp(const uint8_t* k1, const uint8_t* k2);
void dictionary_clear() {
kl_clear(&key_list);
}
int dictionary_add(const uint8_t* key) {
return kl_add(&key_list, key) != NULL;
}
key_list_t* dictionary_get() {
return key_list;
}
// Append a node to the list. Don't append duplicates, O(n) operation.
key_list_t* kl_add(key_list_t** list, const uint8_t* key) {
if (list == NULL)
return NULL;
// A new list
if (*list == NULL)
return *list = kl_make_node(key);
// Append (but no duplicates)
key_list_t* it = *list;
key_list_t* last = NULL;
while(it) {
// Don't add duplicates, but move the key first in the list
if (key_cmp(it->key, key) == 0) {
if (last) {
last->next = it->next; // disconnect it
it->next = *list; // move it fisrt
*list = it; // re-point the head
}
return NULL; // Return null on duplicates
}
last = it;
it = it->next;
}
return last->next = kl_make_node(key);
}
void kl_clear(key_list_t** list) {
if (list == NULL || *list == NULL)
return;
// Free the list nodes
key_list_t* it = *list;
do {
key_list_t* next = it->next;
free(it);
it = next;
} while(it);
*list = 0;
}
key_list_t* kl_make_node(const uint8_t* key) {
// Create the list node
key_list_t* new_node = (key_list_t*) malloc(sizeof(key_list_t));
memcpy((void*)new_node, key, 6);
new_node->next = NULL;
return new_node;
}
int key_cmp(const uint8_t* k1, const uint8_t* k2) {
for (int i = 0; i < 6; ++i) {
if (k1[i] != k2[i])
return -1;
}
return 0;
}