X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp.c;h=67e6ed5b450c719348625fd85957efe80ff6a9e3;hb=d67f758a0dd914b6dbffcd64837273b20be282b3;hp=79fcce6d4a6f594ceada5d2f3c981da449f5b711;hpb=12cf1b3a4702e58c5797be638ab48e899ef932a4;p=ppastats.git diff --git a/src/lp.c b/src/lp.c index 79fcce6..67e6ed5 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 @@ -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, @@ -111,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) @@ -170,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); } @@ -195,7 +182,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 +192,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 +211,138 @@ 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 *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 ** +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, **ddts, **result; + 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++; + } + } + + ddts = ddts_clone(totals); + + 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, + struct daily_download_total **ddts2) +{ + struct daily_download_total **ddts, **cur, **tmp; + + if (ddts1) { + ddts = ddts_clone(ddts1); + } else { + ddts = malloc(sizeof(struct daily_download_total *)); + ddts[0] = NULL; + } + + if (ddts2) + for (cur = ddts2; *cur; cur++) { + tmp = add_ddt(ddts, *cur); + if (tmp != ddts) { + daily_download_total_list_free(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; +}