1
0
Fork 0
forked from len0rd/rockbox

rbutil: Simplify reading rockbox-info.txt.

Simplify, and replace use of QRegExp with QRegularExpression for Qt6
compatibility.

Also fix the test running on Windows. RockboxInfo constructs the
filename from path and filename, so we cannot pass an empty path, since
that results in an invalid path. On Linux / MacOS this works only
because we use an absolute path.

Change-Id: Ieaf30a2df005291d3e997aabf42d64ee832381c2
This commit is contained in:
Dominik Riebeling 2021-12-21 10:52:27 +01:00
parent dc677208d0
commit f2798c225a
2 changed files with 47 additions and 37 deletions

View file

@ -18,15 +18,18 @@
#include "rockboxinfo.h" #include "rockboxinfo.h"
#include <QtCore> #include <QRegularExpression>
#include <QDebug> #include <QString>
#include <QFile>
#include "Logger.h" #include "Logger.h"
RockboxInfo::RockboxInfo(QString mountpoint, QString fname) RockboxInfo::RockboxInfo(QString mountpoint, QString fname) :
m_ram(0),
m_voicefmt(0),
m_success(false)
{ {
LOG_INFO() << "Getting version info from rockbox-info.txt"; LOG_INFO() << "Getting version info from rockbox-info.txt";
QFile file(mountpoint + "/" + fname); QFile file(mountpoint + "/" + fname);
m_success = false;
m_voicefmt = 400; // default value for compatibility m_voicefmt = 400; // default value for compatibility
if(!file.exists()) if(!file.exists())
return; return;
@ -35,44 +38,47 @@ RockboxInfo::RockboxInfo(QString mountpoint, QString fname)
return; return;
// read file contents // read file contents
QRegExp hash("^Version:\\s+(r?)([0-9a-fM]+)"); QRegularExpression parts("^([A-Z][a-z ]+):\\s+(.*)");
QRegExp version("^Version:\\s+(\\S.*)");
QRegExp release("^Version:\\s+([0-9\\.]+)\\s*$");
QRegExp target("^Target:\\s+(\\S.*)");
QRegExp features("^Features:\\s+(\\S.*)");
QRegExp targetid("^Target id:\\s+(\\S.*)");
QRegExp memory("^Memory:\\s+(\\S.*)");
QRegExp voicefmt("^Voice format:\\s+(\\S.*)");
while (!file.atEnd()) while (!file.atEnd())
{ {
QString line = file.readLine().trimmed(); QString line = file.readLine().trimmed();
if(version.indexIn(line) >= 0) { auto match = parts.match(line);
m_version = version.cap(1); if(!match.isValid())
{
continue;
} }
if(release.indexIn(line) >= 0) {
m_release = release.cap(1); if(match.captured(1) == "Version") {
m_version = match.captured(2);
if(match.captured(2).contains(".")) {
// version number
m_release = match.captured(2);
}
if(match.captured(2).contains("-")) {
// hash-date format. Revision is first part.
m_revision = match.captured(2).split("-").at(0);
if(m_revision.startsWith("r")) {
m_revision.remove(0, 1);
}
}
} }
if(hash.indexIn(line) >= 0) { else if(match.captured(1) == "Target") {
// git hashes are usually at least 7 characters. m_target = match.captured(2);
// svn revisions are expected to be at least 4 digits.
if(hash.cap(2).size() > 3)
m_revision = hash.cap(2);
} }
else if(target.indexIn(line) >= 0) { else if(match.captured(1) == "Features") {
m_target = target.cap(1); m_features = match.captured(2);
} }
else if(features.indexIn(line) >= 0) { else if(match.captured(1) == "Target id") {
m_features = features.cap(1); m_targetid = match.captured(2);
} }
else if(targetid.indexIn(line) >= 0) { else if(match.captured(1) == "Memory") {
m_targetid = targetid.cap(1); m_ram = match.captured(2).toInt();
} }
else if(memory.indexIn(line) >= 0) { else if(match.captured(1) == "Voice format") {
m_ram = memory.cap(1).toInt(); m_voicefmt = match.captured(2).toInt();
}
else if(voicefmt.indexIn(line) >= 0) {
m_voicefmt = voicefmt.cap(1).toInt();
} }
} }

View file

@ -20,6 +20,7 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <QObject> #include <QObject>
#include <QFileInfo>
#include "rockboxinfo.h" #include "rockboxinfo.h"
@ -83,12 +84,12 @@ void TestRockboxInfo::testVersion()
QFETCH(QString, release); QFETCH(QString, release);
QTemporaryFile tf(this); QTemporaryFile tf(this);
tf.open(); tf.open();
QString filename = tf.fileName(); QFileInfo finfo(tf);
tf.write(input.toLatin1()); tf.write(input.toLatin1());
tf.write("\n"); tf.write("\n");
tf.close(); tf.close();
RockboxInfo info("", filename); RockboxInfo info(finfo.path(), finfo.fileName());
QCOMPARE(info.version(), QString(version)); QCOMPARE(info.version(), QString(version));
QCOMPARE(info.revision(), QString(revision)); QCOMPARE(info.revision(), QString(revision));
QCOMPARE(info.release(), QString(release)); QCOMPARE(info.release(), QString(release));
@ -112,13 +113,14 @@ void TestRockboxInfo::testTarget()
QFETCH(QString, target); QFETCH(QString, target);
QTemporaryFile tf(this); QTemporaryFile tf(this);
tf.open(); tf.open();
QFileInfo finfo(tf);
QString filename = tf.fileName(); QString filename = tf.fileName();
tf.write(prefix.at(j).toLatin1()); tf.write(prefix.at(j).toLatin1());
tf.write(target.toLatin1()); tf.write(target.toLatin1());
tf.write("\n"); tf.write("\n");
tf.close(); tf.close();
RockboxInfo info("", filename); RockboxInfo info(finfo.path(), finfo.fileName());
QCOMPARE(info.target(), target); QCOMPARE(info.target(), target);
} }
} }
@ -141,13 +143,14 @@ void TestRockboxInfo::testMemory()
QFETCH(QString, memory); QFETCH(QString, memory);
QTemporaryFile tf(this); QTemporaryFile tf(this);
tf.open(); tf.open();
QFileInfo finfo(tf);
QString filename = tf.fileName(); QString filename = tf.fileName();
tf.write(prefix.at(j).toLatin1()); tf.write(prefix.at(j).toLatin1());
tf.write(memory.toLatin1()); tf.write(memory.toLatin1());
tf.write("\n"); tf.write("\n");
tf.close(); tf.close();
RockboxInfo info("", filename); RockboxInfo info(finfo.path(), finfo.fileName());
QCOMPARE(info.ram(), memory.toInt()); QCOMPARE(info.ram(), memory.toInt());
} }
} }
@ -168,13 +171,14 @@ void TestRockboxInfo::testFeatures()
QFETCH(QString, features); QFETCH(QString, features);
QTemporaryFile tf(this); QTemporaryFile tf(this);
tf.open(); tf.open();
QFileInfo finfo(tf);
QString filename = tf.fileName(); QString filename = tf.fileName();
tf.write(prefix.at(j).toLatin1()); tf.write(prefix.at(j).toLatin1());
tf.write(features.toLatin1()); tf.write(features.toLatin1());
tf.write("\n"); tf.write("\n");
tf.close(); tf.close();
RockboxInfo info("", filename); RockboxInfo info(finfo.path(), finfo.fileName());
QCOMPARE(info.features(), features); QCOMPARE(info.features(), features);
} }
} }