The following commit has been merged in the master branch:
commit 2e9ea71aeff38e53b56348b6b89b00a1fe0cb9e9
Author: Guillem Jover <[email protected]>
Date: Sun Feb 21 05:52:10 2010 +0100
libdpkg: Add a new pkg-queue module
diff --git a/lib/dpkg/Makefile.am b/lib/dpkg/Makefile.am
index 2c2009f..1699784 100644
--- a/lib/dpkg/Makefile.am
+++ b/lib/dpkg/Makefile.am
@@ -47,6 +47,7 @@ libdpkg_a_SOURCES = \
pkg-array.c pkg-array.h \
pkg-format.c pkg-format.h \
pkg-list.c pkg-list.h \
+ pkg-queue.c pkg-queue.h \
progress.c progress.h \
string.c string.h \
subproc.c subproc.h \
diff --git a/lib/dpkg/pkg-queue.c b/lib/dpkg/pkg-queue.c
new file mode 100644
index 0000000..c85c965
--- /dev/null
+++ b/lib/dpkg/pkg-queue.c
@@ -0,0 +1,90 @@
+/*
+ * dpkg - main program for package management
+ * pkg-queue.c - primitives for pkg queue handling
+ *
+ * Copyright © 2010 Guillem Jover <[email protected]>
+ *
+ * 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <stdlib.h>
+
+#include <dpkg/dpkg-db.h>
+#include <dpkg/pkg-queue.h>
+
+void
+pkg_queue_init(struct pkg_queue *queue)
+{
+ queue->head = NULL;
+ queue->tail = NULL;
+ queue->length = 0;
+}
+
+void
+pkg_queue_destroy(struct pkg_queue *queue)
+{
+ pkg_list_free(queue->head);
+ pkg_queue_init(queue);
+}
+
+int
+pkg_queue_is_empty(struct pkg_queue *queue)
+{
+ return (queue->head == NULL);
+}
+
+struct pkg_list *
+pkg_queue_push(struct pkg_queue *queue, struct pkginfo *pkg)
+{
+ struct pkg_list *node;
+
+ node = pkg_list_new(pkg, NULL);
+
+ if (queue->tail == NULL)
+ queue->head = node;
+ else
+ queue->tail->next = node;
+
+ queue->tail = node;
+
+ queue->length++;
+
+ return node;
+}
+
+struct pkginfo *
+pkg_queue_pop(struct pkg_queue *queue)
+{
+ struct pkg_list *node;
+ struct pkginfo *pkg;
+
+ if (pkg_queue_is_empty(queue))
+ return NULL;
+
+ node = queue->head;
+ pkg = node->pkg;
+
+ queue->head = node->next;
+ if (queue->head == NULL)
+ queue->tail = NULL;
+
+ free(node);
+ queue->length--;
+
+ return pkg;
+}
+
diff --git a/lib/dpkg/ar.h b/lib/dpkg/pkg-queue.h
similarity index 54%
copy from lib/dpkg/ar.h
copy to lib/dpkg/pkg-queue.h
index 0aa64f4..751dbce 100644
--- a/lib/dpkg/ar.h
+++ b/lib/dpkg/pkg-queue.h
@@ -1,6 +1,6 @@
/*
- * libdpkg - Debian packaging suite library routines
- * ar.c - primitives for ar handling
+ * dpkg - main program for package management
+ * pkg-queue.h - primitives for pkg queue handling
*
* Copyright © 2010 Guillem Jover <[email protected]>
*
@@ -18,20 +18,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef LIBDPKG_AR_H
-#define LIBDPKG_AR_H
-
-#include <config.h>
-#include <compat.h>
-
-#include <ar.h>
+#ifndef DPKG_PKG_QUEUE_H
+#define DPKG_PKG_QUEUE_H
#include <dpkg/macros.h>
+#include <dpkg/pkg-list.h>
DPKG_BEGIN_DECLS
-void dpkg_ar_normalize_name(struct ar_hdr *arh);
+struct pkg_queue {
+ struct pkg_list *head, *tail;
+ int length;
+};
+
+#define PKG_QUEUE_INIT \
+ (struct pkg_queue){ .head = NULL, .tail = NULL, .length = 0 }
+
+void pkg_queue_init(struct pkg_queue *queue);
+void pkg_queue_destroy(struct pkg_queue *queue);
+
+int pkg_queue_is_empty(struct pkg_queue *queue);
+
+struct pkg_list *pkg_queue_push(struct pkg_queue *queue, struct pkginfo *pkg);
+struct pkginfo *pkg_queue_pop(struct pkg_queue *queue);
DPKG_END_DECLS
-#endif /* LIBDPKG_AR_H */
+#endif /* DPKG_PKG_QUEUE_H */
diff --git a/lib/dpkg/test/.gitignore b/lib/dpkg/test/.gitignore
index 405576b..93819ca 100644
--- a/lib/dpkg/test/.gitignore
+++ b/lib/dpkg/test/.gitignore
@@ -5,6 +5,7 @@ t-macros
t-path
t-pkginfo
t-pkg-list
+t-pkg-queue
t-string
t-test
t-varbuf
diff --git a/lib/dpkg/test/Makefile.am b/lib/dpkg/test/Makefile.am
index db302c5..610ca27 100644
--- a/lib/dpkg/test/Makefile.am
+++ b/lib/dpkg/test/Makefile.am
@@ -18,7 +18,8 @@ check_PROGRAMS = \
t-ar \
t-version \
t-pkginfo \
- t-pkg-list
+ t-pkg-list \
+ t-pkg-queue
CHECK_LDADD = ../libdpkg.a
@@ -28,6 +29,7 @@ t_macros_LDADD = $(CHECK_LDADD)
t_path_LDADD = $(CHECK_LDADD)
t_pkginfo_LDADD = $(CHECK_LDADD)
t_pkg_list_LDADD = $(CHECK_LDADD)
+t_pkg_queue_LDADD = $(CHECK_LDADD)
t_string_LDADD = $(CHECK_LDADD)
t_buffer_LDADD = $(CHECK_LDADD)
t_test_LDADD = $(CHECK_LDADD)
diff --git a/lib/dpkg/test/t-pkg-queue.c b/lib/dpkg/test/t-pkg-queue.c
new file mode 100644
index 0000000..ece9a04
--- /dev/null
+++ b/lib/dpkg/test/t-pkg-queue.c
@@ -0,0 +1,115 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-pkg-queue.c - test pkg-queue implementation
+ *
+ * Copyright © 2010 Guillem Jover <[email protected]>
+ *
+ * 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <dpkg/test.h>
+#include <dpkg/dpkg-db.h>
+#include <dpkg/pkg-queue.h>
+
+static void
+test_pkg_queue_init(void)
+{
+ struct pkg_queue q = PKG_QUEUE_INIT;
+ struct pkg_list l;
+
+ test_pass(q.length == 0);
+ test_pass(q.head == NULL);
+ test_pass(q.tail == NULL);
+
+ test_pass(pkg_queue_is_empty(&q));
+
+ q = (struct pkg_queue){ .length = 10, .head = &l, .tail = &l };
+
+ pkg_queue_init(&q);
+ test_pass(q.length == 0);
+ test_pass(q.head == NULL);
+ test_pass(q.tail == NULL);
+
+ test_pass(pkg_queue_is_empty(&q));
+}
+
+static void
+test_pkg_queue_push_pop(void)
+{
+ struct pkg_queue q = PKG_QUEUE_INIT;
+ struct pkg_list *l1, *l2, *l3;
+ struct pkginfo pkg, *pkgp;
+
+ blankpackage(&pkg);
+
+ test_pass(pkg_queue_is_empty(&q));
+
+ /* Test push operations. */
+
+ l1 = pkg_queue_push(&q, &pkg);
+ test_pass(l1 != NULL);
+ test_pass(q.head == l1);
+ test_pass(q.tail == l1);
+ test_pass(q.length == 1);
+
+ l2 = pkg_queue_push(&q, &pkg);
+ test_pass(l2 != NULL);
+ test_pass(q.head == l1);
+ test_pass(q.tail == l2);
+ test_pass(q.length == 2);
+
+ l3 = pkg_queue_push(&q, &pkg);
+ test_pass(l3 != NULL);
+ test_pass(q.head == l1);
+ test_pass(q.tail == l3);
+ test_pass(q.length == 3);
+
+ /* Test pop operations. */
+
+ pkgp = pkg_queue_pop(&q);
+ test_pass(pkgp != NULL);
+ test_pass(q.head == l2);
+ test_pass(q.tail == l3);
+ test_pass(q.length == 2);
+
+ pkgp = pkg_queue_pop(&q);
+ test_pass(pkgp != NULL);
+ test_pass(q.head == l3);
+ test_pass(q.tail == l3);
+ test_pass(q.length == 1);
+
+ pkgp = pkg_queue_pop(&q);
+ test_pass(pkgp != NULL);
+ test_pass(q.head == NULL);
+ test_pass(q.tail == NULL);
+ test_pass(q.length == 0);
+
+ test_pass(pkg_queue_is_empty(&q));
+
+ pkgp = pkg_queue_pop(&q);
+ test_pass(pkgp == NULL);
+ test_pass(q.head == NULL);
+ test_pass(q.tail == NULL);
+ test_pass(q.length == 0);
+
+ pkg_queue_destroy(&q);
+}
+
+static void
+test(void)
+{
+ test_pkg_queue_init();
+ test_pkg_queue_push_pop();
+}
+
diff --git a/po/POTFILES.in b/po/POTFILES.in
index c322be5..1fad898 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -25,6 +25,7 @@ lib/dpkg/pkg.c
lib/dpkg/pkg-array.c
lib/dpkg/pkg-format.c
lib/dpkg/pkg-list.c
+lib/dpkg/pkg-queue.c
lib/dpkg/progress.c
lib/dpkg/string.c
lib/dpkg/subproc.c
--
dpkg's main repository
--
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]