2 * libdpkg - Debian packaging suite library routines
3 * pkg-array.c - primitives for pkg array handling
5 * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2009-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
28 #include <dpkg/dpkg.h>
29 #include <dpkg/dpkg-db.h>
30 #include <dpkg/pkg-spec.h>
31 #include <dpkg/pkg-array.h>
34 * Initialize a package array from package names.
36 * @param a The array to initialize.
37 * @param pkg_mapper A function that maps a package name to a package instance.
38 * @param pkg_names The package names list.
41 pkg_array_init_from_names(struct pkg_array
*a
, pkg_mapper_func pkg_mapper
,
42 const char **pkg_names
)
50 a
->pkgs
= m_malloc(sizeof(a
->pkgs
[0]) * a
->n_pkgs
);
52 for (i
= 0; pkg_names
[i
]; i
++)
53 a
->pkgs
[i
] = pkg_mapper(pkg_names
[i
]);
57 * Initialize a package array from the package database.
59 * @param a The array to initialize.
62 pkg_array_init_from_hash(struct pkg_array
*a
)
64 struct pkg_hash_iter
*iter
;
68 a
->n_pkgs
= pkg_hash_count_pkg();
69 a
->pkgs
= m_malloc(sizeof(a
->pkgs
[0]) * a
->n_pkgs
);
71 iter
= pkg_hash_iter_new();
72 for (i
= 0; (pkg
= pkg_hash_iter_next_pkg(iter
)); i
++)
74 pkg_hash_iter_free(iter
);
77 internerr("inconsistent state in pkg array: i=%d != npkgs=%d",
82 * Visit each non-NULL package in a package array.
84 * @param a The array to visit.
85 * @param pkg_visitor The function to visit each item of the array.
86 * @param pkg_data Data to pass pkg_visit for each package visited.
89 pkg_array_foreach(struct pkg_array
*a
, pkg_array_visitor_func
*pkg_visitor
,
94 for (i
= 0; i
< a
->n_pkgs
; i
++) {
95 struct pkginfo
*pkg
= a
->pkgs
[i
];
100 pkg_visitor(a
, pkg
, pkg_data
);
105 * Sort a package array.
107 * @param a The array to sort.
108 * @param pkg_sort The function to sort the array.
111 pkg_array_sort(struct pkg_array
*a
, pkg_sorter_func
*pkg_sort
)
113 qsort(a
->pkgs
, a
->n_pkgs
, sizeof(a
->pkgs
[0]), pkg_sort
);
117 * Destroy a package array.
119 * Frees the allocated memory and resets the members.
121 * @param a The array to destroy.
124 pkg_array_destroy(struct pkg_array
*a
)