From c3a6daeacf5c6873cec41fa35ef5b917d335ad6a Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Thu, 30 Mar 2017 23:51:18 -0500 Subject: [PATCH] add vercmp functions to libzpm --- lib/vercmp.c | 4 ++-- lib/zpm.c | 21 +++++++++++++++++++++ zpm-vercmp.c | 15 +++++++++++++++ zpm.h | 9 ++++++++- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 zpm-vercmp.c diff --git a/lib/vercmp.c b/lib/vercmp.c index 8cbad73..91d5363 100644 --- a/lib/vercmp.c +++ b/lib/vercmp.c @@ -17,7 +17,7 @@ struct ver { char *relstr; }; -static void init_ver(struct ver *v, char *s) { +static void init_ver(struct ver *v, const char *s) { strncpy(v->str, s, 1023); v->str[1023] = 0; v->s = 0; @@ -109,7 +109,7 @@ static int next_comp(struct ver *v) { /* * alphabetic less than numeric */ -int zpm_vercmp(char *vsa, char *vsb) { +int zpm_vercmp(const char *vsa, const char *vsb) { struct ver a, b; int an, bn; int cmp; diff --git a/lib/zpm.c b/lib/zpm.c index d9041ff..8e06875 100644 --- a/lib/zpm.c +++ b/lib/zpm.c @@ -183,6 +183,11 @@ int zpm_commit(struct zpm *z) { return 1; } +/* wrapper for sqlite3_exec */ +int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) { + return sqlite3_exec(z->db, sql, callback, arg, errmsg); +} + int zpm_rollback(struct zpm *z) { char *errstr = 0; sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr); @@ -349,6 +354,22 @@ int zpm_close(struct zpm *pkg) { return 1; } +static int zpm_sqlite_vercmp(void *not_used, int unknown, const void *a, + int unk2, const void *b) { + /* not sure what the ints are, possibly string lengths */ + not_used = 0; /* suppress warning */ + unknown = 0; /* suppress warning */ + unk2 = 0; + return zpm_vercmp(a, b); +} + +int zpm_addvercmp(struct zpm *pkg) { + return sqlite3_create_collation( + pkg->db, "vercmp", SQLITE_UTF8, NULL, + zpm_sqlite_vercmp + ); +} + /* set package struct variables, database, environment, then command line */ int zpm_readopts(struct zpm *pkg, int ac, char **av) { char *ev; diff --git a/zpm-vercmp.c b/zpm-vercmp.c new file mode 100644 index 0000000..bfa5412 --- /dev/null +++ b/zpm-vercmp.c @@ -0,0 +1,15 @@ +#include +#include + +#include "zpm.h" + +int main(int ac, char *av[]) { + int cmp; + + if (ac < 2) return 1; + + cmp = zpm_vercmp(av[1], av[2]); + printf("%d\n", cmp); + if (cmp == -1) cmp = 2; + return cmp; +} diff --git a/zpm.h b/zpm.h index ee80722..68a536f 100644 --- a/zpm.h +++ b/zpm.h @@ -98,5 +98,12 @@ void *compresslzma(void *buf, size_t bufsize, size_t *len); int zpm_hash(char *path, char *hash, uint32_t flags); int zpm_readopts(struct zpm *pkg, int ac, char **av); -int zpm_vercmp(char *a, char *b); +int zpm_vercmp(const char *a, const char *b); + +/* add vercmp collation to db */ +int zpm_addvercmp(struct zpm *pkg); + +int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg); + + #endif -- 2.40.0