bd9ba5d95fd1aa11162e51e2e975ba8eeb4145f0
[ppastats.git] / src / lp.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
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "list.h"
25 #include "lp.h"
26
27 struct distro_series *distro_series_new(const char *name,
28                                         const char *version,
29                                         const char *title,
30                                         const char *displayname)
31 {
32         struct distro_series *d;
33
34         d = malloc(sizeof(struct distro_series));
35
36         d->name = strdup(name);
37         d->version = strdup(version);
38         d->title = strdup(title);
39         d->displayname = strdup(displayname);
40
41         return d;
42 }
43
44 void distro_series_free(struct distro_series *d)
45 {
46         if (d) {
47                 free(d->name);
48                 free(d->version);
49                 free(d->title);
50                 free(d->displayname);
51
52                 free(d);
53         }
54 }
55
56 void bpph_free(struct bpph *b)
57 {
58         if (b) {
59                 free(b->binary_package_name);
60                 free(b->binary_package_version);
61                 free(b->distro_arch_series_link);
62                 free(b->self_link);
63                 free(b->status);
64                 free(b);
65         }
66 }
67
68 struct bpph *bpph_new(const char *binary_package_name,
69                       const char *binary_package_version,
70                       const char *distro_arch_series_link,
71                       const char *self_link,
72                       int architecture_specific)
73 {
74         struct bpph *h;
75
76         h = malloc(sizeof(struct bpph));
77
78         h->binary_package_name = strdup(binary_package_name);
79         h->binary_package_version = strdup(binary_package_version);
80         h->distro_arch_series_link = strdup(distro_arch_series_link);
81         h->self_link = strdup(self_link);
82         h->architecture_specific = architecture_specific;
83         h->status = NULL;
84         h->date_created.tm_isdst = -1;
85
86         return h;
87 }
88
89 void bpph_list_free(struct bpph **list)
90 {
91         struct bpph **l_cur = list;
92
93         while (*l_cur) {
94                 bpph_free(*l_cur);
95                 l_cur++;
96         }
97
98         free(list);
99 }
100
101 char *get_archive_url(const char *owner, const char *ppa)
102 {
103         char *url = malloc(strlen(URL_BASE_LP)
104                            +strlen("/~")
105                            +strlen(owner)
106                            +strlen("/+archive/")
107                            +strlen(ppa)
108                            +1);
109
110         strcpy(url, URL_BASE_LP);
111         strcat(url, "/~");
112         strcat(url, owner);
113         strcat(url, "/+archive/");
114         strcat(url, ppa);
115
116         return url;
117 }
118
119 struct distro_arch_series *distro_arch_series_new(const char *display_name,
120                                                   const char *title,
121                                                   const char *architecture_tag,
122                                                   int is_nominated_arch_indep,
123                                                   const char *distroseries_link)
124 {
125         struct distro_arch_series *d;
126
127         d = malloc(sizeof(struct distro_arch_series));
128
129         d->display_name = strdup(display_name);
130         d->title = strdup(title);
131         d->architecture_tag = strdup(architecture_tag);
132         d->is_nominated_arch_indep = is_nominated_arch_indep;
133         d->distroseries_link = strdup(distroseries_link);
134
135         return d;
136 }
137
138 void distro_arch_series_free(struct distro_arch_series *d)
139 {
140         free(d->display_name);
141         free(d->title);
142         free(d->architecture_tag);
143         free(d->distroseries_link);
144
145         free(d);
146 }
147
148 void distro_arch_series_list_free(struct distro_arch_series **list)
149 {
150         if (list) {
151                 while (*list) {
152                         distro_arch_series_free(*list);
153                         list++;
154                 }
155                 free(list);
156         }
157 }
158
159 void daily_download_total_list_free(struct daily_download_total **list)
160 {
161         if (list) {
162                 struct daily_download_total **cur = list;
163
164                 while (*cur) {
165                         free(*cur);
166                         cur++;
167                 }
168
169                 free(list);
170         }
171 }
172
173 struct bpph **bpph_list_add(struct bpph **list, struct bpph *new)
174 {
175         struct bpph **cur, *bpph;
176
177         if (list)
178                 for (cur = list; *cur; cur++) {
179                         bpph = *cur;
180
181                         if (!strcmp(bpph->self_link, new->self_link))
182                                 return list;
183                 }
184
185         return (struct bpph **)list_add((void **)list, new);
186 }
187
188 struct bpph **bpph_list_append_list(struct bpph **list1, struct bpph **list2)
189 {
190         struct bpph **cur;
191
192         if (!list2)
193                 return list1;
194
195         for (cur = list2; *cur; cur++)
196                 list1 = bpph_list_add(list1, *cur);
197
198         return list1;
199 }