dictionary.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*-------------------------------------------------------------------------*/
  2. /**
  3. @file dictionary.h
  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. #ifndef _DICTIONARY_H_
  12. #define _DICTIONARY_H_
  13. /*---------------------------------------------------------------------------
  14. Includes
  15. ---------------------------------------------------------------------------*/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /*---------------------------------------------------------------------------
  24. New types
  25. ---------------------------------------------------------------------------*/
  26. /*-------------------------------------------------------------------------*/
  27. /**
  28. @brief Dictionary object
  29. This object contains a list of string/string associations. Each
  30. association is identified by a unique string key. Looking up values
  31. in the dictionary is speeded up by the use of a (hopefully collision-free)
  32. hash function.
  33. */
  34. /*-------------------------------------------------------------------------*/
  35. typedef struct _dictionary_ {
  36. int n ; /** Number of entries in dictionary */
  37. ssize_t size ; /** Storage size */
  38. char ** val ; /** List of string values */
  39. char ** key ; /** List of string keys */
  40. unsigned * hash ; /** List of hash values for keys */
  41. } dictionary ;
  42. /*---------------------------------------------------------------------------
  43. Function prototypes
  44. ---------------------------------------------------------------------------*/
  45. /*-------------------------------------------------------------------------*/
  46. /**
  47. @brief Compute the hash key for a string.
  48. @param key Character string to use for key.
  49. @return 1 unsigned int on at least 32 bits.
  50. This hash function has been taken from an Article in Dr Dobbs Journal.
  51. This is normally a collision-free function, distributing keys evenly.
  52. The key is stored anyway in the struct so that collision can be avoided
  53. by comparing the key itself in last resort.
  54. */
  55. /*--------------------------------------------------------------------------*/
  56. unsigned dictionary_hash(const char * key);
  57. /*-------------------------------------------------------------------------*/
  58. /**
  59. @brief Create a new dictionary object.
  60. @param size Optional initial size of the dictionary.
  61. @return 1 newly allocated dictionary objet.
  62. This function allocates a new dictionary object of given size and returns
  63. it. If you do not know in advance (roughly) the number of entries in the
  64. dictionary, give size=0.
  65. */
  66. /*--------------------------------------------------------------------------*/
  67. dictionary * dictionary_new(size_t size);
  68. /*-------------------------------------------------------------------------*/
  69. /**
  70. @brief Delete a dictionary object
  71. @param d dictionary object to deallocate.
  72. @return void
  73. Deallocate a dictionary object and all memory associated to it.
  74. */
  75. /*--------------------------------------------------------------------------*/
  76. void dictionary_del(dictionary * vd);
  77. /*-------------------------------------------------------------------------*/
  78. /**
  79. @brief Get a value from a dictionary.
  80. @param d dictionary object to search.
  81. @param key Key to look for in the dictionary.
  82. @param def Default value to return if key not found.
  83. @return 1 pointer to internally allocated character string.
  84. This function locates a key in a dictionary and returns a pointer to its
  85. value, or the passed 'def' pointer if no such key can be found in
  86. dictionary. The returned character pointer points to data internal to the
  87. dictionary object, you should not try to free it or modify it.
  88. */
  89. /*--------------------------------------------------------------------------*/
  90. const char * dictionary_get(const dictionary * d, const char * key, const char * def);
  91. /*-------------------------------------------------------------------------*/
  92. /**
  93. @brief Set a value in a dictionary.
  94. @param d dictionary object to modify.
  95. @param key Key to modify or add.
  96. @param val Value to add.
  97. @return int 0 if Ok, anything else otherwise
  98. If the given key is found in the dictionary, the associated value is
  99. replaced by the provided one. If the key cannot be found in the
  100. dictionary, it is added to it.
  101. It is Ok to provide a NULL value for val, but NULL values for the dictionary
  102. or the key are considered as errors: the function will return immediately
  103. in such a case.
  104. Notice that if you dictionary_set a variable to NULL, a call to
  105. dictionary_get will return a NULL value: the variable will be found, and
  106. its value (NULL) is returned. In other words, setting the variable
  107. content to NULL is equivalent to deleting the variable from the
  108. dictionary. It is not possible (in this implementation) to have a key in
  109. the dictionary without value.
  110. This function returns non-zero in case of failure.
  111. */
  112. /*--------------------------------------------------------------------------*/
  113. int dictionary_set(dictionary * vd, const char * key, const char * val);
  114. /*-------------------------------------------------------------------------*/
  115. /**
  116. @brief Delete a key in a dictionary
  117. @param d dictionary object to modify.
  118. @param key Key to remove.
  119. @return void
  120. This function deletes a key in a dictionary. Nothing is done if the
  121. key cannot be found.
  122. */
  123. /*--------------------------------------------------------------------------*/
  124. void dictionary_unset(dictionary * d, const char * key);
  125. /*-------------------------------------------------------------------------*/
  126. /**
  127. @brief Dump a dictionary to an opened file pointer.
  128. @param d Dictionary to dump
  129. @param f Opened file pointer.
  130. @return void
  131. Dumps a dictionary onto an opened file pointer. Key pairs are printed out
  132. as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
  133. output file pointers.
  134. */
  135. /*--------------------------------------------------------------------------*/
  136. void dictionary_dump(const dictionary * d, FILE * out);
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif