List.c 3.6 KB

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