qint32 FiffDirTree::make_dir_tree(FiffStream* p_pStream, QList<FiffDirEntry>& p_Dir, FiffDirTree& p_Tree, qint32 start)
{
//    if (p_pTree != NULL)
//        delete p_pTree;
    p_Tree.clear();

    FiffTag::SPtr t_pTag;

    qint32 block;
    if(p_Dir[start].kind == FIFF_BLOCK_START)
    {
        FiffTag::read_tag(p_pStream, t_pTag, p_Dir[start].pos);
        block = *t_pTag->toInt();
    }
    else
    {
        block = 0;
    }

//    qDebug() << "start { " << p_pTree->block;

    qint32 current = start;

    p_Tree.block = block;
    p_Tree.nent = 0;
    p_Tree.nchild = 0;

    while (current < p_Dir.size())
    {
        if (p_Dir[current].kind == FIFF_BLOCK_START)
        {
            if (current != start)
            {
                FiffDirTree t_ChildTree;
                current = FiffDirTree::make_dir_tree(p_pStream,p_Dir,t_ChildTree, current);
                ++p_Tree.nchild;
                p_Tree.children.append(t_ChildTree);
            }
        }
        else if(p_Dir[current].kind == FIFF_BLOCK_END)
        {
            FiffTag::read_tag(p_pStream, t_pTag, p_Dir[start].pos);
            if (*t_pTag->toInt() == p_Tree.block)
                break;
        }
        else
        {
            ++p_Tree.nent;
            p_Tree.dir.append(p_Dir[current]);

            //
            //  Add the id information if available
            //
            if (block == 0)
            {
                if (p_Dir[current].kind == FIFF_FILE_ID)
                {
                    FiffTag::read_tag(p_pStream, t_pTag, p_Dir[current].pos);
                    p_Tree.id = t_pTag->toFiffID();
                }
            }
            else
            {
                if (p_Dir[current].kind == FIFF_BLOCK_ID)
                {
                    FiffTag::read_tag(p_pStream, t_pTag, p_Dir[current].pos);
                    p_Tree.id = t_pTag->toFiffID();
                }
                else if (p_Dir[current].kind == FIFF_PARENT_BLOCK_ID)
                {
                    FiffTag::read_tag(p_pStream, t_pTag, p_Dir[current].pos);
                    p_Tree.parent_id = t_pTag->toFiffID();
                }
            }
        }
        ++current;
    }

    //
    // Eliminate the empty directory
    //
    if(p_Tree.nent == 0)
        p_Tree.dir.clear();

//    qDebug() << "block =" << p_pTree->block << "nent =" << p_pTree->nent << "nchild =" << p_pTree->nchild;
//    qDebug() << "end } " << block;

    return current;
}