Просмотр исходного кода

libdpkg: Add a new pkg-queue module

Guillem Jover лет назад: 16
Родитель
Сommit
2e9ea71aef
7 измененных файлов с 258 добавлено и 1 удалено
  1. 1 0
      lib/dpkg/Makefile.am
  2. 90 0
      lib/dpkg/pkg-queue.c
  3. 47 0
      lib/dpkg/pkg-queue.h
  4. 1 0
      lib/dpkg/test/.gitignore
  5. 3 1
      lib/dpkg/test/Makefile.am
  6. 115 0
      lib/dpkg/test/t-pkg-queue.c
  7. 1 0
      po/POTFILES.in

+ 1 - 0
lib/dpkg/Makefile.am

@@ -47,6 +47,7 @@ libdpkg_a_SOURCES = \
 	pkg-array.c pkg-array.h \
 	pkg-array.c pkg-array.h \
 	pkg-format.c pkg-format.h \
 	pkg-format.c pkg-format.h \
 	pkg-list.c pkg-list.h \
 	pkg-list.c pkg-list.h \
+	pkg-queue.c pkg-queue.h \
 	progress.c progress.h \
 	progress.c progress.h \
 	string.c string.h \
 	string.c string.h \
 	subproc.c subproc.h \
 	subproc.c subproc.h \

+ 90 - 0
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 <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 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;
+}
+

+ 47 - 0
lib/dpkg/pkg-queue.h

@@ -0,0 +1,47 @@
+/*
+ * dpkg - main program for package management
+ * pkg-queue.h - primitives for pkg queue handling
+ *
+ * Copyright © 2010 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 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/>.
+ */
+
+#ifndef DPKG_PKG_QUEUE_H
+#define DPKG_PKG_QUEUE_H
+
+#include <dpkg/macros.h>
+#include <dpkg/pkg-list.h>
+
+DPKG_BEGIN_DECLS
+
+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 /* DPKG_PKG_QUEUE_H */

+ 1 - 0
lib/dpkg/test/.gitignore

@@ -5,6 +5,7 @@ t-macros
 t-path
 t-path
 t-pkginfo
 t-pkginfo
 t-pkg-list
 t-pkg-list
+t-pkg-queue
 t-string
 t-string
 t-test
 t-test
 t-varbuf
 t-varbuf

+ 3 - 1
lib/dpkg/test/Makefile.am

@@ -18,7 +18,8 @@ check_PROGRAMS = \
 	t-ar \
 	t-ar \
 	t-version \
 	t-version \
 	t-pkginfo \
 	t-pkginfo \
-	t-pkg-list
+	t-pkg-list \
+	t-pkg-queue
 
 
 CHECK_LDADD = ../libdpkg.a
 CHECK_LDADD = ../libdpkg.a
 
 
@@ -28,6 +29,7 @@ t_macros_LDADD = $(CHECK_LDADD)
 t_path_LDADD = $(CHECK_LDADD)
 t_path_LDADD = $(CHECK_LDADD)
 t_pkginfo_LDADD = $(CHECK_LDADD)
 t_pkginfo_LDADD = $(CHECK_LDADD)
 t_pkg_list_LDADD = $(CHECK_LDADD)
 t_pkg_list_LDADD = $(CHECK_LDADD)
+t_pkg_queue_LDADD = $(CHECK_LDADD)
 t_string_LDADD = $(CHECK_LDADD)
 t_string_LDADD = $(CHECK_LDADD)
 t_buffer_LDADD = $(CHECK_LDADD)
 t_buffer_LDADD = $(CHECK_LDADD)
 t_test_LDADD = $(CHECK_LDADD)
 t_test_LDADD = $(CHECK_LDADD)

+ 115 - 0
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 <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 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();
+}
+

+ 1 - 0
po/POTFILES.in

@@ -25,6 +25,7 @@ lib/dpkg/pkg.c
 lib/dpkg/pkg-array.c
 lib/dpkg/pkg-array.c
 lib/dpkg/pkg-format.c
 lib/dpkg/pkg-format.c
 lib/dpkg/pkg-list.c
 lib/dpkg/pkg-list.c
+lib/dpkg/pkg-queue.c
 lib/dpkg/progress.c
 lib/dpkg/progress.c
 lib/dpkg/string.c
 lib/dpkg/string.c
 lib/dpkg/subproc.c
 lib/dpkg/subproc.c