diff options
author | Richard Hughes <richard@hughsie.com> | 2008-06-30 08:51:26 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2008-06-30 08:51:26 +0100 |
commit | c6045dca519b0d576d9b08ef2c9833527d11f2e1 (patch) | |
tree | f4164e492959773ef5e25151532bd26d7e4da8f3 /librazor | |
parent | 82692a3772e5a18d3202cd09db759054ac9a5e52 (diff) |
convert razor_package_get_details() and razor_package_iterator_next() to varargs
The functions for getting package details about a package were limited to a few
things, when in the future we will want to support much more about a package.
The iterator was also limited to name,version,arch when most of the time we
didn't need all this data.
Diffstat (limited to 'librazor')
-rw-r--r-- | librazor/iterator.c | 36 | ||||
-rw-r--r-- | librazor/razor-internal.h | 8 | ||||
-rw-r--r-- | librazor/razor.c | 122 | ||||
-rw-r--r-- | librazor/razor.h | 19 | ||||
-rw-r--r-- | librazor/transaction.c | 41 |
5 files changed, 171 insertions, 55 deletions
diff --git a/librazor/iterator.c b/librazor/iterator.c index 5a870cc..e40425a 100644 --- a/librazor/iterator.c +++ b/librazor/iterator.c @@ -19,6 +19,7 @@ #define _GNU_SOURCE +#include <stdarg.h> #include <string.h> #include <assert.h> @@ -105,14 +106,21 @@ razor_package_iterator_create_for_file(struct razor_set *set, return razor_package_iterator_create_with_index(set, index); } +/** + * razor_package_iterator_next: + * @pi: a %razor_package_iterator + * @package: a %razor_package + * + * Gets the next iteratr along with any vararg data. + * The vararg must be terminated with zero. + * + * Example: razor_package_iterator_next (pi, package, RAZOR_DETAIL_NAME, &name, 0); + **/ RAZOR_EXPORT int razor_package_iterator_next(struct razor_package_iterator *pi, - struct razor_package **package, - const char **name, - const char **version, - const char **arch) + struct razor_package **package, ...) { - char *pool; + va_list args; int valid; struct razor_package *p, *packages; @@ -129,16 +137,17 @@ razor_package_iterator_next(struct razor_package_iterator *pi, } else valid = 0; - if (valid) { - pool = pi->set->string_pool.data; - *package = p; - *name = &pool[p->name]; - *version = &pool[p->version]; - *arch = &pool[p->arch]; - } else { + if (valid == 0) { *package = NULL; + goto out; } + *package = p; + + va_start(args, NULL); + razor_package_get_details_varg (pi->set, p, args); + va_end (args); +out: return valid; } @@ -260,13 +269,12 @@ razor_package_query_add_iterator(struct razor_package_query *pq, struct razor_package_iterator *pi) { struct razor_package *packages, *p; - const char *name, *version, *arch; assert (pq != NULL); assert (pi != NULL); packages = pq->set->packages.data; - while (razor_package_iterator_next(pi, &p, &name, &version, &arch)) { + while (razor_package_iterator_next(pi, &p, 0)) { pq->count += pq->vector[p - packages] ^ 1; pq->vector[p - packages] = 1; } diff --git a/librazor/razor-internal.h b/librazor/razor-internal.h index bf98cde..c47e4f1 100644 --- a/librazor/razor-internal.h +++ b/librazor/razor-internal.h @@ -22,6 +22,9 @@ #include <stdlib.h> #include <stdint.h> +#include <stdarg.h> + +#include "razor.h" /* GCC visibility */ #if defined(__GNUC__) && __GNUC__ >= 4 @@ -207,6 +210,11 @@ razor_merger_finish(struct razor_merger *merger); /* Utility functions */ +void +razor_package_get_details_varg(struct razor_set *set, + struct razor_package *package, + va_list args); + int razor_create_dir(const char *root, const char *path); int razor_write(int fd, const void *data, size_t size); diff --git a/librazor/razor.c b/librazor/razor.c index 88ff7a7..d3cab33 100644 --- a/librazor/razor.c +++ b/librazor/razor.c @@ -23,6 +23,7 @@ #include <stddef.h> #include <stdint.h> #include <stdio.h> +#include <stdarg.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> @@ -387,13 +388,13 @@ razor_set_get_package(struct razor_set *set, const char *package) { struct razor_package_iterator *pi; struct razor_package *p; - const char *name, *version, *arch; + const char *name; assert (set != NULL); assert (package != NULL); pi = razor_package_iterator_create(set); - while (razor_package_iterator_next(pi, &p, &name, &version, &arch)) { + while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_NAME, &name, 0)) { if (strcmp(package, name) == 0) break; } @@ -402,25 +403,94 @@ razor_set_get_package(struct razor_set *set, const char *package) return p; } +static const char * +razor_package_get_details_type(struct razor_set *set, + struct razor_package *package, + enum razor_detail_type type) +{ + const char *pool; + + switch (type) { + case RAZOR_DETAIL_NAME: + pool = set->string_pool.data; + return &pool[package->name]; + + case RAZOR_DETAIL_VERSION: + pool = set->string_pool.data; + return &pool[package->version]; + + case RAZOR_DETAIL_ARCH: + pool = set->string_pool.data; + return &pool[package->arch]; + + case RAZOR_DETAIL_SUMMARY: + pool = set->details_string_pool.data; + return &pool[package->summary]; + + case RAZOR_DETAIL_DESCRIPTION: + pool = set->details_string_pool.data; + return &pool[package->description]; + + case RAZOR_DETAIL_URL: + pool = set->details_string_pool.data; + return &pool[package->url]; + + case RAZOR_DETAIL_LICENSE: + pool = set->details_string_pool.data; + return &pool[package->license]; + + default: + fprintf(stderr, "type %u not found\n", type); + return NULL; + } +} + +/** + * razor_package_get_details_varg: + * @set: a %razor_set + * @package: a %razor_package + * @args: a va_list of arguments to set + **/ +void +razor_package_get_details_varg(struct razor_set *set, + struct razor_package *package, + va_list args) +{ + int i; + enum razor_detail_type type; + const char **data; + + for (i = 0;; i += 2) { + type = va_arg(args, enum razor_detail_type); + if (type == 0) + break; + data = va_arg(args, const char **); + *data = razor_package_get_details_type(set, package, type); + } + +} + +/** + * razor_package_get_details: + * @set: a %razor_set + * @package: a %razor_package + * + * Gets details about a package using a varg interface + * The vararg must be terminated with zero. + * + * Example: razor_package_get_details (set, package, RAZOR_DETAIL_URL, &url, 0); + **/ RAZOR_EXPORT void -razor_package_get_details(struct razor_set *set, - struct razor_package *package, - const char **summary, const char **description, - const char **url, const char **license) +razor_package_get_details(struct razor_set *set, struct razor_package *package, ...) { - const char *pool = set->details_string_pool.data; + va_list args; assert (set != NULL); assert (package != NULL); - if (summary != NULL) - *summary = &pool[package->summary]; - if (description != NULL) - *description = &pool[package->description]; - if (url != NULL) - *url = &pool[package->url]; - if (license != NULL) - *license = &pool[package->license]; + va_start(args, NULL); + razor_package_get_details_varg (set, package, args); + va_end (args); } RAZOR_EXPORT const char * @@ -647,8 +717,16 @@ razor_set_diff(struct razor_set *set, struct razor_set *upstream, pi1 = razor_package_iterator_create(set); pi2 = razor_package_iterator_create(upstream); - razor_package_iterator_next(pi1, &p1, &name1, &version1, &arch1); - razor_package_iterator_next(pi2, &p2, &name2, &version2, &arch2); + razor_package_iterator_next(pi1, &p1, + RAZOR_DETAIL_NAME, &name1, + RAZOR_DETAIL_VERSION, &version1, + RAZOR_DETAIL_ARCH, &arch1, + 0); + razor_package_iterator_next(pi2, &p2, + RAZOR_DETAIL_NAME, &name2, + RAZOR_DETAIL_VERSION, &version2, + RAZOR_DETAIL_ARCH, &arch2, + 0); while (p1 || p2) { if (p1 && p2) { @@ -668,10 +746,16 @@ razor_set_diff(struct razor_set *set, struct razor_set *upstream, if (p1 != NULL && res <= 0) razor_package_iterator_next(pi1, &p1, - &name1, &version1, &arch1); + RAZOR_DETAIL_NAME, &name1, + RAZOR_DETAIL_VERSION, &version1, + RAZOR_DETAIL_ARCH, &arch1, + 0); if (p2 != NULL && res >= 0) razor_package_iterator_next(pi2, &p2, - &name2, &version2, &arch2); + RAZOR_DETAIL_NAME, &name2, + RAZOR_DETAIL_VERSION, &version2, + RAZOR_DETAIL_ARCH, &arch2, + 0); } razor_package_iterator_destroy(pi1); diff --git a/librazor/razor.h b/librazor/razor.h index 61edc1c..9f3c668 100644 --- a/librazor/razor.h +++ b/librazor/razor.h @@ -28,6 +28,16 @@ enum razor_repo_file_type { RAZOR_REPO_FILE_FILES }; +enum razor_detail_type { + RAZOR_DETAIL_NAME = 1, /* 0 is the marker for the vararg */ + RAZOR_DETAIL_VERSION, + RAZOR_DETAIL_ARCH, + RAZOR_DETAIL_SUMMARY, + RAZOR_DETAIL_DESCRIPTION, + RAZOR_DETAIL_URL, + RAZOR_DETAIL_LICENSE +}; + enum razor_property_flags { RAZOR_PROPERTY_LESS = 1 << 0, RAZOR_PROPERTY_GREATER = 1 << 1, @@ -84,9 +94,7 @@ struct razor_package * razor_set_get_package(struct razor_set *set, const char *package); void -razor_package_get_details(struct razor_set *set, struct razor_package *package, - const char **summary, const char **description, - const char **url, const char **license); +razor_package_get_details(struct razor_set *set, struct razor_package *package, ...); /** @@ -136,10 +144,7 @@ razor_package_iterator_create_for_file(struct razor_set *set, const char *filename); int razor_package_iterator_next(struct razor_package_iterator *pi, - struct razor_package **package, - const char **name, - const char **version, - const char **arch); + struct razor_package **package, ...); void razor_package_iterator_destroy(struct razor_package_iterator *pi); struct razor_package_query * diff --git a/librazor/transaction.c b/librazor/transaction.c index 11ccc74..9efd5cd 100644 --- a/librazor/transaction.c +++ b/librazor/transaction.c @@ -294,7 +294,7 @@ remove_matching_providers(struct razor_transaction *trans, struct razor_package *pkg, *pkgs; struct razor_package_iterator pkg_iter; struct razor_set *set; - const char *n, *v, *a; + const char *n, *v; uint32_t type; if (ppi->present == trans->system.properties) @@ -316,8 +316,9 @@ remove_matching_providers(struct razor_transaction *trans, continue; razor_package_iterator_init_for_property(&pkg_iter, set, p); - while (razor_package_iterator_next(&pkg_iter, - &pkg, &n, &v, &a)) { + while (razor_package_iterator_next(&pkg_iter, &pkg, + RAZOR_DETAIL_NAME, &n, + RAZOR_DETAIL_VERSION, &v, 0)) { fprintf(stderr, "removing %s-%s\n", n, v); razor_transaction_remove_package(trans, pkg); } @@ -335,7 +336,7 @@ flag_matching_providers(struct razor_transaction *trans, struct razor_package *pkg, *pkgs; struct razor_package_iterator pkg_iter; struct razor_set *set; - const char *name, *version, *arch; + const char *name, *version; uint32_t *flags, type; if (ppi->present == trans->system.properties) { @@ -362,7 +363,8 @@ flag_matching_providers(struct razor_transaction *trans, razor_package_iterator_init_for_property(&pkg_iter, set, p); while (razor_package_iterator_next(&pkg_iter, &pkg, - &name, &version, &arch)) { + RAZOR_DETAIL_NAME, &name, + RAZOR_DETAIL_VERSION, &version, 0)) { fprintf(stderr, "flagging %s-%s for providing %s matching %s %s\n", name, version, @@ -511,7 +513,7 @@ update_unsatisfied_packages(struct razor_transaction *trans) struct razor_property *sp; struct prop_iter spi; struct razor_package_iterator pkg_iter; - const char *name, *version, *arch; + const char *name; spkgs = trans->system.set->packages.data; prop_iter_init(&spi, &trans->system); @@ -524,7 +526,7 @@ update_unsatisfied_packages(struct razor_transaction *trans) trans->system.set, sp); while (razor_package_iterator_next(&pkg_iter, &pkg, - &name, &version, &arch)) { + RAZOR_DETAIL_NAME, &name, 0)) { fprintf(stderr, "updating %s because %s %s %s " "isn't satisfied\n", name, spi.pool + sp->name, @@ -556,7 +558,7 @@ update_conflicted_packages(struct razor_transaction *trans) struct razor_property *up, *sp; struct prop_iter spi, upi; struct razor_package_iterator pkg_iter; - const char *name, *version, *arch; + const char *name, *version; spkgs = trans->system.set->packages.data; prop_iter_init(&spi, &trans->system); @@ -575,7 +577,8 @@ update_conflicted_packages(struct razor_transaction *trans) trans->system.set, sp); while (razor_package_iterator_next(&pkg_iter, &pkg, - &name, &version, &arch)) { + RAZOR_DETAIL_NAME, &name, + RAZOR_DETAIL_VERSION, &version, 0)) { fprintf(stderr, "updating %s %s because it " "conflicts with %s\n", name, version, spi.pool + sp->name); @@ -657,13 +660,15 @@ flush_scheduled_system_updates(struct razor_transaction *trans) struct razor_package_iterator *pi; struct razor_package *p, *pkg, *spkgs; struct prop_iter ppi; - const char *name, *version, *arch; + const char *name, *version; spkgs = trans->system.set->packages.data; pi = razor_package_iterator_create(trans->system.set); prop_iter_init(&ppi, &trans->upstream); - while (razor_package_iterator_next(pi, &p, &name, &version, &arch)) { + while (razor_package_iterator_next(pi, &p, + RAZOR_DETAIL_NAME, &name, + RAZOR_DETAIL_VERSION, &version, 0)) { if (!(trans->system.packages[p - spkgs] & TRANS_PACKAGE_UPDATE)) continue; @@ -692,13 +697,15 @@ flush_scheduled_upstream_updates(struct razor_transaction *trans) struct razor_package_iterator *pi; struct razor_package *p, *upkgs; struct prop_iter spi; - const char *name, *version, *arch; + const char *name, *version; upkgs = trans->upstream.set->packages.data; pi = razor_package_iterator_create(trans->upstream.set); prop_iter_init(&spi, &trans->system); - while (razor_package_iterator_next(pi, &p, &name, &version, &arch)) { + while (razor_package_iterator_next(pi, &p, + RAZOR_DETAIL_NAME, &name, + RAZOR_DETAIL_VERSION, &version, 0)) { if (!(trans->upstream.packages[p - upkgs] & TRANS_PACKAGE_UPDATE)) continue; @@ -745,14 +752,18 @@ describe_unsatisfied(struct razor_set *set, struct razor_property *rp) if (pool[rp->version] == '\0') { razor_package_iterator_init_for_property(&pi, set, rp); while (razor_package_iterator_next(&pi, &pkg, - &name, &version, &arch)) + RAZOR_DETAIL_NAME, &name, + RAZOR_DETAIL_VERSION, &version, + RAZOR_DETAIL_ARCH, &arch, 0)) fprintf(stderr, "%s is needed by %s-%s.%s\n", &pool[rp->name], name, version, arch); } else { razor_package_iterator_init_for_property(&pi, set, rp); while (razor_package_iterator_next(&pi, &pkg, - &name, &version, &arch)) + RAZOR_DETAIL_NAME, &name, + RAZOR_DETAIL_VERSION, &version, + RAZOR_DETAIL_ARCH, &arch, 0)) fprintf(stderr, "%s %s %s is needed by %s-%s.%s\n", &pool[rp->name], razor_property_relation_to_string(rp), |