AmdaResultParser.cpp
149 lines
| 4.0 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r380 | #include "AmdaResultParser.h" | ||
Alexandre Leroux
|
r948 | #include "AmdaResultParserHelper.h" | ||
Alexandre Leroux
|
r380 | |||
#include <QFile> | ||||
r1416 | #include <cmath> | |||
r400 | ||||
Alexandre Leroux
|
r380 | Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser") | ||
r1416 | namespace | |||
{ | ||||
Alexandre Leroux
|
r380 | |||
Alexandre Leroux
|
r446 | /// Message in result file when the file was not found on server | ||
const auto FILE_NOT_FOUND_MESSAGE = QStringLiteral("Not Found"); | ||||
Alexandre Leroux
|
r492 | /// Checks if a line is a comment line | ||
r1416 | bool isCommentLine(const QString& line) | |||
Alexandre Leroux
|
r492 | { | ||
return line.startsWith("#"); | ||||
} | ||||
Alexandre Leroux
|
r948 | /** | ||
* Creates helper that will be used to read AMDA file, according to the type passed as parameter | ||||
* @param valueType the type of values expected in the AMDA file (scalars, vectors, spectrograms...) | ||||
* @return the helper created | ||||
*/ | ||||
Alexandre Leroux
|
r1278 | std::unique_ptr<IAmdaResultParserHelper> createHelper(DataSeriesType valueType) | ||
Alexandre Leroux
|
r564 | { | ||
r1416 | switch (valueType) | |||
{ | ||||
Alexandre Leroux
|
r1278 | case DataSeriesType::SCALAR: | ||
Alexandre Leroux
|
r948 | return std::make_unique<ScalarParserHelper>(); | ||
Alexandre Leroux
|
r1278 | case DataSeriesType::SPECTROGRAM: | ||
Alexandre Leroux
|
r949 | return std::make_unique<SpectrogramParserHelper>(); | ||
Alexandre Leroux
|
r1278 | case DataSeriesType::VECTOR: | ||
Alexandre Leroux
|
r948 | return std::make_unique<VectorParserHelper>(); | ||
r1416 | case DataSeriesType::NONE: | |||
Alexandre Leroux
|
r564 | // Invalid case | ||
break; | ||||
} | ||||
// Invalid cases | ||||
qCCritical(LOG_AmdaResultParser()) | ||||
Alexandre Leroux
|
r948 | << QObject::tr("Can't create helper to read result file: unsupported type"); | ||
return nullptr; | ||||
Alexandre Leroux
|
r564 | } | ||
Alexandre Leroux
|
r393 | /** | ||
Alexandre Leroux
|
r948 | * Reads properties of the stream passed as parameter | ||
* @param helper the helper used to read properties line by line | ||||
Alexandre Leroux
|
r393 | * @param stream the stream to read | ||
*/ | ||||
r1416 | void readProperties(IAmdaResultParserHelper& helper, QTextStream& stream) | |||
Alexandre Leroux
|
r393 | { | ||
Alexandre Leroux
|
r948 | // Searches properties in the comment lines (as long as the reading has not reached the data) | ||
Alexandre Leroux
|
r791 | // AMDA V2: while (stream.readLineInto(&line) && !line.contains(DATA_HEADER_REGEX)) { | ||
r1416 | QString line {}; | |||
while (stream.readLineInto(&line) && isCommentLine(line)) | ||||
{ | ||||
Alexandre Leroux
|
r948 | helper.readPropertyLine(line); | ||
Alexandre Leroux
|
r393 | } | ||
} | ||||
Alexandre Leroux
|
r394 | /** | ||
Alexandre Leroux
|
r948 | * Reads results of the stream passed as parameter | ||
* @param helper the helper used to read results line by line | ||||
Alexandre Leroux
|
r394 | * @param stream the stream to read | ||
*/ | ||||
r1416 | void readResults(IAmdaResultParserHelper& helper, QTextStream& stream) | |||
Alexandre Leroux
|
r394 | { | ||
r1416 | QString line {}; | |||
Alexandre Leroux
|
r492 | |||
Alexandre Leroux
|
r775 | // Skip comment lines | ||
r1416 | while (stream.readLineInto(&line) && isCommentLine(line)) | |||
{ | ||||
Alexandre Leroux
|
r775 | } | ||
r1416 | if (!stream.atEnd()) | |||
{ | ||||
do | ||||
{ | ||||
Alexandre Leroux
|
r948 | helper.readResultLine(line); | ||
Alexandre Leroux
|
r775 | } while (stream.readLineInto(&line)); | ||
Alexandre Leroux
|
r394 | } | ||
} | ||||
Alexandre Leroux
|
r380 | } // namespace | ||
r1416 | std::shared_ptr<IDataSeries> AmdaResultParser::readTxt( | |||
const QString& filePath, DataSeriesType type) noexcept | ||||
Alexandre Leroux
|
r380 | { | ||
r1416 | if (type == DataSeriesType::NONE) | |||
{ | ||||
Alexandre Leroux
|
r563 | qCCritical(LOG_AmdaResultParser()) | ||
<< QObject::tr("Can't retrieve AMDA data: the type of values to be read is unknown"); | ||||
return nullptr; | ||||
} | ||||
r1416 | QFile file { filePath }; | |||
Alexandre Leroux
|
r380 | |||
r1416 | if (!file.open(QFile::ReadOnly | QIODevice::Text)) | |||
{ | ||||
Alexandre Leroux
|
r380 | qCCritical(LOG_AmdaResultParser()) | ||
<< QObject::tr("Can't retrieve AMDA data from file %1: %2") | ||||
.arg(filePath, file.errorString()); | ||||
return nullptr; | ||||
} | ||||
r1416 | return std::shared_ptr<IDataSeries> { AmdaResultParser::readTxt(QTextStream { &file }, type) }; | |||
r1351 | } | |||
r1416 | IDataSeries* AmdaResultParser::readTxt(QTextStream stream, DataSeriesType type) noexcept | |||
r1351 | { | |||
r1416 | if (type == DataSeriesType::NONE) | |||
r1351 | { | |||
return nullptr; | ||||
} | ||||
Alexandre Leroux
|
r380 | |||
Alexandre Leroux
|
r446 | // Checks if the file was found on the server | ||
auto firstLine = stream.readLine(); | ||||
r1351 | if (firstLine.compare(FILE_NOT_FOUND_MESSAGE) == 0) | |||
{ | ||||
Alexandre Leroux
|
r446 | return nullptr; | ||
} | ||||
Alexandre Leroux
|
r1278 | auto helper = createHelper(type); | ||
Alexandre Leroux
|
r948 | Q_ASSERT(helper != nullptr); | ||
Alexandre Leroux
|
r393 | |||
Alexandre Leroux
|
r948 | // Reads header file to retrieve properties | ||
Alexandre Leroux
|
r791 | stream.seek(0); // returns to the beginning of the file | ||
Alexandre Leroux
|
r948 | readProperties(*helper, stream); | ||
Alexandre Leroux
|
r564 | |||
Alexandre Leroux
|
r948 | // Checks properties | ||
r1416 | if (helper->checkProperties()) | |||
{ | ||||
Alexandre Leroux
|
r948 | // Reads results | ||
// AMDA V2: remove line | ||||
stream.seek(0); // returns to the beginning of the file | ||||
readResults(*helper, stream); | ||||
Alexandre Leroux
|
r380 | |||
Alexandre Leroux
|
r948 | // Creates data series | ||
return helper->createSeries(); | ||||
} | ||||
r1416 | else | |||
{ | ||||
Alexandre Leroux
|
r948 | return nullptr; | ||
} | ||||
Alexandre Leroux
|
r380 | } | ||