text
stringlengths
0
357
const char* dc_attr_find(char** attr, const char* key)
{
if (attr && key) {
int i = 0;
while (attr[i] && strcmp(key, attr[i])) {
i += 2;
}
if (attr[i]) {
return attr[i + 1];
}
}
return NULL;
}
void dc_saxparser_init(dc_saxparser_t* saxparser, void* userdata)
{
saxparser->userdata = userdata;
saxparser->starttag_cb = def_starttag_cb;
saxparser->endtag_cb = def_endtag_cb;
saxparser->text_cb = def_text_cb;
}
void dc_saxparser_set_tag_handler(dc_saxparser_t* saxparser, dc_saxparser_starttag_cb_t starttag_cb, dc_saxparser_endtag_cb_t endtag_cb)
{
if (saxparser==NULL) {
return;
}
saxparser->starttag_cb = starttag_cb? starttag_cb : def_starttag_cb;
saxparser->endtag_cb = endtag_cb? endtag_cb : def_endtag_cb;
}
void dc_saxparser_set_text_handler (dc_saxparser_t* saxparser, dc_saxparser_text_cb_t text_cb)
{
if (saxparser==NULL) {
return;
}
saxparser->text_cb = text_cb? text_cb : def_text_cb;
}
void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
{
char bak = 0;
char* buf_start = NULL;
char* last_text_start = NULL;
char* p = NULL;
#define MAX_ATTR 100 /* attributes per tag - a fixed border here is a security feature, not a limit */
char* attr[(MAX_ATTR+1)*2]; /* attributes as key/value pairs, +1 for terminating the list */
int free_attr[MAX_ATTR]; /* free the value at attr[i*2+1]? */
attr[0] = NULL; /* null-terminate list, this also terminates "free_values" */
if (saxparser==NULL) {
return;
}
buf_start = dc_strdup(buf_start__); /* we make a copy as we can easily null-terminate tag names and attributes "in place" */
last_text_start = buf_start;
p = buf_start;
while (*p)
{
if (*p=='<')
{
call_text_cb(saxparser, last_text_start, p - last_text_start, '&'); /* flush pending text */
p++;
if (strncmp(p, "!--", 3)==0)
{
/* skip <!-- ... --> comment
**************************************************************/
p = strstr(p, "-->");
if (p==NULL) { goto cleanup; }
p += 3;
}
else if (strncmp(p, "![CDATA[", 8)==0)
{
/* process <![CDATA[ ... ]]> text
**************************************************************/
char* text_beg = p + 8;
if ((p = strstr(p, "]]>"))!=NULL) /* `]]>` itself is not allowed in CDATA and must be escaped by dividing into two CDATA parts */ {
call_text_cb(saxparser, text_beg, p-text_beg, 'c');
p += 3;
}
else {
call_text_cb(saxparser, text_beg, strlen(text_beg), 'c'); /* CDATA not closed, add all remaining text */
goto cleanup;
}
}
else if (strncmp(p, "!DOCTYPE", 8)==0)