TreeT* createStaticTree() { TreeT *root=createNodeTree(10); root->left=createNodeTree(12); root->right=createNodeTree(15); root->left->left=createNodeTree(14); root->left->right=createNodeTree(11); return root; }
TreeT* Insert(TreeT *root, int k) { if(root==NULL) return createNodeTree(k); if(k < root->value) root->left=Insert(root->left,k); else root->right=Insert(root->right,k); return getBalance(root); }
// NodeTree Offset<NodeTree> FlatBuffersSerialize::createNodeTree(const tinyxml2::XMLElement *objectData, std::string classType) { std::string classname = classType.substr(0, classType.find("ObjectData")); CCLOG("classname = %s", classname.c_str()); std::string name = ""; Offset<Options> options; std::vector<Offset<NodeTree>> children; if (classname == "ProjectNode") { auto reader = ProjectNodeReader::getInstance(); options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder)); } else if (classname == "SimpleAudio") { auto reader = ComAudioReader::getInstance(); options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder)); } else { std::string readername = getGUIClassName(classname); readername.append("Reader"); NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername)); options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder)); } // children bool containChildrenElement = false; const tinyxml2::XMLElement* child = objectData->FirstChildElement(); while (child) { CCLOG("child name = %s", child->Name()); if (strcmp("Children", child->Name()) == 0) { containChildrenElement = true; break; } child = child->NextSiblingElement(); } if (containChildrenElement) { child = child->FirstChildElement(); CCLOG("element name = %s", child->Name()); while (child) { const tinyxml2::XMLAttribute* attribute = child->FirstAttribute(); bool bHasType = false; while (attribute) { std::string attriname = attribute->Name(); std::string value = attribute->Value(); if (attriname == "ctype") { children.push_back(createNodeTree(child, value)); bHasType = true; break; } attribute = attribute->Next(); } if(!bHasType) { children.push_back(createNodeTree(child, "NodeObjectData")); } child = child->NextSiblingElement(); } } // std::string customClassName = ""; const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute(); while (attribute) { std::string attriname = attribute->Name(); std::string value = attribute->Value(); if (attriname == "CustomClassName") { customClassName = value; break; } attribute = attribute->Next(); } return CreateNodeTree(*_builder, _builder->CreateString(classname), _builder->CreateVector(children), options, _builder->CreateString(customClassName)); }
std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(const std::string &xmlFileName, const std::string &flatbuffersFileName) { std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName).c_str(); // xml read if (!FileUtils::getInstance()->isFileExist(inFullpath)) { return ".csd file doesn not exists "; } ssize_t size; std::string content =(char*)FileUtils::getInstance()->getFileData(inFullpath, "r", &size); // xml parse tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument(); document->Parse(content.c_str()); const tinyxml2::XMLElement* rootElement = document->RootElement();// Root CCLOG("rootElement name = %s", rootElement->Name()); const tinyxml2::XMLElement* element = rootElement->FirstChildElement(); bool serializeEnabled = false; std::string rootType = ""; while (element) { CCLOG("entity name = %s", element->Name()); if (strcmp("Content", element->Name()) == 0) { const tinyxml2::XMLAttribute* attribute = element->FirstAttribute(); // if (!attribute) { serializeEnabled = true; rootType = "NodeObjectData"; } // // // while (attribute) // { // std::string name = attribute->Name(); // std::string value = attribute->Value(); // CCLOG("attribute name = %s, value = %s", name, value); // if (name == "") // { // serializeEnabled = true; // rootType = (strcmp("", value) == 0) ? "Node" : value; // } // // if (serializeEnabled) // { // break; // } // // attribute = attribute->Next(); // } // } if (serializeEnabled) { break; } const tinyxml2::XMLElement* child = element->FirstChildElement(); if (child) { element = child; } else { element = element->NextSiblingElement(); } } if (serializeEnabled) { FlatBufferBuilder builder; _builder = &builder; Offset<NodeTree> nodeTree; Offset<NodeAction> aciton; const tinyxml2::XMLElement* child = element->FirstChildElement(); while (child) { std::string name = child->Name(); if (name == "Animation") // action { const tinyxml2::XMLElement* animation = child; aciton = createNodeAction(animation); } else if (name == "ObjectData") // nodeTree { const tinyxml2::XMLElement* objectData = child; nodeTree = createNodeTree(objectData, rootType); } child = child->NextSiblingElement(); } auto csparsebinary = CreateCSParseBinary(builder, builder.CreateVector(_textures), builder.CreateVector(_texturePngs), nodeTree, aciton); builder.Finish(csparsebinary); _textures.clear(); _texturePngs.clear(); std::string outFullPath = FileUtils::getInstance()->fullPathForFilename(flatbuffersFileName); size_t pos = outFullPath.find_last_of('.'); std::string convert = outFullPath.substr(0, pos).append(".csb"); auto save = flatbuffers::SaveFile(convert.c_str(), reinterpret_cast<const char *>(builder.GetBufferPointer()), builder.GetSize(), true); if (!save) { return "couldn't save files!"; } } return ""; }