[ccan] [PATCH] Add a set of simple version comparison helpers

Peter Hutterer peter.hutterer at who-t.net
Tue Mar 26 10:34:52 EST 2013


These version helpers help to compare major.minor style version numbers,
without the need for open-coded and error-prone bitshifting, multiplication,
etc.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
This could be of use to others, I know I'm sick of doing the manual version
comparison. Comments welcome.

 ccan/versioncmp/LICENSE             |   1 +
 ccan/versioncmp/_info               |  37 +++++++++++++
 ccan/versioncmp/test/compile_fail.c |  11 ++++
 ccan/versioncmp/test/run.c          |  69 ++++++++++++++++++++++++
 ccan/versioncmp/versioncmp.h        | 103 ++++++++++++++++++++++++++++++++++++
 5 files changed, 221 insertions(+)
 create mode 120000 ccan/versioncmp/LICENSE
 create mode 100644 ccan/versioncmp/_info
 create mode 100644 ccan/versioncmp/test/compile_fail.c
 create mode 100644 ccan/versioncmp/test/run.c
 create mode 100644 ccan/versioncmp/versioncmp.h

diff --git a/ccan/versioncmp/LICENSE b/ccan/versioncmp/LICENSE
new file mode 120000
index 0000000..2354d12
--- /dev/null
+++ b/ccan/versioncmp/LICENSE
@@ -0,0 +1 @@
+../../licenses/BSD-MIT
\ No newline at end of file
diff --git a/ccan/versioncmp/_info b/ccan/versioncmp/_info
new file mode 100644
index 0000000..d13916a
--- /dev/null
+++ b/ccan/versioncmp/_info
@@ -0,0 +1,37 @@
+#include <string.h>
+#include "config.h"
+
+/**
+ * versioncmp - helper functions for major.minor-style version numbers
+ *
+ * This code provides some helper functions to deal with version numbers in
+ * the form major.minor.
+ *
+ * Author: Peter Hutterer <peter.hutterer at who-t.net>
+ * Maintainer: Peter Hutterer <peter.hutterer at who-t.net>
+ * License: BSD-MIT
+ *
+ * Example:
+ *	version_t a = version_new(1, 0);
+ *	version_t b = version_new(2, 2);
+ *
+ *	if (version_cmp(&a, &b) < 0)
+ *		printf("Feature supported in version 2.2 but we have %d.%d\n",
+ *			version_major(&a), version_minor(&a));
+ *
+ *	if (version_cmp_numeric(&a, 3, 4) < 0)
+ *		printf("Feature only supported in version 3.4\n");
+ * 	
+ */
+int main(int argc, char *argv[])
+{
+	/* Expect exactly one argument */
+	if (argc != 2)
+		return 1;
+
+	if (strcmp(argv[1], "depends") == 0) {
+		return 0;
+	}
+
+	return 1;
+}
diff --git a/ccan/versioncmp/test/compile_fail.c b/ccan/versioncmp/test/compile_fail.c
new file mode 100644
index 0000000..d2cf6bb
--- /dev/null
+++ b/ccan/versioncmp/test/compile_fail.c
@@ -0,0 +1,11 @@
+#include <ccan/versioncmp/versioncmp.h>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+#ifdef FAIL
+	version_t a;
+	a = 0; /* no direct assignment */
+#endif
+	return 0;
+}
diff --git a/ccan/versioncmp/test/run.c b/ccan/versioncmp/test/run.c
new file mode 100644
index 0000000..e88513f
--- /dev/null
+++ b/ccan/versioncmp/test/run.c
@@ -0,0 +1,69 @@
+#include <ccan/versioncmp/versioncmp.h>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+	version_t a, b;
+
+	plan_tests(26);
+
+	/* cmp with normal versions */
+	a = version_new(1, 0);
+	b = version_new(2, 0);
+	ok1(version_cmp(&a, &b) < 0);
+
+	a = version_new(1, 1);
+	ok1(version_cmp(&a, &b) < 0);
+
+	a = version_new(2, 0);
+	ok1(version_cmp(&a, &b) == 0);
+
+	a = version_new(2, 1);
+	ok1(version_cmp(&a, &b) > 0);
+
+	b = version_new(2, 1);
+	ok1(version_cmp(&a, &b) == 0);
+
+	a = version_new(3, 0);
+	ok1(version_cmp(&a, &b) > 0);
+
+	a = version_new(3, 1);
+	ok1(version_cmp(&a, &b) > 0);
+
+	/* cmp_numeric */
+	ok1(version_cmp_numeric(&a, 1, 0) > 0);
+	ok1(version_cmp_numeric(&a, 1, 1) > 0);
+	ok1(version_cmp_numeric(&a, 3, 0) > 0);
+	ok1(version_cmp_numeric(&a, 3, 1) == 0);
+	ok1(version_cmp_numeric(&a, 3, 2) < 0);
+	ok1(version_cmp_numeric(&a, 4, 0) < 0);
+	ok1(version_cmp_numeric(&a, 4, 1) < 0);
+
+	/* limits */
+	a = version_new(0xFFFF, 0xFFFF);
+	b = version_new(0xFFFE, 0xFFFF);
+	ok1(version_cmp(&a, &b) > 0);
+	ok1(version_cmp(&b, &a) < 0);
+
+	b = version_new(0xFFFF, 0xFFFE);
+	ok1(version_cmp(&a, &b) > 0);
+	ok1(version_cmp(&b, &a) < 0);
+
+	b = version_new(0xFFFF, 0xFFFF);
+	ok1(version_cmp(&a, &b) == 0);
+	ok1(version_cmp(&b, &a) == 0);
+
+	b = version_new(0, 1);
+	ok1(version_cmp(&a, &b) > 0);
+	ok1(version_cmp(&b, &a) < 0);
+
+	b = version_new(1, 0);
+	ok1(version_cmp(&a, &b) > 0);
+	ok1(version_cmp(&b, &a) < 0);
+
+	b = version_new(0, 0);
+	ok1(version_cmp(&a, &b) > 0);
+	ok1(version_cmp(&b, &a) < 0);
+
+	return exit_status();
+}
diff --git a/ccan/versioncmp/versioncmp.h b/ccan/versioncmp/versioncmp.h
new file mode 100644
index 0000000..194c7d0
--- /dev/null
+++ b/ccan/versioncmp/versioncmp.h
@@ -0,0 +1,103 @@
+/*****************************************************************************
+ *
+ * versioncmp - simple version handling functions for major.minor version
+ * types
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ ****************************************************************************/
+
+#ifndef __VERSIONCMP_H__
+#define __VERSIONCMP_H__
+
+#include <stdint.h>
+
+typedef struct {
+	uint32_t _v; /* major << 16  | minor */
+} version_t;
+
+/**
+ * version_major - return the major version of the given struct
+ * @v: the version number to obtain the major number from
+ */
+static inline uint16_t version_major(const version_t* v) {
+	return (v->_v & 0xFFFF0000) >> 16;
+}
+
+/**
+ * version_minor - return the minor version of the given struct
+ * @v: the version number to obtain the minor number from
+ */
+static inline uint16_t version_minor(const version_t* v) {
+	return v->_v & 0xFFFF;
+}
+
+/**
+ * version_new - create a new version number
+ * @major: major version number
+ * @minor: minor version number
+ */
+static inline version_t version_new(uint16_t major, uint16_t minor)
+{
+	version_t v = { ._v = major << 16 | minor };
+	return v;
+}
+
+/**
+ * version_cmp - compare two versions
+ * @a: the first version number
+ * @b: the second version number
+ * @return a number greater, equal, or less than 0 if a is greater, equal or
+ * less than b, respectively
+ *
+ * Example:
+ *	version_t a = version_new(1, 0);
+ *	version_t b = version_new(1, 3);
+ *	if (version_cmp(&a, &b) < 0)
+ *		printf("b is smaller than b\n");
+ */
+static inline int version_cmp(const version_t *a, const version_t *b)
+{
+	return  (a->_v == b->_v) ? 0 : (a->_v > b->_v) ? 1 : - 1;
+}
+
+/**
+ * version_cmp_numeric - compare a version number to a fixed version
+ * @version: the version number
+ * @major: fixed major version to compare to
+ * @minor: fixed minor version to compare to
+ *
+ * @return a number greater, equal, or less than 0 if a is greater, equal or
+ * less than major.minor, respectively
+ *
+ * Example:
+ *	version_t a = version_new(2, 3);
+ *	if (version_cmp_numeric(&a, 4, 0) < 0)
+ *		printf("a is less than version 4.0\n");
+ *
+ */
+static inline int
+version_cmp_numeric(version_t *version, uint16_t major, uint16_t minor)
+{
+	version_t b = version_new(major, minor);
+	return version_cmp(version, &b);
+}
+
+#endif /* __VERSIONCMP_H__ */
+
-- 
1.8.1.4



More information about the ccan mailing list