bool CCDictionarySaver::saveDictionary(CCDictionary* pDict, xmlNodePtr node)
{
    do
    {
        xmlNodePtr dict_node = xmlNewNode(NULL, DICT_NODE_DICT);
        CC_BREAK_IF(!dict_node);
        
        CC_BREAK_IF(!xmlAddChild(node, dict_node));
        
        CCDictElement* pElem;
        CCDICT_FOREACH(pDict, pElem)
        {
            saveKey(pElem->getStrKey(), dict_node);
            
            CCObject* obj = pElem->getObject();

            if (dynamic_cast<CCString*>(obj))
            {
                CCString* s = dynamic_cast<CCString*>(obj);
                CC_BREAK_IF(!saveString(s->getCString(), dict_node));
            }
            else if (dynamic_cast<CCDictionary*>(obj))
            {
                CC_BREAK_IF(!saveDictionary(dynamic_cast<CCDictionary*>(obj), dict_node));
            }
            else if (dynamic_cast<CCArray*>(obj))
            {
                CC_BREAK_IF(!saveArray(dynamic_cast<CCArray*>(obj), dict_node));
            }
        }
        return true;
    } while (false);
bool CCDictionarySaver::saveArray(CCArray* pArray, xmlNodePtr node)
{
    do
    {
        xmlNodePtr array_node = xmlNewNode(NULL, DICT_NODE_ARRAY);
        CC_BREAK_IF(!array_node);
        
        CC_BREAK_IF(!xmlAddChild(node, array_node));
        
        CCObject* obj;
        CCARRAY_FOREACH(pArray, obj)
        {
            if (dynamic_cast<CCString*>(obj))
            {
                CCString* s = dynamic_cast<CCString*>(obj);
                CC_BREAK_IF(!saveString(s->getCString(), array_node));
            }
            else if (dynamic_cast<CCDictionary*>(obj))
            {
                CC_BREAK_IF(!saveDictionary(dynamic_cast<CCDictionary*>(obj), array_node));
            }
            else if (dynamic_cast<CCArray*>(obj))
            {
                CC_BREAK_IF(!saveArray(dynamic_cast<CCArray*>(obj), array_node));
            }
        }
        return true;
    } while (false);
    
    return false;
}
bool CCDictionarySaver::save2Plist(CCDictionary* pDict, const char* filename)
{
    do
    {
        xmlDocPtr xmlDoc = xmlNewDoc(BAD_CAST"1.0");
        CC_BREAK_IF(!xmlDoc);
        CC_BREAK_IF(!pDict);
        CC_BREAK_IF(!filename);
        
        xmlNodePtr root = xmlNewNode(NULL, ROOT_NODE_NAME);
        CC_BREAK_IF(!root);
        
        xmlNewProp(root, ROOT_PLIST_PROP, ROOT_PLIST_VERSION);
        
        CC_BREAK_IF(!saveDictionary(pDict, root));
        
        // not sure how to check the return value
        xmlDocSetRootElement(xmlDoc, root);
        
        CC_BREAK_IF(!xmlSaveFile(filename, xmlDoc));
        
        xmlFreeDoc(xmlDoc);
        return true;
    } while (false);
    
    return false;
}
Example #4
0
void MainWindow::saveLevelPlist(QString filename)
{
    QDomDocument doc("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
    QDomElement root = doc.createElement("plist");
    doc.appendChild(root);

    // Temporarily add game objects back to level plist
    QList<QVariant> tempList;
    for(int i = 0; i < levelObjects.size(); i++)
    {
        tempList.append(levelObjects.at(i));
    }
    levelPlist.insert("game_objects", tempList);

    // Save levelPlist
    root.appendChild(saveDictionary(levelPlist, doc));

    QFile data(filename);
    if (data.open(QFile::WriteOnly | QFile::Truncate))
    {
        QTextStream out(&data);
        out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
        //out << "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" << endl;
        doc.save(out, 1);
    }

    // Remove game objects from level plist again
    levelPlist.remove("game_objects");

}
Example #5
0
QDomElement MainWindow::saveDictionary(QMap<QString, QVariant> inputDict, QDomDocument doc)
{
    QDomElement rootDict = doc.createElement("dict");

    QMap<QString, QVariant>::const_iterator i;
    for (i = inputDict.constBegin(); i != inputDict.constEnd(); ++i)
    {
        QDomElement tag = doc.createElement("key");
        rootDict.appendChild(tag);

        QDomText t = doc.createTextNode(i.key());
        tag.appendChild(t);

        if(i.value().toInt() != 0)
        {
            tag = doc.createElement("integer");
            rootDict.appendChild(tag);
            t = doc.createTextNode(i.value().toString());
            tag.appendChild(t);
        }
        else if(i.value().toFloat() != 0)
        {
            tag = doc.createElement("real");
            rootDict.appendChild(tag);
            t = doc.createTextNode(i.value().toString());
            tag.appendChild(t);
        }
        else if(i.value().type() == QVariant::List)
        {
            rootDict.appendChild(saveArray(i.value().toList(), doc));
        }
        else if(i.value().type() == QVariant::Map)
        {
            rootDict.appendChild(saveDictionary(i.value().toMap(), doc));
        }
        else // Assuming it's a string
        {
            tag = doc.createElement("string");
            rootDict.appendChild(tag);
            t = doc.createTextNode(i.value().toString());
            tag.appendChild(t);
        }
    }

    return rootDict;
}
Example #6
0
QDomElement MainWindow::saveArray(QList<QVariant> list, QDomDocument doc)
{
    QDomElement rootArray = doc.createElement("array");

    for(int i = 0; i < list.count(); i++)
    {
        if(list.at(i).type() == QVariant::Map)
        {
            rootArray.appendChild(saveDictionary(list.at(i).toMap(), doc));
        }
        else if(list.at(i).type() == QVariant::List)
        {
            rootArray.appendChild(saveArray(list.at(i).toList(), doc));
        }
        else if(list.at(i).toInt() != 0)
        {
            QDomElement tag = doc.createElement("integer");
            rootArray.appendChild(tag);
            QDomText t = doc.createTextNode(list.at(i).toString());
            tag.appendChild(t);
        }
        else if(list.at(i).toFloat() != 0)
        {
            QDomElement tag = doc.createElement("real");
            rootArray.appendChild(tag);
            QDomText t = doc.createTextNode(list.at(i).toString());
            tag.appendChild(t);
        }
        else
        {
            QDomElement tag = doc.createElement("string");
            rootArray.appendChild(tag);
            QDomText t = doc.createTextNode(list.at(i).toString());
            tag.appendChild(t);
        }
    }

    return rootArray;
}