Parcourir la source

libdpkg: Add new pkg_array_init_from_names() function

Guillem Jover il y a 12 ans
Parent
commit
c52a62fdf8
3 fichiers modifiés avec 31 ajouts et 2 suppressions
  1. 1 0
      lib/dpkg/libdpkg.map
  2. 25 1
      lib/dpkg/pkg-array.c
  3. 5 1
      lib/dpkg/pkg-array.h

+ 1 - 0
lib/dpkg/libdpkg.map

@@ -257,6 +257,7 @@ LIBDPKG_PRIVATE {
 
 	# Package array handling
 	pkg_array_init_from_db;
+	pkg_array_init_from_names;
 	pkg_array_sort;
 	pkg_array_destroy;
 

+ 25 - 1
lib/dpkg/pkg-array.c

@@ -3,7 +3,7 @@
  * pkg-array.c - primitives for pkg array handling
  *
  * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
- * Copyright © 2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,8 +28,32 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/pkg-spec.h>
 #include <dpkg/pkg-array.h>
 
+/**
+ * Initialize a package array from package names.
+ *
+ * @param a          The array to initialize.
+ * @param pkg_mapper A function that maps a package name to a package instance.
+ * @param pkg_names  The package names list.
+ */
+void
+pkg_array_init_from_names(struct pkg_array *a, pkg_mapper_func pkg_mapper,
+                          const char **pkg_names)
+{
+	int i = 0;
+
+	while (pkg_names[i])
+		i++;
+
+	a->n_pkgs = i;
+	a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
+
+	for (i = 0; pkg_names[i]; i++)
+		a->pkgs[i] = pkg_mapper(pkg_names[i]);
+}
+
 /**
  * Initialize a package array from the package database.
  *

+ 5 - 1
lib/dpkg/pkg-array.h

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * pkg-array.h - primitives for pkg array handling
  *
- * Copyright © 2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -40,7 +40,11 @@ struct pkg_array {
 	struct pkginfo **pkgs;
 };
 
+typedef struct pkginfo *pkg_mapper_func(const char *name);
+
 void pkg_array_init_from_db(struct pkg_array *a);
+void pkg_array_init_from_names(struct pkg_array *a, pkg_mapper_func *pkg_mapper,
+                               const char **pkg_names);
 void pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort);
 void pkg_array_destroy(struct pkg_array *a);