X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp.c;h=47d024ec106cd0a1def4203c50f339dfb5c1f25d;hb=3fdf52a7dba9c3bcbd6e4ef8b945c6d91fc1d807;hp=c307a3632168af7d2627816b13bf9dc5fce7f814;hpb=9a8af404ffbfb331575dc4b37b044d49d728be8b;p=ppastats.git diff --git a/src/lp.c b/src/lp.c index c307a36..47d024e 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) @@ -230,7 +214,7 @@ time_t ddts_get_last_date(struct daily_download_total **ddts) return 0; last_t = 0; - for(cur = ddts; *cur; cur++) { + for (cur = ddts; *cur; cur++) { t = mktime(&(*cur)->date); if (t > last_t) last_t = t; @@ -247,17 +231,50 @@ int ddts_length(struct daily_download_total **ddts) n = 0; if (ddts) - for(cur = ddts; *cur; cur++) + for (cur = ddts; *cur; cur++) n++; return n; } +static 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; +} + +static 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]); -struct daily_download_total **add_total + 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) { @@ -276,13 +293,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, @@ -291,20 +310,32 @@ 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; } if (ddts2) - for(cur = ddts2; *cur; cur++) { - tmp = add_total(ddts, *cur); + 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; +}