+/*
+ * Copyright (C) 2010-2013 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 <stdlib.h>
+#include <string.h>
+
+#include "ttrss_model.h"
+
+static int list_length(void **list)
+{
+ int n;
+
+ if (!list)
+ return 0;
+
+ n = 0;
+ while (*list) {
+ n++;
+ list++;
+ }
+
+ return n;
+}
+
+static void **list_add(void **list, void *item)
+{
+ int n;
+ void **result;
+
+ n = list_length(list);
+
+ result = malloc((n + 1 + 1) * sizeof(void *));
+
+ if (list)
+ memcpy(result, list, n * sizeof(void *));
+
+ result[n] = item;
+ result[n + 1] = NULL;
+
+ return result;
+}
+
+struct headline **headlines_add(struct headline **list, struct headline *h)
+{
+ return (struct headline **)list_add((void **)list, (void *)h);
+}
+
+struct feed **feeds_add(struct feed **list, struct feed *f)
+{
+ return (struct feed **)list_add((void **)list, (void *)f);
+}
+
+struct headline *feed_get_headline(struct feed *feed, int id)
+{
+ struct headline **headlines;
+
+ headlines = feed->headlines;
+ if (headlines)
+ while (*headlines) {
+ if ((*headlines)->id == id)
+ return *headlines;
+ headlines++;
+ }
+
+ return NULL;
+}
+
+void headlines_free(struct headline **headlines)
+{
+}
+
+void feed_free(struct feed *feed)
+{
+ if (feed) {
+ if (feed->title)
+ free(feed->title);
+ if (feed->url)
+ free(feed->url);
+ headlines_free(feed->headlines);
+ free(feed);
+ }
+}
+
+void feeds_free(struct feed **feeds)
+{
+ struct feed **cur;
+
+ if (feeds) {
+ cur = feeds;
+ while (*cur) {
+ feed_free(*cur);
+ cur++;
+ }
+ free(feeds);
+ }
+}