List.h 4.4 KB

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