List.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef List_H
  16. #define List_H
  17. #include "memory/Allocator.h"
  18. #include "benc/Object.h"
  19. #include "util/Linker.h"
  20. Linker_require("benc/List.c")
  21. struct List_Item;
  22. struct List_Item {
  23. struct List_Item* next;
  24. Object* elem;
  25. };
  26. List* List_new(struct Allocator* alloc);
  27. /**
  28. * Get the length of a list.
  29. *
  30. * @param a list
  31. * @return the length of the given list or -1 if the list argument is NULL.
  32. */
  33. int32_t List_size(const List* list);
  34. /**
  35. * Add an integer to a list, if the list does not exist then it is allocated.
  36. *
  37. * @param list the list to add the integer item to, if NULL then it is allocated.
  38. * @param toAdd the integer to add to the list. int64_t is an alias for int64_t
  39. * @param allocator the means of getting memory space for storing the list entry.
  40. */
  41. void List_addInt(List* list, int64_t toAdd, struct Allocator* allocator);
  42. /**
  43. * Add a string to a list, if the list does not exist then it is allocated.
  44. * NOTE: This will not copy the given string, only add a pointer to it in the list.
  45. *
  46. * @param list the list to add the string item to, if NULL then it is allocated.
  47. * @param toAdd the string to add to the list.
  48. * @param allocator the means of getting memory space for storing the list entry.
  49. */
  50. void List_addString(List* list, String* toAdd, struct Allocator* allocator);
  51. /**
  52. * Add a dictionary to a list, if the list does not exist then it is allocated.
  53. * NOTE: This will not copy the given dictionary, only add a pointer to it in the list.
  54. *
  55. * @param list the list to add the string item to, if NULL then it is allocated.
  56. * @param toAdd the dictionary to add to the list.
  57. * @param allocator the means of getting memory space for storing the list entry.
  58. */
  59. void List_addDict(List* list, Dict* toAdd, struct Allocator* allocator);
  60. /**
  61. * Add a list as an item to another list, if the list does not exist then it is allocated.
  62. * NOTE: This will not copy the list which it is adding, only add a pointer to it in the list.
  63. *
  64. * @param list the list to add the item to, if NULL then it is allocated.
  65. * @param toAdd the list to add as an item.
  66. * @param allocator the means of getting memory space for storing the list entry.
  67. */
  68. void List_addList(List* list, List* toAdd, struct Allocator* allocator);
  69. /**
  70. * Get an integer from a list.
  71. *
  72. * @param list the list to get the integer item from.
  73. * @param index the index of the item.
  74. * @return a pointer to the integer at that index or NULL if either the index
  75. * is out of range or the item at that index is not an integer.
  76. */
  77. int64_t* List_getInt(const List* list, uint32_t index);
  78. /**
  79. * Get a string from a list.
  80. *
  81. * @param list the list to get the string item from.
  82. * @param index the index of the item.
  83. * @return a pointer to the string at that index or NULL if either the index
  84. * is out of range or the item at that index is not a string.
  85. */
  86. String* List_getString(const List* list, uint32_t index);
  87. /**
  88. * Get a dictionary from a list.
  89. *
  90. * @param list the list to get the dictionary from.
  91. * @param index the index of the item.
  92. * @return a pointer to the string at that index or NULL if either the index
  93. * is out of range or the item at that index is not a dictionary.
  94. */
  95. Dict* List_getDict(const List* list, uint32_t index);
  96. /**
  97. * Get a list from another list.
  98. *
  99. * @param list the list to get the item from.
  100. * @param index the index of the item.
  101. * @return a pointer to the string at that index or NULL if either the index
  102. * is out of range or the item at that index is not a list.
  103. */
  104. List* List_getList(const List* list, uint32_t index);
  105. #define List_OBJ(x) (&(Object) { .type = Object_LIST, .as.list = x })
  106. #endif