Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | /*-------------------------------------------------------------------------*/ |
michael@0 | 3 | /** |
michael@0 | 4 | @file dictionary.h |
michael@0 | 5 | @author N. Devillard |
michael@0 | 6 | @date Sep 2007 |
michael@0 | 7 | @version $Revision: 1.12 $ |
michael@0 | 8 | @brief Implements a dictionary for string variables. |
michael@0 | 9 | |
michael@0 | 10 | This module implements a simple dictionary object, i.e. a list |
michael@0 | 11 | of string/string associations. This object is useful to store e.g. |
michael@0 | 12 | informations retrieved from a configuration file (ini files). |
michael@0 | 13 | */ |
michael@0 | 14 | /*--------------------------------------------------------------------------*/ |
michael@0 | 15 | |
michael@0 | 16 | /* |
michael@0 | 17 | $Id: dictionary.h,v 1.12 2007-11-23 21:37:00 ndevilla Exp $ |
michael@0 | 18 | $Author: ndevilla $ |
michael@0 | 19 | $Date: 2007-11-23 21:37:00 $ |
michael@0 | 20 | $Revision: 1.12 $ |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | #ifndef _DICTIONARY_H_ |
michael@0 | 24 | #define _DICTIONARY_H_ |
michael@0 | 25 | |
michael@0 | 26 | /*--------------------------------------------------------------------------- |
michael@0 | 27 | Includes |
michael@0 | 28 | ---------------------------------------------------------------------------*/ |
michael@0 | 29 | |
michael@0 | 30 | #include <stdio.h> |
michael@0 | 31 | #include <stdlib.h> |
michael@0 | 32 | #include <string.h> |
michael@0 | 33 | #ifndef _WIN32 |
michael@0 | 34 | #include <unistd.h> |
michael@0 | 35 | #endif |
michael@0 | 36 | |
michael@0 | 37 | /*--------------------------------------------------------------------------- |
michael@0 | 38 | New types |
michael@0 | 39 | ---------------------------------------------------------------------------*/ |
michael@0 | 40 | |
michael@0 | 41 | |
michael@0 | 42 | /*-------------------------------------------------------------------------*/ |
michael@0 | 43 | /** |
michael@0 | 44 | @brief Dictionary object |
michael@0 | 45 | |
michael@0 | 46 | This object contains a list of string/string associations. Each |
michael@0 | 47 | association is identified by a unique string key. Looking up values |
michael@0 | 48 | in the dictionary is speeded up by the use of a (hopefully collision-free) |
michael@0 | 49 | hash function. |
michael@0 | 50 | */ |
michael@0 | 51 | /*-------------------------------------------------------------------------*/ |
michael@0 | 52 | typedef struct _dictionary_ { |
michael@0 | 53 | int n ; /** Number of entries in dictionary */ |
michael@0 | 54 | int size ; /** Storage size */ |
michael@0 | 55 | char ** val ; /** List of string values */ |
michael@0 | 56 | char ** key ; /** List of string keys */ |
michael@0 | 57 | unsigned * hash ; /** List of hash values for keys */ |
michael@0 | 58 | } dictionary ; |
michael@0 | 59 | |
michael@0 | 60 | |
michael@0 | 61 | /*--------------------------------------------------------------------------- |
michael@0 | 62 | Function prototypes |
michael@0 | 63 | ---------------------------------------------------------------------------*/ |
michael@0 | 64 | |
michael@0 | 65 | /*-------------------------------------------------------------------------*/ |
michael@0 | 66 | /** |
michael@0 | 67 | @brief Compute the hash key for a string. |
michael@0 | 68 | @param key Character string to use for key. |
michael@0 | 69 | @return 1 unsigned int on at least 32 bits. |
michael@0 | 70 | |
michael@0 | 71 | This hash function has been taken from an Article in Dr Dobbs Journal. |
michael@0 | 72 | This is normally a collision-free function, distributing keys evenly. |
michael@0 | 73 | The key is stored anyway in the struct so that collision can be avoided |
michael@0 | 74 | by comparing the key itself in last resort. |
michael@0 | 75 | */ |
michael@0 | 76 | /*--------------------------------------------------------------------------*/ |
michael@0 | 77 | unsigned dictionary_hash(char * key); |
michael@0 | 78 | |
michael@0 | 79 | /*-------------------------------------------------------------------------*/ |
michael@0 | 80 | /** |
michael@0 | 81 | @brief Create a new dictionary object. |
michael@0 | 82 | @param size Optional initial size of the dictionary. |
michael@0 | 83 | @return 1 newly allocated dictionary objet. |
michael@0 | 84 | |
michael@0 | 85 | This function allocates a new dictionary object of given size and returns |
michael@0 | 86 | it. If you do not know in advance (roughly) the number of entries in the |
michael@0 | 87 | dictionary, give size=0. |
michael@0 | 88 | */ |
michael@0 | 89 | /*--------------------------------------------------------------------------*/ |
michael@0 | 90 | dictionary * dictionary_new(int size); |
michael@0 | 91 | |
michael@0 | 92 | /*-------------------------------------------------------------------------*/ |
michael@0 | 93 | /** |
michael@0 | 94 | @brief Delete a dictionary object |
michael@0 | 95 | @param d dictionary object to deallocate. |
michael@0 | 96 | @return void |
michael@0 | 97 | |
michael@0 | 98 | Deallocate a dictionary object and all memory associated to it. |
michael@0 | 99 | */ |
michael@0 | 100 | /*--------------------------------------------------------------------------*/ |
michael@0 | 101 | void dictionary_del(dictionary * vd); |
michael@0 | 102 | |
michael@0 | 103 | /*-------------------------------------------------------------------------*/ |
michael@0 | 104 | /** |
michael@0 | 105 | @brief Get a value from a dictionary. |
michael@0 | 106 | @param d dictionary object to search. |
michael@0 | 107 | @param key Key to look for in the dictionary. |
michael@0 | 108 | @param def Default value to return if key not found. |
michael@0 | 109 | @return 1 pointer to internally allocated character string. |
michael@0 | 110 | |
michael@0 | 111 | This function locates a key in a dictionary and returns a pointer to its |
michael@0 | 112 | value, or the passed 'def' pointer if no such key can be found in |
michael@0 | 113 | dictionary. The returned character pointer points to data internal to the |
michael@0 | 114 | dictionary object, you should not try to free it or modify it. |
michael@0 | 115 | */ |
michael@0 | 116 | /*--------------------------------------------------------------------------*/ |
michael@0 | 117 | char * dictionary_get(dictionary * d, char * key, char * def); |
michael@0 | 118 | |
michael@0 | 119 | |
michael@0 | 120 | /*-------------------------------------------------------------------------*/ |
michael@0 | 121 | /** |
michael@0 | 122 | @brief Set a value in a dictionary. |
michael@0 | 123 | @param d dictionary object to modify. |
michael@0 | 124 | @param key Key to modify or add. |
michael@0 | 125 | @param val Value to add. |
michael@0 | 126 | @return int 0 if Ok, anything else otherwise |
michael@0 | 127 | |
michael@0 | 128 | If the given key is found in the dictionary, the associated value is |
michael@0 | 129 | replaced by the provided one. If the key cannot be found in the |
michael@0 | 130 | dictionary, it is added to it. |
michael@0 | 131 | |
michael@0 | 132 | It is Ok to provide a NULL value for val, but NULL values for the dictionary |
michael@0 | 133 | or the key are considered as errors: the function will return immediately |
michael@0 | 134 | in such a case. |
michael@0 | 135 | |
michael@0 | 136 | Notice that if you dictionary_set a variable to NULL, a call to |
michael@0 | 137 | dictionary_get will return a NULL value: the variable will be found, and |
michael@0 | 138 | its value (NULL) is returned. In other words, setting the variable |
michael@0 | 139 | content to NULL is equivalent to deleting the variable from the |
michael@0 | 140 | dictionary. It is not possible (in this implementation) to have a key in |
michael@0 | 141 | the dictionary without value. |
michael@0 | 142 | |
michael@0 | 143 | This function returns non-zero in case of failure. |
michael@0 | 144 | */ |
michael@0 | 145 | /*--------------------------------------------------------------------------*/ |
michael@0 | 146 | int dictionary_set(dictionary * vd, char * key, char * val); |
michael@0 | 147 | |
michael@0 | 148 | /*-------------------------------------------------------------------------*/ |
michael@0 | 149 | /** |
michael@0 | 150 | @brief Delete a key in a dictionary |
michael@0 | 151 | @param d dictionary object to modify. |
michael@0 | 152 | @param key Key to remove. |
michael@0 | 153 | @return void |
michael@0 | 154 | |
michael@0 | 155 | This function deletes a key in a dictionary. Nothing is done if the |
michael@0 | 156 | key cannot be found. |
michael@0 | 157 | */ |
michael@0 | 158 | /*--------------------------------------------------------------------------*/ |
michael@0 | 159 | void dictionary_unset(dictionary * d, char * key); |
michael@0 | 160 | |
michael@0 | 161 | |
michael@0 | 162 | /*-------------------------------------------------------------------------*/ |
michael@0 | 163 | /** |
michael@0 | 164 | @brief Dump a dictionary to an opened file pointer. |
michael@0 | 165 | @param d Dictionary to dump |
michael@0 | 166 | @param f Opened file pointer. |
michael@0 | 167 | @return void |
michael@0 | 168 | |
michael@0 | 169 | Dumps a dictionary onto an opened file pointer. Key pairs are printed out |
michael@0 | 170 | as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as |
michael@0 | 171 | output file pointers. |
michael@0 | 172 | */ |
michael@0 | 173 | /*--------------------------------------------------------------------------*/ |
michael@0 | 174 | void dictionary_dump(dictionary * d, FILE * out); |
michael@0 | 175 | |
michael@0 | 176 | #endif |