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 /* Declares json_bool to have consistent code even with
30 old json lib releases using boolean instead of json_bool.*/
32 typedef boolean json_bool;
35 static time_t json_to_time(json_object *json)
41 str = json_object_get_string(json);
46 ret = strptime(str, "%FT%T", &tm);
54 static json_object *time_to_json(time_t t)
61 return 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)
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 json_object_object_add
140 (json, "date_created", time_to_json(bpph->date_created));
145 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
147 const char *display_name;
149 const char *architecture_tag;
150 json_bool is_nominated_arch_indep;
151 const char *distroseries_link;
153 display_name = json_object_get_string
154 (json_object_object_get(o, "display_name"));
156 title = json_object_get_string
157 (json_object_object_get(o, "title"));
159 architecture_tag = json_object_get_string
160 (json_object_object_get(o, "architecture_tag"));
162 distroseries_link = json_object_get_string
163 (json_object_object_get(o, "distroseries_link"));
165 is_nominated_arch_indep = json_object_get_boolean
166 (json_object_object_get(o, "is_nominated_arch_indep"));
168 return distro_arch_series_new(display_name,
171 is_nominated_arch_indep,
175 struct distro_series *json_object_to_distro_series(json_object *o)
177 const char *displayname;
182 displayname = json_object_get_string
183 (json_object_object_get(o, "displayname"));
185 title = json_object_get_string(json_object_object_get(o, "title"));
187 version = json_object_get_string(json_object_object_get(o, "version"));
189 name = json_object_get_string(json_object_object_get(o, "name"));
191 return distro_series_new(name,
197 struct bpph **json_object_to_bpph_list(json_object *o)
199 json_object *o_entries;
201 struct bpph **entries, *h;
202 const struct distro_arch_series *distro;
204 o_entries = json_object_object_get(o, "entries");
209 n = json_object_array_length(o_entries);
212 (sizeof(struct bpph *)*(n+1));
214 for (i = 0, i2 = 0; i < n; i++) {
215 h = json_to_bpph(json_object_array_get_idx(o_entries,
218 if (!h->architecture_specific) {
219 distro = get_distro_arch_series
220 (h->distro_arch_series_link);
222 if (!distro || !distro->is_nominated_arch_indep) {
236 json_object *bpph_list_to_json(struct bpph **list)
238 json_object *result, *entries;
241 result = json_object_new_object();
243 entries = json_object_new_array();
244 json_object_object_add(result, "entries", entries);
247 for (cur = list; *cur; cur++)
248 json_object_array_add(entries, bpph_to_json(*cur));
253 struct daily_download_total *
254 json_object_to_daily_download_total(const char *d, json_object *o_c)
256 struct daily_download_total *result;
258 result = malloc(sizeof(struct daily_download_total));
259 result->count = json_object_get_int(o_c);
261 strptime(d, "%FT%T%z", &result->date);
266 static int json_object_get_fields_count(json_object *o)
269 struct lh_entry *entry;
271 entry = json_object_get_object(o)->head;
280 struct daily_download_total * *
281 json_object_to_daily_download_totals(json_object *o)
284 struct daily_download_total **result;
286 n = json_object_get_fields_count(o);
289 (sizeof(struct daily_download_total *)*(n+1));
292 json_object_object_foreach(o, key, val) {
293 result[i] = json_object_to_daily_download_total(key, val);