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
29 static time_t json_to_time(json_object *json)
35 str = json_object_get_string(json);
40 memset(&tm, 0, sizeof(struct tm));
42 ret = strptime(str, "%FT%T", &tm);
50 static json_object *time_to_json(time_t t)
57 return json_object_new_string(str);
62 static struct bpph *json_to_bpph(json_object *o)
64 const char *binary_package_name;
65 const char *binary_package_version;
66 const char *distro_arch_series_link;
67 const char *self_link;
73 binary_package_name = json_object_get_string
74 (json_object_object_get(o, "binary_package_name"));
76 binary_package_version = json_object_get_string
77 (json_object_object_get(o, "binary_package_version"));
79 distro_arch_series_link = json_object_get_string
80 (json_object_object_get(o, "distro_arch_series_link"));
82 self_link = json_object_get_string
83 (json_object_object_get(o, "self_link"));
85 arch_specific = json_object_get_boolean
86 (json_object_object_get(o, "architecture_specific"));
88 date_created = json_to_time(json_object_object_get(o, "date_created"));
90 status = json_object_get_string(json_object_object_get(o, "status"));
92 bpph = bpph_new(binary_package_name,
93 binary_package_version,
94 distro_arch_series_link,
103 static json_object *bpph_to_json(struct bpph *bpph)
107 json = json_object_new_object();
109 json_object_object_add
111 "binary_package_name",
112 json_object_new_string(bpph->binary_package_name));
114 json_object_object_add
116 "binary_package_version",
117 json_object_new_string(bpph->binary_package_version));
119 json_object_object_add
121 "distro_arch_series_link",
122 json_object_new_string(bpph->distro_arch_series_link));
124 json_object_object_add
125 (json, "self_link", json_object_new_string(bpph->self_link));
127 json_object_object_add
129 "architecture_specific",
130 json_object_new_boolean(bpph->architecture_specific));
132 json_object_object_add
133 (json, "status", json_object_new_string(bpph->status));
135 json_object_object_add
136 (json, "date_created", time_to_json(bpph->date_created));
141 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
143 const char *display_name;
145 const char *architecture_tag;
146 json_bool is_nominated_arch_indep;
147 const char *distroseries_link;
149 display_name = json_object_get_string
150 (json_object_object_get(o, "display_name"));
152 title = json_object_get_string
153 (json_object_object_get(o, "title"));
155 architecture_tag = json_object_get_string
156 (json_object_object_get(o, "architecture_tag"));
158 distroseries_link = json_object_get_string
159 (json_object_object_get(o, "distroseries_link"));
161 is_nominated_arch_indep = json_object_get_boolean
162 (json_object_object_get(o, "is_nominated_arch_indep"));
164 return distro_arch_series_new(display_name,
167 is_nominated_arch_indep,
171 struct distro_series *json_object_to_distro_series(json_object *o)
173 const char *displayname;
178 displayname = json_object_get_string
179 (json_object_object_get(o, "displayname"));
181 title = json_object_get_string(json_object_object_get(o, "title"));
183 version = json_object_get_string(json_object_object_get(o, "version"));
185 name = json_object_get_string(json_object_object_get(o, "name"));
187 return distro_series_new(name,
193 struct bpph **json_object_to_bpph_list(json_object *o)
195 json_object *o_entries;
197 struct bpph **entries, *h;
198 const struct distro_arch_series *distro;
200 o_entries = json_object_object_get(o, "entries");
205 n = json_object_array_length(o_entries);
208 (sizeof(struct bpph *)*(n+1));
210 for (i = 0, i2 = 0; i < n; i++) {
211 h = json_to_bpph(json_object_array_get_idx(o_entries,
214 if (!h->architecture_specific) {
215 distro = get_distro_arch_series
216 (h->distro_arch_series_link);
218 if (!distro || !distro->is_nominated_arch_indep) {
232 json_object *bpph_list_to_json(struct bpph **list)
234 json_object *result, *entries;
237 result = json_object_new_object();
239 entries = json_object_new_array();
240 json_object_object_add(result, "entries", entries);
243 for (cur = list; *cur; cur++)
244 json_object_array_add(entries, bpph_to_json(*cur));
249 struct daily_download_total *
250 json_object_to_daily_download_total(const char *d, json_object *o_c)
252 struct daily_download_total *result;
254 result = malloc(sizeof(struct daily_download_total));
255 result->count = json_object_get_int(o_c);
257 memset(&result->date, 0, sizeof(struct tm));
258 strptime(d, "%FT%T%z", &result->date);
263 static int json_object_get_fields_count(json_object *o)
266 struct lh_entry *entry;
268 entry = json_object_get_object(o)->head;
277 struct daily_download_total * *
278 json_object_to_daily_download_totals(json_object *o)
281 struct daily_download_total **result;
283 n = json_object_get_fields_count(o);
286 (sizeof(struct daily_download_total *)*(n+1));
289 json_object_object_foreach(o, key, val) {
290 result[i] = json_object_to_daily_download_total(key, val);
299 struct json_object *date_to_json(struct tm *tm)
303 json = json_object_new_array();
304 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
305 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
306 json_object_array_add(json, json_object_new_int(tm->tm_mday));
311 json_object *ddts_to_json(struct daily_download_total **ddts)
313 json_object *json_ddt, *json_ddts;
314 struct daily_download_total *ddt;
316 json_ddts = json_object_new_array();
318 while (ddts && *ddts) {
321 json_ddt = json_object_new_object();
322 json_object_object_add(json_ddt,
324 json_object_new_int(ddt->count));
325 json_object_object_add(json_ddt,
327 date_to_json(&ddt->date));
329 json_object_array_add(json_ddts, json_ddt);