pkg-queue.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * dpkg - main program for package management
  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 <http://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. void
  26. pkg_queue_init(struct pkg_queue *queue)
  27. {
  28. queue->head = NULL;
  29. queue->tail = NULL;
  30. queue->length = 0;
  31. }
  32. void
  33. pkg_queue_destroy(struct pkg_queue *queue)
  34. {
  35. pkg_list_free(queue->head);
  36. pkg_queue_init(queue);
  37. }
  38. int
  39. pkg_queue_is_empty(struct pkg_queue *queue)
  40. {
  41. return (queue->head == NULL);
  42. }
  43. struct pkg_list *
  44. pkg_queue_push(struct pkg_queue *queue, struct pkginfo *pkg)
  45. {
  46. struct pkg_list *node;
  47. node = pkg_list_new(pkg, NULL);
  48. if (queue->tail == NULL)
  49. queue->head = node;
  50. else
  51. queue->tail->next = node;
  52. queue->tail = node;
  53. queue->length++;
  54. return node;
  55. }
  56. struct pkginfo *
  57. pkg_queue_pop(struct pkg_queue *queue)
  58. {
  59. struct pkg_list *node;
  60. struct pkginfo *pkg;
  61. if (pkg_queue_is_empty(queue))
  62. return NULL;
  63. node = queue->head;
  64. pkg = node->pkg;
  65. queue->head = node->next;
  66. if (queue->head == NULL)
  67. queue->tail = NULL;
  68. free(node);
  69. queue->length--;
  70. return pkg;
  71. }