| 1 |
#include "utilxmltools.h" |
| 2 |
|
| 3 |
namespace UtilXmlTools{ |
| 4 |
|
| 5 |
QStringList getAllXmlFilesByWildcard(const QString &wildcard){ |
| 6 |
QStringList validFilesMatching; |
| 7 |
QStringList filesMatching; |
| 8 |
|
| 9 |
// Get all files matching the wildcard |
| 10 |
|
| 11 |
filesMatching=Util::getAllFilesByWildcard(wildcard); |
| 12 |
|
| 13 |
// Check if all are XmlFiles, only return valid XmlFiles |
| 14 |
|
| 15 |
for(int i=0; i<filesMatching.size(); i++){ |
| 16 |
if(filesMatching[i].endsWith(".xml",Qt::CaseInsensitive)){ |
| 17 |
validFilesMatching << filesMatching[i]; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
return validFilesMatching; |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
QStringList getAllPatchFilesByWildcard(const QString &wildcard){ |
| 26 |
QStringList validFilesMatching; |
| 27 |
QStringList filesMatching; |
| 28 |
|
| 29 |
// Get all files matching the wildcard |
| 30 |
|
| 31 |
filesMatching=Util::getAllFilesByWildcard(wildcard); |
| 32 |
|
| 33 |
// Check if all are PatchFiles, only return valid PatchFiles |
| 34 |
|
| 35 |
for(int i=0; i<filesMatching.size(); i++){ |
| 36 |
if(filesMatching[i].endsWith(".patch",Qt::CaseInsensitive) || filesMatching[i].endsWith(".oni-patch",Qt::CaseInsensitive)){ |
| 37 |
validFilesMatching << filesMatching[i]; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return validFilesMatching; |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
void backupFile(const QString &file){ |
| 46 |
if(!QFile::exists(file+".bak")){ |
| 47 |
if(!Util::backupFile(file)){ |
| 48 |
std::cerr << "Couldn't backup file '" << file.toLatin1().constData() << "'. Aborting." << std::endl; |
| 49 |
exit(1); |
| 50 |
} |
| 51 |
} |
| 52 |
else{ |
| 53 |
std::cout << "Backup file '" << file.toLatin1().constData() << "'' already exists. Skipping..." << std::endl; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters){ |
| 58 |
for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode) |
| 59 |
{ |
| 60 |
|
| 61 |
if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name()) |
| 62 |
&& (filters.getAttributeName() == "" || |
| 63 |
Util::toQString((*currNode).attribute(filters.getAttributeName().toLatin1().constData()).value()) == filters.getAttributeValue()) ){ // Seems node attribute must be converted to qtsring to remove \r\n needs to check in future! |
| 64 |
|
| 65 |
result << *currNode; |
| 66 |
continue; |
| 67 |
} |
| 68 |
getAllNamedElements(*currNode,result,filters); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
pugi::xml_node getFirstNamedElements(pugi::xml_node &node, XmlFilter &filters){ |
| 73 |
|
| 74 |
pugi::xml_node foundNode; |
| 75 |
|
| 76 |
for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode) |
| 77 |
{ |
| 78 |
if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name()) |
| 79 |
&& (filters.getAttributeName() == "" || |
| 80 |
Util::toQString((*currNode).attribute(filters.getAttributeName().toLatin1().constData()).value()) == filters.getAttributeValue()) ){ |
| 81 |
return *currNode; |
| 82 |
} |
| 83 |
|
| 84 |
foundNode=getFirstNamedElements(*currNode,filters); |
| 85 |
|
| 86 |
if(foundNode.type()!=pugi::node_null){ |
| 87 |
return foundNode; |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
return foundNode; |
| 93 |
} |
| 94 |
|
| 95 |
void displaySuccessMessage(const int numberOperations, const QString &operation){ |
| 96 |
std::cout << "------------------------------------------------------------------------" << std::endl; |
| 97 |
std::cout << numberOperations << " " << operation.toLatin1().constData() << " operation(s) completed with success!" << std::endl; |
| 98 |
std::cout << "------------------------------------------------------------------------" << std::endl; |
| 99 |
} |
| 100 |
|
| 101 |
void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram){ |
| 102 |
std::cerr << "************************************************************************" << std::endl; |
| 103 |
std::cerr << operation.toLatin1().constData() << " operation failed!" << std::endl << std::endl; |
| 104 |
std::cerr << message.toLatin1().constData() << std::endl << std::endl; |
| 105 |
if(exitProgram) std::cerr << "Aborting..." << std::endl; |
| 106 |
std::cerr << "************************************************************************" << std::endl; |
| 107 |
if(exitProgram) exit(1); |
| 108 |
} |
| 109 |
|
| 110 |
} |