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.
21 #include <curl/curl.h>
33 static void _handler_set(XML_Parser parser, guint type)
39 case FEED_TYPE_RSS_20:
40 XML_SetElementHandler(parser,
41 feed_parser_rss20_start,
42 feed_parser_rss20_end);
46 XML_SetElementHandler(parser,
47 feed_parser_rdf_start,
51 case FEED_TYPE_ATOM_10:
52 XML_SetElementHandler(parser,
53 feed_parser_atom10_start,
54 feed_parser_atom10_end);
59 static void _elparse_start_chooser(void *data,
60 const gchar *el, const gchar **attr)
62 FeedParserCtx *ctx = (FeedParserCtx *)data;
63 guint feedtype = FEED_TYPE_NONE;
66 if( ctx->depth == 0 ) {
68 /* RSS 2.0 detected */
69 if( !strcmp(el, "rss") ) {
70 feedtype = FEED_TYPE_RSS_20;
71 } else if( !strcmp(el, "rdf:RDF") ) {
72 feedtype = FEED_TYPE_RDF;
73 } else if( !strcmp(el, "feed") ) {
75 /* ATOM feed detected, let's check version */
76 version = feed_parser_get_attribute_value(attr, "xmlns");
77 if( !strcmp(version, "http://www.w3.org/2005/Atom") ||
78 !strcmp(version, "https://www.w3.org/2005/Atom") )
79 feedtype = FEED_TYPE_ATOM_10;
81 feedtype = FEED_TYPE_ATOM_03;
85 _handler_set(ctx->parser, feedtype);
90 static void _elparse_end_dummy(void *data, const gchar *el)
92 FeedParserCtx *ctx = (FeedParserCtx *)data;
94 if( ctx->str != NULL ) {
95 g_string_free(ctx->str, TRUE);
102 void libfeed_expat_chparse(void *data, const gchar *s, gint len)
104 FeedParserCtx *ctx = (FeedParserCtx *)data;
109 strncpy(buf, s, len);
112 /* check if the string is blank, ... */
113 for( i = 0; i < strlen(buf); i++ )
114 if( !isspace(buf[i]) )
117 /* ...because we do not want the blanks if we're just starting new GString */
118 if( xblank > 0 && ctx->str == NULL ) {
123 if( ctx->str == NULL ) {
124 ctx->str = g_string_sized_new(len + 1);
127 g_string_append(ctx->str, buf);
132 void feed_parser_set_expat_handlers(FeedParserCtx *ctx)
134 XML_SetUserData(ctx->parser, (void *)ctx);
136 XML_SetElementHandler(ctx->parser,
137 _elparse_start_chooser,
140 XML_SetCharacterDataHandler(ctx->parser,
141 libfeed_expat_chparse);
143 XML_SetUnknownEncodingHandler(ctx->parser, feed_parser_unknown_encoding_handler,
147 size_t feed_writefunc(void *ptr, size_t size, size_t nmemb, void *data)
149 gint len = size * nmemb;
150 FeedParserCtx *ctx = (FeedParserCtx *)data;
153 status = XML_Parse(ctx->parser, ptr, len, FALSE);
155 if( status == XML_STATUS_ERROR ) {
156 err = XML_GetErrorCode(ctx->parser);
157 printf("\nExpat: --- %s\n\n", XML_ErrorString(err));
163 gchar *feed_parser_get_attribute_value(const gchar **attr, const gchar *name)
167 if( attr == NULL || name == NULL )
170 for( i = 0; attr[i] != NULL && attr[i+1] != NULL; i += 2 ) {
171 if( !strcmp( attr[i], name) )
172 return (gchar *)attr[i+1];
175 /* We haven't found anything. */
179 #define CHARSIZEUTF32 4
189 static gint giconv_utf32_char(GIConv cd, const gchar *inbuf, size_t insize,
194 guchar outbuf[CHARSIZEUTF32];
198 outsize = sizeof(outbuf);
199 outbufp = (gchar *)outbuf;
200 #ifdef HAVE_ICONV_PROTO_CONST
201 r = g_iconv(cd, (const gchar **)&inbuf, &insize,
204 r = g_iconv(cd, (gchar **)&inbuf, &insize,
208 g_iconv(cd, 0, 0, 0, 0);
211 return LEP_ICONV_ILSEQ;
213 return LEP_ICONV_INVAL;
215 return LEP_ICONV_UNKNOWN;
221 if( (insize > 0) || (outsize > 0) )
222 return LEP_ICONV_FAILED;
225 for( i = 0; i < sizeof(outbuf); i++ ) {
226 value = (value << 8) + outbuf[i];
232 return LEP_ICONV_FAILED;
236 static gint feed_parser_setup_unknown_encoding(const gchar *charset,
245 cd = g_iconv_open("UTF-32BE", charset);
246 if( cd == (GIConv) -1 )
250 for( i = 0; i < 256; i++ ) {
254 r = giconv_utf32_char(cd, buf, 1, &value);
255 if( r == LEP_ICONV_OK) {
256 info->map[i] = value;
257 } else if( r != LEP_ICONV_INVAL ) {
259 for( j = 0; j < 256; j++ ) {
262 r = giconv_utf32_char(cd, buf, 2, &value);
263 if( r == LEP_ICONV_OK ) {
266 } else if( r != LEP_ICONV_INVAL ) {
268 for( k = 0; k < 256; k++ ) {
271 r = giconv_utf32_char(cd, buf, 3, &value);
272 if( r == LEP_ICONV_OK) {
286 struct FeedParserUnknownEncoding {
291 static gint feed_parser_unknown_encoding_convert(void *data, const gchar *s)
294 struct FeedParserUnknownEncoding *enc_data;
304 r = giconv_utf32_char(enc_data->cd, s, insize, &value);
305 if( r != LEP_ICONV_OK )
311 static void feed_parser_unknown_encoding_data_free(void *data)
313 struct FeedParserUnknownEncoding *enc_data;
316 free(enc_data->charset);
317 g_iconv_close(enc_data->cd);
321 int feed_parser_unknown_encoding_handler(void *encdata, const XML_Char *name,
325 struct FeedParserUnknownEncoding *data;
328 result = feed_parser_setup_unknown_encoding(name, info);
331 info->convert = NULL;
332 info->release = NULL;
333 return XML_STATUS_OK;
336 cd = g_iconv_open("UTF-32BE", name);
337 if( cd == (GIConv)-1 )
338 return XML_STATUS_ERROR;
340 data = malloc( sizeof(*data) );
343 return XML_STATUS_ERROR;
346 data->charset = strdup(name);
347 if( data->charset == NULL ) {
350 return XML_STATUS_ERROR;
355 info->convert = feed_parser_unknown_encoding_convert;
356 info->release = feed_parser_unknown_encoding_data_free;
358 return XML_STATUS_OK;