Enlightenment CVS committal

Author  : sarajervi
Project : misc
Module  : erss

Dir     : misc/erss/src


Modified Files:
        Makefile.am erss.c erss.h parse.c parse.h 


Log Message:
Total rewrite of the xml parsing code, now using libxml2 instead.
Old functions for parsing is still there, config files still use them. Fix
that later or switch to ecore_config.

Also added a new option in the config files since some feeds require a root element 
for parsing to be done corretly.

Added the e-cvs rss feed (got that one from xcomp)

JFYI: new parsing code is not tested much yet... :-)



===================================================================
RCS file: /cvsroot/enlightenment/misc/erss/src/Makefile.am,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -3 -r1.1.1.1 -r1.2
--- Makefile.am 22 Dec 2003 15:17:29 -0000      1.1.1.1
+++ Makefile.am 14 Jan 2004 18:57:07 -0000      1.2
@@ -1,9 +1,9 @@
 ## Process this file with automake to produce Makefile.in
 
-INCLUDES = @edje_cflags@ @esmart_cflags@ @ewd_cflags@
+INCLUDES = @edje_cflags@ @esmart_cflags@ @ewd_cflags@ @xml2_cflags@
 
 bin_PROGRAMS = erss
 
 erss_SOURCES = erss.c parse.c
 
-erss_LDADD = @edje_libs@ @esmart_libs@ @ewd_libs@
+erss_LDADD = @edje_libs@ @esmart_libs@ @ewd_libs@ @xml2_libs@
===================================================================
RCS file: /cvsroot/enlightenment/misc/erss/src/erss.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -3 -r1.16 -r1.17
--- erss.c      13 Jan 2004 23:04:25 -0000      1.16
+++ erss.c      14 Jan 2004 18:57:07 -0000      1.17
@@ -199,7 +199,8 @@
 {
        Ecore_Con_Event_Server_Del *e = event;
        char *buf = main_buffer;
-       char *leader;
+       xmlDocPtr doc;
+       char *temp;
 
        if (total_connects == 1)
                printf ("%s info: disconnecting ...\n", PACKAGE);
@@ -208,23 +209,10 @@
         * Now split our buffer in each newline and then parse the line.
         */
 
-       while (buf != NULL)
-       {
-               char temp;
+       temp = strchr(buf, '<');
+       doc = xmlParseMemory (temp, main_bufsize - (temp - main_buffer));
 
-               leader = strchr (buf, '\n');
-               if (leader)
-               {
-                       temp = *leader;
-                       *leader = '\0';
-                       parse_data (buf);
-                       *leader = temp;
-                       buf = leader + 1;
-               } else
-               {
-                       buf = leader;
-               }
-       }
+       parse_rss (doc);
 
        ecore_con_server_del (e->server);
        server = NULL;
===================================================================
RCS file: /cvsroot/enlightenment/misc/erss/src/erss.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -3 -r1.4 -r1.5
--- erss.h      12 Jan 2004 22:59:34 -0000      1.4
+++ erss.h      14 Jan 2004 18:57:07 -0000      1.5
@@ -5,7 +5,6 @@
 #include <Ecore_X.h>
 #include <Ecore_Evas.h>
 #include <Ecore_Con.h>
-#include <Ecore_Txt.h>
 #include <Evas.h>
 #include <Edje.h>
 #include <Ewd.h>
@@ -25,7 +24,8 @@
 #include <errno.h>
 #include <time.h>
 
-#include <assert.h>
+#include <libxml2/libxml/xmlmemory.h>
+#include <libxml2/libxml/parser.h>
 
 #include "config.h"
 
@@ -39,6 +39,7 @@
 void cb_mouse_in (void *data, Evas *e, Evas_Object *obj, void *event_info);
 void cb_mouse_out (void *data, Evas *e, Evas_Object *obj, void *event_info);
 void list_config_files (int output);
+void parse_rss (xmlDocPtr doc);
        
 
 #endif
===================================================================
RCS file: /cvsroot/enlightenment/misc/erss/src/parse.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -3 -r1.14 -r1.15
--- parse.c     13 Jan 2004 23:04:25 -0000      1.14
+++ parse.c     14 Jan 2004 18:57:07 -0000      1.15
@@ -105,125 +105,118 @@
                return FALSE;
 }
 
