2 * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
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.
9 * This program is distributed in the hope that it will be useful,
10 * but 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.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include <curl/curl.h>
37 static void _handler_set(XML_Parser parser, guint type)
43 case FEED_TYPE_RSS_20:
44 XML_SetElementHandler(parser,
45 feed_parser_rss20_start,
46 feed_parser_rss20_end);
50 XML_SetElementHandler(parser,
51 feed_parser_rdf_start,
55 case FEED_TYPE_ATOM_10:
56 XML_SetElementHandler(parser,
57 feed_parser_atom10_start,
58 feed_parser_atom10_end);
63 static void _elparse_start_chooser(void *data,
64 const gchar *el, const gchar **attr)
66 FeedParserCtx *ctx = (FeedParserCtx *)data;
67 guint feedtype = FEED_TYPE_NONE;
70 if( ctx->depth == 0 ) {
72 /* RSS 2.0 detected */
73 if( !strcmp(el, "rss") ) {
74 feedtype = FEED_TYPE_RSS_20;
75 } else if( !strcmp(el, "rdf:RDF") ) {
76 feedtype = FEED_TYPE_RDF;
77 } else if( !strcmp(el, "feed") ) {
79 /* ATOM feed detected, let's check version */
80 version = feed_parser_get_attribute_value(attr, "xmlns");
81 if( !strcmp(version, "http://www.w3.org/2005/Atom") ||
82 !strcmp(version, "https://www.w3.org/2005/Atom") )
83 feedtype = FEED_TYPE_ATOM_10;
85 feedtype = FEED_TYPE_ATOM_03;
89 _handler_set(ctx->parser, feedtype);
94 static void _elparse_end_dummy(void *data, const gchar *el)
96 FeedParserCtx *ctx = (FeedParserCtx *)data;
98 if( ctx->str != NULL ) {
99 g_string_free(ctx->str, TRUE);
106 void libfeed_expat_chparse(void *data, const gchar *s, gint len)
108 FeedParserCtx *ctx = (FeedParserCtx *)data;
113 strncpy(buf, s, len);
116 /* check if the string is blank, ... */
117 for( i = 0; i < strlen(buf); i++ )
118 if( !isspace(buf[i]) )
121 /* ...because we do not want the blanks if we're just starting new GString */
122 if( xblank > 0 && ctx->str == NULL ) {
127 if( ctx->str == NULL ) {
128 ctx->str = g_string_sized_new(len + 1);
131 g_string_append(ctx->str, buf);
136 void feed_parser_set_expat_handlers(FeedParserCtx *ctx)
138 XML_SetUserData(ctx->parser, (void *)ctx);
140 XML_SetElementHandler(ctx->parser,
141 _elparse_start_chooser,
144 XML_SetCharacterDataHandler(ctx->parser,
145 libfeed_expat_chparse);
147 XML_SetUnknownEncodingHandler(ctx->parser, feed_parser_unknown_encoding_handler,
151 size_t feed_writefunc(void *ptr, size_t size, size_t nmemb, void *data)
153 gint len = size * nmemb;
154 FeedParserCtx *ctx = (FeedParserCtx *)data;
157 status = XML_Parse(ctx->parser, ptr, len, FALSE);
159 if( status == XML_STATUS_ERROR ) {
160 err = XML_GetErrorCode(ctx->parser);
161 printf("\nExpat: --- %s\n\n", XML_ErrorString(err));
167 gchar *feed_parser_get_attribute_value(const gchar **attr, const gchar *name)
171 if( attr == NULL || name == NULL )
174 for( i = 0; attr[i] != NULL && attr[i+1] != NULL; i += 2 ) {
175 if( !strcmp( attr[i], name) )
176 return (gchar *)attr[i+1];
179 /* We haven't found anything. */
183 #define CHARSIZEUTF32 4
193 static gint giconv_utf32_char(GIConv cd, const gchar *inbuf, size_t insize,
198 guchar outbuf[CHARSIZEUTF32];
202 outsize = sizeof(outbuf);
203 outbufp = (gchar *)outbuf;
204 #ifdef HAVE_ICONV_PROTO_CONST
205 r = g_iconv(cd, (const gchar **)&inbuf, &insize,
208 r = g_iconv(cd, (gchar **)&inbuf, &insize,
212 g_iconv(cd, 0, 0, 0, 0);
215 return LEP_ICONV_ILSEQ;
217 return LEP_ICONV_INVAL;
219 return LEP_ICONV_UNKNOWN;
225 if( (insize > 0) || (outsize > 0) )
226 return LEP_ICONV_FAILED;
229 for( i = 0; i < sizeof(outbuf); i++ ) {
230 value = (value << 8) + outbuf[i];
236 return LEP_ICONV_FAILED;
240 static gint feed_parser_setup_unknown_encoding(const gchar *charset,
249 cd = g_iconv_open("UTF-32BE", charset);
250 if( cd == (GIConv) -1 )
254 for( i = 0; i < 256; i++ ) {
258 r = giconv_utf32_char(cd, buf, 1, &value);
259 if( r == LEP_ICONV_OK) {
260 info->map[i] = value;
261 } else if( r != LEP_ICONV_INVAL ) {
263 for( j = 0; j < 256; j++ ) {
266 r = giconv_utf32_char(cd, buf, 2, &value);
267 if( r == LEP_ICONV_OK ) {
270 } else if( r != LEP_ICONV_INVAL ) {
272 for( k = 0; k < 256; k++ ) {
275 r = giconv_utf32_char(cd, buf, 3, &value);
276 if( r == LEP_ICONV_OK) {
290 struct FeedParserUnknownEncoding {
295 static gint feed_parser_unknown_encoding_convert(void *data, const gchar *s)
298 struct FeedParserUnknownEncoding *enc_data;
308 r = giconv_utf32_char(enc_data->cd, s, insize, &value);
309 if( r != LEP_ICONV_OK )
315 static void feed_parser_unknown_encoding_data_free(void *data)
317 struct FeedParserUnknownEncoding *enc_data;
320 free(enc_data->charset);
321 g_iconv_close(enc_data->cd);
325 int feed_parser_unknown_encoding_handler(void *encdata, const XML_Char *name,
329 struct FeedParserUnknownEncoding *data;
332 result = feed_parser_setup_unknown_encoding(name, info);
335 info->convert = NULL;
336 info->release = NULL;
337 return XML_STATUS_OK;
340 cd = g_iconv_open("UTF-32BE", name);
341 if( cd == (GIConv)-1 )
342 return XML_STATUS_ERROR;
344 data = malloc( sizeof(*data) );
347 return XML_STATUS_ERROR;
350 data->charset = strdup(name);
351 if( data->charset == NULL ) {
354 return XML_STATUS_ERROR;
359 info->convert = feed_parser_unknown_encoding_convert;
360 info->release = feed_parser_unknown_encoding_data_free;
362 return XML_STATUS_OK;