fixed missing memory free
[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                 free(d);
52         }
53 }
54
55 void
56 binary_package_publishing_history_free(struct binary_package_publishing_history *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);
64         }
65 }
66
67 struct binary_package_publishing_history *
68 binary_package_publishing_history_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 {
73         struct binary_package_publishing_history *b;
74
75         b = malloc(sizeof(struct binary_package_publishing_history));
76
77         b->binary_package_name = strdup(binary_package_name);
78         b->binary_package_version = strdup(binary_package_version);
79         b->distro_arch_series_link = strdup(distro_arch_series_link);
80         b->self_link = strdup(self_link);
81
82         return b;
83 }
84
85 void
86 binary_package_publishing_history_list_free(struct binary_package_publishing_history **list)
87 {
88         struct binary_package_publishing_history **l_cur = list;
89
90         while (*l_cur) {
91                 binary_package_publishing_history_free(*l_cur);
92                 l_cur++;
93         }
94
95         free(list);
96 }
97
98 char *get_archive_url(const char *owner, const char *ppa)
99 {
100         char *url = malloc(strlen(URL_BASE_LP)
101                            +strlen("/~")
102                            +strlen(owner)
103                            +strlen("/+archive/")
104                            +strlen(ppa)
105                            +1);
106
107         strcpy(url, URL_BASE_LP);
108         strcat(url, "/~");
109         strcat(url, owner);
110         strcat(url, "/+archive/");
111         strcat(url, ppa);
112
113         return url;
114 }
115
116 struct distro_arch_series *distro_arch_series_new(const char *display_name,
117                                                   const char *title,
118                                                   const char *architecture_tag,
119                                                   int is_nominated_arch_indep,
120                                                   const char *distroseries_link)
121 {
122         struct distro_arch_series *d;
123
124         d = malloc(sizeof(struct distro_arch_series));
125
126         d->display_name = strdup(display_name);
127         d->title = strdup(title);
128         d->architecture_tag = strdup(architecture_tag);
129         d->is_nominated_arch_indep = is_nominated_arch_indep;
130         d->distroseries_link = strdup(distroseries_link);
131
132         return d;
133 }
134
135 void distro_arch_series_free(struct distro_arch_series *d)
136 {
137         free(d->display_name);
138         free(d->title);
139         free(d->architecture_tag);
140         free(d->distroseries_link);
141
142         free(d);
143 }
144
145 void distro_arch_series_list_free(struct distro_arch_series **list)
146 {
147         if (list) {
148                 while (*list) {
149                         distro_arch_series_free(*list);
150                         list++;
151                 }
152                 free(list);
153         }
154 }
155
156 void daily_download_total_list_free(struct daily_download_total **list)
157 {
158         if (list) {
159                 struct daily_download_total **cur = list;
160
161                 while (*cur) {
162                         free(*cur);
163                         cur++;
164                 }
165
166                 free(list);
167         }
168 }
169
170 int list_length(void **list)
171 {
172         int n;
173
174         n = 0;
175         while (*list) {
176                 list++;
177                 n++;
178         }
179
180         return n;
181 }
182