PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/benc/List.c

https://github.com/elliotschlegelmilch/cjdns
C | 138 lines | 104 code | 13 blank | 21 comment | 30 complexity | 1bdb184632fb39f96f0b3500ce972caf MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * You may redistribute this program and/or modify it under the terms of
  3. * the GNU General Public License as published by the Free Software Foundation,
  4. * either version 3 of the License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "memory/Allocator.h"
  15. #include "benc/List.h"
  16. int32_t List_size(const List* list)
  17. {
  18. if (list != NULL) {
  19. struct List_Item* item = *list;
  20. int32_t i;
  21. for (i = 0; item != NULL; i++) {
  22. item = item->next;
  23. }
  24. return i;
  25. }
  26. return -1;
  27. }
  28. static List* addObject(List* list, Object* item, const struct Allocator* allocator)
  29. {
  30. if (list == NULL) {
  31. List* newList = allocator->calloc(sizeof(List), 1, allocator);
  32. return addObject(newList, item, allocator);
  33. }
  34. struct List_Item* entry = allocator->malloc(sizeof(struct List_Item), allocator);
  35. entry->next = *list;
  36. entry->elem = item;
  37. *list = entry;
  38. return list;
  39. }
  40. /** @see Object.h */
  41. List* List_addInt(List* list, int64_t toAdd, const struct Allocator* allocator)
  42. {
  43. Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
  44. .type = Object_INTEGER,
  45. .as.number = toAdd
  46. });
  47. return addObject(list, obj, allocator);
  48. }
  49. /** @see Object.h */
  50. List* List_addString(List* list, String* toAdd, const struct Allocator* allocator)
  51. {
  52. Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
  53. .type = Object_STRING,
  54. .as.string = toAdd
  55. });
  56. return addObject(list, obj, allocator);
  57. }
  58. /** @see Object.h */
  59. List* List_addDict(List* list, Dict* toAdd, const struct Allocator* allocator)
  60. {
  61. Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
  62. .type = Object_DICT,
  63. .as.dictionary = toAdd
  64. });
  65. return addObject(list, obj, allocator);
  66. }
  67. /** @see Object.h */
  68. List* List_addList(List* list, List* toAdd, const struct Allocator* allocator)
  69. {
  70. Object* obj = allocator->clone(sizeof(Object), allocator, &(Object) {
  71. .type = Object_LIST,
  72. .as.list = toAdd
  73. });
  74. return addObject(list, obj, allocator);
  75. }
  76. static Object* getObject(const List* list, uint32_t index)
  77. {
  78. if (list != NULL && *list != NULL) {
  79. struct List_Item* entry = *list;
  80. uint32_t i;
  81. for (i = 0; entry != NULL; i++) {
  82. if (i == index) {
  83. return entry->elem;
  84. }
  85. entry = entry->next;
  86. }
  87. }
  88. return NULL;
  89. }
  90. /** @see Object.h */
  91. int64_t* List_getInt(const List* list, uint32_t index)
  92. {
  93. Object* o = getObject(list, index);
  94. if (o != NULL && o->type == Object_INTEGER) {
  95. return &(o->as.number);
  96. }
  97. return NULL;
  98. }
  99. /** @see Object.h */
  100. String* List_getString(const List* list, uint32_t index)
  101. {
  102. Object* o = getObject(list, index);
  103. if (o != NULL && o->type == Object_STRING) {
  104. return o->as.string;
  105. }
  106. return NULL;
  107. }
  108. /** @see Object.h */
  109. Dict* List_getDict(const List* list, uint32_t index)
  110. {
  111. Object* o = getObject(list, index);
  112. if (o != NULL && o->type == Object_DICT) {
  113. return o->as.dictionary;
  114. }
  115. return NULL;
  116. }
  117. /** @see Object.h */
  118. List* List_getList(const List* list, uint32_t index)
  119. {
  120. Object* o = getObject(list, index);
  121. if (o != NULL && o->type == Object_LIST) {
  122. return o->as.list;
  123. }
  124. return NULL;
  125. }