From ac2f99f6af378ebb572ba62ad4445e539ff47f7a Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 24 Feb 2013 07:20:50 -0600 Subject: [PATCH] pkg: refactor pkg_get_comparator(). --- pkg.c | 68 ++++++++++++++++++++++++++++++++--------------------------- pkg.h | 3 ++- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/pkg.c b/pkg.c index fbfc6cc..c26c840 100644 --- a/pkg.c +++ b/pkg.c @@ -582,37 +582,6 @@ pkg_compare_version(const char *a, const char *b) return 1; } -/* - * pkg_get_comparator(pkgdep) - * - * returns the comparator used in a depgraph dependency node as a string. - */ -const char * -pkg_get_comparator(pkg_dependency_t *pkgdep) -{ - switch(pkgdep->compare) - { - case PKG_LESS_THAN: - return "<"; - case PKG_GREATER_THAN: - return ">"; - case PKG_LESS_THAN_EQUAL: - return "<="; - case PKG_GREATER_THAN_EQUAL: - return ">="; - case PKG_EQUAL: - return "="; - case PKG_NOT_EQUAL: - return "!="; - case PKG_ANY: - return "(any)"; - default: - return "???"; - } - - return "???"; -} - static pkg_t pkg_config_virtual = { .id = "pkg-config", .realname = "pkg-config", @@ -626,6 +595,43 @@ static pkg_t pkg_config_virtual = { }, }; +typedef bool (*pkg_vercmp_res_func_t)(pkg_t *pkg, pkg_dependency_t *pkgdep); + +typedef struct { + const char *name; + pkg_comparator_t compare; +} pkg_comparator_name_t; + +static pkg_comparator_name_t pkg_comparator_names[PKG_CMP_SIZE + 1] = { + {"<", PKG_LESS_THAN}, + {">", PKG_GREATER_THAN}, + {"<=", PKG_LESS_THAN_EQUAL}, + {">=", PKG_GREATER_THAN_EQUAL}, + {"=", PKG_EQUAL}, + {"!=", PKG_NOT_EQUAL}, + {"(any)", PKG_ANY}, + {"???", PKG_CMP_SIZE}, +}; + +/* + * pkg_get_comparator(pkgdep) + * + * returns the comparator used in a depgraph dependency node as a string. + */ +const char * +pkg_get_comparator(pkg_dependency_t *pkgdep) +{ + const pkg_comparator_name_t *i; + + for (i = pkg_comparator_names; i->compare != PKG_CMP_SIZE; i++) + { + if (i->compare == pkgdep->compare) + return i->name; + } + + return "???"; +} + /* * pkg_verify_dependency(pkgdep, flags) * diff --git a/pkg.h b/pkg.h index 1a3bf37..2672b0a 100644 --- a/pkg.h +++ b/pkg.h @@ -32,7 +32,8 @@ typedef enum { PKG_GREATER_THAN_EQUAL, PKG_EQUAL, PKG_NOT_EQUAL, - PKG_ALWAYS_MATCH + PKG_ALWAYS_MATCH, + PKG_CMP_SIZE } pkg_comparator_t; typedef struct pkg_ pkg_t;