pkg-array.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * pkg-array.c - primitives for pkg array handling
  4. *
  5. * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2009-2015 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <assert.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <dpkg/dpkg.h>
  27. #include <dpkg/dpkg-db.h>
  28. #include <dpkg/pkg-spec.h>
  29. #include <dpkg/pkg-array.h>
  30. /**
  31. * Initialize a package array from package names.
  32. *
  33. * @param a The array to initialize.
  34. * @param pkg_mapper A function that maps a package name to a package instance.
  35. * @param pkg_names The package names list.
  36. */
  37. void
  38. pkg_array_init_from_names(struct pkg_array *a, pkg_mapper_func pkg_mapper,
  39. const char **pkg_names)
  40. {
  41. int i = 0;
  42. while (pkg_names[i])
  43. i++;
  44. a->n_pkgs = i;
  45. a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
  46. for (i = 0; pkg_names[i]; i++)
  47. a->pkgs[i] = pkg_mapper(pkg_names[i]);
  48. }
  49. /**
  50. * Initialize a package array from the package database.
  51. *
  52. * @param a The array to initialize.
  53. */
  54. void
  55. pkg_array_init_from_db(struct pkg_array *a)
  56. {
  57. struct pkgiterator *iter;
  58. struct pkginfo *pkg;
  59. int i;
  60. a->n_pkgs = pkg_db_count_pkg();
  61. a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
  62. iter = pkg_db_iter_new();
  63. for (i = 0; (pkg = pkg_db_iter_next_pkg(iter)); i++)
  64. a->pkgs[i] = pkg;
  65. pkg_db_iter_free(iter);
  66. assert(i == a->n_pkgs);
  67. }
  68. /**
  69. * Visit each non-NULL package in a package array.
  70. *
  71. * @param a The array to visit.
  72. * @param pkg_visitor The function to visit each item of the array.
  73. * @param pkg_data Data to pass pkg_visit for each package visited.
  74. */
  75. void
  76. pkg_array_foreach(struct pkg_array *a, pkg_array_visitor_func *pkg_visitor,
  77. void *pkg_data)
  78. {
  79. int i;
  80. for (i = 0; i < a->n_pkgs; i++) {
  81. struct pkginfo *pkg = a->pkgs[i];
  82. if (pkg == NULL)
  83. continue;
  84. pkg_visitor(a, pkg, pkg_data);
  85. }
  86. }
  87. /**
  88. * Sort a package array.
  89. *
  90. * @param a The array to sort.
  91. * @param pkg_sort The function to sort the array.
  92. */
  93. void
  94. pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort)
  95. {
  96. qsort(a->pkgs, a->n_pkgs, sizeof(a->pkgs[0]), pkg_sort);
  97. }
  98. /**
  99. * Destroy a package array.
  100. *
  101. * Frees the allocated memory and resets the members.
  102. *
  103. * @param a The array to destroy.
  104. */
  105. void
  106. pkg_array_destroy(struct pkg_array *a)
  107. {
  108. a->n_pkgs = 0;
  109. free(a->pkgs);
  110. a->pkgs = NULL;
  111. }