int Module_SOW_Car::load(const XMLReader& xmldata) { /* //See comment below if (ModuleG2D::load(xmldata) != 0) { //Some error happened return -1; }*/ //xmldata.getXMLGroupHandle("")->dump(); //Load from xml xmldata.for_all("object", [=](const XMLGroup* g) { XMLReader r(*g); SFG::Pointer<SOW_Car> c(new SOW_Car(this)); int loadableret = c.cast<Loadable>()->Loadable::load(r); int gobjret = c.cast<GObjectBase>()->GObjectBase::load(XMLReader(*r.getXMLGroupHandle("GObjectBase/"))); int carret = c->load(XMLReader(*r.getXMLGroupHandle("Car"))); SFG::Util::printLog(SFG::Util::Information, __FILE__, __LINE__, "Loading car module has return values %d - %d -%d", loadableret, gobjret, carret); this->addObject(c); }); return 0; }
int SOW_Car::load(const XMLReader& data) { data.for_all("blinker", [=](const XMLGroup* g){ XMLReader(*g).for_all("Light",[=](const XMLGroup* g2){ //Add lights and stuff }); }); return 0; }
std::vector<XMLReader> XMLReader::get_sections() const { std::vector<XMLReader> lst; if (root) { for(std::vector<XMLNode*>::const_iterator i = root->children.begin(); i != root->children.end(); ++i) { lst.push_back(XMLReader(dynamic_cast<XMLListNode*>(*i))); } } return lst; }
QStringList XMLReader::getContext(const QString &_inFile, bool _keepTitles) { QStringList Output; QFile File(_inFile); File.open(QIODevice::ReadOnly); if (File.isReadable() == false) throw exAppE4SMT("Unable to open <" + _inFile + "> for reading."); QXmlStreamReader XMLReader(&File); bool Report = false; while (!XMLReader.atEnd()) { XMLReader.readNext(); switch(XMLReader.tokenType()) { case QXmlStreamReader::StartElement: if (XMLReader.name().toString().toLower() == "paragraph" || (XMLReader.name().toString().toLower() == "title" && _keepTitles)) Report = true; else Report = false; break; case QXmlStreamReader::Characters: if (Report) Output.append( XMLReader.text().toString().replace( ">",">").replace( "<","<").replace( "&","&").replace( "\n"," ").replace( "-\n","")); Report = false; break; case QXmlStreamReader::StartDocument: case QXmlStreamReader::NoToken: case QXmlStreamReader::Invalid: case QXmlStreamReader::EndDocument: case QXmlStreamReader::EndElement: case QXmlStreamReader::Comment: case QXmlStreamReader::DTD: case QXmlStreamReader::EntityReference: case QXmlStreamReader::ProcessingInstruction: Report = false; break; } } return Output; }
void SslServer::processCommandXML(QSslSocket *socket, const QByteArray &data) { qint32 argc; QString cmd; QByteArray arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10; if (!XMLReader(data, &argc, &cmd, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8, &arg9, &arg10)) { writeWarning(socket, "Invalid XML", true); return; } if (cmd == "LOGIN") { if (arg1.isEmpty() || arg2.isEmpty() || arg3.isEmpty() || arg4.isEmpty() || arg5.isEmpty()) { writeWarning(socket, "Invalid XML", true); return; } QByteArray peer_negotiation_string = arg1; QByteArray peer_id = arg2; QByteArray password = arg3; bool user_unavailable = getValue<bool>(arg4); bool new_user = getValue<bool>(arg5); QString code = QLatin1String(arg6); if (peer_negotiation_string != NEGOTIATION_STRING) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "Invalid NEGOTIATION STRING!"); writeWarning(socket, "Invalid NEGOTIATION STRING!", true); return; } if (m_id_list.contains(peer_id)) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "This user is already connected!"); writeWarning(socket, "This user is already connected!", true); return; } if (new_user) { if (m_sql.userExists("root")) { if (!m_code_list.contains(code)) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "Invalid code!"); writeWarning(socket, "Invalid code!", true); return; } else { m_code_list.removeAll(code); } } else if (cleanString(QLatin1String(peer_id)) != "root") { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "Need to create root account first!"); writeWarning(socket, "Need to create root account first!", true); return; } if (m_sql.userExists(cleanString(QLatin1String(peer_id)))) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "User already exists!"); writeWarning(socket, "User already exists!", true); return; } else if (!m_sql.createUser(cleanString(QLatin1String(peer_id)), cleanString(QLatin1String(password)))) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "Can't create new user!"); writeWarning(socket, "Can't create new user!", true); return; } } if (!m_sql.loginUser(cleanString(QLatin1String(peer_id)), cleanString(QLatin1String(password)))) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "Can't login with this user and password!"); writeWarning(socket, "Can't login with this user and password!", true); return; } DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(peer_id) << "User logged in!"); m_id_list.append(peer_id); m_id_hash.insert(socket, peer_id); if (user_unavailable) m_unavailable_list.append(peer_id); QByteArray data; data.append(getBytes<quint8>(ServerCommand::LoggedIn)); socket->write(getBytes<qint32>(data.size())); socket->write(data); } else if (cmd == "REQUEST_SETTINGS") { QString ui_path; QString js_path; QString id = QLatin1String(m_id_hash[socket]); if (id != "root") { ui_path = "../data/settings.ui"; js_path = "../data/settings.js"; } else { ui_path = "../data/settings_root.ui"; js_path = "../data/settings_root.js"; } QFile ui_file(ui_path); QFile js_file(js_path); if (!ui_file.open(QFile::ReadOnly) || !js_file.open(QFile::ReadOnly)) { DEBUG_FUNCTION("Can't send settings file"); writeWarning(socket, "Can't send settings file", true); return; } QByteArray ui_data = ui_file.readAll(); QByteArray js_data = js_file.readAll(); QByteArray xml_data = XMLWriter("SETTINGS", ui_data, js_data); writeCommandXML(socket, xml_data); } else if (cmd == "DELETE_ACCOUNT") { QString id = QLatin1String(m_id_hash[socket]); if (id == "root") { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "Invalid operation!"); writeWarning(socket, "Invalid operation!", true); return; } if (m_sql.deleteUser(id)) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "User deleted sucessfully!"); writeWarning(socket, "User deleted sucessfully!", true); } else { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "Error deleting user!"); writeWarning(socket, "Error deleting user!", true); } } else if (cmd == "CHANGE_ACCOUNT_PASSWORD") { if (arg1.isEmpty()) { writeWarning(socket, "Invalid XML", true); return; } QByteArray password = arg1; if (m_sql.changePassword(m_id_hash[socket], cleanString(QLatin1String(password)))) { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "User password changed sucessfully!"); writeWarning(socket, "User password changed sucessfully!", true); } else { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "Error changing user password!"); writeWarning(socket, "Error changing user password!", true); } } else if (cmd == "REQUEST_CODE") { QByteArray id = m_id_hash[socket]; if (QLatin1String(id) != "root") { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "Invalid operation!"); writeWarning(socket, "Invalid operation!", true); return; } QString code; do { code = QString(OpenSslLib::RANDbytes(4).toHex()); code.prepend("#"); } while (m_code_list.contains(code)); m_code_list.append(code); QTimer::singleShot(60 * 60 * 1000, [=]{ m_code_list.removeAll(code); }); DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "Sending code!"); QByteArray xml_data = XMLWriter("CODE", "edit_code", code.toLatin1()); writeCommandXML(socket, xml_data); } else { DEBUG_FUNCTION("IP:" << qPrintable(socket->peerAddress().toString()) << "User origin:" << qPrintable(m_id_hash.value(socket)) << "Invalid operation!"); writeWarning(socket, "Invalid operation!", true); } }
int SSG_Planet::load(const XMLReader& reader) { //SFG::Util::printLog(SFG::Util::Development, __FILE__, __LINE__, "Still to do"); //Load values accordingly this->m_name = reader.getValue("name."); bool real = false; this->setMass(PE::Mass(reader.asDouble("mass/", real))); if(!real) { //Failed to load mass SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to load Mass"); } sf::FloatRect r = reader.asFloatRect("PosAndVel/", real); if(!real) { //Failed to load pos and Velocity SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to load PosAndVel"); } //this->setPosition(r.left, r.top); this->setPosition(mpf_class(r.left), mpf_class(r.top)); this->setVelocity(PE::Velocity(r.width, r.height)); auto radius = reader.asDouble("radius/", real); if(!real) { SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to get radius for planet %s", this->m_name.toAnsiString().c_str()); } this->getShape().setRadius(radius); SFG::FloatRect rect(this->getShape().getLocalBounds()); this->getShape().setOrigin(rect.center()); m_planet_surface.reset(new SSG_PlanetSurface(radius)); m_planet_surface->load(); //Get Moons reader.for_all("Moon", [=](const XMLGroup* g){ SFG::Pointer<SSG_Planet> ptr(new SSG_Planet()); ptr->m_parentSys = this->m_parentSys; ptr->setGuiDesktop(this->desktop()); int ret = ptr->load(XMLReader(*g)); if(ret != 0) { SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to load moon"); return; } printf("Position: %f + %f | %f + %f\n", getShape().getPosition().x , ptr->getShape().getPosition().x, getShape().getPosition().y, ptr->getShape().getPosition().y); //Correct relative values //ptr->setPosition(getShape().getPosition().x + ptr->getShape().getPosition().x, // getShape().getPosition().y + ptr->getShape().getPosition().y); ptr->setPosition(x() + ptr->x(), y() + ptr->y()); ptr->setVelocity(getVelocity() + ptr->getVelocity()); //Add to system this->m_parentSys->addSpecificToSystem(ptr); SFG::Util::printLog(SFG::Util::Information, __FILE__, __LINE__, "Moon \"%s\" has been added", ptr->getName().toAnsiString().c_str()); }); //TESTING this->getShape().setFillColor(sf::Color(255, 255, 255)); //!TESTING return 0; }
int main (int argc, char **argv) { #ifdef HAVE_MPI MPI_Init(&argc, &argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // define some Epetra objects int n = Comm.NumProc() * 4; Epetra_Map Map(n, 0, Comm); Epetra_MultiVector x(Map, 2); x.Random(); Epetra_MultiVector b(Map, 2); x.Random(); Epetra_CrsMatrix Matrix(Copy, Map, 0); // diagonal matrix for (int i = 0; i < Map.NumMyElements(); ++i) { int ii = Map.GID(i); double one = 1.0; Matrix.InsertGlobalValues(ii, 1, &one, &ii); } Matrix.FillComplete(); Teuchos::ParameterList List; List.set("int parameter", 10); List.set("double parameter", 10.0); List.set("std::string parameter", "std::string"); // ========================= // // Part I: generate XML file // // ========================= // EpetraExt::XMLWriter XMLWriter(Comm, "data.xml"); std::vector<std::string> Content; Content.push_back("This is an example of description"); Content.push_back("The description is as long as desired,"); Content.push_back("just put it in a std::vector of strings."); XMLWriter.Create("MyProblem"); XMLWriter.Write("Author", "myself and others"); XMLWriter.Write("Date", "May 2006"); XMLWriter.Write("MyMap", Map); XMLWriter.Write("MyMatrix", Matrix); XMLWriter.Write("MyLHS", x); XMLWriter.Write("MyRHS", b); XMLWriter.Write("MyContent", Content); XMLWriter.Write("MyParameters", List); XMLWriter.Close(); // ================== // // Part II: read data // // ================== // EpetraExt::XMLReader XMLReader(Comm, "data.xml"); Epetra_Map* MyMap; Epetra_CrsMatrix* MyMatrix; Epetra_MultiVector* MyLHS; Epetra_MultiVector* MyRHS; Teuchos::ParameterList MyParameters; std::vector<std::string> Author; std::vector<std::string> Date; std::vector<std::string> MyContent; XMLReader.Read("Author", Author); XMLReader.Read("Date", Date); XMLReader.Read("MyMap", MyMap); XMLReader.Read("MyMatrix", MyMatrix); XMLReader.Read("MyLHS", MyLHS); XMLReader.Read("MyRHS", MyRHS); XMLReader.Read("MyContent", MyContent); XMLReader.Read("MyParameters", MyParameters); std::cout << *MyMap; std::cout << *MyMatrix; std::cout << *MyLHS; std::cout << *MyRHS; if (Comm.MyPID() == 0) { int Msize = (int) MyContent.size(); for (int i = 0; i < Msize; ++i) std::cout << MyContent[i] << std::endl; std::cout << MyParameters; std::cout << "Author = " << Author[0] << std::endl; std::cout << "Date = " << Date[0] << std::endl; } delete MyMap; delete MyMatrix; delete MyLHS; delete MyRHS; #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); }
int Game::loadGamestate(const XMLReader& reader, const sf::String& GSname, const SFG::Pointer<GameState>& included) { SFG::Pointer<GameState> GS_ptr; sf::String GSxmlpath = L"xml/gamestate[" + GSname + L"]"; auto str = reader.getValue(GSxmlpath, 0); if (str == L"__xml_failure") { printf("[Error] Unspecified startup gamestate in %s:%d.\n", __FILE__, __LINE__); return -2; } else { if (str == L"#included") { //Use default GS_ptr.reset(included); if (!GS_ptr.isValid() && included.getElement() != nullptr) { printf("[Critical] Failed to allocate memory for \"%s\" in %s:%d\n",GSname.toAnsiString().c_str(), __FILE__, __LINE__); return -1; } } else { //Use specified //#TODO STILL_TO_DO(__FILE__, __LINE__); //We first need an instance of a DLLLoadedGameState GS_ptr.reset(new DLLLoadedGameState()); //Set the name GS_ptr->setName(GSname); if (GS_ptr->getName() == "__xml_failure") { SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to get Gamestate name, aborting."); return -1; } //Then, load the specified file if (GS_ptr.cast<DLLLoadedGameState>()->loadFromDll(str.toAnsiString().c_str()) != 0) { SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to load gamestate \"%s\" from DLL", str.toAnsiString().c_str()); return -2; } //We actually should be done } //Get all modules sf::String uses = reader.getValue(GSxmlpath + L"uses."); if (uses != L"__xml_failure") { //We have specifiers size_t begin = 0; size_t last = 0; //Begin needs to be checked as the last module wouldn't be counted otherwise while ((last = uses.find(L',', last)) != sf::String::InvalidPos || begin != sf::String::InvalidPos) { sf::String string; if (last == sf::String::InvalidPos) { //If the end would otherwise exceed the string length, set it to the maximum (sf::String::end()). //string = sf::String(uses.begin() + begin, uses.end()); string = uses.substring(begin, uses.getSize() - begin); } else { //If a ',' was found, use its position //string = sf::String(uses.begin() + begin, uses.begin() + last); string = uses.substring(begin, last - begin); } //Check for module names if (string == L"G2D") { SFG::Pointer<ModuleG2D> ptr(new ModuleG2D()); if (!ptr.isValid()) { char buf[512]; #ifdef _WIN32 strerror_s(buf, errno); #else strerror_r(errno, buf, 512); #endif // _WIN32 printf("[Error] Failed to create Module G2D in %s:%d: %s\n", __FILE__, __LINE__, buf); return -3; } GS_ptr->addModule(ptr); } else { //#TODO: Add more modules (sound etc.) STILL_TO_DO(__FILE__, __LINE__); } if(last != sf::String::InvalidPos) begin = last + 1; //if a ',' was found, go search on for the next one else begin = last; //Meaning begin is sf::String::npos } } //Check for external asset list auto ext_assets = reader.getValue(GSxmlpath + "assets."); if (ext_assets != "__xml_failure") { //We found external assets, load them } auto assets = reader.getXMLGroupHandle(L"xml/assetlist[" + GSname + L"]"); if (assets == nullptr) { printf("[Warning] No assets for Gamestate \"%s\" found. This is not encouraged and might be an error. Please check the game.xml file. In: %s:%d\n", GSname.toAnsiString().c_str(), __FILE__, __LINE__); //return 1; } else { auto ret = GS_ptr->loadAssets(XMLReader(*assets)); if (ret != 0) { printf("[Error] Failed to load assets for Gamestate \"%s\" in %s:%d.\n", GSname.toAnsiString().c_str(), __FILE__, __LINE__); } } //Empty path to tell the gamestate to just update what it has (or do nothing if nothing has to be done) GS_ptr->load(L""); //this->g_gamestates.push_back(GS_ptr); this->addGamestate(GS_ptr); } return 0; }
XMLReader XMLReader::get_section(const std::string& name) const { return XMLReader(dynamic_cast<XMLListNode*>(get_node(name))); }