X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp_json.c;h=c9379c5a707ef023b089da1485aea3161d8ce742;hb=8fbc54a7960312d1626f4032d5fef5bf8a37db70;hp=a2b88f6a25cdc68cadf78ef5e0c57a474e9b05bc;hpb=60892f2ef45fa1ee656e1a00cac0578f9d6f9536;p=ppastats.git diff --git a/src/lp_json.c b/src/lp_json.c index a2b88f6..c9379c5 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 @@ -16,10 +16,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ - +#define _XOPEN_SOURCE_EXTENDED #define _XOPEN_SOURCE -#include - #include #include #include @@ -28,15 +26,47 @@ #include "lp_json.h" #include "lp_ws.h" +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; - const char *date_created; + 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")); @@ -50,24 +80,20 @@ static struct bpph *json_to_bpph(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, - architecture_specific); - - date_created = json_object_get_string - (json_object_object_get(o, "date_created")); - if (date_created) { - bpph->date_created.tm_isdst = -1; - strptime(date_created, "%FT%T%z", &bpph->date_created); - } + status, + arch_specific, + date_created); return bpph; } @@ -75,7 +101,6 @@ static struct bpph *json_to_bpph(json_object *o) static json_object *bpph_to_json(struct bpph *bpph) { json_object *json; - char *date; json = json_object_new_object(); @@ -95,22 +120,18 @@ 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)); - 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); - json_object_object_add - (json, - "date_created", - json_object_new_string(date)); + (json, "status", json_object_new_string(bpph->status)); - free(date); + json_object_object_add + (json, "date_created", time_to_json(bpph->date_created)); return json; } @@ -120,7 +141,7 @@ 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 @@ -155,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, @@ -219,8 +237,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; }