+static struct daily_download_total **add_total
+(struct daily_download_total **totals, struct daily_download_total *total)
+{
+ struct daily_download_total **cur, **result, *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++;
+ }
+ }
+
+ result = (struct daily_download_total **)list_add((void **)totals,
+ ddt_clone(total));
+ return result;
+}
+
+static struct daily_download_total **add_totals
+(struct daily_download_total **total1, struct daily_download_total **total2)
+{
+ struct daily_download_total **cur, **result, **tmp;
+
+ result = total1;
+ cur = total2;
+ while (*cur) {
+ tmp = add_total(result, *cur);
+ if (result != total1 && result != tmp)
+ free(result);
+ result = tmp;
+ cur++;
+ }
+
+ return result;
+}
+
+static void
+pkg_add_distro(struct package_stats *pkg,
+ const char *distro_name,
+ int distro_count,
+ struct daily_download_total **ddts)
+{
+ struct distro_stats **pkg_distros, *pkg_distro, **tmp;
+ struct daily_download_total **tmp_ddts;
+
+ pkg_distros = pkg->distros;
+ pkg_distro = NULL;
+
+ if (pkg_distros)
+ while (*pkg_distros) {
+ if (!strcmp((*pkg_distros)->name, distro_name)) {
+ pkg_distro = *pkg_distros;
+ break;
+ }
+
+ pkg_distros++;
+ }
+
+ if (!pkg_distro) {
+ pkg_distro = distro_stats_new(distro_name);
+ tmp = (struct distro_stats **)list_add((void **)pkg->distros,
+ (void *)pkg_distro);
+ if (pkg->distros != tmp)
+ free(pkg->distros);
+ pkg->distros = tmp;
+ }
+
+ pkg_distro->download_count += distro_count;
+
+ tmp_ddts = add_totals(pkg_distro->ddts, ddts);
+ if (pkg_distro->ddts && pkg_distro->ddts != tmp_ddts)
+ free(pkg_distro->ddts);
+ pkg_distro->ddts = tmp_ddts;
+}
+
+static struct ppa_stats *ppa_stats_new(const char *owner, const char *ppa_name)
+{
+ struct ppa_stats *ppa;
+
+ ppa = malloc(sizeof(struct ppa_stats));
+ ppa->name = strdup(ppa_name);
+ ppa->owner = strdup(owner);
+ ppa->packages = NULL;
+ ppa->daily_download_totals = NULL;
+ ppa->download_count = 0;
+
+ return ppa;
+}
+