Branch data Line data Source code
1 : : /*
2 : : * libdpkg - Debian packaging suite library routines
3 : : * pkg-array.c - primitives for pkg array handling
4 : : *
5 : : * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 : : * Copyright © 2009-2015 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 <https://www.gnu.org/licenses/>.
20 : : */
21 : :
22 : : #include <config.h>
23 : : #include <compat.h>
24 : :
25 : : #include <string.h>
26 : : #include <stdlib.h>
27 : :
28 : : #include <dpkg/dpkg.h>
29 : : #include <dpkg/dpkg-db.h>
30 : : #include <dpkg/pkg-spec.h>
31 : : #include <dpkg/pkg-array.h>
32 : :
33 : : /**
34 : : * Initialize a package array from package names.
35 : : *
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.
39 : : */
40 : : void
41 : 0 : pkg_array_init_from_names(struct pkg_array *a, pkg_mapper_func pkg_mapper,
42 : : const char **pkg_names)
43 : : {
44 : 0 : int i = 0;
45 : :
46 [ # # ]: 0 : while (pkg_names[i])
47 : 0 : i++;
48 : :
49 : 0 : a->n_pkgs = i;
50 : 0 : a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
51 : :
52 [ # # ]: 0 : for (i = 0; pkg_names[i]; i++)
53 : 0 : a->pkgs[i] = pkg_mapper(pkg_names[i]);
54 : 0 : }
55 : :
56 : : /**
57 : : * Initialize a package array from the package database.
58 : : *
59 : : * @param a The array to initialize.
60 : : */
61 : : void
62 : 0 : pkg_array_init_from_hash(struct pkg_array *a)
63 : : {
64 : : struct pkg_hash_iter *iter;
65 : : struct pkginfo *pkg;
66 : : int i;
67 : :
68 : 0 : a->n_pkgs = pkg_hash_count_pkg();
69 : 0 : a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
70 : :
71 : 0 : iter = pkg_hash_iter_new();
72 [ # # ]: 0 : for (i = 0; (pkg = pkg_hash_iter_next_pkg(iter)); i++)
73 : 0 : a->pkgs[i] = pkg;
74 : 0 : pkg_hash_iter_free(iter);
75 : :
76 [ # # ]: 0 : if (i != a->n_pkgs)
77 : 0 : internerr("inconsistent state in pkg array: i=%d != npkgs=%d",
78 : : i, a->n_pkgs);
79 : 0 : }
80 : :
81 : : /**
82 : : * Visit each non-NULL package in a package array.
83 : : *
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.
87 : : */
88 : : void
89 : 0 : pkg_array_foreach(struct pkg_array *a, pkg_array_visitor_func *pkg_visitor,
90 : : void *pkg_data)
91 : : {
92 : : int i;
93 : :
94 [ # # ]: 0 : for (i = 0; i < a->n_pkgs; i++) {
95 : 0 : struct pkginfo *pkg = a->pkgs[i];
96 : :
97 [ # # ]: 0 : if (pkg == NULL)
98 : 0 : continue;
99 : :
100 : 0 : pkg_visitor(a, pkg, pkg_data);
101 : : }
102 : 0 : }
103 : :
104 : : /**
105 : : * Sort a package array.
106 : : *
107 : : * @param a The array to sort.
108 : : * @param pkg_sort The function to sort the array.
109 : : */
110 : : void
111 : 0 : pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort)
112 : : {
113 : 0 : qsort(a->pkgs, a->n_pkgs, sizeof(a->pkgs[0]), pkg_sort);
114 : 0 : }
115 : :
116 : : /**
117 : : * Destroy a package array.
118 : : *
119 : : * Frees the allocated memory and resets the members.
120 : : *
121 : : * @param a The array to destroy.
122 : : */
123 : : void
124 : 0 : pkg_array_destroy(struct pkg_array *a)
125 : : {
126 : 0 : a->n_pkgs = 0;
127 : 0 : free(a->pkgs);
128 : 0 : a->pkgs = NULL;
129 : 0 : }
|