X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp.c;h=67e6ed5b450c719348625fd85957efe80ff6a9e3;hb=4f7d6ce1bef70a80070b9b8cd005e1418177ea44;hp=11866d76b46944bdbe610c4119469bb8756489f3;hpb=7322824fadde9e95f79c630b70b25a8a660ab503;p=ppastats.git diff --git a/src/lp.c b/src/lp.c index 11866d7..67e6ed5 100644 --- a/src/lp.c +++ b/src/lp.c @@ -91,14 +91,18 @@ struct bpph *bpph_new(const char *binary_package_name, void bpph_list_free(struct bpph **list) { - struct bpph **l_cur = list; + struct bpph **cur; - while (*l_cur) { - bpph_free(*l_cur); - l_cur++; - } + if (list) { + cur = list; - free(list); + while (*cur) { + bpph_free(*cur); + cur++; + } + + free(list); + } } char *get_archive_url(const char *owner, const char *ppa) @@ -150,10 +154,13 @@ void distro_arch_series_free(struct distro_arch_series *d) void distro_arch_series_list_free(struct distro_arch_series **list) { + struct distro_arch_series **cur; + if (list) { - while (*list) { - distro_arch_series_free(*list); - list++; + cur = list; + while (*cur) { + distro_arch_series_free(*cur); + cur++; } free(list); } @@ -237,11 +244,44 @@ int ddts_length(struct daily_download_total **ddts) return n; } +struct daily_download_total *ddt_clone(struct daily_download_total *ddt) +{ + struct daily_download_total *new; + + new = malloc(sizeof(struct daily_download_total)); + + new->date = ddt->date; + new->count = ddt->count; + + return new; +} -struct daily_download_total **add_total +struct daily_download_total ** +ddts_clone(struct daily_download_total **ddts) +{ + int n, i; + struct daily_download_total **new; + + n = ddts_length(ddts); + + new = malloc((n + 1) * sizeof(struct daily_download_total *)); + + for (i = 0; i < n; i++) + new[i] = ddt_clone(ddts[i]); + + new[n] = NULL; + + return new; +} + +/* + Return a newly allocated list with an additional ddt. + All ddts are cloned. + */ +static struct daily_download_total **add_ddt (struct daily_download_total **totals, struct daily_download_total *total) { - struct daily_download_total **cur; + struct daily_download_total **cur, **ddts, **result; struct daily_download_total *item; if (totals) { @@ -260,13 +300,15 @@ struct daily_download_total **add_total } } - item = malloc(sizeof(struct daily_download_total)); - memcpy(item, total, sizeof(struct daily_download_total)); + ddts = ddts_clone(totals); - return (struct daily_download_total **) - list_add((void **)totals, (void *)item); -} + result = (struct daily_download_total **) + list_add((void **)ddts, ddt_clone((void *)total)); + free(ddts); + + return result; +} struct daily_download_total ** ddts_merge(struct daily_download_total **ddts1, @@ -275,9 +317,7 @@ ddts_merge(struct daily_download_total **ddts1, struct daily_download_total **ddts, **cur, **tmp; if (ddts1) { - ddts = malloc((ddts_length(ddts1) + 1) - * sizeof(struct daily_download_total *)); - memcpy(ddts, ddts1, (ddts_length(ddts1) + 1) * sizeof(void *)); + ddts = ddts_clone(ddts1); } else { ddts = malloc(sizeof(struct daily_download_total *)); ddts[0] = NULL; @@ -285,9 +325,11 @@ ddts_merge(struct daily_download_total **ddts1, if (ddts2) for (cur = ddts2; *cur; cur++) { - tmp = add_total(ddts, *cur); - if (tmp != ddts) + tmp = add_ddt(ddts, *cur); + if (tmp != ddts) { + daily_download_total_list_free(ddts); ddts = tmp; + } } return ddts;