Dict.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Dict_H
  16. #define Dict_H
  17. #include "memory/Allocator.h"
  18. #include "benc/Object.h"
  19. #include "util/Linker.h"
  20. Linker_require("benc/Dict.c")
  21. struct Dict_Entry;
  22. struct Dict_Entry {
  23. struct Dict_Entry* next;
  24. const String* key;
  25. Object* val;
  26. };
  27. /**
  28. * Remove an entry from the dictionary.
  29. *
  30. * @param dictionary the dictionary to remove the entry from.
  31. * @param key the key which the entry is entered under.
  32. * @return 1 if there was an entry removed, 0 if there was not.
  33. */
  34. int32_t Dict_remove(Dict* dictionary, const String* key);
  35. /*----------------------- Size Functions -----------------------*/
  36. /**
  37. * Get the number of entries in a dictionary.
  38. *
  39. * @param a dictionary
  40. * @return the number of entries in the dictionary or -1 if the dictionary argument is NULL.
  41. */
  42. int32_t Dict_size(const Dict* dictionary);
  43. /*----------------------- Dictionary Lookup Functions -----------------------*/
  44. /**
  45. * Lookup an integer value from a dictionary, if the value is not present
  46. * or is not an integer type then NULL is returned.
  47. *
  48. * @param dictionary the dictionary to look the entry up in.
  49. * @param key the key to look the entry up with.
  50. * @return an integer which is in the dictionary under the given key or else NULL.
  51. */
  52. int64_t* Dict_getInt(const Dict* dictionary, const String* key);
  53. /**
  54. * Lookup a string value from a dictionary, if the value is not present
  55. * or is not a string type then NULL is returned.
  56. *
  57. * @param dictionary the dictionary to look the entry up in.
  58. * @param key the key to look the entry up with.
  59. * @return a string which is in the dictionary under the given key or else NULL.
  60. */
  61. String* Dict_getString(const Dict* dictionary, const String* key);
  62. /**
  63. * Lookup a dictionary value from another dictionary, if the value is not present
  64. * or is not a dictionary type then NULL is returned.
  65. *
  66. * @param dictionary the dictionary to look the entry up in.
  67. * @param key the key to look the entry up with.
  68. * @return a dictionary which is in the dictionary under the given key or else NULL.
  69. */
  70. Dict* Dict_getDict(const Dict* dictionary, const String* key);
  71. /**
  72. * Lookup a list value from a dictionary, if the value is not present
  73. * or is not a list type then NULL is returned.
  74. *
  75. * @param dictionary the dictionary to look the entry up in.
  76. * @param key the key to look the entry up with.
  77. * @return a list which is in the dictionary under the given key or else NULL.
  78. */
  79. List* Dict_getList(const Dict* dictionary, const String* key);
  80. /*----------------------- Dictionary Put Functions -----------------------*/
  81. /**
  82. * Insert an integer into a dictionary.
  83. *
  84. * @param putIntoThis the dictionary to insert an entry into.
  85. * @param key the reference key to use for putting the entry in the dictionary.
  86. * @param value the integer to insert. int64_t is an alias to int64_t.
  87. * @param allocator the means to get memory for storing the dictionary entry wrapper.
  88. * @return if the key already exists in the dictionary then the value which was
  89. * displaced by the put, otherwise NULL.
  90. */
  91. Object* Dict_putInt(Dict* putIntoThis,
  92. const String* key,
  93. int64_t value,
  94. struct Allocator* allocator);
  95. /**
  96. * Insert a String object into another dictionary.
  97. * NOTE: This will not copy the given object, only add a pointer to it in the dictionary.
  98. *
  99. * @param putIntoThis the dictionary to insert an entry into.
  100. * @param key the reference key to use for putting the entry in the dictionary.
  101. * @param value the string to insert.
  102. * @param allocator the means to get memory for storing the dictionary entry wrapper.
  103. * @return if the key already exists in the dictionary then the value which was
  104. * displaced by the put, otherwise NULL.
  105. */
  106. Object* Dict_putString(Dict* putIntoThis,
  107. const String* key,
  108. String* value,
  109. struct Allocator* allocator);
  110. /**
  111. * Insert a Dictionary object into another dictionary.
  112. * NOTE: This will not copy the given object, only add a pointer to it in the dictionary.
  113. *
  114. * @param putIntoThis the dictionary to insert an entry into.
  115. * @param key the reference key to use for putting the entry in the dictionary.
  116. * @param value the value to insert.
  117. * @param allocator the memory allocator to use for getting memory for the entry.
  118. * @return if the key already exists in the dictionary then the value which was
  119. * displaced by the put, otherwise NULL.
  120. */
  121. Object* Dict_putDict(Dict* putIntoThis,
  122. const String* key,
  123. Dict* value,
  124. struct Allocator* allocator);
  125. /**
  126. * Insert a List object into a dictionary.
  127. * NOTE: This will not copy the given object, only add a pointer to it in the dictionary.
  128. *
  129. * @param putIntoThis the dictionary to insert an entry into.
  130. * @param key the reference key to use for putting the entry in the dictionary.
  131. * @param value the list to insert.
  132. * @param allocator the memory allocator to use for getting memory for the entry.
  133. * @return if the key already exists in the dictionary then the value which was
  134. * displaced by the put, otherwise NULL.
  135. */
  136. Object* Dict_putList(Dict* putIntoThis,
  137. const String* key,
  138. List* value,
  139. struct Allocator* allocator);
  140. /*----------------------- Constructors -----------------------*/
  141. /**
  142. * Create a new bencoded dictionary type.
  143. *
  144. * @param allocator the place to allocate the memory for storing the dictionary.
  145. */
  146. Dict* Dict_new(struct Allocator* allocator);
  147. /**
  148. * Create a dictionary on the stack.
  149. *
  150. * @param entryKey the key which must be a string.
  151. * @param value the value which must be an object.
  152. * @param next another Dict, can be used to chain Dict_CONST() calls.
  153. * @return a Dict. NOTE: This does *NOT* return a Dict*, just a Dict.
  154. */
  155. #define Dict_CONST(entryKey, value, nextDict) \
  156. (&(struct Dict_Entry) { \
  157. .key = entryKey, \
  158. .val = value, \
  159. .next = nextDict \
  160. })
  161. #define Dict_OBJ(x) (&(Object) { .type = Object_DICT, .as.dictionary = x })
  162. #endif