[sword-devel] Best way to work with Genbooks

Klaus R. H. Walther Klaus.R.H.Walther at gmx.de
Thu Apr 4 11:19:56 EDT 2024


Hi David,

I don't know the classes you are using, but maybe you have to do
something like this:

     if(treeKey->firstChild()) {
         do {
             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);
             }
         }
         while(treeKey->nextSibling());

         treeKey->parent();
     }

Klaus


Am 04.04.2024 um 16:57 schrieb David "Judah's Shadow" Blue:
> 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();
>      }
> }
>
>
> _______________________________________________
> sword-devel mailing list: sword-devel at crosswire.org
> http://crosswire.org/mailman/listinfo/sword-devel
> Instructions to unsubscribe/change your settings at above page


More information about the sword-devel mailing list