[sword-devel] Best way to work with Genbooks

David "Judah's Shadow" Blue yudahsshadow at gmx.com
Thu Apr 4 10:57:42 EDT 2024


On Tuesday, March 5, 2024 2:05:39 PM EDT Troy A. Griffitts wrote:
> Hi David,
> 
> Traversing a tree without recursion usually required keeping a stack you can
> push onto and pop when you hit a leaf.  This is all handled for you in the
> call stack when you use recursion.  If you really do want to avoid
> recursion, I would recommend having a look at general strategies for tree
> traversal without recursion. Something like:
> 
> https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
> 
> With recursion, our TreeIndexUtil printTree function serves as a great
> example:
> 
> https://crosswire.org/svn/sword/trunk/utilities/treeidxutil.cpp

So between this example, and the example on the wiki, I've got a working 
function with recursion, however it seems to skip the first node of each 
branch. So, for instance, in ABS_Essay_GoodSam_SWB it misses the very first 
node in the module /About and goes straight into /Level1 /Level2 /Level3 
(that's how I found this problem to begin with). On other modules it misses 
that very first node (Usually an introduction of some sort) and then on the 
child nodes it skips, say, chapter 1 and starts with chapter 2. I am not 
certain what I'm doing wrong to miss that first node. Here's my code as it 
currently stands.

void Genbook::walkTree(sword::TreeKey *treeKey) {

    if(!treeKey) {
        treeKey = dynamic_cast<sword::TreeKey*>(this->mod->getKey());
    }

    this->toc += treeKey->getText();

    if(treeKey->firstChild()) {
        while(treeKey->nextSibling()) {
            this->toc += treeKey->getText();
            //Add a space around the new line for the tokenizer to split on
            this->toc += "\n ";
            if(treeKey->hasChildren()) {
                this->walkTree(treeKey);
            }
        }
        treeKey->parent();
    }
}




More information about the sword-devel mailing list