X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp.c;h=e738a6c1e909a7740b57160b5725ee71f047cdf5;hb=f534061fc8230481a3ca40abc1d1e9b79d3c4976;hp=99cf9b2d6ceb1ecd488f430c60296d1a8f40078d;hpb=967d20c38c65689c96a0276e6f916b4f8e2617de;p=ppastats.git diff --git a/src/lp.c b/src/lp.c index 99cf9b2..e738a6c 100644 --- a/src/lp.c +++ b/src/lp.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2012 jeanfi@gmail.com + * Copyright (C) 2011-2014 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -21,7 +21,9 @@ #include #include +#include "list.h" #include "lp.h" +#include struct distro_series *distro_series_new(const char *name, const char *version, @@ -52,23 +54,25 @@ void distro_series_free(struct distro_series *d) } } -void binary_package_publishing_history_free(struct bpph *b) +void bpph_free(struct bpph *b) { if (b) { free(b->binary_package_name); free(b->binary_package_version); free(b->distro_arch_series_link); free(b->self_link); + free(b->status); free(b); } } -struct bpph * -binary_package_publishing_history_new(const char *binary_package_name, - const char *binary_package_version, - const char *distro_arch_series_link, - const char *self_link, - int architecture_specific) +struct bpph *bpph_new(const char *binary_package_name, + const char *binary_package_version, + const char *distro_arch_series_link, + const char *self_link, + const char *status, + int architecture_specific, + time_t date_created) { struct bpph *h; @@ -79,18 +83,18 @@ binary_package_publishing_history_new(const char *binary_package_name, h->distro_arch_series_link = strdup(distro_arch_series_link); h->self_link = strdup(self_link); h->architecture_specific = architecture_specific; + h->status = strdup(status); + h->date_created = date_created; return h; } -void -binary_package_publishing_history_list_free\ -(struct bpph **list) +void bpph_list_free(struct bpph **list) { struct bpph **l_cur = list; while (*l_cur) { - binary_package_publishing_history_free(*l_cur); + bpph_free(*l_cur); l_cur++; } @@ -169,3 +173,130 @@ void daily_download_total_list_free(struct daily_download_total **list) } } +struct bpph **bpph_list_add(struct bpph **list, struct bpph *new) +{ + struct bpph **cur, *bpph; + + if (list) + for (cur = list; *cur; cur++) { + bpph = *cur; + + if (!strcmp(bpph->self_link, new->self_link)) + return list; + } + + return (struct bpph **)list_add((void **)list, new); +} + +struct bpph **bpph_list_append_list(struct bpph **list1, struct bpph **list2) +{ + struct bpph **cur; + + if (!list2) + return list1; + + for (cur = list2; *cur; cur++) + list1 = bpph_list_add(list1, *cur); + + return list1; +} + +time_t ddts_get_last_date(struct daily_download_total **ddts) +{ + struct daily_download_total **cur; + time_t t, last_t; + + if (!ddts) + return 0; + + last_t = 0; + for (cur = ddts; *cur; cur++) { + t = mktime(&(*cur)->date); + if (t > last_t) + last_t = t; + } + + return last_t; +} + +int ddts_length(struct daily_download_total **ddts) +{ + int n; + struct daily_download_total **cur; + + n = 0; + + if (ddts) + for (cur = ddts; *cur; cur++) + n++; + + return n; +} + + +struct daily_download_total **add_total +(struct daily_download_total **totals, struct daily_download_total *total) +{ + struct daily_download_total **cur; + struct daily_download_total *item; + + if (totals) { + cur = totals; + while (*cur) { + item = *cur; + + if (item->date.tm_year == total->date.tm_year && + item->date.tm_mon == total->date.tm_mon && + item->date.tm_mday == total->date.tm_mday) { + item->count = total->count; + return totals; + } + + cur++; + } + } + + item = malloc(sizeof(struct daily_download_total)); + memcpy(item, total, sizeof(struct daily_download_total)); + + return (struct daily_download_total **) + list_add((void **)totals, (void *)item); +} + + +struct daily_download_total ** +ddts_merge(struct daily_download_total **ddts1, + struct daily_download_total **ddts2) +{ + 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 *)); + } else { + ddts = malloc(sizeof(struct daily_download_total *)); + ddts[0] = NULL; + } + + if (ddts2) + for (cur = ddts2; *cur; cur++) { + tmp = add_total(ddts, *cur); + if (tmp != ddts) + ddts = tmp; + } + + return ddts; +} + +int ddts_get_count(struct daily_download_total **ddts) +{ + struct daily_download_total **cur; + int i; + + i = 0; + for (cur = ddts; *cur; cur++) + i += (*cur)->count; + + return i; +}