pkg-array.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * dpkg - main program for package management
  3. * pkg-array.c - primitives for pkg array handling
  4. *
  5. * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2009 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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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 <http://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-array.h>
  29. /**
  30. * Initialize a package array from the package database.
  31. *
  32. * @param a The array to initialize.
  33. */
  34. void
  35. pkg_array_init_from_db(struct pkg_array *a)
  36. {
  37. struct pkgiterator *it;
  38. struct pkginfo *pkg;
  39. int i;
  40. a->n_pkgs = countpackages();
  41. a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
  42. it = iterpkgstart();
  43. for (i = 0; (pkg = iterpkgnext(it)); i++)
  44. a->pkgs[i] = pkg;
  45. iterpkgend(it);
  46. assert(i == a->n_pkgs);
  47. }
  48. /**
  49. * Sort a package array.
  50. *
  51. * @param a The array to sort.
  52. * @param pkg_sort The function to sort the array.
  53. */
  54. void
  55. pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort)
  56. {
  57. qsort(a->pkgs, a->n_pkgs, sizeof(a->pkgs[0]), pkg_sort);
  58. }
  59. /**
  60. * Free a package array.
  61. *
  62. * @param a The array to free.
  63. */
  64. void
  65. pkg_array_free(struct pkg_array *a)
  66. {
  67. a->n_pkgs = 0;
  68. free(a->pkgs);
  69. a->pkgs = NULL;
  70. }