| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2003, 2004, 2005, 2007 Antti-Juhani Kaijanaho |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
#include <ctype.h> |
| 21 |
#include <string.h> |
| 22 |
#include "fieldtrie.h" |
| 23 |
#include "msg.h" |
| 24 |
|
| 25 |
struct field_bucket { |
| 26 |
struct field_attr attr; |
| 27 |
struct field_bucket * next; |
| 28 |
}; |
| 29 |
|
| 30 |
struct fieldtrie_private { |
| 31 |
size_t nextfree; |
| 32 |
/* A one-level trie listing all field names occurring in the |
| 33 |
* atomic predicates. */ |
| 34 |
struct field_bucket * fields[UCHAR_MAX]; |
| 35 |
/* An array mapping each field inx to its attributes */ |
| 36 |
struct field_attr * field_map[65536]; |
| 37 |
}; |
| 38 |
|
| 39 |
static struct fieldtrie_private trie; |
| 40 |
|
| 41 |
void fieldtrie_init(void) |
| 42 |
{ |
| 43 |
for (size_t i = 0; i < UCHAR_MAX; i++) { |
| 44 |
trie.fields[i] = 0; |
| 45 |
} |
| 46 |
trie.nextfree = 0; |
| 47 |
} |
| 48 |
|
| 49 |
struct field_attr *fieldtrie_insert_n(char const * s, size_t slen) |
| 50 |
{ |
| 51 |
struct field_attr *l_attr = fieldtrie_lookup(s, slen); |
| 52 |
if (l_attr != 0) return l_attr; |
| 53 |
struct field_bucket * b = malloc(sizeof *b); |
| 54 |
if (b == 0) fatal_enomem(0); |
| 55 |
char *name = malloc(slen+1); |
| 56 |
if (name == 0) fatal_enomem(0); |
| 57 |
strncpy(name, s, slen); |
| 58 |
(name)[slen] = '\0'; |
| 59 |
*(char**)&b->attr.name = name; |
| 60 |
*(size_t*)&b->attr.namelen = slen; |
| 61 |
assert(trie.nextfree < sizeof trie.field_map / sizeof *trie.field_map); |
| 62 |
*(size_t*)&b->attr.inx = trie.nextfree++; |
| 63 |
b->attr.is_show_field = 0; |
| 64 |
b->attr.backup_field = (size_t)-1; |
| 65 |
unsigned char c = tolower((unsigned char)(b->attr.name[0])); |
| 66 |
b->next = trie.fields[c]; |
| 67 |
trie.fields[c] = b; |
| 68 |
trie.field_map[b->attr.inx] = &b->attr; |
| 69 |
return &b->attr; |
| 70 |
} |
| 71 |
|
| 72 |
struct field_attr *fieldtrie_insert(char const * s) |
| 73 |
{ |
| 74 |
return fieldtrie_insert_n(s, strlen(s)); |
| 75 |
} |
| 76 |
|
| 77 |
struct field_attr *fieldtrie_lookup(char const * s, size_t n) |
| 78 |
{ |
| 79 |
for (struct field_bucket * b = trie.fields[tolower((unsigned char)s[0])]; |
| 80 |
b != 0; |
| 81 |
b = b->next) { |
| 82 |
if (n == b->attr.namelen && |
| 83 |
strncasecmp(s, b->attr.name, n) == 0) return &b->attr; |
| 84 |
} |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
size_t fieldtrie_count(void) |
| 89 |
{ |
| 90 |
return trie.nextfree; |
| 91 |
} |
| 92 |
|
| 93 |
struct field_attr *fieldtrie_get(size_t inx) |
| 94 |
{ |
| 95 |
assert(inx < sizeof trie.field_map / sizeof *trie.field_map); |
| 96 |
return trie.field_map[inx]; |
| 97 |
} |
| 98 |
|
| 99 |
#if 0 |
| 100 |
void fieldtrie_clear(void) |
| 101 |
{ |
| 102 |
for (size_t i = 0; i < UCHAR_MAX; i++) { |
| 103 |
struct field_bucket * b = trie.fields[i]; |
| 104 |
while (b != 0) { |
| 105 |
struct field_bucket * bn = b->next; |
| 106 |
free(b); |
| 107 |
b = bn; |
| 108 |
} |
| 109 |
trie->fields[i] = 0; |
| 110 |
} |
| 111 |
trie->nextfree = 0; |
| 112 |
} |
| 113 |
#endif |