按数字顺序比较xmlChar

时间:2013-12-06 14:22:08

标签: c libxml2

我写了一个函数来防止XML文件中出现重复:

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);

与此类似,我想编写一个比较每个xmlChar位置的函数,看看它们是否为数字和连续顺序(以“0”开头)。

有效XML示例:

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

示例无效的XML:

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

1 个答案:

答案 0 :(得分:0)

要检查单调性,请记住最后一个值并要求当前值更大。将您的代码作为模板:

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;
    }
 ...