style
[ppastats.git] / src / ppastats.c
index 74ed02b..7acc059 100644 (file)
@@ -1,20 +1,20 @@
 /*
* 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
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
- *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
+  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
+  published by the Free Software Foundation; either version 2 of the
+  License, or (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+  02110-1301 USA
  */
 
 #include <libintl.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "list.h"
-#include "lp_ws.h"
+#include <list.h>
+#include <lp_ws.h>
 #include <plog.h>
-#include "ppastats.h"
+#include <ppastats.h>
 
 static struct package_stats *get_package_stats(struct ppa_stats *stats,
                                               const char *name)
@@ -181,7 +181,6 @@ static struct daily_download_total **add_total
 
        result = (struct daily_download_total **)list_add((void **)totals,
                                                          ddt_clone(total));
-
        return result;
 }
 
@@ -229,7 +228,8 @@ pkg_add_distro(struct package_stats *pkg,
                pkg_distro = distro_stats_new(distro_name);
                tmp = (struct distro_stats **)list_add((void **)pkg->distros,
                                                       (void *)pkg_distro);
-               free(pkg->distros);
+               if (pkg->distros != tmp)
+                       free(pkg->distros);
                pkg->distros = tmp;
        }
 
@@ -241,6 +241,20 @@ pkg_add_distro(struct package_stats *pkg,
        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;
+}
+
 struct ppa_stats *
 create_ppa_stats(const char *owner,
                 const char *ppa_name,
@@ -268,12 +282,7 @@ create_ppa_stats(const char *owner,
                exit(EXIT_FAILURE);
        }
 
-       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;
+       ppa = ppa_stats_new(owner, ppa_name);
 
        for (h_cur = history; *h_cur; ++h_cur) {
                h = *h_cur;
@@ -346,6 +355,8 @@ static void distro_stats_free(struct distro_stats *distro)
                free(distro->archs);
        }
 
+       daily_download_total_list_free(distro->ddts);
+
        free(distro->name);
        free(distro);
 }
@@ -357,21 +368,14 @@ static void distro_stats_list_free(struct distro_stats **distros)
                        distro_stats_free(*distros);
                        distros++;
                }
+               free(distros);
        }
 }
 
 static void version_stats_free(struct version_stats *version)
 {
-       struct distro_stats **distros;
-
-       distros = version->distros;
-       if (distros) {
-               while (*distros) {
-                       distro_stats_free(*distros);
-                       distros++;
-               }
-               free(version->distros);
-       }
+       distro_stats_list_free(version->distros);
+       daily_download_total_list_free(version->daily_download_totals);
 
        free(version->version);
        free(version);
@@ -390,6 +394,7 @@ static void package_stats_free(struct package_stats *package)
                free(package->versions);
        }
        distro_stats_list_free(package->distros);
+       daily_download_total_list_free(package->daily_download_totals);
        free(package->name);
        free(package);
 }