pkg-array.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <assert.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <dpkg/dpkg.h>
  28. #include <dpkg/dpkg-db.h>
  29. #include <dpkg/pkg-array.h>
  30. int
  31. pkglistqsortcmp(const void *a, const void *b)
  32. {
  33. const struct pkginfo *pa = *(const struct pkginfo **)a;
  34. const struct pkginfo *pb = *(const struct pkginfo **)b;
  35. return strcmp(pa->name, pb->name);
  36. }
  37. void
  38. pkg_array_init_from_db(struct pkg_array *a)
  39. {
  40. struct pkgiterator *it;
  41. struct pkginfo *pkg;
  42. int i;
  43. a->n_pkgs = countpackages();
  44. a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
  45. it = iterpkgstart();
  46. for (i = 0; (pkg = iterpkgnext(it)); i++)
  47. a->pkgs[i] = pkg;
  48. iterpkgend(it);
  49. assert(i == a->n_pkgs);
  50. }
  51. void
  52. pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort)
  53. {
  54. qsort(a->pkgs, a->n_pkgs, sizeof(a->pkgs[0]), pkg_sort);
  55. }
  56. void
  57. pkg_array_free(struct pkg_array *a)
  58. {
  59. a->n_pkgs = 0;
  60. free(a->pkgs);
  61. a->pkgs = NULL;
  62. }