(no commit message)
[prss.git] / src / ttrss_model.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "ttrss_model.h"
24
25 static int list_length(void **list)
26 {
27         int n;
28
29         if (!list)
30                 return 0;
31
32         n = 0;
33         while (*list) {
34                 n++;
35                 list++;
36         }
37
38         return n;
39 }
40
41 static void **list_add(void **list, void *item)
42 {
43         int n;
44         void **result;
45
46         n = list_length(list);
47
48         result = malloc((n + 1 + 1) * sizeof(void *));
49
50         if (list)
51                 memcpy(result, list, n * sizeof(void *));
52
53         result[n] = item;
54         result[n + 1] = NULL;
55
56         return result;
57 }
58
59 struct headline **headlines_add(struct headline **list, struct headline *h)
60 {
61         return (struct headline **)list_add((void **)list, (void *)h);
62 }
63
64 struct feed **feeds_add(struct feed **list, struct feed *f)
65 {
66         return (struct feed **)list_add((void **)list, (void *)f);
67 }
68
69 struct headline *feed_get_headline(struct feed *feed, int id)
70 {
71         struct headline **headlines;
72
73         headlines = feed->headlines;
74         if (headlines)
75                 while (*headlines) {
76                         if ((*headlines)->id == id)
77                                 return *headlines;
78                         headlines++;
79                 }
80
81         return NULL;
82 }
83
84 void headline_free(struct headline *headline)
85 {
86         if (headline) {
87                 free(headline->url);
88                 free(headline->title);
89                 free(headline->excerpt);
90                 free(headline->content);
91                 free(headline);
92         }
93 }
94
95 struct headline *headline_new(int id, const char *url, const char *title)
96 {
97         struct headline *h;
98
99         h = malloc(sizeof(struct headline));
100         h->id = id;
101         h->url = strdup(url);
102         h->title = strdup(title);
103         h->unread = -1;
104         h->excerpt = NULL;
105         h->content = NULL;
106         
107         return h;
108 }
109
110 void headlines_free(struct headline **headlines)
111 {
112         struct headline **cur;
113
114         if (headlines) {
115                 cur = headlines;
116                 while (*cur) {
117                         headline_free(*cur);
118                         cur++;
119                 }
120                 free(headlines);
121         }
122 }
123
124 struct feed *feed_new(int id, const char *url, const char *title)
125 {
126         struct feed *f;
127
128         f = malloc(sizeof(struct feed));
129
130         f->id = id;
131         f->url = strdup(url);
132         f->title = strdup(title);
133         f->unread = -1;
134         f->headlines = NULL;
135
136         return f;
137 }
138
139 void feed_free(struct feed *feed)
140 {
141         if (feed) {
142                 if (feed->title)
143                         free(feed->title);
144                 if (feed->url)
145                         free(feed->url);
146                 headlines_free(feed->headlines);
147                 free(feed);
148         }
149 }
150
151 void feeds_free(struct feed **feeds)
152 {
153         struct feed **cur;
154
155         if (feeds) {
156                 cur = feeds;
157                 while (*cur) {
158                         feed_free(*cur);
159                         cur++;
160                 }
161                 free(feeds);
162         }
163 }