fix json_bool
[ppastats.git] / src / lp_json.c
1 /*
2  * Copyright (C) 2011-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #define _XOPEN_SOURCE_EXTENDED
20 #define _XOPEN_SOURCE
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <time.h>
25
26 #include <config.h>
27 #include "lp_json.h"
28 #include "lp_ws.h"
29
30 /* Declares json_bool to have consistent code even with
31    old json lib releases using boolean instead of json_bool.*/
32 #ifndef HAVE_JSON_BOOL
33 typedef boolean json_bool;
34 #endif
35
36 static time_t json_to_time(json_object *json)
37 {
38         const char *str;
39         struct tm tm;
40         char *ret;
41
42         str = json_object_get_string(json);
43         if (!str)
44                 return -1;
45
46         tm.tm_isdst = -1;
47         ret = strptime(str, "%FT%T", &tm);
48
49         if (ret)
50                 return mktime(&tm);
51         else
52                 return -1;
53 }
54
55 static json_object *time_to_json(time_t t)
56 {
57         char *str;
58
59         str = time_to_str(t);
60
61         if (str)
62                 return json_object_new_string(str);
63         else
64                 return NULL;
65 }
66
67 static struct bpph *json_to_bpph(json_object *o)
68 {
69         const char *binary_package_name;
70         const char *binary_package_version;
71         const char *distro_arch_series_link;
72         const char *self_link;
73         int arch_specific;
74         struct bpph *bpph;
75         const char *status;
76         time_t date_created;
77
78         binary_package_name = json_object_get_string
79                 (json_object_object_get(o, "binary_package_name"));
80
81         binary_package_version = json_object_get_string
82                 (json_object_object_get(o, "binary_package_version"));
83
84         distro_arch_series_link = json_object_get_string
85                 (json_object_object_get(o, "distro_arch_series_link"));
86
87         self_link = json_object_get_string
88                 (json_object_object_get(o, "self_link"));
89
90         arch_specific = json_object_get_boolean
91                 (json_object_object_get(o, "architecture_specific"));
92
93         date_created = json_to_time(json_object_object_get(o, "date_created"));
94
95         status = json_object_get_string(json_object_object_get(o, "status"));
96
97         bpph =  bpph_new(binary_package_name,
98                          binary_package_version,
99                          distro_arch_series_link,
100                          self_link,
101                          status,
102                          arch_specific,
103                          date_created);
104
105         return bpph;
106 }
107
108 static json_object *bpph_to_json(struct bpph *bpph)
109 {
110         json_object *json;
111
112         json = json_object_new_object();
113
114         json_object_object_add
115                 (json,
116                  "binary_package_name",
117                  json_object_new_string(bpph->binary_package_name));
118
119         json_object_object_add
120                 (json,
121                  "binary_package_version",
122                  json_object_new_string(bpph->binary_package_version));
123
124         json_object_object_add
125                 (json,
126                  "distro_arch_series_link",
127                  json_object_new_string(bpph->distro_arch_series_link));
128
129         json_object_object_add
130                 (json, "self_link", json_object_new_string(bpph->self_link));
131
132         json_object_object_add
133                 (json,
134                  "architecture_specific",
135                  json_object_new_boolean(bpph->architecture_specific));
136
137         json_object_object_add
138                 (json, "status", json_object_new_string(bpph->status));
139
140         json_object_object_add
141                 (json, "date_created", time_to_json(bpph->date_created));
142
143         return json;
144 }
145
146 struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
147 {
148         const char *display_name;
149         const char *title;
150         const char *architecture_tag;
151         json_bool is_nominated_arch_indep;
152         const char *distroseries_link;
153
154         display_name = json_object_get_string
155                 (json_object_object_get(o, "display_name"));
156
157         title = json_object_get_string
158                 (json_object_object_get(o, "title"));
159
160         architecture_tag = json_object_get_string
161                 (json_object_object_get(o, "architecture_tag"));
162
163         distroseries_link = json_object_get_string
164                 (json_object_object_get(o, "distroseries_link"));
165
166         is_nominated_arch_indep = json_object_get_boolean
167                 (json_object_object_get(o, "is_nominated_arch_indep"));
168
169         return distro_arch_series_new(display_name,
170                                       title,
171                                       architecture_tag,
172                                       is_nominated_arch_indep,
173                                       distroseries_link);
174 }
175
176 struct distro_series *json_object_to_distro_series(json_object *o)
177 {
178         const char *displayname;
179         const char *title;
180         const char *name;
181         const char *version;
182
183         displayname = json_object_get_string
184                 (json_object_object_get(o, "displayname"));
185
186         title = json_object_get_string(json_object_object_get(o, "title"));
187
188         version = json_object_get_string(json_object_object_get(o, "version"));
189
190         name = json_object_get_string(json_object_object_get(o, "name"));
191
192         return distro_series_new(name,
193                                  version,
194                                  title,
195                                  displayname);
196 }
197
198 struct bpph **json_object_to_bpph_list(json_object *o)
199 {
200         json_object *o_entries;
201         int i, n, i2;
202         struct bpph **entries, *h;
203         const struct distro_arch_series *distro;
204
205         o_entries = json_object_object_get(o, "entries");
206
207         if (!o_entries)
208                 return NULL;
209
210         n = json_object_array_length(o_entries);
211
212         entries = malloc
213                 (sizeof(struct bpph *)*(n+1));
214
215         for (i = 0, i2 = 0; i < n; i++) {
216                 h = json_to_bpph(json_object_array_get_idx(o_entries,
217                                                            i));
218
219                 if (!h->architecture_specific) {
220                         distro = get_distro_arch_series
221                                 (h->distro_arch_series_link);
222
223                         if (!distro || !distro->is_nominated_arch_indep) {
224                                 bpph_free(h);
225                                 continue ;
226                         }
227                 }
228
229                 entries[i2] = h;
230                 i2++;
231         }
232         entries[i2] = NULL;
233
234         return entries;
235 }
236
237 json_object *bpph_list_to_json(struct bpph **list)
238 {
239         json_object *result, *entries;
240         struct bpph **cur;
241
242         result = json_object_new_object();
243
244         entries = json_object_new_array();
245         json_object_object_add(result, "entries", entries);
246
247         if (list)
248                 for (cur = list; *cur; cur++)
249                         json_object_array_add(entries, bpph_to_json(*cur));
250
251         return result;
252 }
253
254 struct daily_download_total *
255 json_object_to_daily_download_total(const char *d, json_object *o_c)
256 {
257         struct daily_download_total *result;
258
259         result = malloc(sizeof(struct daily_download_total));
260         result->count = json_object_get_int(o_c);
261
262         strptime(d, "%FT%T%z", &result->date);
263
264         return result;
265 }
266
267 static int json_object_get_fields_count(json_object *o)
268 {
269         int n = 0;
270         struct lh_entry *entry;
271
272         entry = json_object_get_object(o)->head;
273         while (entry) {
274                 entry = entry->next;
275                 n++;
276         }
277
278         return n;
279 }
280
281 struct daily_download_total * *
282 json_object_to_daily_download_totals(json_object *o)
283 {
284         int n, i;
285         struct daily_download_total **result;
286
287         n = json_object_get_fields_count(o);
288
289         result = malloc
290                 (sizeof(struct daily_download_total *)*(n+1));
291
292         i = 0;
293         json_object_object_foreach(o, key, val) {
294                 result[i] = json_object_to_daily_download_total(key, val);
295                 i++;
296         }
297
298         result[n] = NULL;
299
300         return result;
301 }