Clean up CSV import parameters

Continuing the crusade against excessive number of parameters for some
functions. This should be the last of the import functions to be cleaned
up.

Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Miika Turkia 2015-08-27 17:36:23 +03:00 committed by Dirk Hohndel
parent c5f92c7501
commit 8e08fdcc85
4 changed files with 77 additions and 53 deletions

2
dive.h
View file

@ -668,7 +668,7 @@ extern int parse_divinglog_buffer(sqlite3 *handle, const char *url, const char *
extern int parse_dlf_buffer(unsigned char *buffer, size_t size);
extern int parse_file(const char *filename);
extern int parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int o2Sensor1f, int o2Sensor2f, int o2Sensor3f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int setpointf, int sepidx, const char *csvtemplate, int unitidx, const char *hw);
extern int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate);
extern int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate);
extern int parse_txt_file(const char *filename, const char *csv);
extern int parse_manual_file(const char *filename, char **params, int pnr);

30
file.c
View file

@ -922,14 +922,14 @@ int init_csv_file_parsing(char **params, time_t *now, struct tm *timep, int time
return pnr - 1;
}
int parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int o2sensor1f, int o2sensor2f, int o2sensor3f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int setpointf, int sepidx, const char *csvtemplate, int unitidx, const char *hw)
int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate)
{
int ret, i;
struct memblock mem;
char *params[37];
time_t now;
struct tm *timep = NULL;
int previous;
char tmpbuf[MAXCOLDIGITS];
/* Increase the limits for recursion and variables on XSLT
* parsing */
@ -938,20 +938,24 @@ int parse_csv_file(const char *filename, int timef, int depthf, int tempf, int p
xsltMaxVars = 150000;
#endif
if (timef >= MAXCOLS || depthf >= MAXCOLS || tempf >= MAXCOLS || po2f >= MAXCOLS || o2sensor1f >= MAXCOLS || o2sensor2f >= MAXCOLS || o2sensor3f >= MAXCOLS || cnsf >= MAXCOLS || ndlf >= MAXCOLS || cnsf >= MAXCOLS || stopdepthf >= MAXCOLS || pressuref >= MAXCOLS || setpointf >= MAXCOLS)
return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
ret = init_csv_file_parsing(params, &now, timep, timef, depthf, tempf, po2f, o2sensor1f, o2sensor2f, o2sensor3f, cnsf, ndlf, ttsf, stopdepthf, pressuref, setpointf, sepidx, csvtemplate, unitidx);
if (strlen(hw)) {
params[ret++] = "hw";
params[ret++] = strdup(hw);
params[ret++] = NULL;
}
if (filename == NULL)
return report_error("No CSV filename");
time(&now);
timep = localtime(&now);
strftime(tmpbuf, MAXCOLDIGITS, "%Y%m%d", timep);
params[pnr++] = "date";
params[pnr++] = strdup(tmpbuf);
/* As the parameter is numeric, we need to ensure that the leading zero
* is not discarded during the transform, thus prepend time with 1 */
strftime(tmpbuf, MAXCOLDIGITS, "1%H%M", timep);
params[pnr++] = "time";
params[pnr++] = strdup(tmpbuf);
params[pnr++] = NULL;
mem.size = 0;
if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
return -1;

View file

@ -623,6 +623,50 @@ char *intdup(int index)
return strdup(tmpbuf);
}
int DiveLogImportDialog::setup_csv_params(QStringList r, char **params, int pnr)
{
params[pnr++] = strdup("timeField");
params[pnr++] = intdup(r.indexOf(tr("Sample time")));
params[pnr++] = strdup("depthField");
params[pnr++] = intdup(r.indexOf(tr("Sample depth")));
params[pnr++] = strdup("tempField");
params[pnr++] = intdup(r.indexOf(tr("Sample temperature")));
params[pnr++] = strdup("po2Field");
params[pnr++] = intdup(r.indexOf(tr("Sample pO₂")));
params[pnr++] = strdup("o2sensor1Field");
params[pnr++] = intdup(r.indexOf(tr("Sample sensor1 pO₂")));
params[pnr++] = strdup("o2sensor2Field");
params[pnr++] = intdup(r.indexOf(tr("Sample sensor2 pO₂")));
params[pnr++] = strdup("o2sensor3Field");
params[pnr++] = intdup(r.indexOf(tr("Sample sensor3 pO₂")));
params[pnr++] = strdup("cnsField");
params[pnr++] = intdup(r.indexOf(tr("Sample CNS")));
params[pnr++] = strdup("ndlField");
params[pnr++] = intdup(r.indexOf(tr("Sample NDL")));
params[pnr++] = strdup("ttsField");
params[pnr++] = intdup(r.indexOf(tr("Sample TTS")));
params[pnr++] = strdup("stopdepthField");
params[pnr++] = intdup(r.indexOf(tr("Sample stopdepth")));
params[pnr++] = strdup("pressureField");
params[pnr++] = intdup(r.indexOf(tr("Sample pressure")));
params[pnr++] = strdup("setpointFiend");
params[pnr++] = intdup(r.indexOf(tr("Sample setpoint")));
params[pnr++] = strdup("separatorIndex");
params[pnr++] = intdup(ui->CSVSeparator->currentIndex());
params[pnr++] = strdup("units");
params[pnr++] = intdup(ui->CSVUnits->currentIndex());
if (hw.length()) {
params[pnr++] = strdup("hw");
params[pnr++] = strdup(hw.toUtf8().data());
} else if (ui->knownImports->currentText().length() > 0) {
params[pnr++] = strdup("hw");
params[pnr++] = strdup(ui->knownImports->currentText().prepend("\"").append("\"").toUtf8().data());
}
params[pnr++] = NULL;
return pnr;
}
void DiveLogImportDialog::on_buttonBox_accepted()
{
QStringList r = resultModel->result();
@ -682,25 +726,12 @@ void DiveLogImportDialog::on_buttonBox_accepted()
sample->tts.seconds *= 60;
}
} else {
parse_csv_file(fileNames[i].toUtf8().data(),
r.indexOf(tr("Sample time")),
r.indexOf(tr("Sample depth")),
r.indexOf(tr("Sample temperature")),
r.indexOf(tr("Sample pO₂")),
r.indexOf(tr("Sample sensor1 pO₂")),
r.indexOf(tr("Sample sensor2 pO₂")),
r.indexOf(tr("Sample sensor3 pO₂")),
r.indexOf(tr("Sample CNS")),
r.indexOf(tr("Sample NDL")),
r.indexOf(tr("Sample TTS")),
r.indexOf(tr("Sample stopdepth")),
r.indexOf(tr("Sample pressure")),
r.indexOf(tr("Sample setpoint")),
ui->CSVSeparator->currentIndex(),
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv",
ui->CSVUnits->currentIndex(),
ui->knownImports->currentText().prepend("\"").append("\"").toUtf8().data()
);
char *params[37];
int pnr = 0;
pnr = setup_csv_params(r, params, pnr);
parse_csv_file(fileNames[i].toUtf8().data(), params, pnr - 1,
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv");
}
}
} else {
@ -761,26 +792,14 @@ void DiveLogImportDialog::on_buttonBox_accepted()
params[pnr++] = NULL;
parse_manual_file(fileNames[i].toUtf8().data(), params, pnr - 1);
} else
parse_csv_file(fileNames[i].toUtf8().data(),
r.indexOf(tr("Sample time")),
r.indexOf(tr("Sample depth")),
r.indexOf(tr("Sample temperature")),
r.indexOf(tr("Sample pO₂")),
r.indexOf(tr("Sample sensor1 pO₂")),
r.indexOf(tr("Sample sensor2 pO₂")),
r.indexOf(tr("Sample sensor3 pO₂")),
r.indexOf(tr("Sample CNS")),
r.indexOf(tr("Sample NDL")),
r.indexOf(tr("Sample TTS")),
r.indexOf(tr("Sample stopdepth")),
r.indexOf(tr("Sample pressure")),
r.indexOf(tr("Sample setpoint")),
ui->CSVSeparator->currentIndex(),
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv",
ui->CSVUnits->currentIndex(),
ui->knownImports->currentText().prepend("\"").append("\"").toUtf8().data()
);
} else {
char *params[37];
int pnr = 0;
pnr = setup_csv_params(r, params, pnr);
parse_csv_file(fileNames[i].toUtf8().data(), params, pnr - 1,
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv");
}
}
}

View file

@ -86,6 +86,7 @@ slots:
void loadFileContentsSeperatorSelected(int value);
void loadFileContentsKnownTypesSelected(int value);
void loadFileContents(int value, enum whatChanged triggeredBy);
int setup_csv_params(QStringList r, char **params, int pnr);
private:
bool selector;