List.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "benc/List.h"
  17. #include <stddef.h>
  18. List* List_new(struct Allocator* alloc)
  19. {
  20. return Allocator_calloc(alloc, sizeof(List), 1);
  21. }
  22. int32_t List_size(const List* list)
  23. {
  24. Assert_true(list);
  25. struct List_Item* item = *list;
  26. uint32_t i = 0;
  27. for (; item; i++) {
  28. item = item->next;
  29. }
  30. return i;
  31. }
  32. static void addObject(List* list, Object* item, struct Allocator* allocator)
  33. {
  34. Assert_true(list);
  35. struct List_Item* entry = Allocator_malloc(allocator, sizeof(struct List_Item));
  36. entry->next = *list;
  37. entry->elem = item;
  38. *list = entry;
  39. }
  40. #define ADD(list, asType, typeName, thing, alloc) \
  41. addObject(list, Allocator_clone(alloc, (&(Object) { \
  42. .type = typeName, \
  43. .as.asType = thing \
  44. })), alloc)
  45. /** @see Object.h */
  46. void List_addInt(List* list, int64_t toAdd, struct Allocator* allocator)
  47. {
  48. ADD(list, number, Object_INTEGER, toAdd, allocator);
  49. }
  50. /** @see Object.h */
  51. void List_addString(List* list, String* toAdd, struct Allocator* allocator)
  52. {
  53. ADD(list, string, Object_STRING, toAdd, allocator);
  54. }
  55. /** @see Object.h */
  56. void List_addDict(List* list, Dict* toAdd, struct Allocator* allocator)
  57. {
  58. ADD(list, dictionary, Object_DICT, toAdd, allocator);
  59. }
  60. /** @see Object.h */
  61. void List_addList(List* list, List* toAdd, struct Allocator* allocator)
  62. {
  63. ADD(list, list, Object_LIST, toAdd, allocator);
  64. }
  65. static Object* getObject(const List* list, uint32_t index)
  66. {
  67. Assert_true(list);
  68. struct List_Item* entry = *list;
  69. for (uint32_t i = 0; entry; i++) {
  70. if (i == index) {
  71. return entry->elem;
  72. }
  73. entry = entry->next;
  74. }
  75. return NULL;
  76. }
  77. /** @see Object.h */
  78. int64_t* List_getInt(const List* list, uint32_t index)
  79. {
  80. Object* o = getObject(list, index);
  81. if (o && o->type == Object_INTEGER) {
  82. return &(o->as.number);
  83. }
  84. return NULL;
  85. }
  86. #define GET(list, index, asType, typeName) \
  87. do { \
  88. Object* o = getObject(list, index); \
  89. if (o && o->type == typeName) { \
  90. return o->as.asType; \
  91. } \
  92. return NULL; \
  93. } while (0)
  94. // CHECKFILES_IGNORE // expecting a ; or bracket
  95. /** @see Object.h */
  96. String* List_getString(const List* list, uint32_t index)
  97. {
  98. GET(list, index, string, Object_STRING);
  99. }
  100. /** @see Object.h */
  101. Dict* List_getDict(const List* list, uint32_t index)
  102. {
  103. GET(list, index, dictionary, Object_DICT);
  104. }
  105. /** @see Object.h */
  106. List* List_getList(const List* list, uint32_t index)
  107. {
  108. GET(list, index, list, Object_LIST);
  109. }