PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/benc/List.c

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