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

libdpkg: New deb format version module

Guillem Jover лет назад: 14
Родитель
Сommit
a44743e16e

+ 2 - 0
lib/dpkg/Makefile.am

@@ -33,6 +33,7 @@ libdpkg_a_SOURCES = \
 	compress.c \
 	compress.c \
 	dbdir.c \
 	dbdir.c \
 	dbmodify.c \
 	dbmodify.c \
+	deb-version.c \
 	debug.c \
 	debug.c \
 	depcon.c \
 	depcon.c \
 	dir.c \
 	dir.c \
@@ -83,6 +84,7 @@ pkginclude_HEADERS = \
 	buffer.h \
 	buffer.h \
 	command.h \
 	command.h \
 	compress.h \
 	compress.h \
+	deb-version.h \
 	debug.h \
 	debug.h \
 	dir.h \
 	dir.h \
 	dpkg.h \
 	dpkg.h \

+ 58 - 0
lib/dpkg/deb-version.c

@@ -0,0 +1,58 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * deb-version.c - deb format version handling routines
+ *
+ * Copyright © 2012 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 <string.h>
+#include <stdlib.h>
+
+#include <dpkg/i18n.h>
+#include <dpkg/dpkg.h>
+#include <dpkg/deb-version.h>
+
+const char *
+deb_version_parse(struct deb_version *version, const char *str)
+{
+	const char *str_minor, *end;
+	int major = 0;
+	int minor = 0;
+
+	for (end = str; *end && cisdigit(*end); end++)
+		major = major * 10  + *end - '0';
+
+	if (end == str)
+		return _("format version with empty major component");
+	if (*end != '.')
+		return _("format version has no dot");
+
+	for (end = str_minor = end + 1; *end && cisdigit(*end); end++)
+		minor = minor * 10 + *end - '0';
+
+	if (end == str_minor)
+		return _("format version with empty minor component");
+	if (*end != '\n' && *end != '\0')
+		return _("format version followed by junk");
+
+	version->major = major;
+	version->minor = minor;
+
+	return NULL;
+}

+ 37 - 0
lib/dpkg/deb-version.h

@@ -0,0 +1,37 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * deb-version.h - deb format version handling routines
+ *
+ * Copyright © 2012 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 LIBDPKG_DEB_VERSION_H
+#define LIBDPKG_DEB_VERSION_H
+
+#include <dpkg/macros.h>
+
+DPKG_BEGIN_DECLS
+
+struct deb_version {
+	int major;
+	int minor;
+};
+
+const char *deb_version_parse(struct deb_version *version, const char *str);
+
+DPKG_END_DECLS
+
+#endif /* LIBDPKG_DEB_VERSION_H */

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

@@ -2,6 +2,7 @@ t-ar
 t-arch
 t-arch
 t-buffer
 t-buffer
 t-command
 t-command
+t-deb-version
 t-macros
 t-macros
 t-mod-db
 t-mod-db
 t-path
 t-path

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

@@ -23,6 +23,7 @@ check_PROGRAMS = \
 	t-command \
 	t-command \
 	t-varbuf \
 	t-varbuf \
 	t-ar \
 	t-ar \
+	t-deb-version \
 	t-arch \
 	t-arch \
 	t-version \
 	t-version \
 	t-pkginfo \
 	t-pkginfo \

+ 69 - 0
lib/dpkg/test/t-deb-version.c

@@ -0,0 +1,69 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-deb-version.c - test deb version handling
+ *
+ * Copyright © 2012 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 <dpkg/test.h>
+#include <dpkg/deb-version.h>
+
+static void
+test_deb_version_parse(void)
+{
+	struct deb_version v;
+
+	/* Test valid versions. */
+	test_pass(deb_version_parse(&v, "0.0") == NULL);
+	test_pass(v.major == 0 && v.minor == 0);
+
+	test_pass(deb_version_parse(&v, "1.1") == NULL);
+	test_pass(v.major == 1 && v.minor == 1);
+
+	test_pass(deb_version_parse(&v, "1.001") == NULL);
+	test_pass(v.major == 1 && v.minor == 1);
+
+	test_pass(deb_version_parse(&v, "1.0010") == NULL);
+	test_pass(v.major == 1 && v.minor == 10);
+
+	test_pass(deb_version_parse(&v, "0.939000") == NULL);
+	test_pass(v.major == 0 && v.minor == 939000);
+
+	test_pass(deb_version_parse(&v, "1.1\n") == NULL);
+	test_pass(v.major == 1 && v.minor == 1);
+
+	/* Test invalid versions. */
+	test_fail(deb_version_parse(&v, "0") == NULL);
+	test_fail(deb_version_parse(&v, "a") == NULL);
+	test_fail(deb_version_parse(&v, "a.b") == NULL);
+	test_fail(deb_version_parse(&v, "a~b") == NULL);
+	test_fail(deb_version_parse(&v, " 1.1") == NULL);
+	test_fail(deb_version_parse(&v, "2 .2") == NULL);
+	test_fail(deb_version_parse(&v, "3. 3") == NULL);
+	test_fail(deb_version_parse(&v, "4.4 ") == NULL);
+	test_fail(deb_version_parse(&v, " 5.5 ") == NULL);
+
+	/* FIXME: Complete. */
+}
+
+static void
+test(void)
+{
+	test_deb_version_parse();
+}

+ 1 - 0
po/POTFILES.in

@@ -8,6 +8,7 @@ lib/dpkg/cleanup.c
 lib/dpkg/command.c
 lib/dpkg/command.c
 lib/dpkg/compress.c
 lib/dpkg/compress.c
 lib/dpkg/dbmodify.c
 lib/dpkg/dbmodify.c
+lib/dpkg/deb-version.c
 lib/dpkg/dir.c
 lib/dpkg/dir.c
 lib/dpkg/dump.c
 lib/dpkg/dump.c
 lib/dpkg/ehandle.c
 lib/dpkg/ehandle.c