make intense use of const keyword
[monky] / src / prss.c
1 /* $Id$
2  *
3  * Copyright (c) 2007 Mikko Sysikaski <mikko.sysikaski@gmail.com>
4  *                                        Toni Spets <toni.spets@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
17
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "prss.h"
24 #include "config.h"
25
26 #ifndef PARSE_OPTIONS
27 #define PARSE_OPTIONS 0
28 #endif
29
30 PRSS *prss_parse_doc(xmlDocPtr doc);
31
32 PRSS *prss_parse_data(const char *xml_data)
33 {
34         xmlDocPtr doc = xmlReadMemory(xml_data, strlen(xml_data), "", NULL,
35                 PARSE_OPTIONS);
36
37         if (!doc) {
38                 return NULL;
39         }
40
41         return prss_parse_doc(doc);
42 }
43
44 PRSS *prss_parse_file(const char *xml_file)
45 {
46         xmlDocPtr doc = xmlReadFile(xml_file, NULL, PARSE_OPTIONS);
47
48         if (!doc) {
49                 return NULL;
50         }
51
52         return prss_parse_doc(doc);
53 }
54
55 void prss_free(PRSS *data)
56 {
57         if (!data) {
58                 return;
59         }
60         xmlFreeDoc(data->_data);
61         free(data->version);
62         free(data->items);
63         free(data);
64 }
65
66 static inline void prss_null(PRSS *p)
67 {
68         memset(p, 0, sizeof(PRSS));
69 }
70 static inline void prss_null_item(PRSS_Item *i)
71 {
72         memset(i, 0, sizeof(PRSS_Item));
73 }
74
75 static inline void read_item(PRSS_Item *res, xmlNodePtr data)
76 {
77         prss_null_item(res);
78
79         res->title = res->link = res->description = NULL;
80         for (; data; data = data->next) {
81                 const char *name;
82
83                 if (data->type != XML_ELEMENT_NODE) {
84                         continue;
85                 }
86                 xmlNodePtr child = data->children;
87
88                 if (!child) {
89                         continue;
90                 }
91
92                 name = (const char *)data->name;
93                 if (!strcasecmp(name, "title")) {
94                         res->title = (char *) child->content;
95                 } else if (!strcasecmp(name, "link")) {
96                         res->link = (char *) child->content;
97                 } else if (!strcasecmp(name, "description")) {
98                         res->description = (char *) child->content;
99                 } else if (!strcasecmp(name, "category")) {
100                         res->category = (char *) child->content;
101                 } else if (!strcasecmp(name, "pubDate")) {
102                         res->pubdate = (char *) child->content;
103                 } else if (!strcasecmp(name, "guid")) {
104                         res->guid = (char *) child->content;
105                 }
106         }
107 }
108 static inline void read_element(PRSS *res, xmlNodePtr n)
109 {
110         const char *name;
111
112         if (n->type != XML_ELEMENT_NODE) {
113                 return;
114         }
115         xmlNodePtr child = n->children;
116
117         if (!child) {
118                 return;
119         }
120
121         name = (const char *)n->name;
122         if (!strcasecmp(name, "title")) {
123                 res->title = (char *) child->content;
124         } else if (!strcasecmp(name, "link")) {
125                 res->link = (char *) child->content;
126         } else if (!strcasecmp(name, "description")) {
127                 res->description = (char *) child->content;
128         } else if (!strcasecmp(name, "language")) {
129                 res->language = (char *) child->content;
130         } else if (!strcasecmp(name, "pubDate")) {
131                 res->pubdate = (char *) child->content;
132         } else if (!strcasecmp(name, "lastBuildDate")) {
133                 res->lastbuilddate = (char *) child->content;
134         } else if (!strcasecmp(name, "generator")) {
135                 res->generator = (char *) child->content;
136         } else if (!strcasecmp(name, "docs")) {
137                 res->docs = (char *) child->content;
138         } else if (!strcasecmp(name, "managingEditor")) {
139                 res->managingeditor = (char *) child->content;
140         } else if (!strcasecmp(name, "webMaster")) {
141                 res->webmaster = (char *) child->content;
142         } else if (!strcasecmp(name, "copyright")) {
143                 res->copyright = (char *) child->content;
144         } else if (!strcasecmp(name, "ttl")) {
145                 res->ttl = (char *) child->content;
146         } else if (!strcasecmp(name, "item")) {
147                 read_item(&res->items[res->item_count++], n->children);
148         }
149 }
150
151 static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root)
152 {
153         xmlNodePtr channel = root->children;
154
155         while (channel && (channel->type != XML_ELEMENT_NODE
156                         || strcmp((const char *) channel->name, "channel"))) {
157                 channel = channel->next;
158         }
159         if (!channel) {
160                 return 0;
161         }
162
163         int items = 0;
164         xmlNodePtr n;
165
166         for (n = channel->children; n; n = n->next) {
167                 if (n->type == XML_ELEMENT_NODE &&
168                                 !strcmp((const char *) n->name, "item")) {
169                         ++items;
170                 }
171         }
172
173         res->version = strdup("2.0");
174         res->items = malloc(items * sizeof(PRSS_Item));
175         res->item_count = 0;
176
177         for (n = channel->children; n; n = n->next) {
178                 read_element(res, n);
179         }
180
181         return 1;
182 }
183 static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root)
184 {
185         int items = 0;
186         xmlNodePtr n;
187
188         for (n = root->children; n; n = n->next) {
189                 if (n->type == XML_ELEMENT_NODE) {
190                         if (!strcmp((const char *) n->name, "item")) {
191                                 ++items;
192                         } else if (!strcmp((const char *) n->name, "channel")) {
193                                 xmlNodePtr i;
194
195                                 for (i = n->children; i; i = i->next) {
196                                         read_element(res, i);
197                                 }
198                         }
199                 }
200         }
201
202         res->version = strdup("1.0");
203         res->items = malloc(items * sizeof(PRSS_Item));
204         res->item_count = 0;
205
206         for (n = root->children; n; n = n->next) {
207                 if (n->type == XML_ELEMENT_NODE &&
208                                 !strcmp((const char *) n->name, "item")) {
209                         read_item(&res->items[res->item_count++], n->children);
210                 }
211         }
212
213         return 1;
214 }
215 static inline int parse_rss_0_9x(PRSS *res, xmlNodePtr root)
216 {
217         // almost same...
218         return parse_rss_2_0(res, root);
219 }
220
221 PRSS *prss_parse_doc(xmlDocPtr doc)
222 {
223         /* FIXME: doc shouldn't be freed after failure when called explicitly from
224          * program! */
225
226         xmlNodePtr root = xmlDocGetRootElement(doc);
227         PRSS *result = malloc(sizeof(PRSS));
228
229         prss_null(result);
230         result->_data = doc;
231         do {
232                 if (root->type == XML_ELEMENT_NODE) {
233                         if (!strcmp((const char *) root->name, "RDF")) {
234                                 // RSS 1.0 document
235                                 if (!parse_rss_1_0(result, root)) {
236                                         free(result);
237                                         xmlFreeDoc(doc);
238                                         return NULL;
239                                 }
240                                 return result;
241                         } else if (!strcmp((const char *) root->name, "rss")) {
242                                 // RSS 2.0 or <1.0 document
243                                 if (!parse_rss_2_0(result, root)) {
244                                         free(result);
245                                         xmlFreeDoc(doc);
246                                         return NULL;
247                                 }
248                                 return result;
249                         }
250                 }
251                 root = root->next;
252         } while (root);
253         free(result);
254         return NULL;
255 }