eina: add eina_simple_xml_attribute_w3c_parse to parse attribute value.

This commit is contained in:
Cedric BAIL 2015-04-03 16:22:29 +02:00
parent c1a189bf10
commit 78bf41b223
2 changed files with 74 additions and 0 deletions

View File

@ -556,6 +556,63 @@ eina_simple_xml_attributes_parse(const char *buf, unsigned buflen, Eina_Simple_X
return EINA_TRUE;
}
EAPI Eina_Bool
eina_simple_xml_attribute_w3c_parse(const char *buf, Eina_Simple_XML_Attribute_Cb func, const void *data)
{
const char *end;
char *key;
char *val;
char *next;
if (!buf) return EINA_FALSE;
end = buf + strlen(buf);
key = alloca(end - buf + 1);
val = alloca(end - buf + 1);
if (buf == end) return EINA_TRUE;
do
{
char *sep = strchr(buf, ':');
next = strchr(buf, ';');
key[0] = '\0';
val[0] = '\0';
if (next == NULL && sep != NULL)
{
memcpy(key, buf, sep - buf);
key[sep - buf] = '\0';
memcpy(val, sep + 1, end - sep - 1);
val[end - sep - 1] = '\0';
}
else if (sep < next && sep != NULL)
{
memcpy(key, buf, sep - buf);
key[sep - buf] = '\0';
memcpy(val, sep + 1, next - sep - 1);
val[next - sep - 1] = '\0';
}
else if (next)
{
memcpy(key, buf, next - buf);
key[next - buf] = '\0';
}
if (key[0])
if (!func((void*) data, key, val))
return EINA_FALSE;
buf = next + 1;
}
while (next != NULL);
return EINA_TRUE;
}
/* Node loader *************************************************************/
EAPI Eina_Simple_XML_Attribute *

View File

@ -277,6 +277,23 @@ EAPI const char * eina_simple_xml_tag_attributes_find(const char *buf, unsigned
EAPI Eina_Bool eina_simple_xml_attributes_parse(const char *buf, unsigned buflen,
Eina_Simple_XML_Attribute_Cb func, const void *data);
/**
* Given a buffer with the xml value of an attributes, parse them to key:value pairs.
*
* @param buf the input string. Need to contain \0 terminator.
* @param func what to call back while parse to do some action. The
* first parameter is the given user @a data, the second is the
* key (null-terminated) and the last is the value (null
* terminated). These strings should not be modified and
* reference is just valid until the function return.
* @param data data to pass to the callback function.
*
* @return #EINA_TRUE on success or #EINA_FALSE if it was aborted by user or
* parsing error.
*/
EAPI Eina_Bool
eina_simple_xml_attribute_w3c_parse(const char *buf, Eina_Simple_XML_Attribute_Cb func, const void *data);
/**
* Create (and append) new attribute to tag.
*