forked from jackburton79/inventory-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXML.cpp
More file actions
114 lines (83 loc) · 2.29 KB
/
XML.cpp
File metadata and controls
114 lines (83 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* XML.cpp
*
* Created on: 01/ago/2014
* Author: Stefano Ceccherini
*/
#include "Support.h"
#include "XML.h"
#include <iostream>
#include <streambuf>
#include <string>
#include <string.h>
#include <zlib.h>
class ElementFinder : public tinyxml2::XMLVisitor {
public:
ElementFinder(std::string elementName);
virtual bool VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* attr);
std::string Response() const;
private:
std::string fElementName;
std::string fResponse;
};
ElementFinder::ElementFinder(std::string elementName)
:
XMLVisitor(),
fElementName(elementName),
fResponse("")
{
}
/* virtual */
bool
ElementFinder::VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute*)
{
if (fElementName.compare(element.Name()) == 0) {
fResponse = element.GetText();
return false;
}
return true;
}
std::string
ElementFinder::Response() const
{
return fResponse;
}
bool
XML::Compress(const tinyxml2::XMLDocument& document, char*& destination, size_t& destLength)
{
tinyxml2::XMLPrinter memoryPrinter;
document.Print(&memoryPrinter);
int fileSize = memoryPrinter.CStrSize() - 1;
destLength = compressBound(fileSize);
destination = new char[destLength];
if (int compressStatus = compress((Bytef*)destination, (uLongf*)&destLength,
(const Bytef*)memoryPrinter.CStr(), (uLong)fileSize) != Z_OK) {
std::cerr << "Compress returned error: " << zError(compressStatus) << std::endl;
delete[] destination;
return false;
}
return true;
}
bool
XML::Uncompress(const char* source, size_t sourceLen, tinyxml2::XMLDocument& document)
{
size_t destLength = 32768;
char* destination = new char[destLength];
if (int status = uncompress((Bytef*)destination, (uLongf*)&destLength,
(const Bytef*)source, (uLong)sourceLen) != Z_OK) {
std::cerr << "UncompressXml: Failed to decompress XML: ";
std::cerr << zError(status) << std::endl;
delete[] destination;
return false;
}
tinyxml2::XMLError result = document.Parse(destination, destLength - 1);
delete[] destination;
return result == tinyxml2::XML_SUCCESS;
}
std::string
XML::GetTextElementValue(const tinyxml2::XMLDocument& document, std::string elementName)
{
ElementFinder responseFinder(elementName);
document.Accept(&responseFinder);
return responseFinder.Response();
}