CSV import dialog: try to guess the separator based on the file

This is taking a very simplistic approach. It picks the predominant
potential separator. If there is no clear winner, it uses the UI default
and makes the user pick (and either way, this can always be overwritten by
the user).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-01-07 08:56:01 -08:00
parent 925bb019c7
commit 481de7da74

View file

@ -341,11 +341,26 @@ void DiveLogImportDialog::loadFileContents() {
QStringList currColumns;
f.open(QFile::ReadOnly);
// guess the separator
QString firstLine = f.readLine();
QString separator;
int tabs = firstLine.count('\t');
int commas = firstLine.count(',');
int semis = firstLine.count(';');
if (tabs > commas && tabs > semis)
separator = "\t";
else if (commas > tabs && commas > semis)
separator = ",";
else if (semis > tabs && semis > commas)
separator = ";";
else
separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t" : ui->CSVSeparator->currentText();
if (ui->CSVSeparator->currentText() != separator)
ui->CSVSeparator->setCurrentText(separator);
f.reset();
int rows = 0;
while (rows < 10 || !f.atEnd()) {
QString currLine = f.readLine();
QString separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t"
: ui->CSVSeparator->currentText();
currColumns = currLine.split(separator);
fileColumns.append(currColumns);
rows += 1;