2 * Copyright (C) 2011-2012 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 struct bpph *json_to_bpph(json_object *o)
31 const char *binary_package_name;
32 const char *binary_package_version;
33 const char *distro_arch_series_link;
34 const char *self_link;
35 int architecture_specific;
36 const char *date_created;
40 binary_package_name = json_object_get_string
41 (json_object_object_get(o, "binary_package_name"));
43 binary_package_version = json_object_get_string
44 (json_object_object_get(o, "binary_package_version"));
46 distro_arch_series_link = json_object_get_string
47 (json_object_object_get(o, "distro_arch_series_link"));
49 self_link = json_object_get_string
50 (json_object_object_get(o, "self_link"));
52 if (json_object_get_boolean
53 (json_object_object_get(o, "architecture_specific")))
54 architecture_specific = 1;
56 architecture_specific = 0;
58 bpph = bpph_new(binary_package_name,
59 binary_package_version,
60 distro_arch_series_link,
62 architecture_specific);
64 date_created = json_object_get_string
65 (json_object_object_get(o, "date_created"));
67 strptime(date_created, "%FT%T%z", &bpph->date_created);
69 status = json_object_get_string(json_object_object_get(o, "status"));
71 bpph->status = strdup(status);
76 static json_object *bpph_to_json(struct bpph *bpph)
81 json = json_object_new_object();
83 json_object_object_add
85 "binary_package_name",
86 json_object_new_string(bpph->binary_package_name));
88 json_object_object_add
90 "binary_package_version",
91 json_object_new_string(bpph->binary_package_version));
93 json_object_object_add
95 "distro_arch_series_link",
96 json_object_new_string(bpph->distro_arch_series_link));
98 json_object_object_add
101 json_object_new_string(bpph->self_link));
103 json_object_object_add
105 "architecture_specific",
106 json_object_new_boolean(bpph->architecture_specific));
108 json_object_object_add
109 (json, "status", json_object_new_string(bpph->status));
111 date = malloc(strlen("YY-MM-DDThh:mm:ss+xxx") + 1);
113 strlen("YY-MM-DDThh:mm:ss+xxx") + 1,
115 &bpph->date_created);
117 json_object_object_add
118 (json, "date_created", json_object_new_string(date));
124 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
126 const char *display_name;
128 const char *architecture_tag;
129 boolean is_nominated_arch_indep;
130 const char *distroseries_link;
132 display_name = json_object_get_string
133 (json_object_object_get(o, "display_name"));
135 title = json_object_get_string
136 (json_object_object_get(o, "title"));
138 architecture_tag = json_object_get_string
139 (json_object_object_get(o, "architecture_tag"));
141 distroseries_link = json_object_get_string
142 (json_object_object_get(o, "distroseries_link"));
144 is_nominated_arch_indep = json_object_get_boolean
145 (json_object_object_get(o, "is_nominated_arch_indep"));
147 return distro_arch_series_new(display_name,
150 is_nominated_arch_indep,
154 struct distro_series *json_object_to_distro_series(json_object *o)
156 const char *displayname;
161 displayname = json_object_get_string
162 (json_object_object_get(o, "displayname"));
164 title = json_object_get_string(json_object_object_get(o, "title"));
166 version = json_object_get_string(json_object_object_get(o, "version"));
168 name = json_object_get_string(json_object_object_get(o, "name"));
170 return distro_series_new(name,
176 struct bpph **json_object_to_bpph_list(json_object *o)
178 json_object *o_entries;
180 struct bpph **entries, *h;
181 const struct distro_arch_series *distro;
183 o_entries = json_object_object_get(o, "entries");
188 n = json_object_array_length(o_entries);
191 (sizeof(struct bpph *)*(n+1));
193 for (i = 0, i2 = 0; i < n; i++) {
194 h = json_to_bpph(json_object_array_get_idx(o_entries,
197 if (!h->architecture_specific) {
198 distro = get_distro_arch_series
199 (h->distro_arch_series_link);
201 if (!distro || !distro->is_nominated_arch_indep) {
215 json_object *bpph_list_to_json(struct bpph **list)
217 json_object *result, *entries;
220 result = json_object_new_object();
222 entries = json_object_new_array();
223 json_object_object_add(result, "entries", entries);
226 for (cur = list; *cur; cur++)
227 json_object_array_add(entries, bpph_to_json(*cur));
232 struct daily_download_total *
233 json_object_to_daily_download_total(const char *d, json_object *o_c)
235 struct daily_download_total *result;
237 result = malloc(sizeof(struct daily_download_total));
238 result->count = json_object_get_int(o_c);
240 strptime(d, "%FT%T%z", &result->date);
245 static int json_object_get_fields_count(json_object *o)
248 struct lh_entry *entry;
250 entry = json_object_get_object(o)->head;
259 struct daily_download_total * *
260 json_object_to_daily_download_totals(json_object *o)
263 struct daily_download_total **result;
265 n = json_object_get_fields_count(o);
268 (sizeof(struct daily_download_total *)*(n+1));
271 json_object_object_foreach(o, key, val) {
272 result[i] = json_object_to_daily_download_total(key, val);