(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 headlines_free(struct headline **headlines)
85 {
86 }
87
88 void feed_free(struct feed *feed)
89 {
90         if (feed) {
91                 if (feed->title)
92                         free(feed->title);
93                 if (feed->url)
94                         free(feed->url);
95                 headlines_free(feed->headlines);
96                 free(feed);
97         }
98 }
99
100 void feeds_free(struct feed **feeds)
101 {
102         struct feed **cur;
103
104         if (feeds) {
105                 cur = feeds;
106                 while (*cur) {
107                         feed_free(*cur);
108                         cur++;
109                 }
110                 free(feeds);
111         }
112 }