int MyPaint::readSVG(string fileName) { string input_xml; string line; fstream in; in.open(fileName, ios_base::in); if (!in) return 1; while (getline(in, line)) input_xml += line; in.close(); vector<char> xml_copy(input_xml.begin(), input_xml.end()); xml_copy.push_back('\0'); xml_document<> doc; doc.parse<parse_declaration_node | parse_no_data_nodes>(&xml_copy[0]); xml_node<> *node = doc.first_node("svg"); node = node->first_node(); FigureFactory factory; do { Figure *_temp = factory.getFigure(node); _temp->setAttribute(node); myFigure.push_back(_temp); _temp = NULL; node = node->next_sibling(); } while (node); }
void Genome::addGene(std::string strChromosomeId ){ rapidxml::xml_document<> doc; std::vector<char> xml_copy(m_strXML.begin(), m_strXML.end()); xml_copy.push_back('\0'); doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* rootNode = doc.first_node("Genome"); for (rapidxml::xml_node<> *child = rootNode->first_node("Chromosome"); child; child = child->next_sibling("Chromosome")) { if(child->first_attribute("id")->value() == strChromosomeId){ // gene node rapidxml::xml_node<>* gene = doc.allocate_node(rapidxml::node_element, "Gene"); child->append_node(gene); break; } } Genome::Save(doc); }
void Genome::addGeneAttribute(std::string strChromosomeId, std::string strGeneId, std::string strName, std::string strValue){ rapidxml::xml_document<> doc; std::vector<char> xml_copy(m_strXML.begin(), m_strXML.end()); xml_copy.push_back('\0'); doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* rootNode = doc.first_node("Genome"); for (rapidxml::xml_node<>* chromosomeNode = rootNode->first_node("Chromosome"); chromosomeNode; chromosomeNode = chromosomeNode->next_sibling("Chromosome")) { if(chromosomeNode->first_attribute("id")->value() == strChromosomeId){ for (rapidxml::xml_node<>* geneNode = chromosomeNode->first_node("Gene"); geneNode; geneNode = geneNode->next_sibling("Gene")) { if(geneNode->first_attribute("id")->value() == strGeneId){ char* pchName = doc.allocate_string(strName.c_str()); char* pchValue = doc.allocate_string(strValue.c_str()); geneNode->append_attribute(doc.allocate_attribute(pchName, pchValue )); break; } } break; } } Genome::Save(doc); }
void Genome::AddEdges(std::string strChromosomeId, std::vector<Edge> edges){ rapidxml::xml_document<> doc; std::vector<char> xml_copy(m_strXML.begin(), m_strXML.end()); xml_copy.push_back('\0'); doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* rootNode = doc.first_node("Genome"); for (rapidxml::xml_node<> *chromosomeNode = rootNode->first_node("Chromosome"); chromosomeNode; chromosomeNode = chromosomeNode->next_sibling("Chromosome")) { if(chromosomeNode->first_attribute("id")->value() == strChromosomeId){ // edges node rapidxml::xml_node<>* edgesNode = doc.allocate_node(rapidxml::node_element, "Vertices"); Edges cEdges(edges); char* pchRandomData = doc.allocate_string(cEdges.ToString().c_str()); edgesNode->value(pchRandomData); chromosomeNode->append_node(edgesNode); break; } } Genome::Save(doc); }
void LevelConfig::initialize(std::string fileName) { CCLOG("LevelConfig:: Loading level file %s.", fileName.c_str()); std::string file = FileUtils::getInstance()->getStringFromFile(fileName + LVL_EXT); std::vector<char> xml_copy(file.begin(), file.end()); xml_copy.push_back('\0'); this->_doc.parse<0>(&xml_copy[0]); CCLOG("LevelConfig:: Loading level file %s finished.", fileName.c_str()); }
string getUrl(string xmlString) { vector<char> xml_copy(xmlString.begin(), xmlString.end()); xml_copy.push_back('\0'); rapidxml::xml_document<> doc; rapidxml::xml_node<> * root_node; rapidxml::xml_node<> * mes_node; doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); root_node = doc.first_node("scramble"); return root_node->first_node("url")->value(); }
int Genome::GetGeneration(){ int nGeneration = 0; rapidxml::xml_document<> doc; std::vector<char> xml_copy(m_strXML.begin(), m_strXML.end()); xml_copy.push_back('\0'); doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* rootNode = doc.first_node("Genome"); nGeneration = rootNode->first_attribute("generation") ? atoi(rootNode->first_attribute("generation")->value()) : 1; return nGeneration; }
EncryptedMessage getEncryptedMessage(string xmlString) { vector<char> xml_copy(xmlString.begin(), xmlString.end()); xml_copy.push_back('\0'); rapidxml::xml_document<> doc; rapidxml::xml_node<> * root_node; rapidxml::xml_node<> * mes_node; doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); root_node = doc.first_node("scramble"); mes_node = root_node->first_node("text"); string encMessage = mes_node->value(); encMessage = base64_decode(encMessage); EncryptedMessage res = EncryptedMessage(encMessage); return res; }
void Population::initGenome(std::string genomeTemplate){ std::ifstream t(genomeTemplate.c_str()); std::stringstream buffer; buffer << t.rdbuf(); std::string input_xml = buffer.str(); // make a safe-to-modify copy of input_xml // (you should never modify the contents of an std::string directly) std::vector<char> xml_copy(input_xml.begin(), input_xml.end()); xml_copy.push_back('\0'); // only use xml_copy from here on! rapidxml::xml_document<> doc; doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* genomeNode = doc.first_node("Genome"); for (rapidxml::xml_node<>* chromosomeNode = genomeNode->first_node("Chromosome"); chromosomeNode; chromosomeNode = chromosomeNode->next_sibling("Chromosome")) { int nGenes = 0; if(chromosomeNode->first_attribute("genes")){ nGenes = atoi(chromosomeNode->first_attribute("genes")->value()); } else{ nGenes = rand() % 10; } for(int i = 0 ; i < nGenes; i++){ // gene node rapidxml::xml_node<>* geneNode = doc.allocate_node(rapidxml::node_element, "Gene"); chromosomeNode->append_node(geneNode); } } std::string xml_as_string; rapidxml::print(std::back_inserter(xml_as_string), doc); // xml_as_string now contains the XML in string form, indented // (in all its angle bracket glory) cGenome.SetXML(xml_as_string); }
PlaintextMessage getPlaintextMessage(string xmlString, PFC * pfc) { vector<char> xml_copy(xmlString.begin(), xmlString.end()); xml_copy.push_back('\0'); rapidxml::xml_document<> doc; rapidxml::xml_node<> * root_node; rapidxml::xml_node<> * mes_node; doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); root_node = doc.first_node("scramble"); mes_node = root_node->first_node("text"); string message = mes_node->value(); PlaintextMessage plainMes = PlaintextMessage(message); vector<string> recips; root_node = root_node->first_node("recipients"); for (rapidxml::xml_node<> * recipient_node = root_node->first_node("recipient"); recipient_node; recipient_node = recipient_node->next_sibling()) { plainMes.addRecipient(recipient_node->value(), pfc); } return plainMes; }
void Genome::SetFitness(double fitness){ std::stringstream ss; ss << fitness; rapidxml::xml_document<> doc; std::vector<char> xml_copy(m_strXML.begin(), m_strXML.end()); xml_copy.push_back('\0'); doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* rootNode = doc.first_node("Genome"); char* pchData = doc.allocate_string(ss.str().c_str()); if(rootNode->first_attribute("fitness")){ rootNode->first_attribute("fitness")->value(pchData); } else{ rootNode->append_attribute(doc.allocate_attribute("fitness", pchData )); } Genome::Save(doc); }
void xmlmethods(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { int j, m, iok = 0; char* file, *key, *val, *nm; int job = getInt(prhs[1]); int i = getInt(prhs[2]); // Check for proper number of arguments if (!nargs_ok(job,nrhs-1)) { mexErrMsgTxt("Wrong number of inputs."); return; } else if (nlhs > 1) { mexErrMsgTxt("Too many output arguments"); } // options that do not return a value if (job < 20) { switch (job) { case 0: nm = getString(prhs[3]); iok = xml_new(nm); break; case 1: iok = xml_del(i); break; case 2: iok = xml_copy(i); break; case 4: file = getString(prhs[3]); iok = xml_build(i, file); break; case 5: key = getString(prhs[3]); val = getString(prhs[4]); iok = xml_addAttrib(i, key, val); break; case 6: key = getString(prhs[3]); iok = xml_child(i, key); break; case 7: m = getInt(prhs[3]); iok = xml_child_bynumber(i, m); break; case 8: key = getString(prhs[3]); iok = xml_findID(i, key); break; case 9: key = getString(prhs[3]); iok = xml_findByName(i, key); break; case 10: iok = xml_nChildren(i); break; case 11: key = getString(prhs[3]); val = getString(prhs[4]); iok = xml_addChild(i, key, val); break; case 12: key = getString(prhs[3]); j = getInt(prhs[4]); iok = xml_addChildNode(i, j); break; case 13: file = getString(prhs[3]); iok = xml_write(i, file); break; case 14: j = getInt(prhs[3]); iok = xml_removeChild(i, j); break; case 15: file = getString(prhs[3]); iok = xml_get_XML_File(file, 0); break; default: mexErrMsgTxt("unknown job parameter"); } plhs[0] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL); double* h = mxGetPr(plhs[0]); *h = double(iok); if (iok < 0) { reportError(); } return; } // options that return strings char* v = (char*)mxCalloc(80, sizeof(char)); switch (job) { case 20: // return an attribute key = getString(prhs[3]); iok = xml_attrib(i, key, v); break; case 21: // return the value of the node iok = xml_value(i, v); break; case 22: iok = xml_tag(i, v); break; default: mexErrMsgTxt("unknown job parameter"); } if (iok < 0) { plhs[0] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL); double* h = mxGetPr(plhs[0]); *h = double(iok); if (iok < 0) { reportError(); } } else { plhs[0] = mxCreateString(v); } }
void Genome::DoMutations(MutationChances mutationChances){ //std::cout << "starting mutations" << std::endl; rapidxml::xml_document<> doc; std::vector<char> xml_copy(m_strXML.begin(), m_strXML.end()); xml_copy.push_back('\0'); doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* rootNode = doc.first_node("Genome"); for (rapidxml::xml_node<>* chromosomeNode = rootNode->first_node("Chromosome"); chromosomeNode; chromosomeNode = chromosomeNode->next_sibling("Chromosome")) { std::string strEncoding = chromosomeNode->first_attribute("encoding")->value(); //do chromosome mutations ChromosomeMutation cChromosomeMutation(chromosomeNode); if(strEncoding == "binary"){ ChromosomeMutations(chromosomeNode, cChromosomeMutation, mutationChances.BinaryMutationChance); } else if(strEncoding == "integer"){ ChromosomeMutations(chromosomeNode, cChromosomeMutation, mutationChances.IntegerMutationChance); } else if(strEncoding == "double"){ ChromosomeMutations(chromosomeNode, cChromosomeMutation, mutationChances.DoubleMutationChance); } else if(strEncoding == "alphanum"){ ChromosomeMutations(chromosomeNode, cChromosomeMutation, mutationChances.AlphanumMutationChance); } else if(strEncoding == "custom"){ ChromosomeMutations(chromosomeNode, cChromosomeMutation, mutationChances.CustomMutationChance); } else if(strEncoding == "list"){ ChromosomeMutations(chromosomeNode, cChromosomeMutation, mutationChances.ListMutationChance); } else{ std::cout << "unknown encoding type" << std::endl; } //do gene mutations for each gene in this chromosome for (rapidxml::xml_node<>* geneNode = chromosomeNode->first_node("Gene"); geneNode; geneNode = geneNode->next_sibling("Gene")) { if(strEncoding == "binary"){ BitMutation cBitMutation(geneNode->value(), chromosomeNode); Mutation& rMutation = cBitMutation; GeneMutations(doc, geneNode, rMutation, mutationChances.BinaryMutationChance); } else if(strEncoding == "integer"){ IntegerMutation cIntegerMutation(geneNode->value(), chromosomeNode); Mutation& rMutation = cIntegerMutation; GeneMutations(doc, geneNode, rMutation, mutationChances.IntegerMutationChance); } else if(strEncoding == "double"){ DoubleMutation cDoubleMutation(geneNode->value(), chromosomeNode); Mutation& rMutation = cDoubleMutation; GeneMutations(doc, geneNode, rMutation, mutationChances.DoubleMutationChance); } else if(strEncoding == "alphanum"){ AlphanumMutation cAlphanumMutation(geneNode->value(), chromosomeNode); Mutation& rMutation = cAlphanumMutation; GeneMutations(doc, geneNode, rMutation, mutationChances.AlphanumMutationChance); } else if(strEncoding == "custom"){ CustomMutation cCustomMutation(geneNode->value(), chromosomeNode); Mutation& rMutation = cCustomMutation; GeneMutations(doc, geneNode, rMutation, mutationChances.CustomMutationChance); } else if(strEncoding == "list"){ ListMutation cListMutation(geneNode->value(), chromosomeNode); Mutation& rMutation = cListMutation; GeneMutations(doc, geneNode, rMutation, mutationChances.ListMutationChance); } else{ std::cout << "unknown encoding type" << std::endl; } } } Genome::Save(doc); }
void Genome::FillWithRandomData(){ rapidxml::xml_document<> doc; std::vector<char> xml_copy(m_strXML.begin(), m_strXML.end()); xml_copy.push_back('\0'); doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_copy[0]); rapidxml::xml_node<>* rootNode = doc.first_node("Genome"); std::string strRandomData; for (rapidxml::xml_node<>* child = rootNode->first_node("Chromosome"); child; child = child->next_sibling("Chromosome")) { std::string strEncoding = child->first_attribute("encoding")->value(); if(strEncoding == "binary"){ //std::cout << "putting random data in binary genes" << std::endl; BinaryEncoding cBinaryEncoding; Encoding& rEncoding = cBinaryEncoding; FillGenes(doc, child, rEncoding); } else if(strEncoding == "integer"){ //std::cout << "putting random data in integer genes" << std::endl; IntegerEncoding cIntegerEncoding; Encoding& rEncoding = cIntegerEncoding; FillGenes(doc, child, rEncoding); } else if(strEncoding == "double"){ //std::cout << "putting random data in double genes" << std::endl; DoubleEncoding cDoubleEncoding; Encoding& rEncoding = cDoubleEncoding; FillGenes(doc, child, rEncoding); } else if(strEncoding == "alphanum"){ //std::cout << "putting random data in alphanum genes" << std::endl; AlphanumEncoding cAlphanumEncoding; Encoding& rEncoding = cAlphanumEncoding; FillGenes(doc, child, rEncoding); } else if(strEncoding == "custom"){ //std::cout << "putting random data in custom genes" << std::endl; CustomEncoding cCustomEncoding; if(child->first_attribute("chars")){ std::string strCharacters = child->first_attribute("chars")->value(); cCustomEncoding.SetChars(strCharacters); } Encoding& rEncoding = cCustomEncoding; FillGenes(doc, child, rEncoding); } else if(strEncoding == "list"){ //std::cout << "putting random data in list genes" << std::endl; ListEncoding cListEncoding; Encoding& rEncoding = cListEncoding; FillGenes(doc, child, rEncoding); } else{ std::cout << "unknown encoding type" << std::endl; } } Genome::Save(doc); }
geoData mapload(char* path,VisiLibity::Environment & mapEnv,VisiLibity::Visibility_Graph & visGraph,float clearDist ) { std::ifstream in(path); std::string s; std::string s1=""; while(getline(in, s)) { // Discards newline char s1=s1+s+"\n"; } std::vector<char> xml_copy(s1.begin(), s1.end()); xml_copy.push_back('\0'); rapidxml::xml_document<> doc; // character type defaults to char doc.parse<0>(&xml_copy[0]); // 0 means default parse flags //std::cout << "Name of my first node is: " << doc.first_node()->name() << "\n"; rapidxml::xml_node<char> *n1; n1=doc.first_node("svg"); n1=n1->first_node("g")->first_node("path"); //std::vector < VisiLibity::Point > poly_temp; //geoData vertices_temp(10,poly_temp); geoData vertices_temp; int i=0; while (n1) { vertices_temp.push_back(loadPath(n1->first_attribute("d")->value())); n1=n1->next_sibling("path"); i++; } Polygon_2 mapEnvOB; for (int j=0;j<vertices_temp[0].size();j++) { Point_2 nextVert= Point_2(vertices_temp[0][j][0],vertices_temp[0][j][1]); mapEnvOB.push_back(nextVert); }; Polygon_set_2 S; mapEnvOB.reverse_orientation(); S.insert(mapEnvOB); for (int k=1;k<vertices_temp.size();k++) { Polygon_2 holeCGAL; for (int j=0;j<vertices_temp[k].size();j++) { holeCGAL.push_back(Point_2(vertices_temp[k][j][0],vertices_temp[k][j][1])); }; //holeCGAL.reverse_orientation(); S.difference(holeCGAL); holeCGAL.clear(); }; std::list<Polygon_with_holes_2> res; std::list<Polygon_with_holes_2>::const_iterator it; S.polygons_with_holes (std::back_inserter (res)); std::vector<VisiLibity::Polygon> envPolys; for (it = res.begin(); it != res.end(); ++it) { if(CGAL::ON_BOUNDED_SIDE==CGAL::bounded_side_2(it->outer_boundary().vertices_begin(),it->outer_boundary().vertices_end(),Point_2(guest1.pos.x(),guest1.pos.y()),Kernel())) { envPolys.push_back(ConvertPolygonCGAL2Vis(it->outer_boundary())); Polygon_with_holes_2::Hole_const_iterator hi; for (hi=it->holes_begin();hi!=it->holes_end();++hi) { envPolys.push_back(ConvertPolygonCGAL2Vis(*hi)); }; break; }; } for (int i=0;i<envPolys.size();i++){ //envPolys.push_back(VisiLibity::Polygon(vertices_temp[i])); //i=0; envPolys[i].eliminate_redundant_vertices(0.0001); VisiLibity::Point cm=envPolys[i].centroid(); for (int j=0;j<envPolys[i].n();j++) { if (j<envPolys[i].n()-1){ VisiLibity::Point n1=clearDist*normal(envPolys[i][j+1]-envPolys[i][j]); envPolys[i][j]=envPolys[i][j]+n1; envPolys[i][j+1]=envPolys[i][j+1]+n1; } } VisiLibity::Point norm1=clearDist*normal(envPolys[i][0]-envPolys[i][envPolys[i].n()-1]); envPolys[i][0]=envPolys[i][0]+norm1; envPolys[i][envPolys[i].n()-1]=envPolys[i][envPolys[i].n()-1]+norm1; }; mapEnv = *(new VisiLibity::Environment(envPolys)); mapEnv.enforce_standard_form(); visGraph = *(new VisiLibity::Visibility_Graph(mapEnv,0.00000001)); return vertices_temp; };
/*! Copy one configuration object to antother * * Works for objects that are items ina yang list with a keyname, eg as: * list sender{ * key name; * leaf name{... * * @param[in] h CLICON handle * @param[in] cvv Vector of variables from CLIgen command-line * @param[in] argv Vector: <db>, <xpath>, <field>, <fromvar>, <tovar> * Explanation of argv fields: * db: Database name, eg candidate|tmp|startup * xpath: XPATH expression with exactly two %s pointing to field and from name * field: Name of list key, eg name * fromvar:Name of variable containing name of object to copy from (given by xpath) * tovar: Name of variable containing name of object to copy to. * @code * cli spec: * copy snd <n1:string> to <n2:string>, cli_copy_config("candidate", "/sender[%s='%s']", "from", "n1", "n2"); * cli command: * copy snd from to to * @endcode */ int cli_copy_config(clicon_handle h, cvec *cvv, cvec *argv) { int retval = -1; char *db; cxobj *x1 = NULL; cxobj *x2 = NULL; cxobj *x; char *xpath; int i; int j; cbuf *cb = NULL; char *keyname; char *fromvar; cg_var *fromcv; char *fromname = NULL; char *tovar; cg_var *tocv; char *toname; cxobj *xerr; if (cvec_len(argv) != 5){ clicon_err(OE_PLUGIN, 0, "Requires four elements: <db> <xpath> <keyname> <from> <to>"); goto done; } /* First argv argument: Database */ db = cv_string_get(cvec_i(argv, 0)); /* Second argv argument: xpath */ xpath = cv_string_get(cvec_i(argv, 1)); /* Third argv argument: name of keyname */ keyname = cv_string_get(cvec_i(argv, 2)); /* Fourth argv argument: from variable */ fromvar = cv_string_get(cvec_i(argv, 3)); /* Fifth argv argument: to variable */ tovar = cv_string_get(cvec_i(argv, 4)); /* Get from variable -> cv -> from name */ if ((fromcv = cvec_find(cvv, fromvar)) == NULL){ clicon_err(OE_PLUGIN, 0, "fromvar '%s' not found in cligen var list", fromvar); goto done; } /* Get from name from cv */ fromname = cv_string_get(fromcv); /* Create xpath */ if ((cb = cbuf_new()) == NULL){ clicon_err(OE_PLUGIN, errno, "cbuf_new"); goto done; } /* Sanity check that xpath contains exactly two %s, ie [%s='%s'] */ j = 0; for (i=0; i<strlen(xpath); i++){ if (xpath[i] == '%') j++; } if (j != 2){ clicon_err(OE_PLUGIN, 0, "xpath '%s' does not have two '%%'", xpath); goto done; } cprintf(cb, xpath, keyname, fromname); /* Get from object configuration and store in x1 */ if (clicon_rpc_get_config(h, db, cbuf_get(cb), &x1) < 0) goto done; if ((xerr = xpath_first(x1, "/rpc-error")) != NULL){ clicon_rpc_generate_error("Get configuration", xerr); goto done; } /* Get to variable -> cv -> to name */ if ((tocv = cvec_find(cvv, tovar)) == NULL){ clicon_err(OE_PLUGIN, 0, "tovar '%s' not found in cligen var list", tovar); goto done; } toname = cv_string_get(tocv); /* Create copy xml tree x2 */ if ((x2 = xml_new("new", NULL, NULL)) == NULL) goto done; if (xml_copy(x1, x2) < 0) goto done; xml_name_set(x2, "config"); cprintf(cb, "/%s", keyname); if ((x = xpath_first(x2, "%s", cbuf_get(cb))) == NULL){ clicon_err(OE_PLUGIN, 0, "Field %s not found in copy tree", keyname); goto done; } x = xml_find(x, "body"); xml_value_set(x, toname); /* resuse cb */ cbuf_reset(cb); /* create xml copy tree and merge it with database configuration */ clicon_xml2cbuf(cb, x2, 0, 0); if (clicon_rpc_edit_config(h, db, OP_MERGE, cbuf_get(cb)) < 0) goto done; retval = 0; done: if (cb) cbuf_free(cb); if (x1 != NULL) xml_free(x1); if (x2 != NULL) xml_free(x2); return retval; }