Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
kvlist: add ability to sort and search
Baptiste Daroussin committed 1 year ago
commit ddc501d48121c5110ef0a6a09611fd9f6041bb30
parent b4c8d82
2 files changed +34 -0
modified libpkg/pkg_attributes.c
@@ -153,6 +153,38 @@ pkg_kv_free(struct pkg_kv *c)
	free(c);
}

+
static int
+
kv_cmp(const void *a, const void *b) {
+
	struct pkg_kv *ka = (struct pkg_kv *)a;
+
	struct pkg_kv *kb = (struct pkg_kv *)b;
+

+
	return (strcmp(ka->key, kb->key));
+
}
+

+
struct pkg_kv *
+
pkg_kv_search(kvlist_t *kv, char *el)
+
{
+
	struct pkg_kv target =  { .key = el, .value = NULL };
+
	printf("Searching %s\n", target.key);
+
	if (kv->len == 0)
+
		return (NULL);
+
	struct pkg_kv *res = bsearch(&target, *kv->d, kv->len, sizeof(struct pkg_kv), kv_cmp);
+
	if (res == NULL) {
+
		printf("Not found\n");
+
		printf("%s\n", kv->d[0]->key);
+
		return (NULL);
+
	}
+
	return (res);
+
}
+

+
void
+
pkg_kv_sort(kvlist_t *kv)
+
{
+
	if (kv->len == 0)
+
		return;
+
	qsort(kv->d, kv->len, sizeof(kv->d[0]), kv_cmp);
+
}
+

struct pkg_kvlist_iterator *
pkg_kvlist_iterator(struct pkg_kvlist *l)
{
modified libpkg/private/pkg.h
@@ -675,6 +675,8 @@ void pkg_list_free(struct pkg *, pkg_list);

struct pkg_kv *pkg_kv_new(const char *key, const char *val);
void pkg_kv_free(struct pkg_kv *);
+
struct pkg_kv *pkg_kv_search(kvlist_t *, char *);
+
void pkg_kv_sort(kvlist_t *);

void pkg_dep_free(struct pkg_dep *);
void pkg_file_free(struct pkg_file *);