From 4ea2cc3bd4a7d9b1c54a9d33e6a1cf82e7c8c21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 23 Jul 2014 09:06:59 +0200 Subject: Imported Upstream version 0.18.1 --- plugins/common/VersionNumber.vala | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 plugins/common/VersionNumber.vala (limited to 'plugins/common/VersionNumber.vala') diff --git a/plugins/common/VersionNumber.vala b/plugins/common/VersionNumber.vala new file mode 100644 index 0000000..5a69cfe --- /dev/null +++ b/plugins/common/VersionNumber.vala @@ -0,0 +1,50 @@ +/* Copyright 2011-2014 Yorba Foundation + * + * This software is licensed under the GNU Lesser General Public License + * (version 2.1 or later). See the COPYING file in this distribution. + */ + +namespace Utils { + +/** + * A class that represents a version number in the form x.y.z and is able to compare + * different versions. + */ +public class VersionNumber : Object, Gee.Comparable { + private int[] version; + + public VersionNumber(int[] version) { + this.version = version; + } + + public VersionNumber.from_string(string str_version, string separator = ".") { + string[] version_items = str_version.split(separator); + this.version = new int[version_items.length]; + for (int i = 0; i < version_items.length; i++) + this.version[i] = int.parse(version_items[i]); + } + + public string to_string() { + string[] version_items = new string[this.version.length]; + for (int i = 0; i < this.version.length; i++) + version_items[i] = this.version[i].to_string(); + return string.joinv(".", version_items); + } + + public int compare_to(VersionNumber other) { + int max_len = ((this.version.length > other.version.length) ? + this.version.length : other.version.length); + int res = 0; + for(int i = 0; i < max_len; i++) { + int this_v = (i < this.version.length ? this.version[i] : 0); + int other_v = (i < other.version.length ? other.version[i] : 0); + res = this_v - other_v; + if (res != 0) + break; + } + return res; + } +} + +} + -- cgit v1.2.3