Question

I wrote a function to prevent duplication in XML file:

xmlNodePtr node;
xmlChar *position;
gchar buf_position[G_ASCII_DTOSTR_BUF_SIZE];

g_ascii_formatd( buf_position, sizeof(buf_position), "%g", point->x);

/* Remove duplicates */
for (node = renderer-root->children; node; node = node->next) {
if ((XML_ELEMENT_NODE == node->type) && xmlStrEqual((const xmlChar *)"point", node->name)) {
    buf_position = xmlGetProp(node, (const xmlChar *)"position");
    if (xmlStrEqual((const xmlChar *)position, (const xmlChar *)buf_position) {
        if(buf_position) xmlFree(buf_position);
            return;
    }
if(propx) xmlFree(propx);

Similar to this I want to write a function that compare each xmlChar position and see if them are in numeric and consecutive order (start with "0").

Example valid XML:

<point position="0"/>
<point position="1"/>
<point position="2"/>
.....................

Example invalid XML:

<point position="0"/>
<point position="5"/> /* need function to return */
<point position="2"/>
.....................
Was it helpful?

Solution

To check for monotonicity, remember the last value and require the current value to be greater. Taking your code as template:

xmlNodePtr node;
double last_position = -1;

for (node = renderer-root->children; node; node = node->next) {
if ((XML_ELEMENT_NODE == node->type) && xmlStrEqual((const xmlChar *)"point", node->name)) {
    xmlChar *position_str = xmlGetProp(node, (const xmlChar *)"position");
    double position = strtod(position_str, NULL);
    xmlFree(position_str);
    if (position <= last_position) {
      /* position out of order */
      return;
    }
 ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top