better code
[ppastats.git] / src / lp.c
index c80210b..79fcce6 100644 (file)
--- a/src/lp.c
+++ b/src/lp.c
 /*
-    Copyright (C) 2011 jeanfi@gmail.com
+ * Copyright (C) 2011-2012 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
+ */
 
-    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.
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "list.h"
+#include "lp.h"
 
-    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.
+char *time_to_str(time_t t)
+{
+       char *str;
+       struct tm *tm;
+       size_t ret;
 
-    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
-*/
+       tm = localtime(&t);
 
+       if (!tm)
+               return NULL;
 
-#include <stdlib.h>
-#include <string.h>
+       str = malloc(strlen("YYYY-MM-DDThh:mm:ss") + 1);
+       ret = strftime(str, strlen("YYYY-MM-DDThh:mm:ss") + 1, "%FT%T", tm);
 
-#include "lp.h"
+       if (ret)
+               return str;
 
-void
-binary_package_publishing_history_free(struct binary_package_publishing_history *b)
+       free(str);
+       return NULL;
+}
+
+struct distro_series *distro_series_new(const char *name,
+                                       const char *version,
+                                       const char *title,
+                                       const char *displayname)
+{
+       struct distro_series *d;
+
+       d = malloc(sizeof(struct distro_series));
+
+       d->name = strdup(name);
+       d->version = strdup(version);
+       d->title = strdup(title);
+       d->displayname = strdup(displayname);
+
+       return d;
+}
+
+void distro_series_free(struct distro_series *d)
+{
+       if (d) {
+               free(d->name);
+               free(d->version);
+               free(d->title);
+               free(d->displayname);
+
+               free(d);
+       }
+}
+
+void bpph_free(struct bpph *b)
 {
        if (b) {
                free(b->binary_package_name);
                free(b->binary_package_version);
                free(b->distro_arch_series_link);
                free(b->self_link);
+               free(b->status);
                free(b);
        }
 }
 
-struct binary_package_publishing_history *
-binary_package_publishing_history_new(const char *binary_package_name,
-                                     const char *binary_package_version,
-                                     const char *distro_arch_series_link,
-                                     const char *self_link)
+struct bpph *bpph_new(const char *binary_package_name,
+                     const char *binary_package_version,
+                     const char *distro_arch_series_link,
+                     const char *self_link,
+                     const char *status,
+                     int architecture_specific,
+                     time_t date_created)
 {
-       struct binary_package_publishing_history *b;
+       struct bpph *h;
 
-       b = malloc(sizeof(struct binary_package_publishing_history));
+       h = malloc(sizeof(struct bpph));
 
-       b->binary_package_name = strdup(binary_package_name);
-       b->binary_package_version = strdup(binary_package_version);
-       b->distro_arch_series_link = strdup(distro_arch_series_link);
-       b->self_link = strdup(self_link);
+       h->binary_package_name = strdup(binary_package_name);
+       h->binary_package_version = strdup(binary_package_version);
+       h->distro_arch_series_link = strdup(distro_arch_series_link);
+       h->self_link = strdup(self_link);
+       h->architecture_specific = architecture_specific;
+       h->status = strdup(status);
+       h->date_created = date_created;
 
-       return b;
+       return h;
 }
 
-void
-binary_package_publishing_history_list_free(struct binary_package_publishing_history **list)
+void bpph_list_free(struct bpph **list)
 {
-       struct binary_package_publishing_history **l_cur = list;
+       struct bpph **l_cur = list;
 
        while (*l_cur) {
-               binary_package_publishing_history_free(*l_cur);
+               bpph_free(*l_cur);
                l_cur++;
        }
 
@@ -87,7 +142,8 @@ char *get_archive_url(const char *owner, const char *ppa)
 struct distro_arch_series *distro_arch_series_new(const char *display_name,
                                                  const char *title,
                                                  const char *architecture_tag,
-                                                 int is_nominated_arch_indep)
+                                                 int is_nominated_arch_indep,
+                                                 const char *distroseries_link)
 {
        struct distro_arch_series *d;
 
@@ -97,6 +153,7 @@ struct distro_arch_series *distro_arch_series_new(const char *display_name,
        d->title = strdup(title);
        d->architecture_tag = strdup(architecture_tag);
        d->is_nominated_arch_indep = is_nominated_arch_indep;
+       d->distroseries_link = strdup(distroseries_link);
 
        return d;
 }
@@ -106,6 +163,7 @@ void distro_arch_series_free(struct distro_arch_series *d)
        free(d->display_name);
        free(d->title);
        free(d->architecture_tag);
+       free(d->distroseries_link);
 
        free(d);
 }
@@ -135,16 +193,30 @@ void daily_download_total_list_free(struct daily_download_total **list)
        }
 }
 
-int list_length(void **list)
+struct bpph **bpph_list_add(struct bpph **list, struct bpph *new)
 {
-       int n;
+       struct bpph **cur, *bpph;
 
-       n = 0;
-       while (*list) {
-               list++;
-               n++;
-       }
+       if (list)
+               for (cur = list; *cur; cur++) {
+                       bpph = *cur;
 
-       return n;
+                       if (!strcmp(bpph->self_link, new->self_link))
+                               return list;
+               }
+
+       return (struct bpph **)list_add((void **)list, new);
 }
 
+struct bpph **bpph_list_append_list(struct bpph **list1, struct bpph **list2)
+{
+       struct bpph **cur;
+
+       if (!list2)
+               return list1;
+
+       for (cur = list2; *cur; cur++)
+               list1 = bpph_list_add(list1, *cur);
+
+       return list1;
+}