diff --git a/plugins/amda/src/AmdaResultParser.cpp b/plugins/amda/src/AmdaResultParser.cpp index b314aaa..8c1b114 100644 --- a/plugins/amda/src/AmdaResultParser.cpp +++ b/plugins/amda/src/AmdaResultParser.cpp @@ -4,6 +4,7 @@ #include #include +#include Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser") @@ -12,6 +13,13 @@ namespace { /// Format for dates in result files const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz"); +/// Regex to find unit in a line. Examples of valid lines: +/// ... - Units : nT - ... +/// ... -Units:nT- ... +/// ... -Units: m²- ... +/// ... - Units : m/s - ... +const auto UNIT_REGEX = QRegularExpression{QStringLiteral("-\\s*Units\\s*:\\s*(.+?)\\s*-")}; + /// @todo ALX double doubleDate(const QString &stringDate) noexcept { @@ -19,6 +27,34 @@ double doubleDate(const QString &stringDate) noexcept return dateTime.toMSecsSinceEpoch() / 1000.; } +/** + * Reads stream to retrieve x-axis unit + * @param stream the stream to read + * @return the unit that has been read in the stream, a default unit (time unit with no label) if an + * error occured during reading + */ +Unit readXAxisUnit(QTextStream &stream) +{ + QString line{}; + + if (stream.readLineInto(&line)) { + auto match = UNIT_REGEX.match(line); + if (match.hasMatch()) { + return Unit{match.captured(1), true}; + } + else { + qCWarning(LOG_AmdaResultParser()) + << QObject::tr("Can't read unit: invalid line %1").arg(line); + } + } + else { + qCWarning(LOG_AmdaResultParser()) << QObject::tr("Can't read unit: end of file"); + } + + // Error cases + return Unit{{}, true}; +} + } // namespace std::shared_ptr AmdaResultParser::readTxt(const QString &filePath) noexcept @@ -37,12 +73,16 @@ std::shared_ptr AmdaResultParser::readTxt(const QString &filePath) QTextStream stream{&file}; - // Ignore comment lines (3 lines) - stream.readLine(); + // Ignore first two lines (comments lines) stream.readLine(); stream.readLine(); QString line{}; + + // Reads x-axis unit + auto xAxisUnit = readXAxisUnit(stream); + + // Reads results auto lineRegex = QRegExp{QStringLiteral("\\s+")}; while (stream.readLineInto(&line)) { auto lineData = line.split(lineRegex, QString::SkipEmptyParts); @@ -58,7 +98,6 @@ std::shared_ptr AmdaResultParser::readTxt(const QString &filePath) } } - /// @todo ALX : handle units - return std::make_shared(std::move(xData), std::move(valuesData), Unit{"nT", true}, + return std::make_shared(std::move(xData), std::move(valuesData), xAxisUnit, Unit{}); }