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

Hans de Goede hdegoede at redhat.com
Fri Mar 8 08:18:26 UTC 2013


Hi,

On 03/07/2013 08:55 PM, Marc-André Lureau wrote:
> ---
>   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.

Ugh, no lets not do that please. Example if I read the above comment and
the code correctly then 1.0a < 1.0 which will surprise some people. We've seen
this kind of problems a lot with rpm packaging and rpmvercmp's magic. There is
even a whole set of packaging guidelines how to deal with this using rpm's
release field to work around all the problems have alpha characters in
versions cause.

Since we are defining an API from scratch here lets do the sane thing,
and simply say that version strings as passed to virt_viewer_compare_version
can only contain 0-9 and .



> + *
> + * 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
>
>

Regards,

Hans




More information about the virt-tools-list mailing list