Skip to content
Snippets Groups Projects
Commit 6377d4f8 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

add separator guessing for CSV parsing

parent 17df05c8
No related branches found
No related tags found
1 merge request!11Reflectometry import - read/write project, show import warnings/errors, item selection, UI improvements
......@@ -502,8 +502,61 @@ bool QREDataLoader::fillImportDetailsTable(QTableWidget* table, bool fileContent
void QREDataLoader::guessSettings(const QString& filename)
{
// #baimport implement estimateSettings (separator)
// search for lines which have start with numbers, then try the separators
// search for lines which start with a number, then try the separators
QFile file(filename);
if (!file.open(QFile::ReadOnly | QIODevice::Text))
return; // silent fail
QTextStream in(&file);
int lineNr = 0;
const int maxLinesToExamine = 100;
while (!in.atEnd() && lineNr < maxLinesToExamine) {
lineNr++;
QString line = in.readLine().trimmed();
if (line.isEmpty())
continue;
if (!line[0].isNumber())
continue;
// line starts with a number => search gap after it
int startOfGap = 1;
while (startOfGap < line.size() && line[startOfGap].isNumber())
startOfGap++;
if (startOfGap == line.size())
continue;
int endOfGap = startOfGap;
while (endOfGap < line.size() && !line[endOfGap].isNumber()) {
endOfGap++;
}
QStringRef gapContent(&line, startOfGap, endOfGap - startOfGap + 1);
if (gapContent.isEmpty())
continue;
if (gapContent.contains("\t")) {
m_importSettings.separator = "\t";
return;
}
gapContent = gapContent.trimmed();
if (gapContent.isEmpty()) {
m_importSettings.separator = " ";
return;
}
if (gapContent.contains(";")) {
m_importSettings.separator = ";";
return;
}
if (gapContent.contains(",")) {
m_importSettings.separator = ",";
return;
}
}
}
void QREDataLoader::parseFile(QString& fileContent) const
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment