Multiple request version for showtags.c

"Michael Qiu" <[email protected]>
Newsgroups gmane.comp.lib.libwww
Message-ID <000501c2d64c$91b1c380$1c01a8c0@michaelchina>
Hello everyone,
  I am newbie of libwww. My task is to develop a module for my application which can get data from web server(I put the data into HTML&XML page). So I should parse HTML&XML page. I have seen the ready to go example for libwww. And I found showtags.c which fit my request well. But I want to use it as a module of my project. It should fetch multiple request. Such like:
initwww()
fetch()
fetch()
.......
fetch()
closewww()

Here is my module. It works in most time. But as the time goes by, It will caught a SIGSEGV signal. Seems like I have make some mistake in memory. Can anybody help me?





#include <stdlib.h>

#include "WWWLib.h"
#include "WWWInit.h"
#include "WWWHTML.h"
#include "WWWXML.h"
#include "http.h"

PRIVATE void beginElement (HText * 		text,
			   int			element_number,
			   const BOOL *		present,
			   const char **	value)
{
    SGML_dtd * dtd = HTML_dtd();
    HTTag * tag = SGML_findTag(dtd, element_number);
    if (tag)
    {
	char * tagname = HTTag_name(tag);
	if (!strcmp(tagname, "A"))
	{
	  int maxcnt = HTTag_attributes(tag);
	  int cnt;
	  for (cnt=0; cnt<maxcnt; cnt++)
	  {
	    if (present[cnt])
	    {
	      char * attrname = HTTag_attributeName(tag, cnt);
	      if (!strcmp(attrname, "HREF"))
	      {
		  if (!strncmp(value[cnt], "RoomId", 6) && (strlen(value[cnt]) > 7))
		  {
		    strncpy(g_strID, value[cnt]+7, 9);
		    g_strID[9] = '\0';
		  }
		  if (!strncmp(value[cnt], "RoomName", 6) && (strlen(value[cnt]) > 9))
		  {
		    strncpy(g_strName, value[cnt]+9, 9);
		    g_strName[9] = '\0';
		  }
	      }
	    }
	  }
	}
	HT_FREE(tagname);
    }
    HT_FREE(tag);
    HT_FREE(dtd);
}


PRIVATE void XML_startElement (void * userData,
	   	    	       const XML_Char *	name, const XML_Char ** atts)
{
  if (!strcmp(name, "row"))
  {
    int i = 0;
    songdata *tmp = NULL;
    tmp = (songdata *)g_malloc(sizeof(songdata));
    
    while(atts[i])
    {
      if (!strcmp(atts[i], "soundid")||!strcmp(atts[i], "songid")
	  ||!strcmp(atts[i], "SongID")||!strcmp(atts[i], "SoundID"))
      {
	strcpy(tmp->songid, atts[i+1]);
      }
      if (!strcmp(atts[i], "title")||!strcmp(atts[i], "Title"))
      {
	strcpy(tmp->songtitle, atts[i+1]);
      }
      if (!strcmp(atts[i], "artistname")||!strcmp(atts[i], "ArtistName"))
      {
	strcpy(tmp->singername, atts[i+1]);
      }
      if (!strcmp(atts[i], "artistid"))
      {
	strcpy(tmp->singerid, atts[i+1]);
      }
      if (!strcmp(atts[i], "played")||!strcmp(atts[i], "Played"))
      {
	tmp->state = atoi(atts[i+1]);
      }
      i++;
    }
    tmp->selected = 0;
    head = g_list_append (head, tmp);
    
  }
	    
}

PRIVATE void XML_endElement (void * userData,
			     const XML_Char * name)
{
    //HTPrint("End......... %s\n", name ? name : "<null>");
}


PRIVATE int printer (const char * fmt, va_list pArgs)
{
    return (vfprintf(stdout, fmt, pArgs));
}


PRIVATE int tracer (const char * fmt, va_list pArgs)
{
    return (vfprintf(stderr, fmt, pArgs));
}

PRIVATE int terminate_handler (HTRequest * request, HTResponse * response,
			       void * param, int status) 
{

    HTEventList_stopLoop ();
    HTRequest_delete(request);
    
    return 0;
}

PRIVATE void HTXML_setHandlers (XML_Parser me)
{
    XML_SetElementHandler(me, XML_startElement, XML_endElement);
}

PRIVATE void HTXML_newInstance (HTStream *		me,
				HTRequest *		request,
				HTFormat 		target_format,
				HTStream *		target_stream,
				XML_Parser              xmlparser,
				void * 			context)
{
    if (me && xmlparser) HTXML_setHandlers(xmlparser);
}

void
initwww()
{
  HTProfile_newHTMLNoCacheClient ("BZKTV-LIBWWW", "1.0");
  HTPrint_setCallback(printer);
  HTTrace_setCallback(tracer);
  HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
  HTXMLCallback_registerNew (HTXML_newInstance, NULL);
  HText_registerElementCallback(beginElement, NULL);
  
//  HTHost_setEventTimeout(5000);
}

void
closewww()
{
  HTProfile_delete();
  exit(0);
}

void
fetch (const char * url)
{
    
    char * uri = NULL;
    
    uri = HTParse(url, NULL, PARSE_ALL);

    if (uri) {
	HTRequest * request = NULL;
	HTAnchor * anchor = NULL;
	BOOL status = NO;

	request = HTRequest_new();
	
//	HTRequest_setPreemptive(request, YES);
	
	anchor = HTAnchor_findAddress(uri);

	status = HTLoadAnchor(anchor, request);

	if (status == YES) HTEventList_loop(request);
    }
    HT_FREE(uri);
    return;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.