-
Notifications
You must be signed in to change notification settings - Fork 2
/
portlist.cpp
232 lines (198 loc) · 5.33 KB
/
portlist.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://www.qt.io
**
** This file is part of Qt Enterprise Embedded.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io
**
****************************************************************************/
#include "portlist.h"
#include <QList>
#include <QPair>
#include <QString>
#include <cctype>
namespace Utils {
namespace Internal {
namespace {
typedef QPair<int, int> Range;
class PortsSpecParser
{
struct ParseException {
ParseException(const char *error) : error(error) {}
const char * const error;
};
public:
PortsSpecParser(const QString &portsSpec)
: m_pos(0), m_portsSpec(portsSpec) { }
/*
* Grammar: Spec -> [ ElemList ]
* ElemList -> Elem [ ',' ElemList ]
* Elem -> Port [ '-' Port ]
*/
bool parse()
{
if (!atEnd()) {
if (!parseElemList()) {
qWarning("Malformed ports specification");
return false;
}
}
return true;
}
const PortList &portList() const
{
return m_portList;
}
private:
bool parseElemList()
{
if (atEnd()) {
qWarning("Element list empty.");
return false;
}
if (!parseElem())
return false;
if (atEnd())
return true;
if (nextChar() != ',') {
qWarning("Element followed by something else than a comma.");
return false;
}
++m_pos;
return parseElemList();
}
bool parseElem()
{
const int startPort = parsePort();
if (startPort < 0)
return false;
if (atEnd() || nextChar() != '-') {
m_portList.addPort(startPort);
return true;
}
++m_pos;
const int endPort = parsePort();
if (endPort < 0)
return false;
if (endPort < startPort) {
qWarning("Invalid range (end < start).");
return false;
}
m_portList.addRange(startPort, endPort);
return true;
}
int parsePort()
{
if (atEnd()) {
qWarning("Empty port string.");
return -1;
}
int port = 0;
do {
const char next = nextChar();
if (!std::isdigit(next))
break;
port = 10*port + next - '0';
++m_pos;
} while (!atEnd());
if (port == 0 || port >= 2 << 16) {
qWarning("Invalid port value.");
return -1;
}
return port;
}
bool atEnd() const { return m_pos == m_portsSpec.length(); }
char nextChar() const { return m_portsSpec.at(m_pos).toLatin1(); }
PortList m_portList;
int m_pos;
const QString &m_portsSpec;
};
} // anonymous namespace
class PortListPrivate
{
public:
QList<Range> ranges;
};
} // namespace Internal
PortList::PortList() : d(new Internal::PortListPrivate)
{
}
PortList::PortList(const PortList &other) : d(new Internal::PortListPrivate(*other.d))
{
}
PortList::~PortList()
{
delete d;
}
PortList &PortList::operator=(const PortList &other)
{
*d = *other.d;
return *this;
}
PortList PortList::fromString(const QString &portsSpec)
{
Internal::PortsSpecParser p(portsSpec);
if (!p.parse()) {
qWarning("Could not parse string");
}
return p.portList();
}
void PortList::addPort(int port) { addRange(port, port); }
void PortList::addRange(int startPort, int endPort)
{
d->ranges << Internal::Range(startPort, endPort);
}
bool PortList::hasMore() const { return !d->ranges.isEmpty(); }
bool PortList::contains(int port) const
{
foreach (const Internal::Range &r, d->ranges) {
if (port >= r.first && port <= r.second)
return true;
}
return false;
}
int PortList::count() const
{
int n = 0;
foreach (const Internal::Range &r, d->ranges)
n += r.second - r.first + 1;
return n;
}
int PortList::getNext()
{
Q_ASSERT(!d->ranges.isEmpty());
Internal::Range &firstRange = d->ranges.first();
const int next = firstRange.first++;
if (firstRange.first > firstRange.second)
d->ranges.removeFirst();
return next;
}
QString PortList::toString() const
{
QString stringRep;
foreach (const Internal::Range &range, d->ranges) {
stringRep += QString::number(range.first);
if (range.second != range.first)
stringRep += QLatin1Char('-') + QString::number(range.second);
stringRep += QLatin1Char(',');
}
if (!stringRep.isEmpty())
stringRep.remove(stringRep.length() - 1, 1); // Trailing comma.
return stringRep;
}
QString PortList::regularExpression()
{
const QLatin1String portExpr("(\\d)+");
const QString listElemExpr = QString::fromLatin1("%1(-%1)?").arg(portExpr);
return QString::fromLatin1("((%1)(,%1)*)?").arg(listElemExpr);
}
} // namespace Utils