Debian Codegraph @

src/table.c

raw
1 /**
2 * This file is part of the Detox package.
3 *
4 * Copyright (c) Doug Harple <[email protected]>
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 */
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "table.h"
14 #include "wrapped.h"
15
16 /*
17 * Internal function declarations
18 */
19 static int table_hash(int table_length, unsigned int key);
20
21 table_t *table_init(int max_rows)
22 {
23 table_t *ret;
24 size_t row_length;
25
26 if (max_rows <= 0) {
27 max_rows = 500;
28 }
29
30 row_length = max_rows * sizeof(table_row_t);
31
32 ret = wrapped_malloc(sizeof(table_t));
33
34 memset(ret, 0, sizeof(table_t));
35
36 ret->rows = wrapped_malloc(row_length);
37
38 memset(ret->rows, 0, row_length);
39
40 ret->length = max_rows;
41 ret->use_hash = 1;
42
43 return ret;
44 }
45
46 table_t *table_resize(table_t *table, int rows, int use_hash)
47 {
48 table_t *ret;
49 int i;
50
51 ret = table_init(rows);
52
53 if (ret == NULL) {
54 return table;
55 }
56
57 ret->use_hash = use_hash;
58
59 if (table == NULL) {
60 return ret;
61 }
62
63 if (table->default_translation != NULL) {
64 ret->default_translation = wrapped_strdup(table->default_translation);
65 }
66
67 ret->overwrites = table->overwrites;
68
69 for (i = 0; i < table->length; i++) {
70 if (table->rows[i].key > 0 && table->rows[i].data != NULL) {
71 table_put(ret, table->rows[i].key, table->rows[i].data);
72 }
73 }
74
75 return ret;
76 }
77
78 void table_free(table_t *table)
79 {
80 int i;
81
82 if (table == NULL || table->builtin == 1) {
83 return;
84 }
85
86 for (i = 0; i < table->length; i++) {
87 if (table->rows[i].key > 0 && table->rows[i].data != NULL) {
88 free(table->rows[i].data);
89 }
90 }
91
92 if (table->default_translation != NULL) {
93 free(table->default_translation);
94 }
95
96 free(table->rows);
97 free(table);
98 }
99
100 static int table_hash(int table_length, unsigned int key)
101 {
102 return key % table_length;
103 }
104
105 /**
106 * Puts a row on the table.
107 *
108 * Key cannot be zero.
109 *
110 * @param table_t * table The table to use.
111 * @param unsigned int key The key for the table.
112 * @param char * data The data to store for the key.
113 *
114 * @return int The stored location, or -1 if an error occurred.
115 */
116 int table_put(table_t *table, unsigned int key, char *data)
117 {
118 int offset;
119 int seek;
120 int i;
121
122 if (table == NULL || key == 0) {
123 return -1;
124 }
125
126 if (table->length == table->used) {
127 return -1;
128 }
129
130 offset = -1;
131
132 if (table->use_hash) {
133 seek = table_hash(table->length, key);
134
135 if (table->rows[seek].key == 0 || table->rows[seek].key == key) {
136 offset = seek;
137 }
138 }
139
140 if (offset == -1) {
141 for (i = 0; i < table->length; i++) {
142 if (table->rows[i].key == 0 || table->rows[i].key == key) {
143 offset = i;
144 break;
145 }
146 }
147
148 if (offset == -1) {
149 return -1;
150 }
151 }
152
153 if (table->rows[offset].key == key) {
154 table->overwrites++;
155 }
156
157 table->rows[offset].key = key;
158 table->rows[offset].data = wrapped_strdup(data);
159 table->used++;
160
161 if (table->max_key < key) {
162 table->max_key = key;
163 }
164
165 if (table->max_data_length < strlen(data)) {
166 table->max_data_length = strlen(data);
167 }
168
169 return offset;
170 }
171
172 /**
173 * Gets a row from the table.
174 *
175 * Key cannot be zero.
176 *
177 * @param table The table to use.
178 * @param key The key for the table.
179 *
180 * @return The value from the table, or NULL if it could not be found.
181 */
182 char *table_get(table_t *table, unsigned int key)
183 {
184 int offset;
185 int seek;
186 int i;
187
188 if (table == NULL || key == 0) {
189 return NULL;
190 }
191
192 offset = -1;
193
194 if (table->use_hash) {
195 seek = table_hash(table->length, key);
196
197 if (table->rows[seek].key == key) {
198 offset = seek;
199 }
200 }
201
202 if (offset == -1) {
203 table->seeks++;
204
205 for (i = 0; i < table->length; i++) {
206 if (table->rows[i].key == key) {
207 offset = i;
208 break;
209 }
210 }
211
212 if (offset == -1) {
213 table->misses++;
214 return NULL;
215 }
216 }
217
218 table->hits++;
219
220 return table->rows[offset].data;
221 }