-void remove_garbage (char *c, char *garbage)
+void erss_story_new ()
 {
-       char *str, *tmp;
-
-       if (!garbage)
-               return;
-
-       str = strstr (c, garbage);
+               
+       item = malloc (sizeof (Article));
+  item->description = NULL;
+  item->url = NULL;
+  memset(item, 0, sizeof(Article));
+}
 
-       if (str) {
-               tmp = strdup(str + strlen(garbage));
-               strcpy (c, tmp);
-               free (tmp);
-       }
+void erss_story_end ()
+{
+       ewd_list_append (list, item);
 }
 
-void parse_data (char *buf)
+void parse_story (xmlDocPtr doc, xmlNodePtr cur)
 {
-       char *c;
-       char *text;
-       char *string;
+       char *str, *text;
        int i;
        
-       /* 
-        * Some of the feeds may have "garbage" in their titles
-        * we want to remove it.
-        */
-       char *garbage [2] = {
-               "<![CDATA[",
-               "]]>"
-       };
-       
-       if (!buf || !*buf)
-               return;
-
-       /*
-       printf ("data: %s\n", buf);
-       */
+       cur = cur->xmlChildrenNode;
 
-       if (ewd_list_nodes (list) >= cfg->num_stories)
-               return;
-
-       if (get_new_story (buf))
-       {
-               /* 
-                * We have a new story, allocate an item for it
-                */
-               item = malloc (sizeof (Article));
-               item->description = NULL;
-               item->url = NULL;
-               memset(item, 0, sizeof(Article));
-
-               return;
-       }
-
-       if (get_end_story (buf))
-       {
-               ewd_list_append (list, item);
-               return;
-       }
-
-       /* If the item is not allocated then we dont have a 
-        * real story. So return with an error
-        */
-       if (!item)
-               return;
-
-       if ((c = get_element (&buf, cfg->item_title)) != NULL)
-       {
-               for (i = 0; i < 2; i++)
-                       remove_garbage (c, garbage[i]);
-               
-               string = ecore_txt_convert (rc->enc_to, rc->enc_from, c);
+       while (cur != NULL) {
+               if (ewd_list_nodes (list) >= cfg->num_stories)
+                       return;
+
+               if (!strcmp(cur->name, cfg->item_start)) {
+                       erss_story_new ();
+                       parse_story (doc, cur);
+                       erss_story_end ();
+               } 
                
-               i = strlen(string) + 3 + strlen(cfg->prefix);
-               text = malloc (i);
-               
-               snprintf (text, i, " %s %s", cfg->prefix, string);
+               if ((!strcmp(cur->name, cfg->item_title)) && item) {
+                       str = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+                       
+                       i = strlen(str) + 3 + strlen(cfg->prefix);
+                       text = malloc (i);
                
-               item->obj = edje_object_add (evas);
-               edje_object_file_set (item->obj, cfg->theme, "erss_item");
-               evas_object_show (item->obj);
-
-               evas_object_event_callback_add (item->obj,
-                               EVAS_CALLBACK_MOUSE_IN, cb_mouse_in, NULL);
-               evas_object_event_callback_add (item->obj,
-                               EVAS_CALLBACK_MOUSE_OUT, cb_mouse_out, NULL);
+                       snprintf (text, i, " %s %s", cfg->prefix, str);
+                       
+                       item->obj = edje_object_add (evas);
+                       edje_object_file_set (item->obj, cfg->theme, "erss_item");
+                       evas_object_show (item->obj);
+
+                       evas_object_event_callback_add (item->obj,
+                                       EVAS_CALLBACK_MOUSE_IN, cb_mouse_in, NULL);
+                       evas_object_event_callback_add (item->obj,
+                                       EVAS_CALLBACK_MOUSE_OUT, cb_mouse_out, NULL);
                
-               e_container_element_append(cont, item->obj);
-               edje_object_part_text_set (item->obj, "article", text);
+                       e_container_element_append(cont, item->obj);
+                       edje_object_part_text_set (item->obj, "article", text);
 
-               free (string);
-               free (text);
-               free (c);
+                       free (text);
+               }
+       
+               if (cfg->item_url) {
+                       if (!strcmp(cur->name, cfg->item_url) && item) {
+                               str = xmlNodeListGetString(doc, cur->xmlChildrenNode, 
1);
+                               item->url = strdup (str);
+
+                               edje_object_signal_callback_add (item->obj, "exec*", 
"*",
+                                               cb_mouse_out_item, item);
+                               edje_object_signal_emit (item->obj, "mouse,in", 
"article");
+                               edje_object_signal_emit (item->obj, "mouse,out", 
"article");
+                       }
+               }
+               
+               if (cfg->item_description) {
+                       if (!strcmp(cur->name, cfg->item_description)) {
+                               str = xmlNodeListGetString(doc, cur->xmlChildrenNode, 
1);
+                               item->description = strdup (str);
+                       }
+               }
 
-               return; 
+               cur = cur->next;
        }
+}
+
+void parse_rss (xmlDocPtr doc)
+{
+       xmlNodePtr cur;
        
-       if ((c = get_element (&buf, cfg->item_url)) != NULL)
-       {
-               item->url = strdup (c);
+       if (doc == NULL ) {
+               fprintf(stderr, "%s warn: buffer not parsed successfully.\n", PACKAGE);
+       }
 
-               edje_object_signal_callback_add (item->obj, "exec*", "*", 
-                               cb_mouse_out_item, item);
-               edje_object_signal_emit (item->obj, "mouse,in", "article");
-               edje_object_signal_emit (item->obj, "mouse,out", "article");
+       cur = xmlDocGetRootElement(doc);
 
-               free (c);
-               return;
+       if (cur == NULL) {
+               fprintf(stderr,"%s error: empty buffer\n", PACKAGE);
+               xmlFreeDoc(doc);
+               exit (-1);
        }
-       
-       if ((c = get_element (&buf, cfg->item_description)) != NULL)
-       {
-               item->description = strdup (c);
+
+       cur = cur->xmlChildrenNode;
+       while (cur != NULL) {
+
+               if (cfg->item_root) {
+                       if (!strcmp(cur->name, cfg->item_root)) {
+                               parse_story (doc, cur);
+                       }
+               } else {
+                       if (!strcmp(cur->name, cfg->item_start)) {
+                               erss_story_new ();
+                               parse_story (doc, cur);
+                               erss_story_end ();
+                       }
+               }
                
-               free (c);
-               return;
+               cur = cur->next;
        }
        
+       xmlFreeDoc(doc);
 }
 
 char *get_next_line (FILE * fp)
@@ -377,6 +370,8 @@
                        cfg->hostname = strdup (c);
                else if ((c = get_element (&line, "url")) != NULL)
                        cfg->url = strdup (c);
+               else if ((c = get_element (&line, "item_root")) != NULL)
+                       cfg->item_root = strdup (c);
                else if ((c = get_element (&line, "item_start")) != NULL)
                        cfg->item_start = strdup (c);
                else if ((c = get_element (&line, "item_title")) != NULL) 
@@ -435,4 +430,6 @@
        if (!cfg->prefix)
                cfg->prefix = strdup(" . ");
 
+       if (!cfg->item_title)
+               fprintf (stderr, "%s error: no <item_title> tag defined\n", PACKAGE);
 }
===================================================================
RCS file: /cvsroot/enlightenment/misc/erss/src/parse.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -3 -r1.5 -r1.6
--- parse.h     11 Jan 2004 14:02:24 -0000      1.5
+++ parse.h     14 Jan 2004 18:57:07 -0000      1.6
@@ -10,6 +10,7 @@
        char *hostname;
        char *url;
 
+       char *item_root;
        char *item_start;
        char *item_title;
        char *item_url;




-------------------------------------------------------
This SF.net email is sponsored by: Perforce Software.
Perforce is the Fast Software Configuration Management System offering
advanced branching capabilities and atomic changes on 50+ platforms.
Free Eval! http://www.perforce.com/perforce/loadprog.html
_______________________________________________
enlightenment-cvs mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs

Reply via email to