X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp.c;h=11866d76b46944bdbe610c4119469bb8756489f3;hb=47415d6ef7b12572e99b59d18dc63e053fdb4f16;hp=fe4c019403c43de03c4c035fe12ccf2b6ca1af66;hpb=b59ec44bffce28192c6408fed838c4d593a17c82;p=ppastats.git diff --git a/src/lp.c b/src/lp.c index fe4c019..11866d7 100644 --- a/src/lp.c +++ b/src/lp.c @@ -23,27 +23,7 @@ #include "list.h" #include "lp.h" - -char *time_to_str(time_t t) -{ - char *str; - struct tm *tm; - size_t ret; - - tm = localtime(&t); - - if (!tm) - return NULL; - - str = malloc(strlen("YYYY-MM-DDThh:mm:ss") + 1); - ret = strftime(str, strlen("YYYY-MM-DDThh:mm:ss") + 1, "%FT%T", tm); - - if (ret) - return str; - - free(str); - return NULL; -} +#include struct distro_series *distro_series_new(const char *name, const char *version, @@ -195,7 +175,7 @@ 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; + struct bpph **cur, *bpph, **result; if (list) for (cur = list; *cur; cur++) { @@ -205,7 +185,11 @@ struct bpph **bpph_list_add(struct bpph **list, struct bpph *new) return list; } - return (struct bpph **)list_add((void **)list, new); + result = (struct bpph **)list_add((void **)list, new); + + free(list); + + return result; } struct bpph **bpph_list_append_list(struct bpph **list1, struct bpph **list2) @@ -220,3 +204,103 @@ struct bpph **bpph_list_append_list(struct bpph **list1, struct bpph **list2) 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; +}