Skip to content
Snippets Groups Projects
Commit 415debe9 authored by qt-info@nokia.com's avatar qt-info@nokia.com Committed by ck
Browse files

Maemo: Prepare completely per-device port specifications.

parent d8bdafd4
No related branches found
No related tags found
No related merge requests found
...@@ -91,6 +91,88 @@ private: ...@@ -91,6 +91,88 @@ private:
const quint64 m_id; const quint64 m_id;
}; };
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 ]
*/
QList<int> parse()
{
try {
if (!atEnd())
parseElemList();
} catch (ParseException &e) {
qWarning("Malformed ports specification: %s", e.error);
}
return m_ports;
}
private:
void parseElemList()
{
if (atEnd())
throw ParseException("Element list empty.");
parseElem();
if (atEnd())
return;
if (nextChar() != ',') {
throw ParseException("Element followed by something else "
"than a comma.");
}
++m_pos;
parseElemList();
}
void parseElem()
{
const int startPort = parsePort();
if (atEnd() || nextChar() != '-') {
m_ports << startPort;
return;
}
++m_pos;
const int endPort = parsePort();
if (endPort < startPort)
throw ParseException("Invalid range (end < start).");
for (int port = startPort; port <= endPort; ++port)
m_ports << port;
}
int parsePort()
{
if (atEnd())
throw ParseException("Empty port string.");
int port = 0;
char next = nextChar();
while (!atEnd() && std::isdigit(next)) {
port = 10*port + next - '0';
++m_pos;
next = nextChar();
}
if (port == 0 || port >= 2 << 16)
throw ParseException("Invalid port value.");
return port;
}
bool atEnd() const { return m_pos == m_portsSpec.length(); }
char nextChar() const { return m_portsSpec.at(m_pos).toAscii(); }
QList<int> m_ports;
int m_pos;
const QString &m_portsSpec;
};
MaemoDeviceConfig::MaemoDeviceConfig(const QString &name, MaemoDeviceConfig::DeviceType devType) MaemoDeviceConfig::MaemoDeviceConfig(const QString &name, MaemoDeviceConfig::DeviceType devType)
: name(name), : name(name),
type(devType), type(devType),
...@@ -131,6 +213,13 @@ MaemoDeviceConfig::MaemoDeviceConfig() ...@@ -131,6 +213,13 @@ MaemoDeviceConfig::MaemoDeviceConfig()
{ {
} }
QString MaemoDeviceConfig::portsRegExpr()
{
const QLatin1String portExpr("(\\d)+");
const QString listElemExpr = QString::fromLatin1("%1(-%1)?").arg(portExpr);
return QString::fromLatin1("((%1)(,%1)*)?").arg(listElemExpr);
}
int MaemoDeviceConfig::defaultSshPort(DeviceType type) const int MaemoDeviceConfig::defaultSshPort(DeviceType type) const
{ {
return type == Physical ? DefaultSshPortHW : DefaultSshPortSim; return type == Physical ? DefaultSshPortHW : DefaultSshPortSim;
...@@ -151,6 +240,11 @@ bool MaemoDeviceConfig::isValid() const ...@@ -151,6 +240,11 @@ bool MaemoDeviceConfig::isValid() const
return internalId != InvalidId; return internalId != InvalidId;
} }
QList<int> MaemoDeviceConfig::freePorts() const
{
return PortsSpecParser(portsSpec).parse();
}
void MaemoDeviceConfig::save(QSettings &settings) const void MaemoDeviceConfig::save(QSettings &settings) const
{ {
settings.setValue(NameKey, name); settings.setValue(NameKey, name);
......
...@@ -57,6 +57,8 @@ public: ...@@ -57,6 +57,8 @@ public:
MaemoDeviceConfig(const QSettings &settings, quint64 &nextId); MaemoDeviceConfig(const QSettings &settings, quint64 &nextId);
void save(QSettings &settings) const; void save(QSettings &settings) const;
bool isValid() const; bool isValid() const;
QList<int> freePorts() const;
static QString portsRegExpr();
static const quint64 InvalidId = 0; static const quint64 InvalidId = 0;
...@@ -64,6 +66,7 @@ public: ...@@ -64,6 +66,7 @@ public:
QString name; QString name;
DeviceType type; DeviceType type;
int debuggingPort; int debuggingPort;
QString portsSpec;
quint64 internalId; quint64 internalId;
private: private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment