summaryrefslogtreecommitdiff
path: root/attic/historic/anode/libanode/impl/dictionary.h
blob: 48e1642a01f7cb43f819777bcb9d0d293c71dd8f (plain)
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
/* libanode: the Anode C reference implementation
 * Copyright (C) 2009-2010 Adam Ierymenko <adam.ierymenko@gmail.com>
 *
 * This program 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.
 *
 * 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 General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */

/* This is a simple string hash table suitable for small tables such as zone
 * files or HTTP header lists. */

#ifndef _ANODE_DICTIONARY_H
#define _ANODE_DICTIONARY_H

#include "misc.h"

/* This is a fixed hash table and is designed for relatively small numbers
 * of keys for things like zone files. */
#define ANODE_DICTIONARY_FIXED_HASH_TABLE_SIZE 16
#define ANODE_DICTIONARY_FIXED_HASH_TABLE_MASK 15

/* Computes a hash code for a string and returns the hash bucket */
static inline unsigned int AnodeDictionary__get_bucket(const char *s)
{
  unsigned int hc = 3;
  while (*s)
    hc = ((hc << 4) + hc) + (unsigned int)*(s++);
  return ((hc ^ (hc >> 4)) & ANODE_DICTIONARY_FIXED_HASH_TABLE_MASK);
}
/* Case insensitive version of get_bucket */
static inline unsigned int AnodeDictionary__get_bucket_ci(const char *s)
{
  unsigned int hc = 3;
  while (*s)
    hc = ((hc << 4) + hc) + (unsigned int)Anode_tolower(*(s++));
  return ((hc ^ (hc >> 4)) & ANODE_DICTIONARY_FIXED_HASH_TABLE_MASK);
}

struct AnodeDictionaryEntry
{
  char *key;
  char *value;
  struct AnodeDictionaryEntry *next;
};

struct AnodeDictionary
{
  struct AnodeDictionaryEntry *ht[ANODE_DICTIONARY_FIXED_HASH_TABLE_SIZE];
  unsigned int size;
  int case_sensitive;
};

static inline void AnodeDictionary_init(struct AnodeDictionary *d,int case_sensitive)
{
  Anode_zero((void *)d,sizeof(struct AnodeDictionary));
  d->case_sensitive = case_sensitive;
}

void AnodeDictionary_clear(struct AnodeDictionary *d);

static inline void AnodeDictionary_destroy(struct AnodeDictionary *d)
{
  AnodeDictionary_clear(d);
}

void AnodeDictionary_put(struct AnodeDictionary *d,const char *key,const char *value);

static inline const char *AnodeDictionary_get(struct AnodeDictionary *d,const char *key)
{
  struct AnodeDictionaryEntry *e;
  unsigned int bucket = (d->case_sensitive) ? AnodeDictionary__get_bucket(key) : AnodeDictionary__get_bucket_ci(key);

  e = d->ht[bucket];
  while (e) {
    if ((d->case_sensitive ? Anode_streq(key,e->key) : Anode_strcaseeq(key,e->key)))
      return e->value;
    e = e->next;
  }

  return (const char *)0;
}

static inline void AnodeDictionary_iterate(
  struct AnodeDictionary *d,
  void *arg,
  int (*func)(void *,const char *,const char *))
{
  struct AnodeDictionaryEntry *e;
  unsigned int bucket;

  for(bucket=0;bucket<ANODE_DICTIONARY_FIXED_HASH_TABLE_SIZE;++bucket) {
    e = d->ht[bucket];
    while (e) {
      if (!func(arg,e->key,e->value))
        return;
      e = e->next;
    }
  }
}

void AnodeDictionary_read(
  struct AnodeDictionary *d,
  char *in,
  const char *line_breaks,
  const char *kv_breaks,
  const char *comment_chars,
  char escape_char,
  int trim_whitespace_from_keys,
  int trim_whitespace_from_values);

long AnodeDictionary_write(
  struct AnodeDictionary *d,
  char *out,
  long out_size,
  const char *line_break,
  const char *kv_break);

#endif