otml node cast fixes

This commit is contained in:
Eduardo Bart 2012-02-01 22:38:42 -02:00
parent 34f9ecb6c8
commit e88774728e
1 changed files with 27 additions and 17 deletions

View File

@ -215,7 +215,11 @@ bool luavalue_cast(int index, Size& size)
void push_otml_subnode_luavalue(const OTMLNodePtr& node) void push_otml_subnode_luavalue(const OTMLNodePtr& node)
{ {
if(node->hasValue()) { if(node->hasValue()) {
g_lua.pushString(node->value()); // convert boolean types
if(node->value() == "true" || node->value() == "false")
g_lua.pushBoolean(node->value<bool>());
else
g_lua.pushString(node->value());
} else if(node->hasChildren()) { } else if(node->hasChildren()) {
g_lua.newTable(); g_lua.newTable();
bool pushedChild = false; bool pushedChild = false;
@ -225,6 +229,7 @@ void push_otml_subnode_luavalue(const OTMLNodePtr& node)
if(!g_lua.isNil()) { if(!g_lua.isNil()) {
if(cnode->isUnique()) { if(cnode->isUnique()) {
g_lua.pushString(cnode->tag()); g_lua.pushString(cnode->tag());
g_lua.insert(-2);
g_lua.rawSet(); g_lua.rawSet();
} else } else
g_lua.rawSeti(currentIndex++); g_lua.rawSeti(currentIndex++);
@ -242,15 +247,18 @@ void push_otml_subnode_luavalue(const OTMLNodePtr& node)
void push_luavalue(const OTMLNodePtr& node) void push_luavalue(const OTMLNodePtr& node)
{ {
g_lua.newTable(); if(node) {
int currentIndex = 1; g_lua.newTable();
for(const OTMLNodePtr& cnode : node->children()) { int currentIndex = 1;
push_otml_subnode_luavalue(cnode); for(const OTMLNodePtr& cnode : node->children()) {
if(cnode->isUnique()) { push_otml_subnode_luavalue(cnode);
g_lua.setField(cnode->tag()); if(cnode->isUnique()) {
} else g_lua.setField(cnode->tag());
g_lua.rawSeti(currentIndex++); } else
} g_lua.rawSeti(currentIndex++);
}
} else
g_lua.pushNil();
} }
bool luavalue_cast(int index, OTMLNodePtr& node) bool luavalue_cast(int index, OTMLNodePtr& node)
@ -260,30 +268,32 @@ bool luavalue_cast(int index, OTMLNodePtr& node)
if(g_lua.isTable(index)) { if(g_lua.isTable(index)) {
g_lua.pushNil(); g_lua.pushNil();
while(g_lua.next(index < 0 ? index-1 : index)) { while(g_lua.next(index < 0 ? index-1 : index)) {
bool listItem = false;
std::string cnodeName; std::string cnodeName;
if(g_lua.isNumber(-2)) if(!g_lua.isNumber(-2))
listItem = true;
else
cnodeName = g_lua.toString(-2); cnodeName = g_lua.toString(-2);
if(g_lua.isTable()) { if(g_lua.isTable()) {
OTMLNodePtr cnode; OTMLNodePtr cnode;
if(luavalue_cast(-1, cnode)) { if(luavalue_cast(-1, cnode)) {
if(listItem) if(cnodeName.empty())
node->setUnique(false); node->setUnique(false);
else else
cnode->setTag(cnodeName); cnode->setTag(cnodeName);
node->addChild(cnode); node->addChild(cnode);
} }
} else { } else {
std::string value = g_lua.toString(); std::string value;
if(listItem) if(g_lua.isBoolean())
value = Fw::unsafeCast<std::string>(g_lua.toBoolean());
else
value = g_lua.toString();
if(cnodeName.empty())
node->writeIn(value); node->writeIn(value);
else else
node->writeAt(cnodeName, value); node->writeAt(cnodeName, value);
} }
g_lua.pop(); g_lua.pop();
} }
dump << node->emit();
return true; return true;
} }
return false; return false;