示例#1
0
文件: nbt.cpp 项目: tvolpes/MCMapper3
CTag* CTagParent::getChildPath( std::string path, boost::int8_t type )
{
	std::vector<std::string> tokens;
	CTag *pNextTag;
	CTagParent *pCurrentParent;

	// Split into tokens
	boost::split( tokens, path, boost::is_any_of( "." ) );
	pCurrentParent = this;
	for( auto it = tokens.begin(); it != tokens.end(); it++ )
	{
		// Search for
		pNextTag = pCurrentParent->getChildName( (*it) );
		if( !pNextTag ) {
			std::cout << "Could not find tag \'" << path.c_str() << "\'" << std::endl;
			return 0;
		}
		// Check if we're at the end
		if( it == tokens.end()-1 ) {
			if( pNextTag->getId() == type )
				return pNextTag;
			else {
				std::cout << "Invalid type for tag specified by path \'" << path.c_str() << "\'" << std::endl;
				return 0;
			}
		}
		else if( pNextTag->isParent() )
			pCurrentParent = reinterpret_cast<CTagParent*>(pNextTag);
		else {
			std::cout << "Invalid path \'" << path.c_str() << "\'" << std::endl;
		}
	}

	return 0;
}
示例#2
0
文件: nbt.cpp 项目: tvolpes/MCMapper3
CTag* CNBTReader::readTag( InputStream &stream, size_t *pBytesRead, bool fullTag = true )
{
	boost::int8_t tagId;
	CTag *pTag;

	// Read the tag ID
	if( fullTag ) {
		stream.read( reinterpret_cast<char*>(&tagId), sizeof( tagId ) );
		(*pBytesRead) += sizeof( tagId );
	}
	else {
		_ASSERT_EXPR( !m_parentStack.empty(), L"can't create non-full tag when parent stack is empty" );
		_ASSERT_EXPR( m_parentStack.top()->getId() == TAGID_LIST, L"list not at top of parent stack" );
		tagId = reinterpret_cast<CTagList*>(m_parentStack.top())->getChildrenId();
	}

	if( stream.eof() )
		return 0;

	// Create the tag
	pTag = this->createTag( tagId );
	if( !pTag )
		return 0;

	// If it has no parents its a root tag
	if( m_parentStack.empty() )
		m_rootTags.push_back( pTag );
	// If not assign it to parent
	else
		m_parentStack.top()->addChild( pTag );

	// Check if its an end tag
	if( pTag->getId() == TAGID_END ) {
		if( m_parentStack.empty() ) {
			std::cout << "Failed: mismatched end tag" << std::endl;
			delete pTag;
			return 0;
		}
		m_parentStack.pop();
	}
	// Check if its a parent
	if( pTag->isParent() )
		m_parentStack.push( reinterpret_cast<CTagParent*>( pTag ) );

	// Read the tag
	pTag->read( stream, pBytesRead, fullTag );

	// Check if the list is full
	if( !m_parentStack.empty() ) {
		if( m_parentStack.top()->getId() == TAGID_LIST ) {
			if( m_parentStack.top()->getChildren().size() == reinterpret_cast<CTagList*>(m_parentStack.top())->getChildrenCount() )
				m_parentStack.pop(); // pop off the list
		}
	}

	/*if( pTag->isParent() )
		for( unsigned int i = 0; i < m_parentStack.size()-1; i++ )
			std::cout << "\t";
	else
		for( unsigned int i = 0; i < m_parentStack.size(); i++ )
			std::cout << "\t";
	std::cout << (int)pTag->getId() << "(" << pTag->getName().c_str() << ")" << std::endl;*/

	return pTag;
}