[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[virt-tools-list] [PATCH virt-viewer 2/4] Add virt_viewer_compare_version()



---
 src/virt-viewer-util.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/virt-viewer-util.h |  1 +
 2 files changed, 55 insertions(+)

diff --git a/src/virt-viewer-util.c b/src/virt-viewer-util.c
index f0b1182..a32138c 100644
--- a/src/virt-viewer-util.c
+++ b/src/virt-viewer-util.c
@@ -414,6 +414,60 @@ spice_hotkey_to_gtk_accelerator(const gchar *key)
     return accel;
 }
 
+/**
+ * virt_viewer_compare_version:
+ * @s1: a version-like string
+ * @s2: a version-like string
+ *
+ * Compare two version-like strings: 1.1 > 1.0, 1.0.1 > 1.0, 1.0c >
+ * 1.0a, but 1.0rc < 1.0 for example.
+ *
+ * Returns: negative value if s1 < s2; zero if s1 = s2; positive value if s1 > s2.
+ **/
+gint
+virt_viewer_compare_version(const gchar *s1, const gchar *s2)
+{
+    gint i, retval = 0;
+    gchar **v1, **v2;
+
+    g_return_val_if_fail(s1 != NULL, 0);
+    g_return_val_if_fail(s2 != NULL, 0);
+
+    v1 = g_strsplit(s1, ".", -1);
+    v2 = g_strsplit(s2, ".", -1);
+
+    for (i = 0; v1[i] && v2[i]; ++i) {
+        gchar *e1 = NULL, *e2 = NULL;
+        guint64 m1 = g_ascii_strtoull(v1[i], &e1, 10);
+        guint64 m2 = g_ascii_strtoull(v2[i], &e2, 10);
+
+        retval = m1 - m2;
+        if (retval != 0)
+            goto end;
+
+        g_return_val_if_fail(e1 && e2, 0);
+        if (!*e1 && !*e2)
+            continue;
+        else if (*e1 && *e2)
+            retval = g_strcmp0(e1, e2);
+        else if (*e1)
+            retval = -1;
+        else if (*e2)
+            retval = 1;
+        if (retval != 0)
+            goto end;
+    }
+
+    if (v1[i])
+        retval = 1;
+    else if (v2[i])
+        retval = -1;
+
+end:
+    g_strfreev(v1);
+    g_strfreev(v2);
+    return retval;
+}
 /*
  * Local variables:
  *  c-indent-level: 4
diff --git a/src/virt-viewer-util.h b/src/virt-viewer-util.h
index fc56310..8157df5 100644
--- a/src/virt-viewer-util.h
+++ b/src/virt-viewer-util.h
@@ -56,6 +56,7 @@ gulong virt_viewer_signal_connect_object(gpointer instance,
                                          GConnectFlags connect_flags);
 
 gchar* spice_hotkey_to_gtk_accelerator(const gchar *key);
+gint virt_viewer_compare_version(const gchar *s1, const gchar *s2);
 
 #endif
 
-- 
1.8.1.1.439.g50a6b54


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]