Pārlūkot izejas kodu

Refactor package array handling

Create a new pkg_array structure, and two new functions to initialize
from the db, and to sort the array.
Guillem Jover 17 gadi atpakaļ
vecāks
revīzija
45e5ee9e4c
5 mainītis faili ar 106 papildinājumiem un 63 dzēšanām
  1. 2 2
      src/Makefile.am
  2. 29 0
      src/pkg-array.c
  3. 40 0
      src/pkg-array.h
  4. 26 43
      src/query.c
  5. 9 18
      src/select.c

+ 2 - 2
src/Makefile.am

@@ -21,7 +21,7 @@ dpkg_SOURCES = \
 	help.c \
 	main.c main.h \
 	packages.c \
-	pkg-array.c \
+	pkg-array.c pkg-array.h \
 	pkg-show.c \
 	processarc.c \
 	remove.c \
@@ -39,7 +39,7 @@ dpkg_LDADD = \
 
 dpkg_query_SOURCES = \
 	filesdb.c filesdb.h \
-	pkg-array.c \
+	pkg-array.c pkg-array.h \
 	pkg-show.c \
 	query.c
 

+ 29 - 0
src/pkg-array.c

@@ -3,6 +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>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
@@ -22,12 +23,16 @@
 #include <config.h>
 #include <compat.h>
 
+#include <assert.h>
 #include <string.h>
+#include <stdlib.h>
 
 #include <dpkg.h>
 #include <dpkg-db.h>
 #include <dpkg-priv.h>
 
+#include "pkg-array.h"
+
 int
 pkglistqsortcmp(const void *a, const void *b)
 {
@@ -37,3 +42,27 @@ pkglistqsortcmp(const void *a, const void *b)
 	return strcmp(pa->name, pb->name);
 }
 
+void
+pkg_array_init_from_db(struct pkg_array *a)
+{
+	struct pkgiterator *it;
+	struct pkginfo *pkg;
+	int i;
+
+	a->n_pkgs = countpackages();
+	a->pkgs = m_malloc(sizeof(a->pkgs[0]) * a->n_pkgs);
+
+	it = iterpkgstart();
+	for (i = 0; (pkg = iterpkgnext(it)); i++)
+		a->pkgs[i] = pkg;
+	iterpkgend(it);
+
+	assert(i == a->n_pkgs);
+}
+
+void
+pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort)
+{
+	qsort(a->pkgs, a->n_pkgs, sizeof(a->pkgs[0]), pkg_sort);
+}
+

+ 40 - 0
src/pkg-array.h

