dictionary.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef _DICTIONARY_H_
  2. #define _DICTIONARY_H_
  3. /* Copyright (c) 2000-2007 by Nicolas Devillard.
  4. * Copyright (x) 2009 by Tim Post <tinkertim@gmail.com>
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. /** @addtogroup ciniparser
  30. * @{
  31. */
  32. /**
  33. * @file dictionary.h
  34. * @author N. Devillard
  35. * @date Sep 2007
  36. * @version $Revision: 1.12 $
  37. * @brief Implements a dictionary for string variables.
  38. *
  39. * This module implements a simple dictionary object, i.e. a list
  40. * of string/string associations. This object is useful to store e.g.
  41. * information retrieved from a configuration file (ini files).
  42. */
  43. /**
  44. * @brief Dictionary object
  45. * @param n Number of entries in the dictionary
  46. * @param size Storage size
  47. * @param val List of string values
  48. * @param key List of string keys
  49. * @param hash List of hash values for keys
  50. *
  51. * This object contains a list of string/string associations. Each
  52. * association is identified by a unique string key. Looking up values
  53. * in the dictionary is speeded up by the use of a (hopefully collision-free)
  54. * hash function.
  55. */
  56. typedef struct _dictionary_ {
  57. int n;
  58. int size;
  59. char **val;
  60. char **key;
  61. unsigned *hash;
  62. } dictionary;
  63. /**
  64. * @brief Compute the hash key for a string.
  65. * @param key Character string to use for key.
  66. * @return 1 unsigned int on at least 32 bits.
  67. *
  68. * This hash function has been taken from an Article in Dr Dobbs Journal.
  69. * This is normally a collision-free function, distributing keys evenly.
  70. * The key is stored anyway in the struct so that collision can be avoided
  71. * by comparing the key itself in last resort.
  72. */
  73. unsigned dictionary_hash(char *key);
  74. /**
  75. * @brief Create a new dictionary object.
  76. * @param size Optional initial size of the dictionary.
  77. * @return allocated dictionary object on success, NULL on failure
  78. *
  79. * This function allocates a new dictionary object of given size and returns
  80. * it. If you do not know in advance (roughly) the number of entries in the
  81. * dictionary, give size=0.
  82. */
  83. dictionary *dictionary_new(int size);
  84. /**
  85. * @brief Delete a dictionary object
  86. * @param d dictionary object to deallocate.
  87. * @return void
  88. *
  89. * Deallocate a dictionary object and all memory associated to it.
  90. */
  91. void dictionary_del(dictionary *vd);
  92. /**
  93. * @brief Get a value from a dictionary.
  94. * @param d dictionary object to search.
  95. * @param key Key to look for in the dictionary.
  96. * @param def Default value to return if key not found.
  97. * @return 1 pointer to internally allocated character string.
  98. *
  99. * This function locates a key in a dictionary and returns a pointer to its
  100. * value, or the passed 'def' pointer if no such key can be found in
  101. * dictionary. The returned character pointer points to data internal to the
  102. * dictionary object, you should not try to free it or modify it.
  103. */
  104. char *dictionary_get(dictionary *d, char *key, char *def);
  105. /**
  106. * @brief Set a value in a dictionary.
  107. * @param d dictionary object to modify.
  108. * @param key Key to modify or add.
  109. * @param val Value to add.
  110. * @return int 0 if Ok, anything else otherwise
  111. *
  112. * If the given key is found in the dictionary, the associated value is
  113. * replaced by the provided one. If the key cannot be found in the
  114. * dictionary, it is added to it.
  115. *
  116. * It is Ok to provide a NULL value for val, but NULL values for the dictionary
  117. * or the key are considered as errors: the function will return immediately
  118. * in such a case.
  119. *
  120. * Notice that if you dictionary_set a variable to NULL, a call to
  121. * dictionary_get will return a NULL value: the variable will be found, and
  122. * its value (NULL) is returned. In other words, setting the variable
  123. * content to NULL is equivalent to deleting the variable from the
  124. * dictionary. It is not possible (in this implementation) to have a key in
  125. * the dictionary without value.
  126. *
  127. * This function returns non-zero in case of failure.
  128. */
  129. int dictionary_set(dictionary *vd, char *key, char *val);
  130. /**
  131. * @brief Delete a key in a dictionary
  132. * @param d dictionary object to modify.
  133. * @param key Key to remove.
  134. * @return void
  135. *
  136. * This function deletes a key in a dictionary. Nothing is done if the
  137. * key cannot be found.
  138. */
  139. void dictionary_unset(dictionary *d, char *key);
  140. /**
  141. * @brief Dump a dictionary to an opened file pointer.
  142. * @param d Dictionary to dump
  143. * @param out Opened file pointer
  144. * @return void
  145. *
  146. * Dumps a dictionary onto an opened file pointer. Key pairs are printed out
  147. * as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
  148. * output file pointers.
  149. */
  150. void dictionary_dump(dictionary *d, FILE *out);
  151. #endif
  152. /** @}
  153. */