-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathshared.cpp
117 lines (94 loc) · 2.04 KB
/
shared.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
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
#include "shared.h"
#include <QCryptographicHash>
#include <QBuffer>
#include <QFile>
#include <QDebug>
namespace Docx{
Parented::Parented()
{
}
Parented *Parented::part()
{
return nullptr;
}
Parented::~Parented()
{
//delete m_parent;
}
/*!
* \brief 查找是否有eleName节点,如果没有则创建
* \param dom
* \param parent
* \param eleName
* \param addToFirst
* \return
*/
QDomElement addOrAssignElement(QDomDocument *dom, QDomElement *parent, const QString &eleName, bool addToFirst)
{
QDomNodeList elements = parent->elementsByTagName(eleName);
if (elements.isEmpty()) {
QDomElement ele = dom->createElement(eleName);
if (addToFirst) {
QDomNode firstnode = parent->firstChild();
parent->insertBefore(ele, firstnode);
} else {
parent->appendChild(ele);
}
return ele;
} else
return elements.at(0).toElement();
}
/*!
* \brief 文件的Hash.
* \param fileName
* \return
*/
QByteArray getFileHash(const QString &fileName)
{
QByteArray byteArray;
QFile file(fileName);
if (!file.exists())
return byteArray;
if (file.open(QIODevice::ReadOnly)) {
byteArray = QCryptographicHash::hash(file.readAll(), QCryptographicHash::Md5);
file.close();
}
return byteArray;
}
/*!
* \brief 图片的Hash
* \param img
* \return
*/
QByteArray imageHash(const QImage &img)
{
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer);
return QCryptographicHash::hash(ba, QCryptographicHash::Md5);
}
/*!
* \brief QByteArray的Hash
* \param bytes
* \return
*/
QByteArray byteHash(const QByteArray &bytes)
{
return QCryptographicHash::hash(bytes, QCryptographicHash::Md5);
}
InvalidSpanError::InvalidSpanError(const QString &errorStr)
: m_error(errorStr)
{
qDebug() << m_error;
}
void InvalidSpanError::raise() const
{
qDebug() << m_error;
throw *this;
}
QException *InvalidSpanError::clone() const
{
return new InvalidSpanError(*this);
}
}