diff --git a/plugins/amda/src/AmdaResultParser.cpp b/plugins/amda/src/AmdaResultParser.cpp index ddc87f6..7b496a6 100644 --- a/plugins/amda/src/AmdaResultParser.cpp +++ b/plugins/amda/src/AmdaResultParser.cpp @@ -45,6 +45,25 @@ bool isCommentLine(const QString &line) return line.startsWith("#"); } +/// @return the number of lines to be read depending on the type of value passed in parameter +int nbValues(AmdaResultParser::ValueType valueType) noexcept +{ + switch (valueType) { + case AmdaResultParser::ValueType::SCALAR: + return 1; + case AmdaResultParser::ValueType::VECTOR: + return 3; + case AmdaResultParser::ValueType::UNKNOWN: + // Invalid case + break; + } + + // Invalid cases + qCCritical(LOG_AmdaResultParser()) + << QObject::tr("Can't get the number of values to read: unsupported type"); + return 0; +} + /** * Reads stream to retrieve x-axis unit * @param stream the stream to read @@ -74,10 +93,13 @@ Unit readXAxisUnit(QTextStream &stream) * @param stream the stream to read * @return the pair of vectors x-axis data/values data that has been read in the stream */ -QPair, QVector > readResults(QTextStream &stream) +QPair, QVector > > +readResults(QTextStream &stream, AmdaResultParser::ValueType valueType) { + auto expectedNbValues = nbValues(valueType); + auto xData = QVector{}; - auto valuesData = QVector{}; + auto valuesData = QVector >(expectedNbValues); QString line{}; @@ -85,27 +107,31 @@ QPair, QVector > readResults(QTextStream &stream) // Ignore comment lines if (!isCommentLine(line)) { auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts); - if (lineData.size() == 2) { + if (lineData.size() == expectedNbValues + 1) { // 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 is valid. Then, if value is invalid, it is set to NaN if (!std::isnan(x)) { xData.push_back(x); - if (!valueOk) { - qCWarning(LOG_AmdaResultParser()) - << QObject::tr( - "Value from line %1 is invalid and will be converted to NaN") - .arg(line); - value = std::numeric_limits::quiet_NaN(); + // Values + for (auto valueIndex = 0; valueIndex < expectedNbValues; ++valueIndex) { + auto column = valueIndex + 1; + + bool valueOk; + auto value = lineData.at(column).toDouble(&valueOk); + + if (!valueOk) { + qCWarning(LOG_AmdaResultParser()) + << QObject::tr( + "Value from (line %1, column %2) is invalid and will be " + "converted to NaN") + .arg(line, column); + value = std::numeric_limits::quiet_NaN(); + } + valuesData[valueIndex].append(value); } - - valuesData.push_back(value); } else { qCWarning(LOG_AmdaResultParser()) @@ -160,7 +186,8 @@ std::shared_ptr AmdaResultParser::readTxt(const QString &filePath, // Reads results stream.seek(0); // returns to the beginning of the file - auto results = readResults(stream); + auto results = readResults(stream, valueType); + return std::make_shared(std::move(results.first), std::move(results.second), xAxisUnit, Unit{});