pkg-queue.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * pkg-queue.c - primitives for pkg queue handling
  4. *
  5. * Copyright © 2010 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <stdlib.h>
  23. #include <dpkg/dpkg-db.h>
  24. #include <dpkg/pkg-queue.h>
  25. /**
  26. * Initialize a package queue.
  27. *
  28. * @param queue The queue to initialize.
  29. */
  30. void
  31. pkg_queue_init(struct pkg_queue *queue)
  32. {
  33. queue->head = NULL;
  34. queue->tail = NULL;
  35. queue->length = 0;
  36. }
  37. /**
  38. * Destroy a package queue.
  39. *
  40. * It frees the contained package list and resets the queue members.
  41. *
  42. * @param queue The queue to destroy.
  43. */
  44. void
  45. pkg_queue_destroy(struct pkg_queue *queue)
  46. {
  47. pkg_list_free(queue->head);
  48. pkg_queue_init(queue);
  49. }
  50. /**
  51. * Check if a package queue is empty.
  52. *
  53. * @param queue The queue to check.
  54. *
  55. * @return A boolean value.
  56. */
  57. int
  58. pkg_queue_is_empty(struct pkg_queue *queue)
  59. {
  60. return (queue->head == NULL);
  61. }
  62. /**
  63. * Push a new node containing pkginfo to the tail of the queue.
  64. *
  65. * @param queue The queue to insert to.
  66. * @param pkg The package to use fo the new node.
  67. *
  68. * @return The newly inserted pkg_list node.
  69. */
  70. struct pkg_list *
  71. pkg_queue_push(struct pkg_queue *queue, struct pkginfo *pkg)
  72. {
  73. struct pkg_list *node;
  74. node = pkg_list_new(pkg, NULL);
  75. if (queue->tail == NULL)
  76. queue->head = node;
  77. else
  78. queue->tail->next = node;
  79. queue->tail = node;
  80. queue->length++;
  81. return node;
  82. }
  83. /**
  84. * Pop a node containing pkginfo from the head of the queue.
  85. *
  86. * This removes and frees the node from the queue, effectively reducing its
  87. * size.
  88. *
  89. * @param queue The queue to remove from.
  90. *
  91. * @return The pkginfo from the removed node, or NULL if the queue was empty.
  92. */
  93. struct pkginfo *
  94. pkg_queue_pop(struct pkg_queue *queue)
  95. {
  96. struct pkg_list *node;
  97. struct pkginfo *pkg;
  98. if (pkg_queue_is_empty(queue))
  99. return NULL;
  100. node = queue->head;
  101. pkg = node->pkg;
  102. queue->head = node->next;
  103. if (queue->head == NULL)
  104. queue->tail = NULL;
  105. free(node);
  106. queue->length--;
  107. return pkg;
  108. }