NetSurf
hashtable.h
Go to the documentation of this file.
1/*
2 * Copyright 2006 Rob Kendrick <rjek@rjek.com>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Interface to Write-Once hash table for string to string mapping
22 */
23
24#ifndef _NETSURF_UTILS_HASHTABLE_H_
25#define _NETSURF_UTILS_HASHTABLE_H_
26
27#include <stdbool.h>
28
29struct hash_table;
30
31/**
32 * Create a new hash table
33 *
34 * Allocate a new hash table and return a context for it. The memory
35 * consumption of a hash table is approximately 8 + (nchains * 12)
36 * bytes if it is empty.
37 *
38 * \param chains Number of chains/buckets this hash table will have. This
39 * should be a prime number, and ideally a prime number just
40 * over a power of two, for best performance and distribution.
41 * \return struct hash_table containing the context of this hash table or NULL
42 * if there is insufficent memory to create it and its chains.
43 */
44struct hash_table *hash_create(unsigned int chains);
45
46/**
47 * Destroys a hash table
48 *
49 * Destroy a hash table freeing all memory associated with it.
50 *
51 * \param ht Hash table to destroy. After the function returns, this
52 * will no longer be valid.
53 */
54void hash_destroy(struct hash_table *ht);
55
56/**
57 * Adds a key/value pair to a hash table.
58 *
59 * If the key you're adding is already in the hash table, it does not
60 * replace it, but it does take precedent over it. The old key/value
61 * pair will be inaccessable but still in memory until hash_destroy()
62 * is called on the hash table.
63 *
64 * \param ht The hash table context to add the key/value pair to.
65 * \param key The key to associate the value with. A copy is made.
66 * \param value The value to associate the key with. A copy is made.
67 * \return true if the add succeeded, false otherwise. (Failure most likely
68 * indicates insufficent memory to make copies of the key and value.
69 */
70bool hash_add(struct hash_table *ht, const char *key, const char *value);
71
72/**
73 * Looks up a the value associated with with a key from a specific hash table.
74 *
75 * \param ht The hash table context to look up the key in.
76 * \param key The key to search for.
77 * \return The value associated with the key, or NULL if it was not found.
78 */
79const char *hash_get(struct hash_table *ht, const char *key);
80
81/**
82 * Add key/value pairs to a hash table with data from a file
83 *
84 * The file should be formatted as a series of lines terminated with
85 * newline character. Each line should contain a key/value pair
86 * separated by a colon. If a line is empty or starts with a #
87 * character it will be ignored.
88 *
89 * The file may be optionally gzip compressed.
90 *
91 * \param ht The hash table context to add the key/value pairs to.
92 * \param path Path to file with key/value pairs in.
93 * \return NSERROR_OK on success else error code
94 */
95nserror hash_add_file(struct hash_table *ht, const char *path);
96
97/**
98 * Add key/value pairs to a hash table with data from a memory buffer
99 *
100 * The data format is the same as in hash_add_file() but held in memory
101 *
102 * The data may optionally be gzip compressed.
103 *
104 * \param ht The hash table context to add the key/value pairs to.
105 * \param data Source of key/value pairs
106 * \param size length of \a data
107 * \return NSERROR_OK on success else error code
108 */
109nserror hash_add_inline(struct hash_table *ht, const uint8_t *data, size_t size);
110
111#endif
nserror
Enumeration of error codes.
Definition: errors.h:29
struct hash_table * hash_create(unsigned int chains)
Create a new hash table.
Definition: hashtable.c:254
bool hash_add(struct hash_table *ht, const char *key, const char *value)
Adds a key/value pair to a hash table.
Definition: hashtable.c:303
nserror hash_add_inline(struct hash_table *ht, const uint8_t *data, size_t size)
Add key/value pairs to a hash table with data from a memory buffer.
Definition: hashtable.c:399
nserror hash_add_file(struct hash_table *ht, const char *path)
Add key/value pairs to a hash table with data from a file.
Definition: hashtable.c:363
void hash_destroy(struct hash_table *ht)
Destroys a hash table.
Definition: hashtable.c:278
const char * hash_get(struct hash_table *ht, const char *key)
Looks up a the value associated with with a key from a specific hash table.
Definition: hashtable.c:339
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821