@@ -0,0 +1,40 @@
+/*
+ * dpkg - main program for package management
+ * pkg-array.h - primitives for pkg array handling
+ *
+ * Copyright © 2009 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 the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <dpkg-db.h>
+
+DPKG_BEGIN_DECLS
+
+typedef int pkg_sorter_func(const void *a, const void *b);
+
+struct pkg_array {
+	int n_pkgs;
+	struct pkginfo **pkgs;
+};
+
+void pkg_array_init_from_db(struct pkg_array *a);
+void pkg_array_sort(struct pkg_array *a, pkg_sorter_func *pkg_sort);
+
+DPKG_END_DECLS
+

+ 26 - 43
src/query.c

@@ -28,7 +28,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <fnmatch.h>
-#include <assert.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -45,6 +44,7 @@
 #include <dpkg-priv.h>
 #include <myopt.h>
 
+#include "pkg-array.h"
 #include "filesdb.h"
 #include "main.h"
 
@@ -71,8 +71,9 @@ static int getwidth(void) {
   }
 }
 
-static void list1package(struct pkginfo *pkg, int *head,
-			 struct pkginfo **pkgl, int np) {
+static void
+list1package(struct pkginfo *pkg, int *head, struct pkg_array *array)
+{
   int i,l,w;
   static int nw,vw,dw;
   const char *pdesc;
@@ -82,15 +83,16 @@ static void list1package(struct pkginfo *pkg, int *head,
     w=getwidth();
     if (w == -1) {
       nw=14, vw=14, dw=44;
-      for (i=0; i<np; i++) {
+      for (i = 0; i < array->n_pkgs; i++) {
 	const char *pdesc;
 	int plen, vlen, dlen;
 
 	pdesc = pkg->installed.valid ? pkg->installed.description : NULL;
 	if (!pdesc) pdesc= _("(no description available)");
 
-	plen= strlen(pkgl[i]->name);
-	vlen = strlen(versiondescribe(&pkgl[i]->installed.version, vdew_nonambig));
+	plen = strlen(array->pkgs[i]->name);
+	vlen = strlen(versiondescribe(&array->pkgs[i]->installed.version,
+	                              vdew_nonambig));
 	dlen= strcspn(pdesc, "\n");
 	if (plen > nw) nw = plen;
 	if (vlen > vw) vw = vlen;
@@ -132,31 +134,22 @@ Desired=Unknown/Install/Remove/Purge/Hold\n\
 }
 
 void listpackages(const char *const *argv) {
-  struct pkgiterator *it;
+  struct pkg_array array;
   struct pkginfo *pkg;
-  struct pkginfo **pkgl;
-  int np, i, head;
+  int i, head;
 
   modstatdb_init(admindir,msdbrw_readonly);
 
-  np= countpackages();
-  pkgl= m_malloc(sizeof(struct pkginfo*)*np);
-  it= iterpkgstart(); i=0;
-  while ((pkg= iterpkgnext(it))) {
-    assert(i<np);
-    pkgl[i++]= pkg;
-  }
-  iterpkgend(it);
-  assert(i==np);
+  pkg_array_init_from_db(&array);
+  pkg_array_sort(&array, pkglistqsortcmp);
 
-  qsort(pkgl, np, sizeof(struct pkginfo*), pkglistqsortcmp);
   head = 0;
 
   if (!*argv) {
-    for (i=0; i<np; i++) {
-      pkg= pkgl[i];
+    for (i = 0; i < array.n_pkgs; i++) {
+      pkg = array.pkgs[i];
       if (pkg->status == stat_notinstalled) continue;
-      list1package(pkg, &head, pkgl, np);
+      list1package(pkg, &head, &array);
     }
   } else {
     int argc, ip, *found;
@@ -165,11 +158,11 @@ void listpackages(const char *const *argv) {
     found = m_malloc(sizeof(int) * argc);
     memset(found, 0, sizeof(int) * argc);
 
-    for (i = 0; i < np; i++) {
-      pkg = pkgl[i];
+    for (i = 0; i < array.n_pkgs; i++) {
+      pkg = array.pkgs[i];
       for (ip = 0; ip < argc; ip++) {
         if (!fnmatch(argv[ip], pkg->name, 0)) {
-          list1package(pkg, &head, pkgl, np);
+          list1package(pkg, &head, &array);
           found[ip]++;
           break;
         }
@@ -383,10 +376,9 @@ void enqperpackage(const char *const *argv) {
 }
 
 void showpackages(const char *const *argv) {
-  struct pkgiterator *it;
+  struct pkg_array array;
   struct pkginfo *pkg;
-  struct pkginfo **pkgl;
-  int np, i;
+  int i;
   struct lstitem* fmt = parseformat(showformat);
 
   if (!fmt) {
@@ -396,21 +388,12 @@ void showpackages(const char *const *argv) {
 
   modstatdb_init(admindir,msdbrw_readonly);
 
-  np= countpackages();
-  pkgl= m_malloc(sizeof(struct pkginfo*)*np);
-  it= iterpkgstart(); i=0;
-  while ((pkg= iterpkgnext(it))) {
-    assert(i<np);
-    pkgl[i++]= pkg;
-  }
-  iterpkgend(it);
-  assert(i==np);
+  pkg_array_init_from_db(&array);
+  pkg_array_sort(&array, pkglistqsortcmp);
 
-  qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
-  
   if (!*argv) {
-    for (i=0; i<np; i++) {
-      pkg= pkgl[i];
+    for (i = 0; i < array.n_pkgs; i++) {
+      pkg = array.pkgs[i];
       if (pkg->status == stat_notinstalled) continue;
       show1package(fmt,pkg);
     }
@@ -421,8 +404,8 @@ void showpackages(const char *const *argv) {
     found = m_malloc(sizeof(int) * argc);
     memset(found, 0, sizeof(int) * argc);
 
-    for (i = 0; i < np; i++) {
-      pkg = pkgl[i];
+    for (i = 0; i < array.n_pkgs; i++) {
+      pkg = array.pkgs[i];
       for (ip = 0; ip < argc; ip++) {
         if (!fnmatch(argv[ip], pkg->name, 0)) {
           show1package(fmt, pkg);

+ 9 - 18
src/select.c

@@ -27,13 +27,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
 #include <fnmatch.h>
 #include <ctype.h>
 
 #include <dpkg.h>
 #include <dpkg-db.h>
 
+#include "pkg-array.h"
 #include "filesdb.h"
 #include "main.h"
 
@@ -46,38 +46,29 @@ static void getsel1package(struct pkginfo *pkg) {
 }         
 
 void getselections(const char *const *argv) {
-  struct pkgiterator *it;
+  struct pkg_array array;
   struct pkginfo *pkg;
-  struct pkginfo **pkgl;
   const char *thisarg;
-  int np, i, head, found;
+  int i, head, found;
 
   modstatdb_init(admindir,msdbrw_readonly);
 
-  np= countpackages();
-  pkgl= m_malloc(sizeof(struct pkginfo*)*np);
-  it= iterpkgstart(); i=0;
-  while ((pkg= iterpkgnext(it))) {
-    assert(i<np);
-    pkgl[i++]= pkg;
-  }
-  iterpkgend(it);
-  assert(i==np);
+  pkg_array_init_from_db(&array);
+  pkg_array_sort(&array, pkglistqsortcmp);
 
-  qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
   head=0;
   
   if (!*argv) {
-    for (i=0; i<np; i++) {
-      pkg= pkgl[i];
+    for (i = 0; i < array.n_pkgs; i++) {
+      pkg = array.pkgs[i];
       if (pkg->status == stat_notinstalled) continue;
       getsel1package(pkg);
     }
   } else {
     while ((thisarg= *argv++)) {
       found= 0;
-      for (i=0; i<np; i++) {
-        pkg= pkgl[i];
+      for (i = 0; i < array.n_pkgs; i++) {
+        pkg = array.pkgs[i];
         if (fnmatch(thisarg,pkg->name,0)) continue;
         getsel1package(pkg); found++;
       }