GithubHelp home page GithubHelp logo

Comments (6)

matthieu-labas avatar matthieu-labas commented on August 22, 2024 1

I'm still polishing v4.3.0 but here is what the final XMLNode_insert_child() will look like, if you want to use it asap:

#define CHECK_NODE(node,ret) if ((node) == NULL || (node)->init_value != XML_INIT_DONE) return (ret)

int XMLNode_insert_child(XMLNode* node, XMLNode* child, int index)
{
	int i, j;

	CHECK_NODE(node, -1);

	/* We could process cases "first" and "last" in an optimized way, but we prefer readability to (micro-)optimization */
	if (index < 0) /* Before first => first */
		index = 0;
	if (index >= node->n_children) /* After last => last */
		index = node->n_children - 1;

	for (i = 0; i < node->n_children; i++) {
		if (!node->children[i]->active || index-- > 0)
			continue;
		/* Insert it here, at 'i' */
		if (_add_node(&node->children, &node->n_children, child) >= 0) {
			node->tag_type = TAG_FATHER;
			child->father = node;
			/* Erase 'child', which is the last node ('n_children' has been incremented by '_add_node()') */
			for (j = node->n_children - 1; j >= i; j--)
				node->children[j] = node->children[j-1];
			node->children[i] = child; /* Set it */
			return true;
		} else
			return false;
	}

	return false; /* Oops! */
}

from sxmlc.

matthieu-labas avatar matthieu-labas commented on August 22, 2024 1

v4.3.0 pushed.
Some more work is coming regarding documentation (tentative switching to Doxygen) and more thorough unit testing.

from sxmlc.

damian-m-g avatar damian-m-g commented on August 22, 2024 1

Wow, this was very fast, and I'll be using it on next days. Thanks!

from sxmlc.

matthieu-labas avatar matthieu-labas commented on August 22, 2024

Good idea! I will implement it in the upcoming v4.3.0.

In the meantime, if you want that type of functionality, you should directly manipulate the node->children array (with XMLNode_add_child() to allocate space, then move all nodes one by one in a loop):

XMLNode_add_child(node, child);
memmove(node->children[i+1], node->children[i], node->n_children - i - 1);
node->children[i] = child;

from sxmlc.

damian-m-g avatar damian-m-g commented on August 22, 2024

This are very good news, and thanks for the explanation on the workaround.

I'll be looking forward for this new version! In the meantime, I'll make use of the workaround. Cheers!

from sxmlc.

damian-m-g avatar damian-m-g commented on August 22, 2024

This is great Matthieu, I'll be using this code these days. I'll be looking forward for 4.3.0.

from sxmlc.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.