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);
41 memset(&tm, 0, sizeof(struct tm));
43 ret = strptime(str, "%FT%T", &tm);
51 static json_object *time_to_json(time_t t)
56 str = time_to_ISO8601_time(&t);
59 j = json_object_new_string(str);
67 static struct bpph *json_to_bpph(json_object *o)
69 const char *binary_package_name;
70 const char *binary_package_version;
71 const char *distro_arch_series_link;
72 const char *self_link;
78 binary_package_name = json_object_get_string
79 (json_object_object_get(o, "binary_package_name"));
81 binary_package_version = json_object_get_string
82 (json_object_object_get(o, "binary_package_version"));
84 distro_arch_series_link = json_object_get_string
85 (json_object_object_get(o, "distro_arch_series_link"));
87 self_link = json_object_get_string
88 (json_object_object_get(o, "self_link"));
90 arch_specific = json_object_get_boolean
91 (json_object_object_get(o, "architecture_specific"));
93 date_created = json_to_time(json_object_object_get(o, "date_created"));
95 status = json_object_get_string(json_object_object_get(o, "status"));
97 bpph = bpph_new(binary_package_name,
98 binary_package_version,
99 distro_arch_series_link,
108 static json_object *bpph_to_json(struct bpph *bpph)
110 json_object *json, *time;
112 json = json_object_new_object();
114 json_object_object_add
116 "binary_package_name",
117 json_object_new_string(bpph->binary_package_name));
119 json_object_object_add
121 "binary_package_version",
122 json_object_new_string(bpph->binary_package_version));
124 json_object_object_add
126 "distro_arch_series_link",
127 json_object_new_string(bpph->distro_arch_series_link));
129 json_object_object_add
130 (json, "self_link", json_object_new_string(bpph->self_link));
132 json_object_object_add
134 "architecture_specific",
135 json_object_new_boolean(bpph->architecture_specific));
137 json_object_object_add
138 (json, "status", json_object_new_string(bpph->status));
140 time = time_to_json(bpph->date_created);
141 json_object_object_add
142 (json, "date_created", time);
147 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
149 const char *display_name;
151 const char *architecture_tag;
152 json_bool is_nominated_arch_indep;
153 const char *distroseries_link;
155 display_name = json_object_get_string
156 (json_object_object_get(o, "display_name"));
158 title = json_object_get_string
159 (json_object_object_get(o, "title"));
161 architecture_tag = json_object_get_string
162 (json_object_object_get(o, "architecture_tag"));
164 distroseries_link = json_object_get_string
165 (json_object_object_get(o, "distroseries_link"));
167 is_nominated_arch_indep = json_object_get_boolean
168 (json_object_object_get(o, "is_nominated_arch_indep"));
170 return distro_arch_series_new(display_name,
173 is_nominated_arch_indep,
177 struct distro_series *json_object_to_distro_series(json_object *o)
179 const char *displayname;
184 displayname = json_object_get_string
185 (json_object_object_get(o, "displayname"));
187 title = json_object_get_string(json_object_object_get(o, "title"));
189 version = json_object_get_string(json_object_object_get(o, "version"));
191 name = json_object_get_string(json_object_object_get(o, "name"));
193 return distro_series_new(name,
199 struct bpph **json_object_to_bpph_list(json_object *o)
201 json_object *o_entries;
203 struct bpph **entries, *h;
204 const struct distro_arch_series *distro;
206 o_entries = json_object_object_get(o, "entries");
211 n = json_object_array_length(o_entries);
214 (sizeof(struct bpph *)*(n+1));
216 for (i = 0, i2 = 0; i < n; i++) {
217 h = json_to_bpph(json_object_array_get_idx(o_entries,
220 if (!h->architecture_specific) {
221 distro = get_distro_arch_series
222 (h->distro_arch_series_link);
224 if (!distro || !distro->is_nominated_arch_indep) {
238 json_object *bpph_list_to_json(struct bpph **list)
240 json_object *result, *entries;
243 result = json_object_new_object();
245 entries = json_object_new_array();
246 json_object_object_add(result, "entries", entries);
249 for (cur = list; *cur; cur++)
250 json_object_array_add(entries, bpph_to_json(*cur));
255 struct daily_download_total *
256 json_object_to_daily_download_total(const char *d, json_object *o_c)
258 struct daily_download_total *result;
260 result = malloc(sizeof(struct daily_download_total));
261 result->count = json_object_get_int(o_c);
263 memset(&result->date, 0, sizeof(struct tm));
264 strptime(d, "%FT%T%z", &result->date);
269 static int json_object_get_fields_count(json_object *o)
272 struct lh_entry *entry;
274 entry = json_object_get_object(o)->head;
283 struct daily_download_total * *
284 json_object_to_daily_download_totals(json_object *o)
287 struct daily_download_total **result;
289 n = json_object_get_fields_count(o);
292 (sizeof(struct daily_download_total *)*(n+1));
295 json_object_object_foreach(o, key, val) {
296 result[i] = json_object_to_daily_download_total(key, val);
305 struct json_object *date_to_json(struct tm *tm)
309 json = json_object_new_array();
310 json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
311 json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
312 json_object_array_add(json, json_object_new_int(tm->tm_mday));
317 json_object *ddts_to_json(struct daily_download_total **ddts)
319 json_object *json_ddt, *json_ddts;
320 struct daily_download_total *ddt;
322 json_ddts = json_object_new_array();
324 while (ddts && *ddts) {
327 json_ddt = json_object_new_object();
328 json_object_object_add(json_ddt,
330 json_object_new_int(ddt->count));
331 json_object_object_add(json_ddt,
333 date_to_json(&ddt->date));
335 json_object_array_add(json_ddts, json_ddt);