#include <ftw.h>
#include <time.h>
#include <curl/curl.h>
+#include <rpm/rpmlib.h>
#include "logging.h"
#include "constants.h"
#include "misc.h"
// 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)
{
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;
+}
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
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)
}
+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);
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();
}