Exemplo n.º 1
0
void NxsReader::BlockReadHook(const NxsString &currBlockName, NxsBlock *currentBlock, NxsToken * token)
	{
	VecBlockPtr implied = currentBlock->GetImpliedBlocks();
	for (VecBlockPtr::iterator impIt = implied.begin(); impIt != implied.end(); ++impIt)
		{
		NxsBlock * nb = *impIt;
		NCL_ASSERT(nb);
		NxsString impID = nb->GetID();
		bool storeBlock = true;
		if (destroyRepeatedTaxaBlocks && impID.EqualsCaseInsensitive("TAXA"))
			{
			NxsTaxaBlockAPI * oldTB = this->GetOriginalTaxaBlock((NxsTaxaBlockAPI *) nb);
			if (oldTB)
				{
				storeBlock = ! currentBlock->SwapEquivalentTaxaBlock(oldTB);
				const std::string altTitle = nb->GetTitle();
				this->RegisterAltTitle(oldTB, altTitle);
				if (!storeBlock)
					{
					delete nb;
					}
				
				}
			}
		if (storeBlock) 
			{
			std::cerr << "storing implied block: " << impID << std::endl;
			this->AddBlockToUsedBlockList(impID, nb, token);
			}
		}
	std::cerr << "storing read block: " << currentBlock->GetID() << std::endl;
	this->AddBlockToUsedBlockList(currBlockName, currentBlock, token);
	}
Exemplo n.º 2
0
/*! Called internally when the NxsReader has found the correct NxsBlock to read
    a block in a file.

    `token` will be at the block ID.
    `currBlockName` will be the block ID as a string.
    `currentBlock` will be the block reader to be used
    `sourceOfBlock` is the factory  that created the block (or 0L). If sourceOfBlock
        is not NULL then it will be alerted if the block is skipped (BlockSkipped() method)
        or there was an error in the read (BlockError() method). The factory is expected
        to delete the block instances in these cases (NxsReader will not refer to those
        instances again).



    The following steps occur:
        - the EnteringBlock hook is called (if it returns false, the block will be skipped by calling
            NxsReader::SkippingBlock
        - NxsBlock::Reset() is called on the reader block
        - NxsBlock::Read() method of the reader block is called
        - If an exception is generated, the NexusError is called.
        - If no exception is generated by Read then the block is processed:
            - if NxsReader::cullIdenticalTaxaBlocks(true) has been called before Execute and this
                is a repeated TAXA block, the block will be deleted.
            - the BlockReadHook() will store all of the implied blocks
                (by calling NxsBlock::GetImpliedBlocks()) and the block itself.
            - if one of the implied blocks is a repeated TAXA block and
                NxsReader::cullIdenticalTaxaBlocks(true) has been called, then
                the blocks NxsBlock::SwapEquivalentTaxaBlock() method will determine
                whether or not the duplicate taxa block can be deleted.
            - each stored block will generate a call to NxsReader::AddBlockToUsedBlockList()
        - ExitingBlock() is called
        - PostBlockReadingHook() is called
 */
bool NxsReader::ExecuteBlock(NxsToken &token, const NxsString &currBlockName, NxsBlock *currentBlock, NxsBlockFactory * sourceOfBlock)
{
    if (!EnteringBlock(currBlockName))
    {
        SkippingBlock(currBlockName);
        if (sourceOfBlock)
        {
            sourceOfBlock->BlockSkipped(currentBlock);
        }
        if (!ReadUntilEndblock(token, currBlockName))
        {
            token.SetBlockName(0L);
            token.SetEOFAllowed(true);
            return false;
        }
        return true;
    }
    this->RemoveBlockFromUsedBlockList(currentBlock);
    currentBlock->Reset();
    // We need to back up currentBlock, because the Read statement might trigger
    // a recursive call to Execute (if the block contains instructions to execute
    // another file, then the same NxsReader object may be used and any member fields (e.g. currentBlock)
    //	could be trashed.
    //
    bool eofFound = false;
    try
    {
        try
        {
            currentBlock->Read(token);
        }
        catch (NxsX_UnexpectedEOF &eofx)
        {
            if (!currentBlock->TolerateEOFInBlock())
            {
                throw eofx;
            }
            NxsString m;
            m << "Unexpected End of file in " << currBlockName << "block";
            currentBlock->WarnDangerousContent(m, token);
            eofFound = true;
        }
        if (destroyRepeatedTaxaBlocks && currBlockName.EqualsCaseInsensitive("TAXA"))
        {
            NxsTaxaBlockAPI * oldTB = this->GetOriginalTaxaBlock((NxsTaxaBlockAPI *) currentBlock);
            if (oldTB)
            {
                const std::string altTitle = currentBlock->GetTitle();
                this->RegisterAltTitle(oldTB, altTitle);
                if (sourceOfBlock)
                {
                    sourceOfBlock->BlockError(currentBlock);
                }
                return true;
            }
        }
        BlockReadHook(currBlockName, currentBlock, &token);
    }
    catch (NxsException &x)
    {
        NxsString m;
        if (currentBlock->errormsg.length() > 0)
        {
            m = currentBlock->errormsg;
        }
        else
        {
            m = x.msg;
        }
        currentBlock->Reset();
        if (sourceOfBlock != 0)
        {

            sourceOfBlock->BlockError(currentBlock);
        }
        else
        {

            token.SetBlockName(0L);
        }
        token.SetEOFAllowed(true);
        currentBlock = NULL;
        NexusError(m, x.pos, x.line, x.col);
        return false;
    }       // catch (NxsException x)
    ExitingBlock(currBlockName);
    PostBlockReadingHook(*currentBlock);
    return !eofFound;
}