2 * Copyright (C) 2011-2012 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define _XOPEN_SOURCE_EXTENDED
29 static struct bpph *json_to_bpph(json_object *o)
31 const char *binary_package_name;
32 const char *binary_package_version;
33 const char *distro_arch_series_link;
34 const char *self_link;
35 int architecture_specific;
36 const char *date_created;
40 binary_package_name = json_object_get_string
41 (json_object_object_get(o, "binary_package_name"));
43 binary_package_version = json_object_get_string
44 (json_object_object_get(o, "binary_package_version"));
46 distro_arch_series_link = json_object_get_string
47 (json_object_object_get(o, "distro_arch_series_link"));
49 self_link = json_object_get_string
50 (json_object_object_get(o, "self_link"));
52 if (json_object_get_boolean
53 (json_object_object_get(o, "architecture_specific")))
54 architecture_specific = 1;
56 architecture_specific = 0;
58 bpph = bpph_new(binary_package_name,
59 binary_package_version,
60 distro_arch_series_link,
62 architecture_specific);
64 date_created = json_object_get_string
65 (json_object_object_get(o, "date_created"));
67 strptime(date_created, "%FT%T%z", &bpph->date_created);
69 status = json_object_get_string(json_object_object_get(o, "status"));
71 bpph->status = strdup(status);
76 static json_object *bpph_to_json(struct bpph *bpph)
81 json = json_object_new_object();
83 json_object_object_add
85 "binary_package_name",
86 json_object_new_string(bpph->binary_package_name));
88 json_object_object_add
90 "binary_package_version",
91 json_object_new_string(bpph->binary_package_version));
93 json_object_object_add
95 "distro_arch_series_link",
96 json_object_new_string(bpph->distro_arch_series_link));
98 json_object_object_add
100 "architecture_specific",
101 json_object_new_boolean(bpph->architecture_specific));
103 json_object_object_add
104 (json, "status", json_object_new_string(bpph->status));
106 date = malloc(strlen("YY-MM-DDThh:mm:ss+xxx") + 1);
108 strlen("YY-MM-DDThh:mm:ss+xxx") + 1,
110 &bpph->date_created);
112 json_object_object_add
113 (json, "date_created", json_object_new_string(date));
119 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
121 const char *display_name;
123 const char *architecture_tag;
124 boolean is_nominated_arch_indep;
125 const char *distroseries_link;
127 display_name = json_object_get_string
128 (json_object_object_get(o, "display_name"));
130 title = json_object_get_string
131 (json_object_object_get(o, "title"));
133 architecture_tag = json_object_get_string
134 (json_object_object_get(o, "architecture_tag"));
136 distroseries_link = json_object_get_string
137 (json_object_object_get(o, "distroseries_link"));
139 is_nominated_arch_indep = json_object_get_boolean
140 (json_object_object_get(o, "is_nominated_arch_indep"));
142 return distro_arch_series_new(display_name,
145 is_nominated_arch_indep,
149 struct distro_series *json_object_to_distro_series(json_object *o)
151 const char *displayname;
156 displayname = json_object_get_string
157 (json_object_object_get(o, "displayname"));
159 title = json_object_get_string(json_object_object_get(o, "title"));
161 version = json_object_get_string(json_object_object_get(o, "version"));
163 name = json_object_get_string(json_object_object_get(o, "name"));
165 return distro_series_new(name,
171 struct bpph **json_object_to_bpph_list(json_object *o)
173 json_object *o_entries;
175 struct bpph **entries, *h;
176 const struct distro_arch_series *distro;
178 o_entries = json_object_object_get(o, "entries");
183 n = json_object_array_length(o_entries);
186 (sizeof(struct bpph *)*(n+1));
188 for (i = 0, i2 = 0; i < n; i++) {
189 h = json_to_bpph(json_object_array_get_idx(o_entries,
192 if (!h->architecture_specific) {
193 distro = get_distro_arch_series
194 (h->distro_arch_series_link);
196 if (!distro || !distro->is_nominated_arch_indep) {
210 json_object *bpph_list_to_json(struct bpph **list)
212 json_object *result, *entries;
215 result = json_object_new_object();
217 entries = json_object_new_array();
218 json_object_object_add(result, "entries", entries);
220 for (cur = list; *cur; cur++)
221 json_object_array_add(entries, bpph_to_json(*cur));
226 struct daily_download_total *
227 json_object_to_daily_download_total(const char *d, json_object *o_c)
229 struct daily_download_total *result;
231 result = malloc(sizeof(struct daily_download_total));
232 result->count = json_object_get_int(o_c);
234 strptime(d, "%FT%T%z", &result->date);
239 static int json_object_get_fields_count(json_object *o)
242 struct lh_entry *entry;
244 entry = json_object_get_object(o)->head;
253 struct daily_download_total * *
254 json_object_to_daily_download_totals(json_object *o)
257 struct daily_download_total **result;
259 n = json_object_get_fields_count(o);
262 (sizeof(struct daily_download_total *)*(n+1));
265 json_object_object_foreach(o, key, val) {
266 result[i] = json_object_to_daily_download_total(key, val);