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>
46 static void _handler_set(XML_Parser parser, guint type)
52 case FEED_TYPE_RSS_20:
53 XML_SetElementHandler(parser,
54 feed_parser_rss20_start,
55 feed_parser_rss20_end);
59 XML_SetElementHandler(parser,
60 feed_parser_rdf_start,
64 case FEED_TYPE_ATOM_10:
65 XML_SetElementHandler(parser,
66 feed_parser_atom10_start,
67 feed_parser_atom10_end);
72 static void _elparse_start_chooser(void *data,
73 const gchar *el, const gchar **attr)
75 FeedParserCtx *ctx = (FeedParserCtx *)data;
76 guint feedtype = FEED_TYPE_NONE;
79 if( ctx->depth == 0 ) {
81 /* RSS 2.0 detected */
82 if( !strcmp(el, "rss") ) {
83 feedtype = FEED_TYPE_RSS_20;
84 } else if( !strcmp(el, "rdf:RDF") ) {
85 feedtype = FEED_TYPE_RDF;
86 } else if( !strcmp(el, "feed") ) {
88 /* ATOM feed detected, let's check version */
89 version = feed_parser_get_attribute_value(attr, "xmlns");
90 if( !strcmp(version, "http://www.w3.org/2005/Atom") ||
91 !strcmp(version, "https://www.w3.org/2005/Atom") )
92 feedtype = FEED_TYPE_ATOM_10;
94 feedtype = FEED_TYPE_ATOM_03;
98 _handler_set(ctx->parser, feedtype);
103 static void _elparse_end_dummy(void *data, const gchar *el)
105 FeedParserCtx *ctx = (FeedParserCtx *)data;
107 if( ctx->str != NULL ) {
108 g_string_free(ctx->str, TRUE);
115 void libfeed_expat_chparse(void *data, const gchar *s, gint len)
117 FeedParserCtx *ctx = (FeedParserCtx *)data;
122 strncpy(buf, s, len);
125 /* check if the string is blank, ... */
126 for( i = 0; i < strlen(buf); i++ )
127 if( !isspace(buf[i]) )
130 /* ...because we do not want the blanks if we're just starting new GString */
131 if( xblank > 0 && ctx->str == NULL ) {
136 if( ctx->str == NULL ) {
137 ctx->str = g_string_sized_new(len + 1);
140 g_string_append(ctx->str, buf);
145 void feed_parser_set_expat_handlers(FeedParserCtx *ctx)
147 XML_SetUserData(ctx->parser, (void *)ctx);
149 XML_SetElementHandler(ctx->parser,
150 _elparse_start_chooser,
153 XML_SetCharacterDataHandler(ctx->parser,
154 libfeed_expat_chparse);
156 XML_SetUnknownEncodingHandler(ctx->parser, feed_parser_unknown_encoding_handler,
160 size_t feed_writefunc(void *ptr, size_t size, size_t nmemb, void *data)
162 gint len = size * nmemb;
163 FeedParserCtx *ctx = (FeedParserCtx *)data;
166 status = XML_Parse(ctx->parser, ptr, len, FALSE);
168 if( status == XML_STATUS_ERROR ) {
169 err = XML_GetErrorCode(ctx->parser);
170 printf("\nExpat: --- %s\n\n", XML_ErrorString(err));
176 gchar *feed_parser_get_attribute_value(const gchar **attr, const gchar *name)
180 if( attr == NULL || name == NULL )
183 for( i = 0; attr[i] != NULL && attr[i+1] != NULL; i += 2 ) {
184 if( !strcmp( attr[i], name) )
185 return (gchar *)attr[i+1];
188 /* We haven't found anything. */
192 #define CHARSIZEUTF32 4
202 static gint giconv_utf32_char(GIConv cd, const gchar *inbuf, size_t insize,
207 guchar outbuf[CHARSIZEUTF32];
211 outsize = sizeof(outbuf);
212 outbufp = (gchar *)outbuf;
213 #ifdef HAVE_ICONV_PROTO_CONST
214 r = g_iconv(cd, (const gchar **)&inbuf, &insize,
217 r = g_iconv(cd, (gchar **)&inbuf, &insize,
221 g_iconv(cd, 0, 0, 0, 0);
224 return LEP_ICONV_ILSEQ;
226 return LEP_ICONV_INVAL;
228 return LEP_ICONV_UNKNOWN;
234 if( (insize > 0) || (outsize > 0) )
235 return LEP_ICONV_FAILED;
238 for( i = 0; i < sizeof(outbuf); i++ ) {
239 value = (value << 8) + outbuf[i];
245 return LEP_ICONV_FAILED;
249 static gint feed_parser_setup_unknown_encoding(const gchar *charset,
258 cd = g_iconv_open("UTF-32BE", charset);
259 if( cd == (GIConv) -1 )
263 for( i = 0; i < 256; i++ ) {
267 r = giconv_utf32_char(cd, buf, 1, &value);
268 if( r == LEP_ICONV_OK) {
269 info->map[i] = value;
270 } else if( r != LEP_ICONV_INVAL ) {
272 for( j = 0; j < 256; j++ ) {
275 r = giconv_utf32_char(cd, buf, 2, &value);
276 if( r == LEP_ICONV_OK ) {
279 } else if( r != LEP_ICONV_INVAL ) {
281 for( k = 0; k < 256; k++ ) {
284 r = giconv_utf32_char(cd, buf, 3, &value);
285 if( r == LEP_ICONV_OK) {
299 struct FeedParserUnknownEncoding {
304 static gint feed_parser_unknown_encoding_convert(void *data, const gchar *s)
307 struct FeedParserUnknownEncoding *enc_data;
317 r = giconv_utf32_char(enc_data->cd, s, insize, &value);
318 if( r != LEP_ICONV_OK )
324 static void feed_parser_unknown_encoding_data_free(void *data)
326 struct FeedParserUnknownEncoding *enc_data;
329 free(enc_data->charset);
330 g_iconv_close(enc_data->cd);
334 int feed_parser_unknown_encoding_handler(void *encdata, const XML_Char *name,
338 struct FeedParserUnknownEncoding *data;
341 result = feed_parser_setup_unknown_encoding(name, info);
344 info->convert = NULL;
345 info->release = NULL;
346 return XML_STATUS_OK;
349 cd = g_iconv_open("UTF-32BE", name);
350 if( cd == (GIConv)-1 )
351 return XML_STATUS_ERROR;
353 data = malloc( sizeof(*data) );
356 return XML_STATUS_ERROR;
359 data->charset = strdup(name);
360 if( data->charset == NULL ) {
363 return XML_STATUS_ERROR;
368 info->convert = feed_parser_unknown_encoding_convert;
369 info->release = feed_parser_unknown_encoding_data_free;
371 return XML_STATUS_OK;