dictionary.h 6.6 KB

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