X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp_json.c;h=af8e8cd6dafee63e0bc318befebc398b4522db46;hb=HEAD;hp=d88587a9f3dd396711d795d120c7175a7fb7ac3d;hpb=48adae7cac47f09a5ac0ce7c3b53359b340a1110;p=ppastats.git diff --git a/src/lp_json.c b/src/lp_json.c index d88587a..af8e8cd 100644 --- a/src/lp_json.c +++ b/src/lp_json.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2012 jeanfi@gmail.com + * Copyright (C) 2011-2014 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -23,8 +23,45 @@ #include #include -#include "lp_json.h" -#include "lp_ws.h" +#include +#include +#include + +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; + + memset(&tm, 0, sizeof(struct tm)); + tm.tm_isdst = -1; + ret = strptime(str, "%FT%T", &tm); + + if (ret) + return mktime(&tm); + else + return -1; +} + +json_object *time_to_json(time_t t) +{ + char *str; + json_object *j; + + str = time_to_ISO8601_time(&t); + + if (str) { + j = json_object_new_string(str); + free(str); + return j; + } else { + return NULL; + } +} static struct bpph *json_to_bpph(json_object *o) { @@ -32,51 +69,47 @@ static struct bpph *json_to_bpph(json_object *o) const char *binary_package_version; const char *distro_arch_series_link; const char *self_link; - int architecture_specific; - const char *date_created; + int arch_specific; struct bpph *bpph; const char *status; + time_t date_created; + json_object *j; - binary_package_name = json_object_get_string - (json_object_object_get(o, "binary_package_name")); + json_object_object_get_ex(o, "binary_package_name", &j); + binary_package_name = json_object_get_string(j); - binary_package_version = json_object_get_string - (json_object_object_get(o, "binary_package_version")); + json_object_object_get_ex(o, "binary_package_version", &j); + binary_package_version = json_object_get_string(j); - distro_arch_series_link = json_object_get_string - (json_object_object_get(o, "distro_arch_series_link")); + json_object_object_get_ex(o, "distro_arch_series_link", &j); + distro_arch_series_link = json_object_get_string(j); - self_link = json_object_get_string - (json_object_object_get(o, "self_link")); + json_object_object_get_ex(o, "self_link", &j); + self_link = json_object_get_string(j); - if (json_object_get_boolean - (json_object_object_get(o, "architecture_specific"))) - architecture_specific = 1; - else - architecture_specific = 0; + json_object_object_get_ex(o, "architecture_specific", &j); + arch_specific = json_object_get_boolean(j); + + json_object_object_get_ex(o, "date_created", &j); + date_created = json_to_time(j); + + json_object_object_get_ex(o, "status", &j); + status = json_object_get_string(j); bpph = bpph_new(binary_package_name, binary_package_version, distro_arch_series_link, self_link, - architecture_specific); - - date_created = json_object_get_string - (json_object_object_get(o, "date_created")); - if (date_created) - strptime(date_created, "%FT%T%z", &bpph->date_created); - - status = json_object_get_string(json_object_object_get(o, "status")); - if (status) - bpph->status = strdup(status); + status, + arch_specific, + date_created); return bpph; } static json_object *bpph_to_json(struct bpph *bpph) { - json_object *json; - char *date; + json_object *json, *time; json = json_object_new_object(); @@ -96,6 +129,9 @@ static json_object *bpph_to_json(struct bpph *bpph) 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)); @@ -103,15 +139,9 @@ static json_object *bpph_to_json(struct bpph *bpph) json_object_object_add (json, "status", json_object_new_string(bpph->status)); - date = malloc(strlen("YY-MM-DDThh:mm:ss+xxx") + 1); - strftime(date, - strlen("YY-MM-DDThh:mm:ss+xxx") + 1, - "%FT%T%z", - &bpph->date_created); - + time = time_to_json(bpph->date_created); json_object_object_add - (json, "date_created", json_object_new_string(date)); - free(date); + (json, "date_created", time); return json; } @@ -121,23 +151,25 @@ 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; + json_object *j; - display_name = json_object_get_string - (json_object_object_get(o, "display_name")); + json_object_object_get_ex(o, "display_name", &j); + display_name = json_object_get_string(j); - title = json_object_get_string - (json_object_object_get(o, "title")); + json_object_object_get_ex(o, "title", &j); + title = json_object_get_string(j); - architecture_tag = json_object_get_string - (json_object_object_get(o, "architecture_tag")); + json_object_object_get_ex(o, "architecture_tag", &j); + architecture_tag = json_object_get_string(j); - distroseries_link = json_object_get_string - (json_object_object_get(o, "distroseries_link")); + json_object_object_get_ex(o, "distroseries_link", &j); + distroseries_link = json_object_get_string(j); + json_object_object_get_ex(o, "is_nominated_arch_indep", &j); is_nominated_arch_indep = json_object_get_boolean - (json_object_object_get(o, "is_nominated_arch_indep")); + (j); return distro_arch_series_new(display_name, title, @@ -152,15 +184,19 @@ struct distro_series *json_object_to_distro_series(json_object *o) const char *title; const char *name; const char *version; + json_object *j; - displayname = json_object_get_string - (json_object_object_get(o, "displayname")); + json_object_object_get_ex(o, "displayname", &j); + displayname = json_object_get_string(j); - title = json_object_get_string(json_object_object_get(o, "title")); + json_object_object_get_ex(o, "title", &j); + title = json_object_get_string(j); - version = json_object_get_string(json_object_object_get(o, "version")); + json_object_object_get_ex(o, "version", &j); + version = json_object_get_string(j); - name = json_object_get_string(json_object_object_get(o, "name")); + json_object_object_get_ex(o, "name", &j); + name = json_object_get_string(j); return distro_series_new(name, version, @@ -175,7 +211,7 @@ struct bpph **json_object_to_bpph_list(json_object *o) struct bpph **entries, *h; const struct distro_arch_series *distro; - o_entries = json_object_object_get(o, "entries"); + json_object_object_get_ex(o, "entries", &o_entries); if (!o_entries) return NULL; @@ -217,8 +253,9 @@ json_object *bpph_list_to_json(struct bpph **list) entries = json_object_new_array(); json_object_object_add(result, "entries", entries); - for (cur = list; *cur; cur++) - json_object_array_add(entries, bpph_to_json(*cur)); + if (list) + for (cur = list; *cur; cur++) + json_object_array_add(entries, bpph_to_json(*cur)); return result; } @@ -231,6 +268,7 @@ json_object_to_daily_download_total(const char *d, json_object *o_c) result = malloc(sizeof(struct daily_download_total)); result->count = json_object_get_int(o_c); + memset(&result->date, 0, sizeof(struct tm)); strptime(d, "%FT%T%z", &result->date); return result; @@ -271,3 +309,41 @@ json_object_to_daily_download_totals(json_object *o) return result; } + +struct json_object *date_to_json(struct tm *tm) +{ + json_object *json; + + json = json_object_new_array(); + json_object_array_add(json, json_object_new_int(tm->tm_year+1900)); + json_object_array_add(json, json_object_new_int(tm->tm_mon+1)); + json_object_array_add(json, json_object_new_int(tm->tm_mday)); + + return json; +} + +json_object *ddts_to_json(struct daily_download_total **ddts) +{ + json_object *json_ddt, *json_ddts; + struct daily_download_total *ddt; + + json_ddts = json_object_new_array(); + + while (ddts && *ddts) { + ddt = *ddts; + + json_ddt = json_object_new_object(); + json_object_object_add(json_ddt, + "value", + json_object_new_int(ddt->count)); + json_object_object_add(json_ddt, + "time", + date_to_json(&ddt->date)); + + json_object_array_add(json_ddts, json_ddt); + + ddts++; + } + + return json_ddts; +}