-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.cpp
96 lines (76 loc) · 2.21 KB
/
lookup.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
#include "lookup.h"
Lookup::Lookup(QObject *parent) : QObject(parent)
{
// Initialize vector
entriesList = new TEntriesList;
}
Lookup::~Lookup() {
delete entriesList;
}
bool Lookup::loadFile(QString path) {
// Open file
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
// Read file line by line
QTextStream in(&file);
while(!in.atEnd()) {
// Read current line
QString line = in.readLine();
QStringList cells = line.split(",");
TEntry tmp;
tmp.first.first = cells[0];
tmp.first.second = cells[1];
tmp.second = cells[2];
// Add to vector
entriesList->push_back(tmp);
}
// Close file
file.close();
return true;
}
TEntriesList *Lookup::getList() {
return entriesList;
}
TEntriesList Lookup::searchByCallsign(QString callsign) {
// Trim string
if (callsign.length() > 3)
callsign = callsign.left(3);
// Remove number
int trPos = callsign.lastIndexOf(QRegularExpression("\\d"));
callsign = callsign.left(trPos);
// Convert to upper case
callsign = callsign.toUpper();
// Create result object
TEntriesList result;
// Iterate over list
for (auto it = entriesList->begin(); it != entriesList->end(); it++) {
QString delimiterA = it->first.first,
delimiterB = it->first.second;
// Trim if required
int csLen = callsign.length();
if (csLen < 3) {
delimiterA = delimiterA.left(csLen);
delimiterB = delimiterB.left(csLen);
}
// Compare strings
if (callsign.compare(delimiterA) >= 0 && callsign.compare(delimiterB) <= 0)
result.push_back(*it);
}
return result;
}
TEntriesList Lookup::searchByCountry(QString country) {
// Normalize string
country = country.toLower();
// Create result object
TEntriesList result;
// Iterate over list
for (auto it = entriesList->begin(); it != entriesList->end(); it++) {
int pos = 0;
while((pos = it->second.toLower().indexOf(country, pos)) != -1) {
result.push_back(*it);
pos++;
}
}
return result;
}