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 ret = strptime(str, "%FT%T", &tm);
48 static json_object *time_to_json(time_t t)
55 return json_object_new_string(str);
60 static struct bpph *json_to_bpph(json_object *o)
62 const char *binary_package_name;
63 const char *binary_package_version;
64 const char *distro_arch_series_link;
65 const char *self_link;
71 binary_package_name = json_object_get_string
72 (json_object_object_get(o, "binary_package_name"));
74 binary_package_version = json_object_get_string
75 (json_object_object_get(o, "binary_package_version"));
77 distro_arch_series_link = json_object_get_string
78 (json_object_object_get(o, "distro_arch_series_link"));
80 self_link = json_object_get_string
81 (json_object_object_get(o, "self_link"));
83 arch_specific = json_object_get_boolean
84 (json_object_object_get(o, "architecture_specific"));
86 date_created = json_to_time(json_object_object_get(o, "date_created"));
88 status = json_object_get_string(json_object_object_get(o, "status"));
90 bpph = bpph_new(binary_package_name,
91 binary_package_version,
92 distro_arch_series_link,
101 static json_object *bpph_to_json(struct bpph *bpph)
105 json = json_object_new_object();
107 json_object_object_add
109 "binary_package_name",
110 json_object_new_string(bpph->binary_package_name));
112 json_object_object_add
114 "binary_package_version",
115 json_object_new_string(bpph->binary_package_version));
117 json_object_object_add
119 "distro_arch_series_link",
120 json_object_new_string(bpph->distro_arch_series_link));
122 json_object_object_add
123 (json, "self_link", json_object_new_string(bpph->self_link));
125 json_object_object_add
127 "architecture_specific",
128 json_object_new_boolean(bpph->architecture_specific));
130 json_object_object_add
131 (json, "status", json_object_new_string(bpph->status));
133 json_object_object_add
134 (json, "date_created", time_to_json(bpph->date_created));
139 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
141 const char *display_name;
143 const char *architecture_tag;
144 json_bool is_nominated_arch_indep;
145 const char *distroseries_link;
147 display_name = json_object_get_string
148 (json_object_object_get(o, "display_name"));
150 title = json_object_get_string
151 (json_object_object_get(o, "title"));
153 architecture_tag = json_object_get_string
154 (json_object_object_get(o, "architecture_tag"));
156 distroseries_link = json_object_get_string
157 (json_object_object_get(o, "distroseries_link"));
159 is_nominated_arch_indep = json_object_get_boolean
160 (json_object_object_get(o, "is_nominated_arch_indep"));
162 return distro_arch_series_new(display_name,
165 is_nominated_arch_indep,
169 struct distro_series *json_object_to_distro_series(json_object *o)
171 const char *displayname;
176 displayname = json_object_get_string
177 (json_object_object_get(o, "displayname"));
179 title = json_object_get_string(json_object_object_get(o, "title"));
181 version = json_object_get_string(json_object_object_get(o, "version"));
183 name = json_object_get_string(json_object_object_get(o, "name"));
185 return distro_series_new(name,
191 struct bpph **json_object_to_bpph_list(json_object *o)
193 json_object *o_entries;
195 struct bpph **entries, *h;
196 const struct distro_arch_series *distro;
198 o_entries = json_object_object_get(o, "entries");
203 n = json_object_array_length(o_entries);
206 (sizeof(struct bpph *)*(n+1));
208 for (i = 0, i2 = 0; i < n; i++) {
209 h = json_to_bpph(json_object_array_get_idx(o_entries,
212 if (!h->architecture_specific) {
213 distro = get_distro_arch_series
214 (h->distro_arch_series_link);
216 if (!distro || !distro->is_nominated_arch_indep) {
230 json_object *bpph_list_to_json(struct bpph **list)
232 json_object *result, *entries;
235 result = json_object_new_object();
237 entries = json_object_new_array();
238 json_object_object_add(result, "entries", entries);
241 for (cur = list; *cur; cur++)
242 json_object_array_add(entries, bpph_to_json(*cur));
247 struct daily_download_total *
248 json_object_to_daily_download_total(const char *d, json_object *o_c)
250 struct daily_download_total *result;
252 result = malloc(sizeof(struct daily_download_total));
253 result->count = json_object_get_int(o_c);
255 strptime(d, "%FT%T%z", &result->date);
260 static int json_object_get_fields_count(json_object *o)
263 struct lh_entry *entry;
265 entry = json_object_get_object(o)->head;
274 struct daily_download_total * *
275 json_object_to_daily_download_totals(json_object *o)
278 struct daily_download_total **result;
280 n = json_object_get_fields_count(o);
283 (sizeof(struct daily_download_total *)*(n+1));
286 json_object_object_foreach(o, key, val) {
287 result[i] = json_object_to_daily_download_total(key, val);