This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.cpp
53 lines (46 loc) · 1.45 KB
/
utils.cpp
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
#include "stdafx.h"
#include <iostream>
#include <iterator>
#include <rapidxml.hpp>
#include <rapidxml_print.hpp>
#include <sstream>
#include <vector>
#include "utils.h"
namespace Utils
{
QString formatXML(const QString &text)
{
// try to format XML document
int startPos = text.indexOf(QChar('<'));
int endPos = text.lastIndexOf(QChar('>'));
if (startPos > 0 && endPos > startPos)
{
QString header = text.mid(0, startPos);
QString xmlIn = text.mid(startPos, endPos - startPos + 1);
#ifndef QT_NO_DEBUG
qDebug() << "raw text:" << text;
qDebug() << "xml in:" << xmlIn;
#endif
using namespace rapidxml;
QString xmlOut;
try
{
xml_document<> doc;
std::istringstream in(xmlIn.toStdString());
std::vector<char> buffer((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
buffer.push_back('\0');
doc.parse<0>(&buffer[0]);
print(std::back_inserter(xmlOut), doc, 0);
}
catch (...)
{
return text;
}
header.append("\n");
header.append(xmlOut);
if (header.length() > text.length())
return header;
}
return text;
}
} // namespace Utils