dictionary.h 6.4 KB

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