bool LocalizationSystem::SaveToYamlFile(const StringFile* stringFile)
{
	if (!stringFile)
	{
		return false;
	}

	YamlParser* parser = YamlParser::Create();
	if (!parser)
	{
		return false;
	}
	
	YamlNode *node = new YamlNode(YamlNode::TYPE_MAP);
	for (Map<WideString, WideString>::const_iterator iter = stringFile->strings.begin();
		 iter != stringFile->strings.end(); iter ++)
	{
		node->Add(WStringToString(iter->first), iter->second);
	}
	
	bool result = parser->SaveStringsList(stringFile->pathName, node);

	SafeDelete(node);
	SafeRelease(parser);
	return result;
}
Exemplo n.º 2
0
bool YamlParser::Parse(YamlDataHolder * dataHolder)
{
	yaml_parser_t parser;
	yaml_event_t event;
	
	int done = 0;
	
	/* Create the Parser object. */
	yaml_parser_initialize(&parser);
	
	yaml_parser_set_encoding(&parser, YAML_UTF8_ENCODING);
	
	/* Set a string input. */
	//yaml_parser_set_input_string(&parser, (const unsigned char*)pathName.c_str(), pathName.length());
		
	yaml_parser_set_input(&parser, read_handler, dataHolder);

	String lastMapKey;
	bool isKeyPresent = false;

	/* Read the event sequence. */
	while (!done) 
	{
		
		/* Get the next event. */
		if (!yaml_parser_parse(&parser, &event))
		{
			Logger::Error("[YamlParser::Parse] error: type: %d %s line: %d pos: %d", parser.error, parser.problem, parser.problem_mark.line, parser.problem_mark.column);
			break;
		}
		
		switch(event.type)
		{
		case YAML_ALIAS_EVENT:
			Logger::FrameworkDebug("[YamlParser::Parse] alias: %s", event.data.alias.anchor);
			break;
		
		case YAML_SCALAR_EVENT:
			{
				String scalarValue = (const char*)event.data.scalar.value;

				if (objectStack.empty())
				{
					YamlNode * node = YamlNode::CreateStringNode();
					node->Set(scalarValue);
					rootObject = node;
				}
				else
				{
					YamlNode * topContainer = objectStack.top();
					DVASSERT(topContainer->GetType() != YamlNode::TYPE_STRING);
					if (topContainer->GetType() == YamlNode::TYPE_MAP)
					{
						if (!isKeyPresent)
						{
							lastMapKey = scalarValue;
						}
						else
						{
							topContainer->Add(lastMapKey, scalarValue);
						}
						isKeyPresent = !isKeyPresent;
					}
					else if (topContainer->GetType() == YamlNode::TYPE_ARRAY)
					{
						topContainer->Add(scalarValue);
					}
				}
			}
			break;
		
		case YAML_DOCUMENT_START_EVENT:
			//Logger::FrameworkDebug("document start:");
			break;
		
		case YAML_DOCUMENT_END_EVENT:
			//Logger::FrameworkDebug("document end:");
			break;

		case YAML_SEQUENCE_START_EVENT:
			{
				YamlNode * node = YamlNode::CreateArrayNode();
				if (objectStack.empty())
				{
					rootObject = node;
				}
				else
				{
					YamlNode * topContainer = objectStack.top();
					DVASSERT(topContainer->GetType() != YamlNode::TYPE_STRING);
					if (topContainer->GetType() == YamlNode::TYPE_MAP)
					{
						DVASSERT(isKeyPresent);
						topContainer->AddNodeToMap(lastMapKey, node);
						isKeyPresent = false;
					}
					else if (topContainer->GetType() == YamlNode::TYPE_ARRAY)
					{
						topContainer->AddNodeToArray(node);
					}
				}
				objectStack.push(node);
			}break;
				
		case YAML_SEQUENCE_END_EVENT:
			{
				objectStack.pop();
			}break;
		
		case YAML_MAPPING_START_EVENT:
			{
				YamlNode * node = YamlNode::CreateMapNode();
				if (objectStack.empty())
				{
					rootObject = node;
				}
				else
				{
					YamlNode * topContainer = objectStack.top();
					if (topContainer->GetType() == YamlNode::TYPE_MAP)
					{
						DVASSERT(isKeyPresent);
						topContainer->AddNodeToMap(lastMapKey, node);
						isKeyPresent = false;
					}
					else if (topContainer->GetType() == YamlNode::TYPE_ARRAY)
					{
						topContainer->AddNodeToArray(node);
					}
				}
				objectStack.push(node);
			}
			break;
				
		case YAML_MAPPING_END_EVENT:
			{
				objectStack.pop();
			}
			break;
        default:
            break;
		};

		/* Are we finished? */
		done = (event.type == YAML_STREAM_END_EVENT);
		
		/* The application is responsible for destroying the event object. */
		yaml_event_delete(&event);
		
	}

	/* Destroy the Parser object. */
	yaml_parser_delete(&parser);

    DVASSERT(objectStack.size() == 0);

    return true;
}