Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : #ifndef EZ_TOOLS_XML_CONFIG
4 : : #define EZ_TOOLS_XML_CONFIG
5 : : #endif // EZ_TOOLS_XML_CONFIG
6 : :
7 : : /*
8 : : MIT License
9 : :
10 : : Copyright (c) 2014-2024 Stephane Cuillerdier (aka aiekick)
11 : :
12 : : Permission is hereby granted, free of charge, to any person obtaining a copy
13 : : of this software and associated documentation files (the "Software"), to deal
14 : : in the Software without restriction, including without limitation the rights
15 : : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 : : copies of the Software, and to permit persons to whom the Software is
17 : : furnished to do so, subject to the following conditions:
18 : :
19 : : The above copyright notice and this permission notice shall be included in all
20 : : copies or substantial portions of the Software.
21 : :
22 : : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 : : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 : : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 : : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 : : SOFTWARE.
29 : : */
30 : :
31 : : // ezXmlConfig is part of the ezLibs project : https://github.com/aiekick/ezLibs.git
32 : :
33 : : #include <string>
34 : : #include <vector>
35 : : #include <fstream>
36 : :
37 : : #include "ezXml.hpp"
38 : : #include "ezLog.hpp"
39 : :
40 : : namespace ez {
41 : : namespace xml {
42 : :
43 : : class Config {
44 : : public:
45 : : virtual ez::xml::Nodes getXmlNodes(const std::string& vUserDatas = "") = 0;
46 : : // return true for continue xml parsing of childs in this node or false for interrupt the child exploration (if we want explore child ourselves)
47 : : virtual bool setFromXmlNodes(const ez::xml::Node& vNode, const ez::xml::Node& vParent, const std::string& vUserDatas) = 0;
48 : :
49 : : public:
50 : 0 : bool LoadConfigString(const std::string& vConfigString, const std::string& vUserDatas) {
51 : 0 : return parseConfigDatas(vConfigString, vUserDatas);
52 : 0 : }
53 : :
54 : 0 : std::string SaveConfigString(const std::string& vUserDatas, const std::string& vFirstElement) {
55 : 0 : return ez::xml::Node(vFirstElement).addChilds(getXmlNodes(vUserDatas)).dump();
56 : 0 : }
57 : :
58 : 0 : bool LoadConfigFile(const std::string& vFilePathName, const std::string& vUserDatas) {
59 : 0 : bool res = true; // if not file found its ok
60 : 0 : std::ifstream docFile(vFilePathName, std::ios::in);
61 : 0 : if (docFile.is_open()) {
62 : 0 : std::stringstream strStream;
63 : 0 : strStream << docFile.rdbuf();
64 : 0 : res = LoadConfigString(strStream.str(), vUserDatas);
65 : 0 : if (!res) {
66 : 0 : LogVarError("%s", "Config.xml file parsing failed");
67 : 0 : }
68 : 0 : docFile.close();
69 : 0 : }
70 : 0 : return res;
71 : 0 : }
72 : :
73 : 0 : bool SaveConfigFile(const std::string& vFilePathName, const std::string& vUserDatas, const std::string& vFirstElement) {
74 : 0 : bool res = false;
75 : 0 : std::string data = SaveConfigString(vUserDatas, vFirstElement);
76 : 0 : std::ofstream configFileWriter(vFilePathName, std::ios::out);
77 : 0 : if (!configFileWriter.bad()) {
78 : 0 : configFileWriter << data;
79 : 0 : configFileWriter.close();
80 : 0 : res = true;
81 : 0 : }
82 : 0 : return res;
83 : 0 : }
84 : :
85 : 0 : bool parseConfigDatas(const std::string& vDatas, const std::string& vUserDatas) {
86 : 0 : bool res = false;
87 : 0 : std::string datas = vDatas;
88 : 0 : Node::replaceAll(datas, "\r\n", "\n");
89 : 0 : ez::Xml doc;
90 : 0 : res = doc.parseString(datas);
91 : 0 : if (res) {
92 : 0 : Node parent("root");
93 : 0 : RecursParsingConfig(doc.getRoot(), parent, vUserDatas);
94 : 0 : }
95 : 0 : return res;
96 : 0 : }
97 : :
98 : 0 : void RecursParsingConfig(const Node& vNode, const Node& vParent, const std::string& vUserDatas) {
99 : 0 : if (setFromXmlNodes(vNode, vParent, vUserDatas)) {
100 : 0 : RecursParsingConfigChilds(vNode, vUserDatas);
101 : 0 : }
102 : 0 : }
103 : :
104 : 0 : void RecursParsingConfigChilds(const Node& vNode, const std::string& vUserDatas) {
105 : 0 : // CHILDS
106 : 0 : // parse through all childs elements
107 : 0 : for (const auto& child : vNode.getChildren()) {
108 : 0 : RecursParsingConfig(child, vNode, vUserDatas);
109 : 0 : }
110 : 0 : }
111 : : };
112 : :
113 : : } // namespace xml
114 : : } // namespace ez
|