#2350. Renaming, robustify

This commit is contained in:
Bjørn Erik Jensen 2018-01-09 11:59:43 +01:00
parent 55147e5b70
commit 766f4da48b
2 changed files with 98 additions and 78 deletions

View File

@ -41,42 +41,52 @@ static QStringList splitLineAndTrim(const QString& line, const QString& separato
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
ElementPropertyMetadata RifElementPropertyTableReader::readMetadata(const QString& fileName) RifElementPropertyMetadata RifElementPropertyTableReader::readMetadata(const QString& fileName)
{ {
ElementPropertyMetadata metadata; RifElementPropertyMetadata metadata;
QFile* file = openFile(fileName); QFile* file = nullptr;
if (file) try
{ {
QTextStream stream(file); file = openFile(fileName);
bool metadataBlockFound = false;
int maxLinesToRead = 50;
int lineNo = 0;
while (lineNo < maxLinesToRead) if (file)
{ {
QString line = stream.readLine(); QTextStream stream(file);
lineNo++; bool metadataBlockFound = false;
int maxLinesToRead = 50;
int lineNo = 0;
if (line.toUpper().startsWith("*DISTRIBUTION TABLE")) while (lineNo < maxLinesToRead)
{ {
metadataBlockFound = true; QString line = stream.readLine();
continue; lineNo++;
if (line.toUpper().startsWith("*DISTRIBUTION TABLE"))
{
metadataBlockFound = true;
continue;
}
if (!metadataBlockFound) continue;
QStringList cols = splitLineAndTrim(line, ",");
metadata.fileName = fileName;
for (QString s : cols)
{
metadata.dataColumns.push_back(s);
}
break;
} }
if (!metadataBlockFound) continue; closeFile(file);
QStringList cols = splitLineAndTrim(line, ",");
metadata.fileName = fileName;
for (QString s : cols)
{
metadata.dataColumns.push_back(s);
}
break;
} }
}
catch(...)
{
closeFile(file); closeFile(file);
throw;
} }
return metadata; return metadata;
@ -85,72 +95,82 @@ ElementPropertyMetadata RifElementPropertyTableReader::readMetadata(const QStrin
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RifElementPropertyTableReader::readData(const ElementPropertyMetadata *metadata, ElementPropertyTable *table) void RifElementPropertyTableReader::readData(const RifElementPropertyMetadata *metadata, RifElementPropertyTable *table)
{ {
CVF_ASSERT(metadata && table); CVF_ASSERT(metadata && table);
QFile* file = openFile(metadata->fileName);
int expectedColumnCount = (int)metadata->dataColumns.size() + 1; int expectedColumnCount = (int)metadata->dataColumns.size() + 1;
QFile* file = nullptr;
if (file && expectedColumnCount > 0) try
{ {
QTextStream stream(file); file = openFile(metadata->fileName);
bool dataBlockFound = false;
int lineNo = 0;
// Init data vectors if (file && expectedColumnCount > 0)
table->elementIds.clear();
table->data = std::vector<std::vector<float>>(metadata->dataColumns.size());
while (!stream.atEnd())
{ {
QString line = stream.readLine(); QTextStream stream(file);
QStringList cols = splitLineAndTrim(line, ","); bool dataBlockFound = false;
lineNo++; int lineNo = 0;
if (!dataBlockFound) // Init data vectors
table->elementIds.clear();
table->data = std::vector<std::vector<float>>(metadata->dataColumns.size());
while (!stream.atEnd())
{ {
if (!line.startsWith("*") && cols.size() == expectedColumnCount) dataBlockFound = true; QString line = stream.readLine();
else continue; QStringList cols = splitLineAndTrim(line, ",");
} lineNo++;
if (cols.size() != expectedColumnCount) if (!dataBlockFound)
{
throw FileParseException(QString("Number of columns mismatch at %1:%2").arg(metadata->fileName).arg(lineNo));
}
for (int c = 0; c < expectedColumnCount; c++)
{
bool parseOk;
if (c == 0)
{ {
// Remove elementId column prefix if (!line.startsWith("*") && cols.size() == expectedColumnCount) dataBlockFound = true;
QStringList parts = cols[0].split("."); else continue;
int elementId = parts.last().toInt(&parseOk);
if (!parseOk)
{
throw FileParseException(QString("Parse failed at %1:%2").arg(metadata->fileName).arg(lineNo));
}
table->elementIds.push_back(elementId);
} }
else
if (cols.size() != expectedColumnCount)
{ {
float value = cols[c].toFloat(&parseOk); throw FileParseException(QString("Number of columns mismatch at %1:%2").arg(metadata->fileName).arg(lineNo));
if (!parseOk) }
for (int c = 0; c < expectedColumnCount; c++)
{
bool parseOk;
if (c == 0)
{ {
throw FileParseException(QString("Parse failed at %1:%2").arg(metadata->fileName).arg(lineNo)); // Remove elementId column prefix
QStringList parts = cols[0].split(".");
int elementId = parts.last().toInt(&parseOk);
if (!parseOk)
{
throw FileParseException(QString("Parse failed at %1:%2").arg(metadata->fileName).arg(lineNo));
}
table->elementIds.push_back(elementId);
}
else
{
float value = cols[c].toFloat(&parseOk);
if (!parseOk)
{
throw FileParseException(QString("Parse failed at %1:%2").arg(metadata->fileName).arg(lineNo));
}
table->data[c - 1].push_back(value);
} }
table->data[c - 1].push_back(value);
} }
} }
table->hasData = true;
} }
table->hasData = true; closeFile(file);
}
catch (...)
{
closeFile(file);
throw;
} }
closeFile(file);
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -29,8 +29,8 @@
#include <QString> #include <QString>
class ElementPropertyTable; class RifElementPropertyTable;
class ElementPropertyMetadata; class RifElementPropertyMetadata;
//================================================================================================== //==================================================================================================
/// ///
@ -38,8 +38,8 @@ class ElementPropertyMetadata;
class RifElementPropertyTableReader : cvf::Object class RifElementPropertyTableReader : cvf::Object
{ {
public: public:
static ElementPropertyMetadata readMetadata(const QString& filePath); static RifElementPropertyMetadata readMetadata(const QString& filePath);
static void readData(const ElementPropertyMetadata *metadata, ElementPropertyTable *table); static void readData(const RifElementPropertyMetadata *metadata, RifElementPropertyTable *table);
}; };
//================================================================================================== //==================================================================================================
@ -55,7 +55,7 @@ public:
//================================================================================================== //==================================================================================================
/// ///
//================================================================================================== //==================================================================================================
class ElementPropertyMetadata class RifElementPropertyMetadata
{ {
public: public:
QString fileName; QString fileName;
@ -66,12 +66,12 @@ public:
//================================================================================================== //==================================================================================================
/// ///
//================================================================================================== //==================================================================================================
class ElementPropertyTable class RifElementPropertyTable
{ {
public: public:
ElementPropertyTable() : hasData(false) {} RifElementPropertyTable() : hasData(false) {}
ElementPropertyMetadata metadata; RifElementPropertyMetadata metadata;
bool hasData; bool hasData;
std::vector<int> elementIds; std::vector<int> elementIds;
std::vector<std::vector<float>> data; std::vector<std::vector<float>> data;