library/src/decoders/HTML/BP-HTMLDecoder.cc
// Will decode a retrieved html buffer into a DOM parsable structure. NULL for default delimiter set.
Will decode a retrieved html buffer into a DOM parsable structure. NULL for default delimiter set.
Will decode a retrieved html buffer into a DOM parsable structure . NULL for default delimiter set .
P_BP_HTML_FULL_EXTRACTION HTMLDecodeBuffer
(
		char *buff,
		int size,
		char *URL,
		char *encoding,
		P_BP_HTML_ANCHOR_DELIMITERS delims,
		int options,
		P_BP_GARBAGE_COLLECTOR_DATA gc_in,
		P_BP_HTTP_SESSION http_session
)
{
	if(!buff || !size)
		return NULL;
	P_BP_HTML_FULL_EXTRACTION extracted = (P_BP_HTML_FULL_EXTRACTION) bpcalloc(sizeof(BP_HTML_FULL_EXTRACTION), 1);
	extracted->tq = BP_CreateLinkL("html extractor", BP_FALSE);
	extracted->http_session_reference = http_session;
	P_BP_HTML_ANCHOR_LIST anchors       = NULL;
	P_BP_HTML_FORM_LIST forms           = NULL;
	P_BP_GARBAGE_COLLECTOR_DATA gc      = NULL;
	if(!gc_in)
		gc = BP_CreateNewGarbageCollector("Decode buff gc.", true);
	else
		gc = gc_in;
	htmlDocPtr  htmlDoc        = NULL;
	htmlNodePtr rootElement   = NULL;
	htmlNodePtr passedElement = NULL;
	LIBXML_TEST_VERSION
	htmlDoc = htmlReadMemory
	(
		buff,
		size,
		URL,
		encoding,
		options
	);
	if(htmlDoc == NULL)
	{
		return NULL;
	}
	P_BP_HTML_ANCHOR_DELIMITERS defaultDelims = NULL;
	if(delims == NULL)
	{
		defaultDelims = (P_BP_HTML_ANCHOR_DELIMITERS) BP_calloc(gc, NULL, sizeof(BP_HTML_ANCHOR_DELIMITERS), 1);
		defaultDelims->startDelimiter = BP_strdup(gc, NULL, "?\0");
			defaultDelims->sLen = 1;
		defaultDelims->nextDelimiter = BP_strdup(gc, NULL, "&\0");
			defaultDelims->nLen = 1;
		defaultDelims->valueDelimiter = BP_strdup(gc, NULL, "=\0");
			defaultDelims->vLen = 1;
	}
	else
	{
		defaultDelims = delims;
	}
	if(defaultDelims == NULL)
		goto end;
	extracted->delims = defaultDelims;
	rootElement = xmlDocGetRootElement(htmlDoc);
	passedElement = rootElement;
	anchors = HTMLParseAnchors(passedElement, defaultDelims, gc, extracted);
	BP_GarbageCollectorLogChunk(gc, ".", anchors, 4, BP_GARBAGE_COLLECTOR_DATA_ALLOC_CALLOC);
	extracted->anchors = anchors;
	passedElement = rootElement;
	forms = HTMLParseForms(rootElement, extracted);
	BP_GarbageCollectorLogChunk(gc, ".", (void *) forms, 4, BP_GARBAGE_COLLECTOR_DATA_ALLOC_CALLOC);
	extracted->forms = forms;
	if(extracted->forms)
	{
		HTMLParseFormSecondaryData(extracted->forms, extracted);
	}
	if(delims != NULL)
		defaultDelims = NULL;
	end:
	xmlFreeDoc(htmlDoc);
	extracted->gc = gc;
	return extracted;
}
P_BP_HTML_FULL_EXTRACTION HTMLDecodeBuffer ( char * buff , int size , char * URL , char * encoding , P_BP_HTML_ANCHOR_DELIMITERS delims , int options , P_BP_GARBAGE_COLLECTOR_DATA gc_in , P_BP_HTTP_SESSION http_session ) { if ( ! buff || ! size ) return NULL ; P_BP_HTML_FULL_EXTRACTION extracted = ( P_BP_HTML_FULL_EXTRACTION ) bpcalloc ( sizeof ( BP_HTML_FULL_EXTRACTION ) , 1 ) ; extracted -> tq = BP_CreateLinkL ( " " , BP_FALSE ) ; extracted -> http_session_reference = http_session ; P_BP_HTML_ANCHOR_LIST anchors = NULL ; P_BP_HTML_FORM_LIST forms = NULL ; P_BP_GARBAGE_COLLECTOR_DATA gc = NULL ; if ( ! gc_in ) gc = BP_CreateNewGarbageCollector ( " " , true ) ; else gc = gc_in ; htmlDocPtr htmlDoc = NULL ; htmlNodePtr rootElement = NULL ; htmlNodePtr passedElement = NULL ; LIBXML_TEST_VERSION htmlDoc = htmlReadMemory ( buff , size , URL , encoding , options ) ; if ( htmlDoc == NULL ) { return NULL ; } P_BP_HTML_ANCHOR_DELIMITERS defaultDelims = NULL ; if ( delims == NULL ) { defaultDelims = ( P_BP_HTML_ANCHOR_DELIMITERS ) BP_calloc ( gc , NULL , sizeof ( BP_HTML_ANCHOR_DELIMITERS ) , 1 ) ; defaultDelims -> startDelimiter = BP_strdup ( gc , NULL , " \0 " ) ; defaultDelims -> sLen = 1 ; defaultDelims -> nextDelimiter = BP_strdup ( gc , NULL , " \0 " ) ; defaultDelims -> nLen = 1 ; defaultDelims -> valueDelimiter = BP_strdup ( gc , NULL , " \0 " ) ; defaultDelims -> vLen = 1 ; } else { defaultDelims = delims ; } if ( defaultDelims == NULL ) goto end ; extracted -> delims = defaultDelims ; rootElement = xmlDocGetRootElement ( htmlDoc ) ; passedElement = rootElement ; anchors = HTMLParseAnchors ( passedElement , defaultDelims , gc , extracted ) ; BP_GarbageCollectorLogChunk ( gc , " " , anchors , 4 , BP_GARBAGE_COLLECTOR_DATA_ALLOC_CALLOC ) ; extracted -> anchors = anchors ; passedElement = rootElement ; forms = HTMLParseForms ( rootElement , extracted ) ; BP_GarbageCollectorLogChunk ( gc , " " , ( void * ) forms , 4 , BP_GARBAGE_COLLECTOR_DATA_ALLOC_CALLOC ) ; extracted -> forms = forms ; if ( extracted -> forms ) { HTMLParseFormSecondaryData ( extracted -> forms , extracted ) ; } if ( delims != NULL ) defaultDelims = NULL ; end : xmlFreeDoc ( htmlDoc ) ; extracted -> gc = gc ; return extracted ; }
Will decode a retrieved html buffer into a DOM parsable structure.
Will decode a retrieved html buffer into a DOM parsable structure .
// ensure we have a buffer and size // create local pointers (extracted is final structure ptr) // create new linkl // set reference to the http session used if desired // declare anchors // declare forms // set local garbage collector // enable garbage collection // Libxml html parser pointers // could cause problems later, investigate // read memory // arbitary delimiter pointer // create default delimiters if pointer to struct has not been passed // default delimiter set // set default asp/php style delimiters // switch value of passed delims parameter into the default delimiter position // set delimiter pointer in extracted structure // Retrieve root element from the html parser // parse out the anchors // log the chunk in the garbage collector // set anchors in the main retrieval struct // set passed ellement // parse forms here // log the forms as a chunk itself //forms = NULL; // set forms in main extraction returning struct // Parse secondary form data if we've got forms to examine // reset the default delims position to null if a valid // delimiter parameter was passed. (to prevent bpfree'ing) //------------------------------------------------ // Destroy xml parsing // set garbage collector in extraction structure // return the extracted form