List.c 3.5 KB

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