From 41e897ecff03f4323f02a1efe42bcd1c8e415906 2017-08-02 15:12:51 From: Alexandre Leroux Date: 2017-08-02 15:12:51 Subject: [PATCH] Improves AMDA result parsing Finds the unit among all the commented lines, in order to handle the case where the number of commented lines differs from one file to another --- diff --git a/plugins/amda/src/AmdaResultParser.cpp b/plugins/amda/src/AmdaResultParser.cpp index 61fb44b..abe18c1 100644 --- a/plugins/amda/src/AmdaResultParser.cpp +++ b/plugins/amda/src/AmdaResultParser.cpp @@ -39,6 +39,12 @@ double doubleDate(const QString &stringDate) noexcept : std::numeric_limits::quiet_NaN(); } +/// Checks if a line is a comment line +bool isCommentLine(const QString &line) +{ + return line.startsWith("#"); +} + /** * Reads stream to retrieve x-axis unit * @param stream the stream to read @@ -49,20 +55,16 @@ Unit readXAxisUnit(QTextStream &stream) { QString line{}; - if (stream.readLineInto(&line)) { + // Searches unit in the comment lines + while (stream.readLineInto(&line) && isCommentLine(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"); } + qCWarning(LOG_AmdaResultParser()) << QObject::tr("The unit could not be found in the file"); + // Error cases return Unit{{}, true}; } @@ -78,32 +80,36 @@ QPair, QVector > readResults(QTextStream &stream) auto valuesData = QVector{}; QString line{}; + while (stream.readLineInto(&line)) { - auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts); - if (lineData.size() == 2) { - // X : the data is converted from date to double (in secs) - auto x = doubleDate(lineData.at(0)); - - // Value - bool valueOk; - auto value = lineData.at(1).toDouble(&valueOk); - - // Adds result only if x and value are valid - if (!std::isnan(x) && !std::isnan(value) && valueOk) { - xData.push_back(x); - valuesData.push_back(value); + // Ignore comment lines + if (!isCommentLine(line)) { + auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts); + if (lineData.size() == 2) { + // X : the data is converted from date to double (in secs) + auto x = doubleDate(lineData.at(0)); + + // Value + bool valueOk; + auto value = lineData.at(1).toDouble(&valueOk); + + // Adds result only if x and value are valid + if (!std::isnan(x) && !std::isnan(value) && valueOk) { + xData.push_back(x); + valuesData.push_back(value); + } + else { + qCWarning(LOG_AmdaResultParser()) + << QObject::tr( + "Can't retrieve results from line %1: x and/or value are invalid") + .arg(line); + } } else { qCWarning(LOG_AmdaResultParser()) - << QObject::tr( - "Can't retrieve results from line %1: x and/or value are invalid") - .arg(line); + << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line); } } - else { - qCWarning(LOG_AmdaResultParser()) - << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line); - } } return qMakePair(std::move(xData), std::move(valuesData)); @@ -133,13 +139,12 @@ std::shared_ptr AmdaResultParser::readTxt(const QString &filePath) return nullptr; } - // Ignore comments lines - stream.readLine(); - // Reads x-axis unit + stream.seek(0); // returns to the beginning of the file auto xAxisUnit = readXAxisUnit(stream); // Reads results + stream.seek(0); // returns to the beginning of the file auto results = readResults(stream); return std::make_shared(std::move(results.first), std::move(results.second),