1 | #ifndef UTILXMLTOOLS_H
|
---|
2 | #define UTILXMLTOOLS_H
|
---|
3 |
|
---|
4 | // Utilities functions specific used for Xml Tools (not general functions)
|
---|
5 |
|
---|
6 | #include "util.h"
|
---|
7 | #include "xmlfilter.h"
|
---|
8 | #include "libs/pugixml.hpp"
|
---|
9 |
|
---|
10 | namespace UtilXmlTools{
|
---|
11 |
|
---|
12 | QStringList getAllXmlFilesByWildcard(const QString &wildcard);
|
---|
13 | QStringList getAllPatchFilesByWildcard(const QString &wildcard);
|
---|
14 | void backupFile(const QString &file);
|
---|
15 | void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters);
|
---|
16 | void displaySuccessMessage(const int numberOperations, const QString &operation);
|
---|
17 | void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram=true);
|
---|
18 | pugi::xml_node getFirstNamedElements(pugi::xml_node &node, XmlFilter &filters);
|
---|
19 |
|
---|
20 | //// inline functions
|
---|
21 |
|
---|
22 | inline void loadXmlFile(const QString &file, pugi::xml_document &document, pugi::xml_node &rootNode, bool backupsEnabled, const QString &operationForErrorMessage){
|
---|
23 |
|
---|
24 | pugi::xml_parse_result result = document.load_file(file.toLatin1().constData());
|
---|
25 | rootNode=document.root();
|
---|
26 |
|
---|
27 | if(result.status==pugi::status_ok){
|
---|
28 | std::cout << "File '" << file.toLatin1().constData() << "' loaded with sucess." << std::endl;
|
---|
29 | }
|
---|
30 | else{
|
---|
31 | UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while loading '" +file + "' XML file\n" + result.description());
|
---|
32 | }
|
---|
33 |
|
---|
34 | if(backupsEnabled){
|
---|
35 | UtilXmlTools::backupFile(file); // bake a backup of the file.
|
---|
36 | }
|
---|
37 |
|
---|
38 | }
|
---|
39 |
|
---|
40 | inline void saveXmlFile(const QString &file, pugi::xml_document &document, const QString &operationForErrorMessage){
|
---|
41 | if(!document.save_file(file.toLatin1().constData(), "\t", pugi::format_indent | pugi::format_save_file_text | pugi::format_no_escapes)){ // output as the system new lines ending
|
---|
42 | UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while saving '" + file + "' XML file");
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | //inline void checkIfValidXmlFile(const QString &file){
|
---|
47 | // if(!file.endsWith(".xml",Qt::CaseInsensitive)){
|
---|
48 | // std::cout << "Tried to load a non xml file: '" << Util::toCstr(file) << "'. XmlTools only work over XmlFiles." << std::endl;
|
---|
49 | // exit(1);
|
---|
50 | // }
|
---|
51 | //}
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | #endif // UTILXMLTOOLS_H
|
---|