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;
77 binary_package_name = json_object_get_string
78 (json_object_object_get(o, "binary_package_name"));
80 binary_package_version = json_object_get_string
81 (json_object_object_get(o, "binary_package_version"));
83 distro_arch_series_link = json_object_get_string
84 (json_object_object_get(o, "distro_arch_series_link"));
86 self_link = json_object_get_string
87 (json_object_object_get(o, "self_link"));
89 arch_specific = json_object_get_boolean
90 (json_object_object_get(o, "architecture_specific"));
92 date_created = json_to_time(json_object_object_get(o, "date_created"));
94 status = json_object_get_string(json_object_object_get(o, "status"));
96 bpph = bpph_new(binary_package_name,
97 binary_package_version,
98 distro_arch_series_link,
107 static json_object *bpph_to_json(struct bpph *bpph)
109 json_object *json, *time;
111 json = json_object_new_object();
113 json_object_object_add
115 "binary_package_name",
116 json_object_new_string(bpph->binary_package_name));
118 json_object_object_add
120 "binary_package_version",
121 json_object_new_string(bpph->binary_package_version));
123 json_object_object_add
125 "distro_arch_series_link",
126 json_object_new_string(bpph->distro_arch_series_link));
128 json_object_object_add
129 (json, "self_link", json_object_new_string(bpph->self_link));
131 json_object_object_add
133 "architecture_specific",
134 json_object_new_boolean(bpph->architecture_specific));
136 json_object_object_add
137 (json, "status", json_object_new_string(bpph->status));
139 time = time_to_json(bpph->date_created);
140 json_object_object_add
141 (json, "date_created", time);
146 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
148 const char *display_name;
150 const char *architecture_tag;
151 json_bool is_nominated_arch_indep;
152 const char *distroseries_link;
154 display_name = json_object_get_string
155 (json_object_object_get(o, "display_name"));
157 title = json_object_get_string
158 (json_object_object_get(o, "title"));
160 architecture_tag = json_object_get_string
161 (json_object_object_get(o, "architecture_tag"));
163 distroseries_link = json_object_get_string
164 (json_object_object_get(o, "distroseries_link"));
166 is_nominated_arch_indep = json_object_get_boolean
167 (json_object_object_get(o, "is_nominated_arch_indep"));
169 return distro_arch_series_new(display_name,
172 is_nominated_arch_indep,
176 struct distro_series *json_object_to_distro_series(json_object *o)
178 const char *displayname;
183 displayname = json_object_get_string
184 (json_object_object_get(o, "displayname"));
186 title = json_object_get_string(json_object_object_get(o, "title"));
188 version = json_object_get_string(json_object_object_get(o, "version"));
190 name = json_object_get_string(json_object_object_get(o, "name"));
192 return distro_series_new(name,
198 struct bpph **json_object_to_bpph_list(json_object *o)
200 json_object *o_entries;
202 struct bpph **entries, *h;
203 const struct distro_arch_series *distro;
205 o_entries = json_object_object_get(o, "entries");
210 n = json_object_array_length(o_entries);
213 (sizeof(struct bpph *)*(n+1));
215 for (i = 0, i2 = 0; i < n; i++) {
216 h = json_to_bpph(json_object_array_get_idx(o_entries,
219 if (!h->architecture_specific) {
220 distro = get_distro_arch_series
221 (h->distro_arch_series_link);
223 if (!distro || !distro->is_nominated_arch_indep) {
237 json_object *bpph_list_to_json(struct bpph **list)
239 json_object *result, *entries;
242 result = json_object_new_object();
244 entries = json_object_new_array();
245 json_object_object_add(result, "entries", entries);
248 for (cur = list; *cur; cur++)
249 json_object_array_add(entries, bpph_to_json(*cur));
254 struct daily_download_total *
255 json_object_to_daily_download_total(const char *d, json_object *o_c)
257 struct daily_download_total *result;
259 result = malloc(sizeof(struct daily_download_total));
260 result->count = json_object_get_int(o_c);
262 memset(&result->date, 0, sizeof(struct tm));
263 strptime(d, "%FT%T%z", &result->date);
268 static int json_object_get_fields_count(json_object *o)
271 struct lh_entry *entry;
273 entry = json_object_get_object(o)->head;
282 struct daily_download_total * *
283 json_object_to_daily_download_totals(json_object *o)
286 struct daily_download_total **result;
288 n = json_object_get_fields_count(o);
291 (sizeof(struct daily_download_total *)*(n+1));
294 json_object_object_foreach(o, key, val) {
295 result[i] = json_object_to_daily_download_total(key, val);
304 struct json_object *date_to_json(struct tm *tm)
308 json = json_object_new_array();
309 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
310 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
311 json_object_array_add(json, json_object_new_int(tm->tm_mday));
316 json_object *ddts_to_json(struct daily_download_total **ddts)
318 json_object *json_ddt, *json_ddts;
319 struct daily_download_total *ddt;
321 json_ddts = json_object_new_array();
323 while (ddts && *ddts) {
326 json_ddt = json_object_new_object();
327 json_object_object_add(json_ddt,
329 json_object_new_int(ddt->count));
330 json_object_object_add(json_ddt,
332 date_to_json(&ddt->date));
334 json_object_array_add(json_ddts, json_ddt);