pkg-array.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * pkg-array.c - primitives for pkg array handling
  4. *
  5. * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2009-2014 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 *it;
  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. it = pkg_db_iter_new();
  63. for (i = 0; (pkg = pkg_db_iter_next_pkg(it)); i++)
  64. a->pkgs[i] = pkg;
  65. pkg_db_iter_free(it);
  66. assert(i == a->n_pkgs);
  67. }
  68. /**
  69. * Sort a package array.
  70. *
  71. * @param a The array to sort.
  72. * @param pkg_sort The function to sort the array.
  73. */
  74. void
  75. pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort)
  76. {
  77. qsort(a->pkgs, a->n_pkgs, sizeof(a->pkgs[0]), pkg_sort);
  78. }
  79. /**
  80. * Destroy a package array.
  81. *
  82. * Frees the allocated memory and resets the members.
  83. *
  84. * @param a The array to destroy.
  85. */
  86. void
  87. pkg_array_destroy(struct pkg_array *a)
  88. {
  89. a->n_pkgs = 0;
  90. free(a->pkgs);
  91. a->pkgs = NULL;
  92. }