fixed style
[ppastats.git] / src / lp.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
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "list.h"
25 #include "lp.h"
26
27 char *time_to_str(time_t t)
28 {
29         char *str;
30         struct tm *tm;
31         size_t ret;
32
33         tm = localtime(&t);
34
35         if (!tm)
36                 return NULL;
37
38         str = malloc(strlen("YYYY-MM-DDThh:mm:ss") + 1);
39         ret = strftime(str, strlen("YYYY-MM-DDThh:mm:ss") + 1, "%FT%T", tm);
40
41         if (ret)
42                 return str;
43
44         free(str);
45         return NULL;
46 }
47
48 struct distro_series *distro_series_new(const char *name,
49                                         const char *version,
50                                         const char *title,
51                                         const char *displayname)
52 {
53         struct distro_series *d;
54
55         d = malloc(sizeof(struct distro_series));
56
57         d->name = strdup(name);
58         d->version = strdup(version);
59         d->title = strdup(title);
60         d->displayname = strdup(displayname);
61
62         return d;
63 }
64
65 void distro_series_free(struct distro_series *d)
66 {
67         if (d) {
68                 free(d->name);
69                 free(d->version);
70                 free(d->title);
71                 free(d->displayname);
72
73                 free(d);
74         }
75 }
76
77 void bpph_free(struct bpph *b)
78 {
79         if (b) {
80                 free(b->binary_package_name);
81                 free(b->binary_package_version);
82                 free(b->distro_arch_series_link);
83                 free(b->self_link);
84                 free(b->status);
85                 free(b);
86         }
87 }
88
89 struct bpph *bpph_new(const char *binary_package_name,
90                       const char *binary_package_version,
91                       const char *distro_arch_series_link,
92                       const char *self_link,
93                       const char *status,
94                       int architecture_specific,
95                       time_t date_created)
96 {
97         struct bpph *h;
98
99         h = malloc(sizeof(struct bpph));
100
101         h->binary_package_name = strdup(binary_package_name);
102         h->binary_package_version = strdup(binary_package_version);
103         h->distro_arch_series_link = strdup(distro_arch_series_link);
104         h->self_link = strdup(self_link);
105         h->architecture_specific = architecture_specific;
106         h->status = strdup(status);
107         h->date_created = date_created;
108
109         return h;
110 }
111
112 void bpph_list_free(struct bpph **list)
113 {
114         struct bpph **l_cur = list;
115
116         while (*l_cur) {
117                 bpph_free(*l_cur);
118                 l_cur++;
119         }
120
121         free(list);
122 }
123
124 char *get_archive_url(const char *owner, const char *ppa)
125 {
126         char *url = malloc(strlen(URL_BASE_LP)
127                            +strlen("/~")
128                            +strlen(owner)
129                            +strlen("/+archive/")
130                            +strlen(ppa)
131                            +1);
132
133         strcpy(url, URL_BASE_LP);
134         strcat(url, "/~");
135         strcat(url, owner);
136         strcat(url, "/+archive/");
137         strcat(url, ppa);
138
139         return url;
140 }
141
142 struct distro_arch_series *distro_arch_series_new(const char *display_name,
143                                                   const char *title,
144                                                   const char *architecture_tag,
145                                                   int is_nominated_arch_indep,
146                                                   const char *distroseries_link)
147 {
148         struct distro_arch_series *d;
149
150         d = malloc(sizeof(struct distro_arch_series));
151
152         d->display_name = strdup(display_name);
153         d->title = strdup(title);
154         d->architecture_tag = strdup(architecture_tag);
155         d->is_nominated_arch_indep = is_nominated_arch_indep;
156         d->distroseries_link = strdup(distroseries_link);
157
158         return d;
159 }
160
161 void distro_arch_series_free(struct distro_arch_series *d)
162 {
163         free(d->display_name);
164         free(d->title);
165         free(d->architecture_tag);
166         free(d->distroseries_link);
167
168         free(d);
169 }
170
171 void distro_arch_series_list_free(struct distro_arch_series **list)
172 {
173         if (list) {
174                 while (*list) {
175                         distro_arch_series_free(*list);
176                         list++;
177                 }
178                 free(list);
179         }
180 }
181
182 void daily_download_total_list_free(struct daily_download_total **list)
183 {
184         if (list) {
185                 struct daily_download_total **cur = list;
186
187                 while (*cur) {
188                         free(*cur);
189                         cur++;
190                 }
191
192                 free(list);
193         }
194 }
195
196 struct bpph **bpph_list_add(struct bpph **list, struct bpph *new)
197 {
198         struct bpph **cur, *bpph;
199
200         if (list)
201                 for (cur = list; *cur; cur++) {
202                         bpph = *cur;
203
204                         if (!strcmp(bpph->self_link, new->self_link))
205                                 return list;
206                 }
207
208         return (struct bpph **)list_add((void **)list, new);
209 }
210
211 struct bpph **bpph_list_append_list(struct bpph **list1, struct bpph **list2)
212 {
213         struct bpph **cur;
214
215         if (!list2)
216                 return list1;
217
218         for (cur = list2; *cur; cur++)
219                 list1 = bpph_list_add(list1, *cur);
220
221         return list1;
222 }
223
224 time_t ddts_get_last_date(struct daily_download_total **ddts)
225 {
226         struct daily_download_total **cur;
227         time_t t, last_t;
228
229         if (!ddts)
230                 return 0;
231
232         last_t = 0;
233         for (cur = ddts; *cur; cur++) {
234                 t = mktime(&(*cur)->date);
235                 if (t > last_t)
236                         last_t = t;
237         }
238
239         return last_t;
240 }
241
242 int ddts_length(struct daily_download_total **ddts)
243 {
244         int n;
245         struct daily_download_total **cur;
246
247         n = 0;
248
249         if (ddts)
250                 for (cur = ddts; *cur; cur++)
251                         n++;
252
253         return n;
254 }
255
256
257 struct daily_download_total **add_total
258 (struct daily_download_total **totals, struct daily_download_total *total)
259 {
260         struct daily_download_total **cur;
261         struct daily_download_total *item;
262
263         if (totals) {
264                 cur = totals;
265                 while (*cur) {
266                         item = *cur;
267
268                         if (item->date.tm_year == total->date.tm_year &&
269                             item->date.tm_mon == total->date.tm_mon &&
270                             item->date.tm_mday == total->date.tm_mday) {
271                                 item->count = total->count;
272                                 return totals;
273                         }
274
275                         cur++;
276                 }
277         }
278
279         item = malloc(sizeof(struct daily_download_total));
280         memcpy(item, total, sizeof(struct daily_download_total));
281
282         return (struct daily_download_total **)
283                 list_add((void **)totals, (void *)item);
284 }
285
286
287 struct daily_download_total **
288 ddts_merge(struct daily_download_total **ddts1,
289            struct daily_download_total **ddts2)
290 {
291         struct daily_download_total **ddts, **cur, **tmp;
292
293         if (ddts1) {
294                 ddts = malloc((ddts_length(ddts1) + 1)
295                               * sizeof(struct daily_download_total *));
296                 memcpy(ddts, ddts1, (ddts_length(ddts1) + 1) * sizeof(void *));
297         } else {
298                 ddts = malloc(sizeof(struct daily_download_total *));
299                 ddts[0] = NULL;
300         }
301
302         if (ddts2)
303                 for (cur = ddts2; *cur; cur++) {
304                         tmp = add_total(ddts, *cur);
305                         if (tmp != ddts)
306                                 ddts = tmp;
307                 }
308
309         return ddts;
310 }
311
312 int ddts_get_count(struct daily_download_total **ddts)
313 {
314         struct daily_download_total **cur;
315         int i;
316
317         i = 0;
318         for (cur = ddts; *cur; cur++)
319                 i += (*cur)->count;
320
321         return i;
322 }