File: convertthread.cpp

package info (click to toggle)
ktorrent 25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,104 kB
  • sloc: cpp: 40,482; xml: 1,163; python: 182; sh: 10; makefile: 5
file content (159 lines) | stat: -rw-r--r-- 4,176 bytes parent folder | download
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
159
/*
    SPDX-FileCopyrightText: 2007 Joris Guisson <joris.guisson@gmail.com>
    SPDX-FileCopyrightText: 2007 Ivan Vasic <ivasic@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <algorithm>
#include <errno.h>
#include <regex>
#include <string>

#include <QFile>
#include <QTextStream>
#include <QTimer>

#include <KIO/Job>
#include <KLocalizedString>

#include "convertdialog.h"
#include "convertthread.h"
#include <interfaces/functions.h>
#include <util/constants.h>
#include <util/fileops.h>
#include <util/log.h>

using namespace bt;

namespace kt
{
ConvertThread::ConvertThread(ConvertDialog *dlg)
    : dlg(dlg)
    , abort(false)
{
    txt_file = kt::DataDir() + QStringLiteral("level1.txt");
    dat_file = kt::DataDir() + QStringLiteral("level1.dat");
    tmp_file = kt::DataDir() + QStringLiteral("level1.dat.tmp");
}

ConvertThread::~ConvertThread()
{
}

void ConvertThread::run()
{
    readInput();
    writeOutput();
}

void ConvertThread::readInput()
{
    /*    READ INPUT FILE  */
    QFile source(txt_file);
    if (!source.open(QIODevice::ReadOnly)) {
        Out(SYS_IPF | LOG_IMPORTANT) << "Cannot find level1.txt file" << endl;
        failure_reason = i18n("Cannot open %1: %2", txt_file, QString::fromLatin1(strerror(errno)));
        return;
    }

    Out(SYS_IPF | LOG_NOTICE) << "Loading " << txt_file << " ..." << endl;
    dlg->message(i18n("Loading txt file..."));

    ulong source_size = source.size();
    QTextStream stream(&source);

    int i = 0;
    const std::regex rx("(?:[0-9]{1,3}\\.){3}[0-9]{1,3}");

    while (!stream.atEnd() && !abort) {
        std::string line = stream.readLine().toStdString();
        i += line.length() * sizeof(char); // rough estimation of string size
        dlg->progress(i, source_size);
        ++i;

        std::vector<std::string> addresses;
        for (auto it = std::sregex_iterator(line.begin(), line.end(), rx); it != std::sregex_iterator(); ++it) {
            addresses.push_back(it->str());
        }

        // if we have found two addresses, create a block out of it
        if (addresses.size() == 2) {
            input += IPBlock(QString::fromStdString(addresses[0]), QString::fromStdString(addresses[1]));
        }
    }
    source.close();
    Out(SYS_IPF | LOG_NOTICE) << "Loaded " << input.count() << " lines" << endl;
    dlg->progress(100, 100);
}

static bool LessThan(const IPBlock &a, const IPBlock &b)
{
    if (a.ip1 == b.ip1)
        return a.ip2 < b.ip2;
    else
        return a.ip1 < b.ip1;
}

void ConvertThread::sort()
{
    std::sort(input.begin(), input.end(), LessThan);
}

void ConvertThread::merge()
{
    if (input.count() < 2) // noting to merge
        return;

    QList<IPBlock>::iterator i = input.begin();
    QList<IPBlock>::iterator j = i;
    j++;
    while (j != input.end() && i != input.end()) {
        IPBlock &a = *i;
        IPBlock &b = *j;
        if (a.ip2 < b.ip1 || b.ip2 < a.ip1) {
            // separate ranges, so go to the next pair
            i = j;
            j++;
        } else {
            // merge b into a
            a.ip1 = (a.ip1 < b.ip1) ? a.ip1 : b.ip1;
            a.ip2 = (a.ip2 > b.ip2) ? a.ip2 : b.ip2;

            // remove b
            j = input.erase(j);
        }
    }
}

void ConvertThread::writeOutput()
{
    if (input.count() == 0) {
        failure_reason = i18n("There are no IP addresses to convert in %1", txt_file);
        return;
    }

    sort(); // sort the block
    merge(); // merge neighbouring blocks

    QFile target(dat_file);
    if (!target.open(QIODevice::WriteOnly)) {
        Out(SYS_IPF | LOG_IMPORTANT) << "Unable to open file for writing" << endl;
        failure_reason = i18n("Cannot open %1: %2", dat_file, QString::fromLatin1(strerror(errno)));
        return;
    }

    Out(SYS_IPF | LOG_NOTICE) << "Loading finished, starting conversion..." << endl;
    dlg->message(i18n("Converting..."));

    int i = 0;
    int tot = input.count();
    for (const IPBlock &block : std::as_const(input)) {
        dlg->progress(i, tot);
        target.write((char *)&block, sizeof(IPBlock));
        if (abort) {
            return;
        }
        i++;
    }
}
}