fixed code style
[prss.git] / src / ttrss_model.c
index c67ad65..5753256 100644 (file)
  */
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 
+#include "list.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);
@@ -81,8 +49,59 @@ struct headline *feed_get_headline(struct feed *feed, int id)
        return NULL;
 }
 
+void headline_free(struct headline *headline)
+{
+       if (headline) {
+               free(headline->url);
+               free(headline->title);
+               free(headline->excerpt);
+               free(headline->content);
+               free(headline);
+       }
+}
+
+struct headline *headline_new(int id, const char *url, const char *title)
+{
+       struct headline *h;
+
+       h = malloc(sizeof(struct headline));
+       h->id = id;
+       h->url = strdup(url);
+       h->title = strdup(title);
+       h->unread = -1;
+       h->excerpt = NULL;
+       h->content = NULL;
+
+       return h;
+}
+
 void headlines_free(struct headline **headlines)
 {
+       struct headline **cur;
+
+       if (headlines) {
+               cur = headlines;
+               while (*cur) {
+                       headline_free(*cur);
+                       cur++;
+               }
+               free(headlines);
+       }
+}
+
+struct feed *feed_new(int id, const char *url, const char *title)
+{
+       struct feed *f;
+
+       f = malloc(sizeof(struct feed));
+
+       f->id = id;
+       f->url = strdup(url);
+       f->title = strdup(title);
+       f->unread = -1;
+       f->headlines = NULL;
+
+       return f;
 }
 
 void feed_free(struct feed *feed)
@@ -110,3 +129,34 @@ void feeds_free(struct feed **feeds)
                free(feeds);
        }
 }
+
+struct feed *feeds_get_feed(struct feed **feeds, int id)
+{
+       struct feed **cur;
+
+       if (feeds)
+               for (cur = feeds; *cur; cur++)
+                       if ((*cur)->id == id)
+                               return *cur;
+
+       return NULL;
+}
+
+struct headline *feeds_get_headline(struct feed **feeds, int id)
+{
+       struct headline *h;
+
+       if (!feeds)
+               return NULL;
+
+       while (*feeds) {
+               h = feed_get_headline(*feeds, id);
+
+               if (h)
+                       return h;
+
+               feeds++;
+       }
+
+       return NULL;
+}