#include "utilxmltools.h" namespace UtilXmlTools{ // As this will not likely be modified we use QVector instead of QList since it is more cache friendly QVector getAllXmlFilesByWildcard(const QString &wildcard){ QStringList validFilesMatching; QStringList filesMatching; // Get all files matching the wildcard filesMatching=UtilXmlTools::getAllFilesByWildcard(wildcard); // Check if all are XmlFiles, only return valid XmlFiles for(int i=0; i getAllPatchFilesByWildcard(const QString &wildcard){ QStringList validFilesMatching; QStringList filesMatching; // Get all files matching the wildcard filesMatching=UtilXmlTools::getAllFilesByWildcard(wildcard); // Check if all are PatchFiles, only return valid PatchFiles for(int i=0; i &result){ pugi::xpath_node_set selectedNodes; pugi::xpath_node node; try { selectedNodes = doc.select_nodes(xPathExpression.toUtf8().constData()); } catch (const pugi::xpath_exception& e) { displayErrorMessage("XPath element selection","Selection of elements using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what()); } for (pugi::xpath_node_set::const_iterator currNode = selectedNodes.begin(); currNode != selectedNodes.end(); ++currNode) { node = *currNode; if(node){ // if node != null result << node.node(); } } if(result.isEmpty()){ result << pugi::xml_node(); // add an empty node if none found } } pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc){ pugi::xpath_node selectedNode; try { selectedNode = doc.select_single_node(xPathExpression.toUtf8().constData()); } catch (const pugi::xpath_exception& e) { displayErrorMessage("XPath element selection","Selection of element using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what()); } return selectedNode.node(); } void getAllNamedElements(pugi::xml_node &node, QList &result, XmlFilter &filters){ for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode) { if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name()) && (filters.getAttributeName() == "" || QString((*currNode).attribute(filters.getAttributeName().toUtf8().constData()).value()) == filters.getAttributeValue()) ){ // Seems node attribute must be converted to qtsring to remove \r\n needs to check in future! result << *currNode; continue; } getAllNamedElements(*currNode,result,filters); } } pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters){ pugi::xml_node foundNode; for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode) { if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name()) && (filters.getAttributeName() == "" || QString((*currNode).attribute(filters.getAttributeName().toUtf8().constData()).value()) == filters.getAttributeValue()) ){ return *currNode; } foundNode=getFirstNamedElement(*currNode,filters); if(foundNode.type()!=pugi::node_null){ return foundNode; } } return foundNode; } void displaySuccessMessage(const int numberOperations, const QString &operation){ std::cout << "------------------------------------------------------------------------" << std::endl; std::cout << numberOperations << " " << operation.toUtf8().constData() << " operation(s) completed with success!" << std::endl; std::cout << "------------------------------------------------------------------------" << std::endl; } void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram){ std::cerr << "************************************************************************" << std::endl; std::cerr << operation.toUtf8().constData() << " operation failed!" << std::endl << std::endl; std::cerr << message.toUtf8().constData() << std::endl << std::endl; if(exitProgram) std::cerr << "Aborting..." << std::endl; std::cerr << "************************************************************************" << std::endl; if(exitProgram) exit(1); } QStringList qStringListFromSpacedString(const QString &mySpacedString){ return mySpacedString.split(" "); } QList qListIntFromSpacedString(const QString &mySpacedString){ QStringList stringList; QList intList; stringList = mySpacedString.split(" "); foreach(QString value, stringList){ bool ok; intList << value.toInt(&ok); if(!ok){ throw std::runtime_error(QString("Impossible to convert string '" + value + "' to int!").toUtf8().constData()); } } return intList; } QList qListDoubleFromSpacedString(const QString &mySpacedString){ QStringList stringList; QList doubleList; stringList = mySpacedString.split(" "); foreach(QString value, stringList){ bool ok; doubleList << value.toDouble(&ok); if(!ok){ throw std::runtime_error(QString("Impossible to convert string '" + value + "' to double!").toUtf8().constData()); } } return doubleList; } void loadXmlFile(const QString &file, pugi::xml_document &document, pugi::xml_node &rootNode, bool backupsEnabled, bool verboseEnabled, const QString &operationForErrorMessage){ pugi::xml_parse_result result = document.load_file(file.toUtf8().constData()); rootNode=document.root(); if(result.status==pugi::status_ok){ if(verboseEnabled){ std::cout << "File '" << file.toUtf8().constData() << "' loaded with sucess." << std::endl; } } else{ UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while loading '" +file + "' XML file\n" + result.description()); } if(backupsEnabled){ UtilXmlTools::backupFile(file,verboseEnabled); // bake a backup of the file. } } void saveXmlFile(const QString &file, pugi::xml_document &document, const QString &operationForErrorMessage){ if(!document.save_file(file.toUtf8().constData(), "\t", pugi::format_indent | pugi::format_save_file_text | pugi::format_no_escapes)){ // output as the system new lines ending UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while saving '" + file + "' XML file"); } } //TODO Needs optimization QStringList QStringToArgsArray(const QString &args){ QStringList result; int startIdx=0, endIdx=0; if(!args.isEmpty()){ // if non empty arguments while(endIdx