dictionary.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*-------------------------------------------------------------------------*/
  2. /**
  3. @file dictionary.c
  4. @author N. Devillard
  5. @brief Implements a dictionary for string variables.
  6. This module implements a simple dictionary object, i.e. a list
  7. of string/string associations. This object is useful to store e.g.
  8. informations retrieved from a configuration file (ini files).
  9. */
  10. /*--------------------------------------------------------------------------*/
  11. /*---------------------------------------------------------------------------
  12. Includes
  13. ---------------------------------------------------------------------------*/
  14. #include "dictionary.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. /** Minimal allocated number of entries in a dictionary */
  19. #define DICTMINSZ 128
  20. /*---------------------------------------------------------------------------
  21. Private functions
  22. ---------------------------------------------------------------------------*/
  23. /*-------------------------------------------------------------------------*/
  24. /**
  25. @brief Duplicate a string
  26. @param s String to duplicate
  27. @return Pointer to a newly allocated string, to be freed with free()
  28. This is a replacement for strdup(). This implementation is provided
  29. for systems that do not have it.
  30. */
  31. /*--------------------------------------------------------------------------*/
  32. static char * xstrdup(const char * s)
  33. {
  34. char * t ;
  35. size_t len ;
  36. if (!s)
  37. return NULL ;
  38. len = strlen(s) + 1 ;
  39. t = (char*) malloc(len) ;
  40. if (t) {
  41. memcpy(t, s, len) ;
  42. }
  43. return t ;
  44. }
  45. /*-------------------------------------------------------------------------*/
  46. /**
  47. @brief Double the size of the dictionary
  48. @param d Dictionary to grow
  49. @return This function returns non-zero in case of failure
  50. */
  51. /*--------------------------------------------------------------------------*/
  52. static int dictionary_grow(dictionary * d)
  53. {
  54. char ** new_val ;
  55. char ** new_key ;
  56. unsigned * new_hash ;
  57. new_val = (char**) calloc(d->size * 2, sizeof *d->val);
  58. new_key = (char**) calloc(d->size * 2, sizeof *d->key);
  59. new_hash = (unsigned*) calloc(d->size * 2, sizeof *d->hash);
  60. if (!new_val || !new_key || !new_hash) {
  61. /* An allocation failed, leave the dictionary unchanged */
  62. if (new_val)
  63. free(new_val);
  64. if (new_key)
  65. free(new_key);
  66. if (new_hash)
  67. free(new_hash);
  68. return -1 ;
  69. }
  70. /* Initialize the newly allocated space */
  71. memcpy(new_val, d->val, d->size * sizeof(char *));
  72. memcpy(new_key, d->key, d->size * sizeof(char *));
  73. memcpy(new_hash, d->hash, d->size * sizeof(unsigned));
  74. /* Delete previous data */
  75. free(d->val);
  76. free(d->key);
  77. free(d->hash);
  78. /* Actually update the dictionary */
  79. d->size *= 2 ;
  80. d->val = new_val;
  81. d->key = new_key;
  82. d->hash = new_hash;
  83. return 0 ;
  84. }
  85. /*---------------------------------------------------------------------------
  86. Function codes
  87. ---------------------------------------------------------------------------*/
  88. /*-------------------------------------------------------------------------*/
  89. /**
  90. @brief Compute the hash key for a string.
  91. @param key Character string to use for key.
  92. @return 1 unsigned int on at least 32 bits.
  93. This hash function has been taken from an Article in Dr Dobbs Journal.
  94. This is normally a collision-free function, distributing keys evenly.
  95. The key is stored anyway in the struct so that collision can be avoided
  96. by comparing the key itself in last resort.
  97. */
  98. /*--------------------------------------------------------------------------*/
  99. unsigned dictionary_hash(const char * key)
  100. {
  101. size_t len ;
  102. unsigned hash ;
  103. size_t i ;
  104. if (!key)
  105. return 0 ;
  106. len = strlen(key);
  107. for (hash=0, i=0 ; i<len ; i++) {
  108. hash += (unsigned)key[i] ;
  109. hash += (hash<<10);
  110. hash ^= (hash>>6) ;
  111. }
  112. hash += (hash <<3);
  113. hash ^= (hash >>11);
  114. hash += (hash <<15);
  115. return hash ;
  116. }
  117. /*-------------------------------------------------------------------------*/
  118. /**
  119. @brief Create a new dictionary object.
  120. @param size Optional initial size of the dictionary.
  121. @return 1 newly allocated dictionary object.
  122. This function allocates a new dictionary object of given size and returns
  123. it. If you do not know in advance (roughly) the number of entries in the
  124. dictionary, give size=0.
  125. */
  126. /*-------------------------------------------------------------------------*/
  127. dictionary * dictionary_new(size_t size)
  128. {
  129. dictionary * d ;
  130. /* If no size was specified, allocate space for DICTMINSZ */
  131. if (size<DICTMINSZ) size=DICTMINSZ ;
  132. d = (dictionary*) calloc(1, sizeof *d) ;
  133. if (d) {
  134. d->size = size ;
  135. d->val = (char**) calloc(size, sizeof *d->val);
  136. d->key = (char**) calloc(size, sizeof *d->key);
  137. d->hash = (unsigned*) calloc(size, sizeof *d->hash);
  138. if (!d->size || !d->val || !d->hash) {
  139. free((void *) d->size);
  140. free((void *) d->val);
  141. free((void *) d->hash);
  142. free(d);
  143. d = NULL;
  144. }
  145. }
  146. return d ;
  147. }
  148. /*-------------------------------------------------------------------------*/
  149. /**
  150. @brief Delete a dictionary object
  151. @param d dictionary object to deallocate.
  152. @return void
  153. Deallocate a dictionary object and all memory associated to it.
  154. */
  155. /*--------------------------------------------------------------------------*/
  156. void dictionary_del(dictionary * d)
  157. {
  158. size_t i ;
  159. if (d==NULL) return ;
  160. for (i=0 ; i<d->size ; i++) {
  161. if (d->key[i]!=NULL)
  162. free(d->key[i]);
  163. if (d->val[i]!=NULL)
  164. free(d->val[i]);
  165. }
  166. free(d->val);
  167. free(d->key);
  168. free(d->hash);
  169. free(d);
  170. return ;
  171. }
  172. /*-------------------------------------------------------------------------*/
  173. /**
  174. @brief Get a value from a dictionary.
  175. @param d dictionary object to search.
  176. @param key Key to look for in the dictionary.
  177. @param def Default value to return if key not found.
  178. @return 1 pointer to internally allocated character string.
  179. This function locates a key in a dictionary and returns a pointer to its
  180. value, or the passed 'def' pointer if no such key can be found in
  181. dictionary. The returned character pointer points to data internal to the
  182. dictionary object, you should not try to free it or modify it.
  183. */
  184. /*--------------------------------------------------------------------------*/
  185. const char * dictionary_get(const dictionary * d, const char * key, const char * def)
  186. {
  187. unsigned hash ;
  188. size_t i ;
  189. if(d == NULL || key == NULL)
  190. return def ;
  191. hash = dictionary_hash(key);
  192. for (i=0 ; i<d->size ; i++) {
  193. if (d->key[i]==NULL)
  194. continue ;
  195. /* Compare hash */
  196. if (hash==d->hash[i]) {
  197. /* Compare string, to avoid hash collisions */
  198. if (!strcmp(key, d->key[i])) {
  199. return d->val[i] ;
  200. }
  201. }
  202. }
  203. return def ;
  204. }
  205. /*-------------------------------------------------------------------------*/
  206. /**
  207. @brief Set a value in a dictionary.
  208. @param d dictionary object to modify.
  209. @param key Key to modify or add.
  210. @param val Value to add.
  211. @return int 0 if Ok, anything else otherwise
  212. If the given key is found in the dictionary, the associated value is
  213. replaced by the provided one. If the key cannot be found in the
  214. dictionary, it is added to it.
  215. It is Ok to provide a NULL value for val, but NULL values for the dictionary
  216. or the key are considered as errors: the function will return immediately
  217. in such a case.
  218. Notice that if you dictionary_set a variable to NULL, a call to
  219. dictionary_get will return a NULL value: the variable will be found, and
  220. its value (NULL) is returned. In other words, setting the variable
  221. content to NULL is equivalent to deleting the variable from the
  222. dictionary. It is not possible (in this implementation) to have a key in
  223. the dictionary without value.
  224. This function returns non-zero in case of failure.
  225. */
  226. /*--------------------------------------------------------------------------*/
  227. int dictionary_set(dictionary * d, const char * key, const char * val)
  228. {
  229. size_t i ;
  230. unsigned hash ;
  231. if (d==NULL || key==NULL) return -1 ;
  232. /* Compute hash for this key */
  233. hash = dictionary_hash(key) ;
  234. /* Find if value is already in dictionary */
  235. if (d->n>0) {
  236. for (i=0 ; i<d->size ; i++) {
  237. if (d->key[i]==NULL)
  238. continue ;
  239. if (hash==d->hash[i]) { /* Same hash value */
  240. if (!strcmp(key, d->key[i])) { /* Same key */
  241. /* Found a value: modify and return */
  242. if (d->val[i]!=NULL)
  243. free(d->val[i]);
  244. d->val[i] = (val ? xstrdup(val) : NULL);
  245. /* Value has been modified: return */
  246. return 0 ;
  247. }
  248. }
  249. }
  250. }
  251. /* Add a new value */
  252. /* See if dictionary needs to grow */
  253. if (d->n==d->size) {
  254. /* Reached maximum size: reallocate dictionary */
  255. if (dictionary_grow(d) != 0)
  256. return -1;
  257. }
  258. /* Insert key in the first empty slot. Start at d->n and wrap at
  259. d->size. Because d->n < d->size this will necessarily
  260. terminate. */
  261. for (i=d->n ; d->key[i] ; ) {
  262. if(++i == d->size) i = 0;
  263. }
  264. /* Copy key */
  265. d->key[i] = xstrdup(key);
  266. d->val[i] = (val ? xstrdup(val) : NULL) ;
  267. d->hash[i] = hash;
  268. d->n ++ ;
  269. return 0 ;
  270. }
  271. /*-------------------------------------------------------------------------*/
  272. /**
  273. @brief Delete a key in a dictionary
  274. @param d dictionary object to modify.
  275. @param key Key to remove.
  276. @return void
  277. This function deletes a key in a dictionary. Nothing is done if the
  278. key cannot be found.
  279. */
  280. /*--------------------------------------------------------------------------*/
  281. void dictionary_unset(dictionary * d, const char * key)
  282. {
  283. unsigned hash ;
  284. size_t i ;
  285. if (key == NULL || d == NULL) {
  286. return;
  287. }
  288. hash = dictionary_hash(key);
  289. for (i=0 ; i<d->size ; i++) {
  290. if (d->key[i]==NULL)
  291. continue ;
  292. /* Compare hash */
  293. if (hash==d->hash[i]) {
  294. /* Compare string, to avoid hash collisions */
  295. if (!strcmp(key, d->key[i])) {
  296. /* Found key */
  297. break ;
  298. }
  299. }
  300. }
  301. if (i>=d->size)
  302. /* Key not found */
  303. return ;
  304. free(d->key[i]);
  305. d->key[i] = NULL ;
  306. if (d->val[i]!=NULL) {
  307. free(d->val[i]);
  308. d->val[i] = NULL ;
  309. }
  310. d->hash[i] = 0 ;
  311. d->n -- ;
  312. return ;
  313. }
  314. /*-------------------------------------------------------------------------*/
  315. /**
  316. @brief Dump a dictionary to an opened file pointer.
  317. @param d Dictionary to dump
  318. @param f Opened file pointer.
  319. @return void
  320. Dumps a dictionary onto an opened file pointer. Key pairs are printed out
  321. as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
  322. output file pointers.
  323. */
  324. /*--------------------------------------------------------------------------*/
  325. void dictionary_dump(const dictionary * d, FILE * out)
  326. {
  327. size_t i ;
  328. if (d==NULL || out==NULL) return ;
  329. if (d->n<1) {
  330. fprintf(out, "empty dictionary\n");
  331. return ;
  332. }
  333. for (i=0 ; i<d->size ; i++) {
  334. if (d->key[i]) {
  335. fprintf(out, "%20s\t[%s]\n",
  336. d->key[i],
  337. d->val[i] ? d->val[i] : "UNDEF");
  338. }
  339. }
  340. return ;
  341. }