The following commit has been merged in the master branch:
commit 45e5ee9e4c9195a488c2e0e246b855e4bcbe6b4f
Author: Guillem Jover <guil...@debian.org>
Date:   Wed Feb 11 03:28:19 2009 +0200

    Refactor package array handling
    
    Create a new pkg_array structure, and two new functions to initialize
    from the db, and to sort the array.

diff --git a/src/Makefile.am b/src/Makefile.am
index d621431..cb3ec93 100644
--- a/src/Makefile.am
+++ b/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
 
diff --git a/src/pkg-array.c b/src/pkg-array.c
index 1b2d306..d5504a0 100644
--- a/src/pkg-array.c
+++ b/src/pkg-array.c
@@ -3,6 +3,7 @@
  * pkg-array.c - primitives for pkg array handling
  *
  * Copyright © 1995,1996 Ian Jackson <i...@chiark.greenend.org.uk>
+ * Copyright © 2009 Guillem Jover <guil...@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);
+}
+
diff --git a/lib/progress.h b/src/pkg-array.h
similarity index 68%
copy from lib/progress.h
copy to src/pkg-array.h
index dad6f3c..b8d336f 100644
--- a/lib/progress.h
+++ b/src/pkg-array.h
@@ -1,6 +1,6 @@
 /*
  * dpkg - main program for package management
- * progress.c - generic progress reporting
+ * pkg-array.h - primitives for pkg array handling
  *
  * Copyright © 2009 Guillem Jover <guil...@debian.org>
  *
@@ -19,28 +19,22 @@
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#ifndef DPKG_PROGRESS_H
-#define DPKG_PROGRESS_H
+#include <config.h>
+#include <compat.h>
 
-#include <dpkg-def.h>
+#include <dpkg-db.h>
 
 DPKG_BEGIN_DECLS
 
-struct progress {
-       const char *text;
+typedef int pkg_sorter_func(const void *a, const void *b);
 
-       int max;
-       int cur;
-       int last_percent;
-
-       int on_tty;
+struct pkg_array {
+       int n_pkgs;
+       struct pkginfo **pkgs;
 };
 
-void progress_init(struct progress *progress, const char *text, int max);
-void progress_step(struct progress *progress);
-void progress_done(struct progress *progress);
+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
 
-#endif
-
diff --git a/src/query.c b/src/query.c
index 31597af..579bd6f 100644
--- a/src/query.c
+++ b/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);
diff --git a/src/select.c b/src/select.c
index c9d6bd6..426e07f 100644
--- a/src/select.c
+++ b/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++;
       }

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to debian-dpkg-cvs-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to