From 81b24fdd96c65985b0a694a4c3a15f380ba3b57e Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Wed, 7 Nov 2012 10:35:38 +0100 Subject: [PATCH] add cr_cmp_evr into the misc --- src/misc.c | 35 +++++++++++++++++++++++++++++++++++ src/misc.h | 22 ++++++++++++++++++++++ tests/testmisc.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/src/misc.c b/src/misc.c index 2eda679..ff406fa 100644 --- a/src/misc.c +++ b/src/misc.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "logging.h" #include "constants.h" #include "misc.h" @@ -789,6 +790,9 @@ cr_str_to_version(const char *str) // 0 - versions are same // 1 - first string is bigger version // 2 - second string is bigger version +// Examples: +// "6.3.2azb" > "6.3.2abc" +// "2.1" < "2.1.3" int cr_cmp_version_str(const char* str1, const char *str2) { @@ -975,3 +979,34 @@ cr_nvrea_free(struct cr_NVREA *nvrea) g_free(nvrea->arch); g_free(nvrea); } + + +int +cr_compare_values(const char *str1, const char *str2) +{ + if (!str1 && !str2) + return 0; + else if (str1 && !str2) + return 1; + else if (!str1 && str2) + return -1; + return rpmvercmp(str1, str2); +} + + +int +cr_cmp_evr(const char *e1, const char *v1, const char *r1, + const char *e2, const char *v2, const char *r2) +{ + int rc; + + if (e1 == NULL) e1 = "0"; + if (e2 == NULL) e2 = "0"; + + rc = cr_compare_values(e1, e2); + if (rc) return rc; + rc = cr_compare_values(v1, v2); + if (rc) return rc; + rc = cr_compare_values(r1, r2); + return rc; +} diff --git a/src/misc.h b/src/misc.h index 8c4ff66..9aab127 100644 --- a/src/misc.h +++ b/src/misc.h @@ -257,6 +257,28 @@ void cr_slist_free_full(GSList *list, GDestroyNotify free_f); struct cr_NVREA *cr_split_rpm_filename(const char *filename); void cr_nvrea_free(struct cr_NVREA *); +/** \ingroup misc + * Compare evr of two cr_NVREA. Name and arch are ignored. + * @param A pointer to first cr_NVREA + * @param B pointer to second cr_NVREA + * @return 0 = same, 1 = first is newer, -1 = second is newer + */ +#define cr_cmp_nvrea(A, B) (cr_cmp_evr((A)->epoch, (A)->version, (A)->release,\ + (B)->epoch, (B)->version, (B)->release)) + +/** \ingroup misc + * Compare two version strings splited into evr chunks. + * param e1 1. epoch + * param v1 1. version + * param r1 1. release + * param e2 2. epoch + * param v2 2. version + * param r2 2. release + * return 0 = same, 1 = first is newer, -1 = second is newer + */ +int cr_cmp_evr(const char *e1, const char *v1, const char *r1, + const char *e2, const char *v2, const char *r2); + #ifdef __cplusplus } #endif diff --git a/tests/testmisc.c b/tests/testmisc.c index 64aaeab..524c090 100644 --- a/tests/testmisc.c +++ b/tests/testmisc.c @@ -939,6 +939,9 @@ static void test_cr_cmp_version_str(void) ret = cr_cmp_version_str("c", "f"); g_assert_cmpint(ret, ==, 2); + + ret = cr_cmp_version_str("2.1", "2.1.3"); + g_assert_cmpint(ret, ==, 2); } static void test_cr_split_rpm_filename(void) @@ -986,6 +989,44 @@ static void test_cr_split_rpm_filename(void) } +static void test_cr_cmp_evr(void) +{ + int res; + + res = cr_cmp_evr(NULL, "2", "1", + "0", "2", "1"); + g_assert_cmpint(res, ==, 0); + + res = cr_cmp_evr(NULL, "2", "2", + "0", "2", "1"); + g_assert_cmpint(res, ==, 1); + + res = cr_cmp_evr("0", "2", "2", + "1", "2", "1"); + g_assert_cmpint(res, ==, -1); + + res = cr_cmp_evr(NULL, "22", "2", + "0", "2", "2"); + g_assert_cmpint(res, ==, 1); + + res = cr_cmp_evr(NULL, "13", "2", + "0", "2", "2"); + g_assert_cmpint(res, ==, 1); + + res = cr_cmp_evr(NULL, "55", "2", + NULL, "55", "2"); + g_assert_cmpint(res, ==, 0); + + res = cr_cmp_evr(NULL, "0", "2a", + "0", "0", "2b"); + g_assert_cmpint(res, ==, -1); + + res = cr_cmp_evr(NULL, "0", "2", + "0", NULL, "3"); + g_assert_cmpint(res, ==, 1); +} + + int main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); @@ -1013,6 +1054,7 @@ int main(int argc, char *argv[]) g_test_add_func("/misc/test_cr_str_to_version", test_cr_str_to_version); g_test_add_func("/misc/test_cr_cmp_version_str", test_cr_cmp_version_str); g_test_add_func("/misc/test_cr_split_rpm_filename", test_cr_split_rpm_filename); + g_test_add_func("/misc/test_cr_cmp_evr", test_cr_cmp_evr); return g_test_run(); } -- 2.7.4