X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp_json.c;h=a48ecaa248c1f290bc3c639b5576480e071db438;hb=f2158409edc0d8c246c9a6ceefcdd5db512210aa;hp=6514cfe37f9d5e0dfb9bbe42d3f65652d326864e;hpb=967d20c38c65689c96a0276e6f916b4f8e2617de;p=ppastats.git diff --git a/src/lp_json.c b/src/lp_json.c index 6514cfe..a48ecaa 100644 --- a/src/lp_json.c +++ b/src/lp_json.c @@ -16,25 +16,57 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ - +#define _XOPEN_SOURCE_EXTENDED #define _XOPEN_SOURCE -#include - - #include #include +#include +#include #include "lp_json.h" #include "lp_ws.h" -struct bpph * -json_object_to_binary_package_publishing_history(json_object *o) +static time_t json_to_time(json_object *json) +{ + const char *str; + struct tm tm; + char *ret; + + str = json_object_get_string(json); + if (!str) + return -1; + + tm.tm_isdst = -1; + ret = strptime(str, "%FT%T", &tm); + + if (ret) + return mktime(&tm); + else + return -1; +} + +static json_object *time_to_json(time_t t) +{ + char *str; + + str = time_to_str(t); + + if (str) + return json_object_new_string(str); + else + return NULL; +} + +static struct bpph *json_to_bpph(json_object *o) { const char *binary_package_name; const char *binary_package_version; const char *distro_arch_series_link; const char *self_link; - int architecture_specific; + int arch_specific; + struct bpph *bpph; + const char *status; + time_t date_created; binary_package_name = json_object_get_string (json_object_object_get(o, "binary_package_name")); @@ -48,17 +80,60 @@ json_object_to_binary_package_publishing_history(json_object *o) self_link = json_object_get_string (json_object_object_get(o, "self_link")); - if (json_object_get_boolean - (json_object_object_get(o, "architecture_specific"))) - architecture_specific = 1; - else - architecture_specific = 0; + arch_specific = json_object_get_boolean + (json_object_object_get(o, "architecture_specific")); + + date_created = json_to_time(json_object_object_get(o, "date_created")); + + status = json_object_get_string(json_object_object_get(o, "status")); + + bpph = bpph_new(binary_package_name, + binary_package_version, + distro_arch_series_link, + self_link, + status, + arch_specific, + date_created); - return binary_package_publishing_history_new(binary_package_name, - binary_package_version, - distro_arch_series_link, - self_link, - architecture_specific); + return bpph; +} + +static json_object *bpph_to_json(struct bpph *bpph) +{ + json_object *json; + + json = json_object_new_object(); + + json_object_object_add + (json, + "binary_package_name", + json_object_new_string(bpph->binary_package_name)); + + json_object_object_add + (json, + "binary_package_version", + json_object_new_string(bpph->binary_package_version)); + + json_object_object_add + (json, + "distro_arch_series_link", + json_object_new_string(bpph->distro_arch_series_link)); + + json_object_object_add + (json, "self_link", json_object_new_string(bpph->self_link)); + + json_object_object_add + (json, + "architecture_specific", + json_object_new_boolean(bpph->architecture_specific)); + + json_object_object_add + (json, "status", json_object_new_string(bpph->status)); + + json_object_object_add + (json, "date_created", time_to_json(bpph->date_created)); + + return json; } struct distro_arch_series *json_object_to_distro_arch_series(json_object *o) @@ -66,10 +141,9 @@ struct distro_arch_series *json_object_to_distro_arch_series(json_object *o) const char *display_name; const char *title; const char *architecture_tag; - boolean is_nominated_arch_indep; + json_bool is_nominated_arch_indep; const char *distroseries_link; - display_name = json_object_get_string (json_object_object_get(o, "display_name")); @@ -102,14 +176,11 @@ struct distro_series *json_object_to_distro_series(json_object *o) displayname = json_object_get_string (json_object_object_get(o, "displayname")); - title = json_object_get_string - (json_object_object_get(o, "title")); + title = json_object_get_string(json_object_object_get(o, "title")); - version = json_object_get_string - (json_object_object_get(o, "version")); + version = json_object_get_string(json_object_object_get(o, "version")); - name = json_object_get_string - (json_object_object_get(o, "name")); + name = json_object_get_string(json_object_object_get(o, "name")); return distro_series_new(name, version, @@ -132,18 +203,18 @@ struct bpph **json_object_to_bpph_list(json_object *o) n = json_object_array_length(o_entries); entries = malloc - (sizeof(struct binary_package_publishing_history *)*(n+1)); + (sizeof(struct bpph *)*(n+1)); for (i = 0, i2 = 0; i < n; i++) { - h = json_object_to_binary_package_publishing_history - (json_object_array_get_idx(o_entries, i)); + h = json_to_bpph(json_object_array_get_idx(o_entries, + i)); if (!h->architecture_specific) { distro = get_distro_arch_series (h->distro_arch_series_link); if (!distro || !distro->is_nominated_arch_indep) { - binary_package_publishing_history_free(h); + bpph_free(h); continue ; } } @@ -156,6 +227,23 @@ struct bpph **json_object_to_bpph_list(json_object *o) return entries; } +json_object *bpph_list_to_json(struct bpph **list) +{ + json_object *result, *entries; + struct bpph **cur; + + result = json_object_new_object(); + + entries = json_object_new_array(); + json_object_object_add(result, "entries", entries); + + if (list) + for (cur = list; *cur; cur++) + json_object_array_add(entries, bpph_to_json(*cur)); + + return result; +} + struct daily_download_total * json_object_to_daily_download_total(const char *d, json_object *o_c) {