added --debug option
[ppastats.git] / src / lp.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "lp.h"
25
26 struct distro_series *distro_series_new(const char *name,
27                                         const char *version,
28                                         const char *title,
29                                         const char *displayname)
30 {
31         struct distro_series *d;
32
33         d = malloc(sizeof(struct distro_series));
34
35         d->name = strdup(name);
36         d->version = strdup(version);
37         d->title = strdup(title);
38         d->displayname = strdup(displayname);
39
40         return d;
41 }
42
43 void distro_series_free(struct distro_series *d)
44 {
45         if (d) {
46                 free(d->name);
47                 free(d->version);
48                 free(d->title);
49                 free(d->displayname);
50         }
51 }
52
53 void
54 binary_package_publishing_history_free(struct binary_package_publishing_history *b)
55 {
56         if (b) {
57                 free(b->binary_package_name);
58                 free(b->binary_package_version);
59                 free(b->distro_arch_series_link);
60                 free(b->self_link);
61                 free(b);
62         }
63 }
64
65 struct binary_package_publishing_history *
66 binary_package_publishing_history_new(const char *binary_package_name,
67                                       const char *binary_package_version,
68                                       const char *distro_arch_series_link,
69                                       const char *self_link)
70 {
71         struct binary_package_publishing_history *b;
72
73         b = malloc(sizeof(struct binary_package_publishing_history));
74
75         b->binary_package_name = strdup(binary_package_name);
76         b->binary_package_version = strdup(binary_package_version);
77         b->distro_arch_series_link = strdup(distro_arch_series_link);
78         b->self_link = strdup(self_link);
79
80         return b;
81 }
82
83 void
84 binary_package_publishing_history_list_free(struct binary_package_publishing_history **list)
85 {
86         struct binary_package_publishing_history **l_cur = list;
87
88         while (*l_cur) {
89                 binary_package_publishing_history_free(*l_cur);
90                 l_cur++;
91         }
92
93         free(list);
94 }
95
96 char *get_archive_url(const char *owner, const char *ppa)
97 {
98         char *url = malloc(strlen(URL_BASE_LP)
99                            +strlen("/~")
100                            +strlen(owner)
101                            +strlen("/+archive/")
102                            +strlen(ppa)
103                            +1);
104
105         strcpy(url, URL_BASE_LP);
106         strcat(url, "/~");
107         strcat(url, owner);
108         strcat(url, "/+archive/");
109         strcat(url, ppa);
110
111         return url;
112 }
113
114 struct distro_arch_series *distro_arch_series_new(const char *display_name,
115                                                   const char *title,
116                                                   const char *architecture_tag,
117                                                   int is_nominated_arch_indep,
118                                                   const char *distroseries_link)
119 {
120         struct distro_arch_series *d;
121
122         d = malloc(sizeof(struct distro_arch_series));
123
124         d->display_name = strdup(display_name);
125         d->title = strdup(title);
126         d->architecture_tag = strdup(architecture_tag);
127         d->is_nominated_arch_indep = is_nominated_arch_indep;
128         d->distroseries_link = strdup(distroseries_link);
129
130         return d;
131 }
132
133 void distro_arch_series_free(struct distro_arch_series *d)
134 {
135         free(d->display_name);
136         free(d->title);
137         free(d->architecture_tag);
138         free(d->distroseries_link);
139
140         free(d);
141 }
142
143 void distro_arch_series_list_free(struct distro_arch_series **list)
144 {
145         if (list) {
146                 while (*list) {
147                         distro_arch_series_free(*list);
148                         list++;
149                 }
150                 free(list);
151         }
152 }
153
154 void daily_download_total_list_free(struct daily_download_total **list)
155 {
156         if (list) {
157                 struct daily_download_total **cur = list;
158
159                 while (*cur) {
160                         free(*cur);
161                         cur++;
162                 }
163
164                 free(list);
165         }
166 }
167
168 int list_length(void **list)
169 {
170         int n;
171
172         n = 0;
173         while (*list) {
174                 list++;
175                 n++;
176         }
177
178         return n;
179 }
180