2 * Copyright (C) 2011-2014 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
30 static time_t json_to_time(json_object *json)
36 str = json_object_get_string(json);
40 memset(&tm, 0, sizeof(struct tm));
42 ret = strptime(str, "%FT%T", &tm);
50 json_object *time_to_json(time_t t)
55 str = time_to_ISO8601_time(&t);
58 j = json_object_new_string(str);
66 static struct bpph *json_to_bpph(json_object *o)
68 const char *binary_package_name;
69 const char *binary_package_version;
70 const char *distro_arch_series_link;
71 const char *self_link;
78 json_object_object_get_ex(o, "binary_package_name", &j);
79 binary_package_name = json_object_get_string(j);
81 json_object_object_get_ex(o, "binary_package_version", &j);
82 binary_package_version = json_object_get_string(j);
84 json_object_object_get_ex(o, "distro_arch_series_link", &j);
85 distro_arch_series_link = json_object_get_string(j);
87 json_object_object_get_ex(o, "self_link", &j);
88 self_link = json_object_get_string(j);
90 json_object_object_get_ex(o, "architecture_specific", &j);
91 arch_specific = json_object_get_boolean(j);
93 json_object_object_get_ex(o, "date_created", &j);
94 date_created = json_to_time(j);
96 json_object_object_get_ex(o, "status", &j);
97 status = json_object_get_string(j);
99 bpph = bpph_new(binary_package_name,
100 binary_package_version,
101 distro_arch_series_link,
110 static json_object *bpph_to_json(struct bpph *bpph)
112 json_object *json, *time;
114 json = json_object_new_object();
116 json_object_object_add
118 "binary_package_name",
119 json_object_new_string(bpph->binary_package_name));
121 json_object_object_add
123 "binary_package_version",
124 json_object_new_string(bpph->binary_package_version));
126 json_object_object_add
128 "distro_arch_series_link",
129 json_object_new_string(bpph->distro_arch_series_link));
131 json_object_object_add
132 (json, "self_link", json_object_new_string(bpph->self_link));
134 json_object_object_add
136 "architecture_specific",
137 json_object_new_boolean(bpph->architecture_specific));
139 json_object_object_add
140 (json, "status", json_object_new_string(bpph->status));
142 time = time_to_json(bpph->date_created);
143 json_object_object_add
144 (json, "date_created", time);
149 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
151 const char *display_name;
153 const char *architecture_tag;
154 json_bool is_nominated_arch_indep;
155 const char *distroseries_link;
158 json_object_object_get_ex(o, "display_name", &j);
159 display_name = json_object_get_string(j);
161 json_object_object_get_ex(o, "title", &j);
162 title = json_object_get_string(j);
164 json_object_object_get_ex(o, "architecture_tag", &j);
165 architecture_tag = json_object_get_string(j);
167 json_object_object_get_ex(o, "distroseries_link", &j);
168 distroseries_link = json_object_get_string(j);
170 json_object_object_get_ex(o, "is_nominated_arch_indep", &j);
171 is_nominated_arch_indep = json_object_get_boolean
174 return distro_arch_series_new(display_name,
177 is_nominated_arch_indep,
181 struct distro_series *json_object_to_distro_series(json_object *o)
183 const char *displayname;
189 json_object_object_get_ex(o, "displayname", &j);
190 displayname = json_object_get_string(j);
192 json_object_object_get_ex(o, "title", &j);
193 title = json_object_get_string(j);
195 json_object_object_get_ex(o, "version", &j);
196 version = json_object_get_string(j);
198 json_object_object_get_ex(o, "name", &j);
199 name = json_object_get_string(j);
201 return distro_series_new(name,
207 struct bpph **json_object_to_bpph_list(json_object *o)
209 json_object *o_entries;
211 struct bpph **entries, *h;
212 const struct distro_arch_series *distro;
214 json_object_object_get_ex(o, "entries", &o_entries);
219 n = json_object_array_length(o_entries);
222 (sizeof(struct bpph *)*(n+1));
224 for (i = 0, i2 = 0; i < n; i++) {
225 h = json_to_bpph(json_object_array_get_idx(o_entries,
228 if (!h->architecture_specific) {
229 distro = get_distro_arch_series
230 (h->distro_arch_series_link);
232 if (!distro || !distro->is_nominated_arch_indep) {
246 json_object *bpph_list_to_json(struct bpph **list)
248 json_object *result, *entries;
251 result = json_object_new_object();
253 entries = json_object_new_array();
254 json_object_object_add(result, "entries", entries);
257 for (cur = list; *cur; cur++)
258 json_object_array_add(entries, bpph_to_json(*cur));
263 struct daily_download_total *
264 json_object_to_daily_download_total(const char *d, json_object *o_c)
266 struct daily_download_total *result;
268 result = malloc(sizeof(struct daily_download_total));
269 result->count = json_object_get_int(o_c);
271 memset(&result->date, 0, sizeof(struct tm));
272 strptime(d, "%FT%T%z", &result->date);
277 static int json_object_get_fields_count(json_object *o)
280 struct lh_entry *entry;
282 entry = json_object_get_object(o)->head;
291 struct daily_download_total * *
292 json_object_to_daily_download_totals(json_object *o)
295 struct daily_download_total **result;
297 n = json_object_get_fields_count(o);
300 (sizeof(struct daily_download_total *)*(n+1));
303 json_object_object_foreach(o, key, val) {
304 result[i] = json_object_to_daily_download_total(key, val);
313 struct json_object *date_to_json(struct tm *tm)
317 json = json_object_new_array();
318 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
319 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
320 json_object_array_add(json, json_object_new_int(tm->tm_mday));
325 json_object *ddts_to_json(struct daily_download_total **ddts)
327 json_object *json_ddt, *json_ddts;
328 struct daily_download_total *ddt;
330 json_ddts = json_object_new_array();
332 while (ddts && *ddts) {
335 json_ddt = json_object_new_object();
336 json_object_object_add(json_ddt,
338 json_object_new_int(ddt->count));
339 json_object_object_add(json_ddt,
341 date_to_json(&ddt->date));
343 json_object_array_add(json_ddts, json_ddt);