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