the big relocation patch
[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                 xmlNodePtr child;
82                 const char *name;
83
84                 if (data->type != XML_ELEMENT_NODE) {
85                         continue;
86                 }
87                 child = data->children;
88
89                 if (!child) {
90                         continue;
91                 }
92
93                 name = (const char *)data->name;
94                 if (!strcasecmp(name, "title")) {
95                         res->title = (char *) child->content;
96                 } else if (!strcasecmp(name, "link")) {
97                         res->link = (char *) child->content;
98                 } else if (!strcasecmp(name, "description")) {
99                         res->description = (char *) child->content;
100                 } else if (!strcasecmp(name, "category")) {
101                         res->category = (char *) child->content;
102                 } else if (!strcasecmp(name, "pubDate")) {
103                         res->pubdate = (char *) child->content;
104                 } else if (!strcasecmp(name, "guid")) {
105                         res->guid = (char *) child->content;
106                 }
107         }
108 }
109 static inline void read_element(PRSS *res, xmlNodePtr n)
110 {
111         xmlNodePtr child;
112         const char *name;
113
114         if (n->type != XML_ELEMENT_NODE) {
115                 return;
116         }
117         child = n->children;
118
119         if (!child) {
120                 return;
121         }
122
123         name = (const char *)n->name;
124         if (!strcasecmp(name, "title")) {
125                 res->title = (char *) child->content;
126         } else if (!strcasecmp(name, "link")) {
127                 res->link = (char *) child->content;
128         } else if (!strcasecmp(name, "description")) {
129                 res->description = (char *) child->content;
130         } else if (!strcasecmp(name, "language")) {
131                 res->language = (char *) child->content;
132         } else if (!strcasecmp(name, "pubDate")) {
133                 res->pubdate = (char *) child->content;
134         } else if (!strcasecmp(name, "lastBuildDate")) {
135                 res->lastbuilddate = (char *) child->content;
136         } else if (!strcasecmp(name, "generator")) {
137                 res->generator = (char *) child->content;
138         } else if (!strcasecmp(name, "docs")) {
139                 res->docs = (char *) child->content;
140         } else if (!strcasecmp(name, "managingEditor")) {
141                 res->managingeditor = (char *) child->content;
142         } else if (!strcasecmp(name, "webMaster")) {
143                 res->webmaster = (char *) child->content;
144         } else if (!strcasecmp(name, "copyright")) {
145                 res->copyright = (char *) child->content;
146         } else if (!strcasecmp(name, "ttl")) {
147                 res->ttl = (char *) child->content;
148         } else if (!strcasecmp(name, "item")) {
149                 read_item(&res->items[res->item_count++], n->children);
150         }
151 }
152
153 static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root)
154 {
155         xmlNodePtr channel = root->children;
156         xmlNodePtr n;
157         int items = 0;
158
159         while (channel && (channel->type != XML_ELEMENT_NODE
160                         || strcmp((const char *) channel->name, "channel"))) {
161                 channel = channel->next;
162         }
163         if (!channel) {
164                 return 0;
165         }
166
167         for (n = channel->children; n; n = n->next) {
168                 if (n->type == XML_ELEMENT_NODE &&
169                                 !strcmp((const char *) n->name, "item")) {
170                         ++items;
171                 }
172         }
173
174         res->version = strdup("2.0");
175         res->items = malloc(items * sizeof(PRSS_Item));
176         res->item_count = 0;
177
178         for (n = channel->children; n; n = n->next) {
179                 read_element(res, n);
180         }
181
182         return 1;
183 }
184 static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root)
185 {
186         int items = 0;
187         xmlNodePtr n;
188
189         for (n = root->children; n; n = n->next) {
190                 if (n->type == XML_ELEMENT_NODE) {
191                         if (!strcmp((const char *) n->name, "item")) {
192                                 ++items;
193                         } else if (!strcmp((const char *) n->name, "channel")) {
194                                 xmlNodePtr i;
195
196                                 for (i = n->children; i; i = i->next) {
197                                         read_element(res, i);
198                                 }
199                         }
200                 }
201         }
202
203         res->version = strdup("1.0");
204         res->items = malloc(items * sizeof(PRSS_Item));
205         res->item_count = 0;
206
207         for (n = root->children; n; n = n->next) {
208                 if (n->type == XML_ELEMENT_NODE &&
209                                 !strcmp((const char *) n->name, "item")) {
210                         read_item(&res->items[res->item_count++], n->children);
211                 }
212         }
213
214         return 1;
215 }
216 static inline int parse_rss_0_9x(PRSS *res, xmlNodePtr root)
217 {
218         // almost same...
219         return parse_rss_2_0(res, root);
220 }
221
222 PRSS *prss_parse_doc(xmlDocPtr doc)
223 {
224         /* FIXME: doc shouldn't be freed after failure when called explicitly from
225          * program! */
226
227         xmlNodePtr root = xmlDocGetRootElement(doc);
228         PRSS *result = malloc(sizeof(PRSS));
229
230         prss_null(result);
231         result->_data = doc;
232         do {
233                 if (root->type == XML_ELEMENT_NODE) {
234                         if (!strcmp((const char *) root->name, "RDF")) {
235                                 // RSS 1.0 document
236                                 if (!parse_rss_1_0(result, root)) {
237                                         free(result);
238                                         xmlFreeDoc(doc);
239                                         return NULL;
240                                 }
241                                 return result;
242                         } else if (!strcmp((const char *) root->name, "rss")) {
243                                 // RSS 2.0 or <1.0 document
244                                 if (!parse_rss_2_0(result, root)) {
245                                         free(result);
246                                         xmlFreeDoc(doc);
247                                         return NULL;
248                                 }
249                                 return result;
250                         }
251                 }
252                 root = root->next;
253         } while (root);
254         free(result);
255         return NULL;
256 }