summaryrefslogtreecommitdiff
path: root/basic_functions.cpp
blob: c6640b097f9dd17af0bded98a5756aab7c8929fe (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "basic_functions.h"

// qt
#include <QDebug>
#include <QDir>

bool copyDir(const QString& src, const QString& dst)
{
    QDir dir(src);

    if (!dir.exists())
    {
        qDebug() << "src dir doesn't exist:" << src;
        return false;
    }

    for (const auto& d : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
    {
        QString dst_path = dst + QDir::separator() + d;

        if (!dir.mkpath(dst_path))
        {
            qDebug() << "cannot make path:" << dst_path;
            return false;
        }

        if (!copyDir(src + QDir::separator() + d, dst_path))
        {
            qDebug() << "cannot copy dir " << src + QDir::separator() + d
                     << "to" << dst_path;
            return false;
        }
    }

    for (const auto f : dir.entryList(QDir::Files))
    {
        if (!QFile::copy(
                src + QDir::separator() + f,
                dst + QDir::separator() + f
            ))
        {
            qDebug() << "cannot copy file" << src + QDir::separator() + f
                     << "to" << dst + QDir::separator() + f;
            return false;
        }
    }

    return true;
}

QHostAddress getLocalNetworkIntefaceBySubnet(const QHostAddress& remote)
{
    const auto allInterfaces = QNetworkInterface::allInterfaces();

    for (const auto& i : allInterfaces)
    {
        if (!(i.flags() & QNetworkInterface::IsUp))
        {
            continue;
        }

        for (const auto& a : i.addressEntries())
        {
            if (remote.isInSubnet(a.ip(), a.netmask().toIPv4Address()))
            {
                return a.ip();
            }
        }
    }

    return QHostAddress();
}

quint16 CalculateChecksum(quint16* usBuf, int size)
{
    unsigned long usChksum = 0;
    while (size > 1)
    {
        usChksum += *usBuf++;
        size -= sizeof(quint16);
    }

    if (size)
    {
        usChksum += *(quint8*)usBuf;
    }

    usChksum = (usChksum >> 16) + (usChksum & 0xffff);
    usChksum += (usChksum >> 16);

    return quint8(~usChksum);
}

quint16 bit_reverse_word(quint16 value)
{
    const quint16 mask0 = 0x5555;
    const quint16 mask1 = 0x3333;
    const quint16 mask2 = 0x0F0F;
    const quint16 mask3 = 0x00FF;

    auto tmp = value;

    tmp = (((~mask0) & tmp) >> 1) | ((mask0 & tmp) << 1);
    tmp = (((~mask1) & tmp) >> 2) | ((mask1 & tmp) << 2);
    tmp = (((~mask2) & tmp) >> 4) | ((mask2 & tmp) << 4);
    tmp = (((~mask3) & tmp) >> 8) | ((mask3 & tmp) << 8);

    return tmp;
}

quint8 reverse(quint8 b)
{
    b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
    b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
    b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
    return b;
}

const QJsonObject point2json(const QPointF& point)
{
    return QJsonObject{{"x", point.x()}, {"y", point.y()}};
}

const QPointF pointFromJson(const QJsonObject& json)
{
    return QPointF(json["x"].toDouble(), json["y"].toDouble());
}

const QJsonObject line2json(const QLineF& line)
{
    return QJsonObject{
        {"p1", point2json(line.p1())},
        {"p2", point2json(line.p2())},
    };
}

const QLineF lineFromJson(const QJsonObject& json)
{
    return QLineF(
        pointFromJson(json["p1"].toObject()),
        pointFromJson(json["p2"].toObject())
    );
}

const QJsonValue real2Json(const qreal& real)
{
    return qIsNaN(real) ? QJsonValue(QJsonValue::Undefined) : real;
}

qreal realFromJson(const QJsonValue& json)
{
    return json.toDouble(std::numeric_limits<qreal>::quiet_NaN());
}

uint qHash(const QVector3D& v) noexcept
{
    return qHash(v.x() + v.y() + v.z(), 0xa03f);  // arbitrary value
}