pkg-array.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 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 <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 = pkg_db_count_pkg();
  41. a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
  42. it = pkg_db_iter_new();
  43. for (i = 0; (pkg = pkg_db_iter_next_pkg(it)); i++)
  44. a->pkgs[i] = pkg;
  45. pkg_db_iter_free(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. * Destroy a package array.
  61. *
  62. * Frees the allocated memory and resets the members.
  63. *
  64. * @param a The array to destroy.
  65. */
  66. void
  67. pkg_array_destroy(struct pkg_array *a)
  68. {
  69. a->n_pkgs = 0;
  70. free(a->pkgs);
  71. a->pkgs = NULL;
  72. }