Dict.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. 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. /*----------------------- Walk the Dictionary -----------------------*/
  44. #define Dict_forEach(_dict, _key) \
  45. for (struct Dict_Entry* e = *_dict; e && (_key = e->key); e = e->next)
  46. // CHECKFILES_IGNORE
  47. /*----------------------- Dictionary Lookup Functions -----------------------*/
  48. /**
  49. * Lookup an integer value from a dictionary, if the value is not present
  50. * or is not an integer type then NULL is returned.
  51. *
  52. * @param dictionary the dictionary to look the entry up in.
  53. * @param key the key to look the entry up with.
  54. * @return an integer which is in the dictionary under the given key or else NULL.
  55. */
  56. int64_t* Dict_getInt(const Dict* dictionary, const String* key);
  57. /**
  58. * Lookup a string value from a dictionary, if the value is not present
  59. * or is not a string type then NULL is returned.
  60. *
  61. * @param dictionary the dictionary to look the entry up in.
  62. * @param key the key to look the entry up with.
  63. * @return a string which is in the dictionary under the given key or else NULL.
  64. */
  65. String* Dict_getString(const Dict* dictionary, const String* key);
  66. /**
  67. * Lookup a dictionary value from another dictionary, if the value is not present
  68. * or is not a dictionary type then NULL is returned.
  69. *
  70. * @param dictionary the dictionary to look the entry up in.
  71. * @param key the key to look the entry up with.
  72. * @return a dictionary which is in the dictionary under the given key or else NULL.
  73. */
  74. Dict* Dict_getDict(const Dict* dictionary, const String* key);
  75. /**
  76. * Lookup a list value from a dictionary, if the value is not present
  77. * or is not a list type then NULL is returned.
  78. *
  79. * @param dictionary the dictionary to look the entry up in.
  80. * @param key the key to look the entry up with.
  81. * @return a list which is in the dictionary under the given key or else NULL.
  82. */
  83. List* Dict_getList(const Dict* dictionary, const String* key);
  84. /*----------------------- Dictionary Put Functions -----------------------*/
  85. /**
  86. * Insert an integer into a dictionary.
  87. *
  88. * @param putIntoThis the dictionary to insert an entry into.
  89. * @param key the reference key to use for putting the entry in the dictionary.
  90. * @param value the integer to insert. int64_t is an alias to int64_t.
  91. * @param allocator the means to get memory for storing the dictionary entry wrapper.
  92. * @return if the key already exists in the dictionary then the value which was
  93. * displaced by the put, otherwise NULL.
  94. */
  95. Object* Dict_putInt(Dict* putIntoThis,
  96. const String* key,
  97. int64_t value,
  98. struct Allocator* allocator);
  99. /**
  100. * Insert a String object into another dictionary.
  101. * NOTE: This will not copy the given object, only add a pointer to it in the dictionary.
  102. *
  103. * @param putIntoThis the dictionary to insert an entry into.
  104. * @param key the reference key to use for putting the entry in the dictionary.
  105. * @param value the string to insert.
  106. * @param allocator the means to get memory for storing the dictionary entry wrapper.
  107. * @return if the key already exists in the dictionary then the value which was
  108. * displaced by the put, otherwise NULL.
  109. */
  110. Object* Dict_putString(Dict* putIntoThis,
  111. const String* key,
  112. String* value,
  113. struct Allocator* allocator);
  114. /**
  115. * Insert a Dictionary object into another dictionary.
  116. * NOTE: This will not copy the given object, only add a pointer to it in the dictionary.
  117. *
  118. * @param putIntoThis the dictionary to insert an entry into.
  119. * @param key the reference key to use for putting the entry in the dictionary.
  120. * @param value the value to insert.
  121. * @param allocator the memory allocator to use for getting memory for the entry.
  122. * @return if the key already exists in the dictionary then the value which was
  123. * displaced by the put, otherwise NULL.
  124. */
  125. Object* Dict_putDict(Dict* putIntoThis,
  126. const String* key,
  127. Dict* value,
  128. struct Allocator* allocator);
  129. /**
  130. * Insert a List object into a dictionary.
  131. * NOTE: This will not copy the given object, only add a pointer to it in the dictionary.
  132. *
  133. * @param putIntoThis the dictionary to insert an entry into.
  134. * @param key the reference key to use for putting the entry in the dictionary.
  135. * @param value the list to insert.
  136. * @param allocator the memory allocator to use for getting memory for the entry.
  137. * @return if the key already exists in the dictionary then the value which was
  138. * displaced by the put, otherwise NULL.
  139. */
  140. Object* Dict_putList(Dict* putIntoThis,
  141. const String* key,
  142. List* value,
  143. struct Allocator* allocator);
  144. /*----------------------- Constructors -----------------------*/
  145. /**
  146. * Create a new bencoded dictionary type.
  147. *
  148. * @param allocator the place to allocate the memory for storing the dictionary.
  149. */
  150. Dict* Dict_new(struct Allocator* allocator);
  151. /**
  152. * Create a dictionary on the stack.
  153. *
  154. * @param entryKey the key which must be a string.
  155. * @param value the value which must be an object.
  156. * @param next another Dict, can be used to chain Dict_CONST() calls.
  157. * @return a Dict. NOTE: This does *NOT* return a Dict*, just a Dict.
  158. */
  159. #define Dict_CONST(entryKey, value, nextDict) \
  160. (&(struct Dict_Entry) { \
  161. .key = entryKey, \
  162. .val = value, \
  163. .next = nextDict \
  164. })
  165. #define Dict_OBJ(x) (&(Object) { .type = Object_DICT, .as.dictionary = x })
  166. #endif