po: Update German man pages translation
[dpkg.git] / lib / dpkg / pkg-array.c
blob0ce285e925a11149387e3e0cfee0032877f8e8bf
1 /*
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/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <string.h>
26 #include <stdlib.h>
28 #include <dpkg/dpkg.h>
29 #include <dpkg/dpkg-db.h>
30 #include <dpkg/pkg-spec.h>
31 #include <dpkg/pkg-array.h>
33 /**
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.
40 void
41 pkg_array_init_from_names(struct pkg_array *a, pkg_mapper_func pkg_mapper,
42 const char **pkg_names)
44 int i = 0;
46 while (pkg_names[i])
47 i++;
49 a->n_pkgs = i;
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]);
56 /**
57 * Initialize a package array from the package database.
59 * @param a The array to initialize.
61 void
62 pkg_array_init_from_hash(struct pkg_array *a)
64 struct pkg_hash_iter *iter;
65 struct pkginfo *pkg;
66 int i;
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++)
73 a->pkgs[i] = pkg;
74 pkg_hash_iter_free(iter);
76 if (i != a->n_pkgs)
77 internerr("inconsistent state in pkg array: i=%d != npkgs=%d",
78 i, a->n_pkgs);
81 /**
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.
88 void
89 pkg_array_foreach(struct pkg_array *a, pkg_array_visitor_func *pkg_visitor,
90 void *pkg_data)
92 int i;
94 for (i = 0; i < a->n_pkgs; i++) {
95 struct pkginfo *pkg = a->pkgs[i];
97 if (pkg == NULL)
98 continue;
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.
110 void
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.
123 void
124 pkg_array_destroy(struct pkg_array *a)
126 a->n_pkgs = 0;
127 free(a->pkgs);
128 a->pkgs = NULL;