1
0

List.c 3.7 KB

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