pkg-array.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.h>
  28. #include <dpkg-db.h>
  29. #include <dpkg-priv.h>
  30. #include "pkg-array.h"
  31. int
  32. pkglistqsortcmp(const void *a, const void *b)
  33. {
  34. const struct pkginfo *pa = *(const struct pkginfo **)a;
  35. const struct pkginfo *pb = *(const struct pkginfo **)b;
  36. return strcmp(pa->name, pb->name);
  37. }
  38. void
  39. pkg_array_init_from_db(struct pkg_array *a)
  40. {
  41. struct pkgiterator *it;
  42. struct pkginfo *pkg;
  43. int i;
  44. a->n_pkgs = countpackages();
  45. a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
  46. it = iterpkgstart();
  47. for (i = 0; (pkg = iterpkgnext(it)); i++)
  48. a->pkgs[i] = pkg;
  49. iterpkgend(it);
  50. assert(i == a->n_pkgs);
  51. }
  52. void
  53. pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort)
  54. {
  55. qsort(a->pkgs, a->n_pkgs, sizeof(a->pkgs[0]), pkg_sort);
  56. }
  57. void
  58. pkg_array_free(struct pkg_array *a)
  59. {
  60. a->n_pkgs = 0;
  61. free(a->pkgs);
  62. a->pkgs = NULL;
  63. }