X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=common%2Ftr_filter_encoders.c;h=a69685da42e1e222eb9c7e9864cb1fb5061358e2;hb=HEAD;hp=d6dd73fe96a6232a319993333bfbc40ba27adc66;hpb=135f5ab1d309fe168cc2e1742845ffdf0a7186c3;p=trust_router.git diff --git a/common/tr_filter_encoders.c b/common/tr_filter_encoders.c index d6dd73f..a69685d 100644 --- a/common/tr_filter_encoders.c +++ b/common/tr_filter_encoders.c @@ -36,65 +36,53 @@ #include #include - -/* helper for below */ -#define OBJECT_SET_OR_FAIL(jobj, key, val) \ -do { \ - if (val) \ - json_object_set_new((jobj),(key),(val)); \ - else \ - goto cleanup; \ -} while (0) - -#define ARRAY_APPEND_OR_FAIL(jary, val) \ -do { \ - if (val) \ - json_array_append_new((jary),(val)); \ - else \ - goto cleanup; \ -} while (0) - +#include +#include typedef json_t *(ITEM_ENCODER_FUNC)(void *); -static json_t *items_to_json_array(void *items[], ITEM_ENCODER_FUNC *item_encoder, size_t max_items) +enum array_type { + ARRAY_TYPE_FSPEC, + ARRAY_TYPE_CONSTRAINT +}; +/** + * Make an array of matches from a TR_FSPEC or TR_CONSTRAINT + * + * @param obj + * @param type + * @return + */ +static json_t *tr_names_to_json_array(void *obj, enum array_type type) { - size_t ii; json_t *jarray = json_array(); json_t *retval = NULL; + TR_FSPEC_ITER fspec_iter = {0}; + TR_CONSTRAINT_ITER cons_iter = {0}; + TR_NAME *this_match = NULL; if (jarray == NULL) goto cleanup; - for (ii=0; iifield)); OBJECT_SET_OR_FAIL(fspec_json, "matches", - tr_matches_to_json_array(fspec)); + tr_names_to_json_array(fspec, ARRAY_TYPE_FSPEC)); /* succeeded - set the return value and increment the reference count */ retval = fspec_json; @@ -143,10 +129,10 @@ static json_t *tr_fspecs_to_json_array(TR_FLINE *fline) if ((jarray == NULL) || (iter == NULL)) goto cleanup; - this_fspec = tr_fline_iter_first(iter, fline); - while(this_fspec) { + for (this_fspec = tr_fline_iter_first(iter, fline); + this_fspec != NULL; + this_fspec = tr_fline_iter_next(iter)) { ARRAY_APPEND_OR_FAIL(jarray, tr_fspec_to_json(this_fspec)); - this_fspec = tr_fline_iter_next(iter); } /* success */ retval = jarray; @@ -176,15 +162,11 @@ static json_t *tr_fline_to_json(TR_FLINE *fline) tr_fspecs_to_json_array(fline)); if (fline->realm_cons) { OBJECT_SET_OR_FAIL(fline_json, "realm_constraints", - items_to_json_array((void **) fline->realm_cons->matches, - (ITEM_ENCODER_FUNC *) tr_name_to_json_string, - TR_MAX_CONST_MATCHES)); + tr_names_to_json_array(fline->realm_cons, ARRAY_TYPE_CONSTRAINT)); } if (fline->domain_cons) { OBJECT_SET_OR_FAIL(fline_json, "domain_constraints", - items_to_json_array((void **) fline->domain_cons->matches, - (ITEM_ENCODER_FUNC *) tr_name_to_json_string, - TR_MAX_CONST_MATCHES)); + tr_names_to_json_array(fline->domain_cons, ARRAY_TYPE_CONSTRAINT)); } /* succeeded - set the return value and increment the reference count */ @@ -207,10 +189,10 @@ static json_t *tr_flines_to_json_array(TR_FILTER *filt) if ((jarray == NULL) || (iter == NULL)) goto cleanup; - this_fline = tr_filter_iter_first(iter, filt); - while(this_fline) { + for(this_fline = tr_filter_iter_first(iter, filt); + this_fline != NULL; + this_fline = tr_filter_iter_next(iter)) { ARRAY_APPEND_OR_FAIL(jarray, tr_fline_to_json(this_fline)); - this_fline = tr_filter_iter_next(iter); } /* success */ retval = jarray;