AmdaResultParser.cpp
133 lines
| 4.0 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r380 | #include "AmdaResultParser.h" | ||
Alexandre Leroux
|
r937 | #include "AmdaResultParserHelper.h" | ||
Alexandre Leroux
|
r380 | |||
#include <QFile> | ||||
r400 | #include <cmath> | |||
Alexandre Leroux
|
r380 | Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser") | ||
namespace { | ||||
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 | ||
bool isCommentLine(const QString &line) | ||||
{ | ||||
return line.startsWith("#"); | ||||
} | ||||
Alexandre Leroux
|
r937 | /** | ||
* 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 | ||||
*/ | ||||
std::unique_ptr<IAmdaResultParserHelper> createHelper(AmdaResultParser::ValueType valueType) | ||||
Alexandre Leroux
|
r564 | { | ||
switch (valueType) { | ||||
case AmdaResultParser::ValueType::SCALAR: | ||||
Alexandre Leroux
|
r937 | return std::make_unique<ScalarParserHelper>(); | ||
Alexandre Leroux
|
r938 | case AmdaResultParser::ValueType::SPECTROGRAM: | ||
return std::make_unique<SpectrogramParserHelper>(); | ||||
Alexandre Leroux
|
r564 | case AmdaResultParser::ValueType::VECTOR: | ||
Alexandre Leroux
|
r937 | return std::make_unique<VectorParserHelper>(); | ||
Alexandre Leroux
|
r564 | case AmdaResultParser::ValueType::UNKNOWN: | ||
// Invalid case | ||||
break; | ||||
} | ||||
// Invalid cases | ||||
qCCritical(LOG_AmdaResultParser()) | ||||
Alexandre Leroux
|
r937 | << QObject::tr("Can't create helper to read result file: unsupported type"); | ||
return nullptr; | ||||
Alexandre Leroux
|
r564 | } | ||
Alexandre Leroux
|
r393 | /** | ||
Alexandre Leroux
|
r937 | * 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 | ||
*/ | ||||
Alexandre Leroux
|
r937 | void readProperties(IAmdaResultParserHelper &helper, QTextStream &stream) | ||
Alexandre Leroux
|
r393 | { | ||
Alexandre Leroux
|
r937 | // Searches properties in the comment lines (as long as the reading has not reached the data) | ||
Alexandre Leroux
|
r792 | // AMDA V2: while (stream.readLineInto(&line) && !line.contains(DATA_HEADER_REGEX)) { | ||
Alexandre Leroux
|
r937 | QString line{}; | ||
Alexandre Leroux
|
r792 | while (stream.readLineInto(&line) && isCommentLine(line)) { | ||
Alexandre Leroux
|
r937 | helper.readPropertyLine(line); | ||
Alexandre Leroux
|
r393 | } | ||
} | ||||
Alexandre Leroux
|
r394 | /** | ||
Alexandre Leroux
|
r937 | * 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 | ||
*/ | ||||
Alexandre Leroux
|
r937 | void readResults(IAmdaResultParserHelper &helper, QTextStream &stream) | ||
Alexandre Leroux
|
r394 | { | ||
QString line{}; | ||||
Alexandre Leroux
|
r492 | |||
Alexandre Leroux
|
r774 | // Skip comment lines | ||
while (stream.readLineInto(&line) && isCommentLine(line)) { | ||||
} | ||||
if (!stream.atEnd()) { | ||||
do { | ||||
Alexandre Leroux
|
r937 | helper.readResultLine(line); | ||
Alexandre Leroux
|
r774 | } while (stream.readLineInto(&line)); | ||
Alexandre Leroux
|
r394 | } | ||
} | ||||
Alexandre Leroux
|
r380 | } // namespace | ||
Alexandre Leroux
|
r563 | std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath, | ||
ValueType valueType) noexcept | ||||
Alexandre Leroux
|
r380 | { | ||
Alexandre Leroux
|
r563 | if (valueType == ValueType::UNKNOWN) { | ||
qCCritical(LOG_AmdaResultParser()) | ||||
<< QObject::tr("Can't retrieve AMDA data: the type of values to be read is unknown"); | ||||
return nullptr; | ||||
} | ||||
Alexandre Leroux
|
r380 | QFile file{filePath}; | ||
if (!file.open(QFile::ReadOnly | QIODevice::Text)) { | ||||
qCCritical(LOG_AmdaResultParser()) | ||||
<< QObject::tr("Can't retrieve AMDA data from file %1: %2") | ||||
.arg(filePath, file.errorString()); | ||||
return nullptr; | ||||
} | ||||
QTextStream stream{&file}; | ||||
Alexandre Leroux
|
r446 | // Checks if the file was found on the server | ||
auto firstLine = stream.readLine(); | ||||
if (firstLine.compare(FILE_NOT_FOUND_MESSAGE) == 0) { | ||||
qCCritical(LOG_AmdaResultParser()) | ||||
<< QObject::tr("Can't retrieve AMDA data from file %1: file was not found on server") | ||||
.arg(filePath); | ||||
return nullptr; | ||||
} | ||||
Alexandre Leroux
|
r937 | auto helper = createHelper(valueType); | ||
Q_ASSERT(helper != nullptr); | ||||
Alexandre Leroux
|
r393 | |||
Alexandre Leroux
|
r937 | // Reads header file to retrieve properties | ||
Alexandre Leroux
|
r792 | stream.seek(0); // returns to the beginning of the file | ||
Alexandre Leroux
|
r937 | readProperties(*helper, stream); | ||
Alexandre Leroux
|
r564 | |||
Alexandre Leroux
|
r937 | // Checks properties | ||
if (helper->checkProperties()) { | ||||
// Reads results | ||||
// AMDA V2: remove line | ||||
stream.seek(0); // returns to the beginning of the file | ||||
readResults(*helper, stream); | ||||
Alexandre Leroux
|
r380 | |||
Alexandre Leroux
|
r937 | // Creates data series | ||
return helper->createSeries(); | ||||
} | ||||
else { | ||||
return nullptr; | ||||
} | ||||
Alexandre Leroux
|
r380 | } | ||