Remove excessive amount of parameters

This removes the excessive amount of parameters on manual CSV import. We
just use appropriate string array than can be directly fed to XSLT
parsing.

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-23 20:56:18 +03:00 committed by Dirk Hohndel
parent e47f62f869
commit 5bda148020
3 changed files with 76 additions and 150 deletions

3
dive.h
View file

@ -671,8 +671,7 @@ 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_seabear_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 sepidx, const char *csvtemplate, int unitidx, const char *delta, const char *hw);
extern int parse_txt_file(const char *filename, const char *csv);
extern int parse_manual_file(const char *filename, int separator_index, int units, int dateformat, int durationformat, int number, int date, int time, int duration, int location, int gps, int maxdepth, int meandepth, int divemaster, int buddy, int suit, int notes, int weight, int tags, int cylsizef, int startpresf, int endpresf, int o2f, int hef, int airtempf, int watertempf);
extern int parse_manual_file(const char *filename, char **params, int pnr);
extern int save_dives(const char *filename);
extern int save_dives_logic(const char *filename, bool select_only);
extern int save_dive(FILE *f, struct dive *dive);

128
file.c
View file

@ -1106,79 +1106,16 @@ int parse_seabear_csv_file(const char *filename, int timef, int depthf, int temp
return ret;
}
int parse_manual_file(const char *filename, int sepidx, int units, int dateformat, int durationformat,
int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf,
int divemasterf, int buddyf, int suitf, int notesf, int weightf, int tagsf, int cylsizef, int startpresf, int endpresf,
int o2f, int hef, int airtempf, int watertempf)
int parse_manual_file(const char *filename, char **params, int pnr)
{
if (verbose > 4) {
fprintf(stderr, "filename %s, sepidx %d, units %d, dateformat %d, durationformat %d\n", filename, sepidx, units, dateformat, durationformat);
fprintf(stderr, "numberf %d, datef %d, timef %d, durationf %d, locationf %d, gpsf %d, maxdepthf %d, meandepthf %d\n", numberf, datef, timef, durationf, locationf, gpsf, maxdepthf, meandepthf);
fprintf(stderr, "divemasterf %d, buddyf %d, suitf %d, notesf %d, weightf %d, tagsf %d, cylsizef %d, startpresf %d, endpresf %d\n", divemasterf, buddyf, suitf, notesf, weightf, tagsf, cylsizef, startpresf, endpresf);
fprintf(stderr, "o2f %d, hef %d, airtempf %d, watertempf %d\n", o2f, hef, airtempf, watertempf);
}
struct memblock mem;
int pnr = 0;
char *params[55];
char numberbuf[MAXCOLDIGITS];
char datebuf[MAXCOLDIGITS];
char timebuf[MAXCOLDIGITS];
char durationbuf[MAXCOLDIGITS];
char locationbuf[MAXCOLDIGITS];
char gpsbuf[MAXCOLDIGITS];
char maxdepthbuf[MAXCOLDIGITS];
char meandepthbuf[MAXCOLDIGITS];
char divemasterbuf[MAXCOLDIGITS];
char buddybuf[MAXCOLDIGITS];
char suitbuf[MAXCOLDIGITS];
char notesbuf[MAXCOLDIGITS];
char weightbuf[MAXCOLDIGITS];
char tagsbuf[MAXCOLDIGITS];
char separator_index[MAXCOLDIGITS];
char unit[MAXCOLDIGITS];
char datefmt[MAXCOLDIGITS];
char durationfmt[MAXCOLDIGITS];
char cylsizebuf[MAXCOLDIGITS];
char startpresbuf[MAXCOLDIGITS];
char endpresbuf[MAXCOLDIGITS];
char o2buf[MAXCOLDIGITS];
char hebuf[MAXCOLDIGITS];
char airtempbuf[MAXCOLDIGITS];
char watertempbuf[MAXCOLDIGITS];
time_t now;
struct tm *timep;
char curdate[9];
char curtime[6];
int ret;
int ret, i;
if (numberf >= MAXCOLS || datef >= MAXCOLS || timef >= MAXCOLS || durationf >= MAXCOLS || locationf >= MAXCOLS || gpsf >= MAXCOLS || maxdepthf >= MAXCOLS || meandepthf >= MAXCOLS || buddyf >= MAXCOLS || suitf >= MAXCOLS || notesf >= MAXCOLS || weightf >= MAXCOLS || tagsf >= MAXCOLS || cylsizef >= MAXCOLS || startpresf >= MAXCOLS || endpresf >= MAXCOLS || o2f >= MAXCOLS || hef >= MAXCOLS || airtempf >= MAXCOLS || watertempf >= MAXCOLS)
return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
snprintf(numberbuf, MAXCOLDIGITS, "%d", numberf);
snprintf(datebuf, MAXCOLDIGITS, "%d", datef);
snprintf(timebuf, MAXCOLDIGITS, "%d", timef);
snprintf(durationbuf, MAXCOLDIGITS, "%d", durationf);
snprintf(locationbuf, MAXCOLDIGITS, "%d", locationf);
snprintf(gpsbuf, MAXCOLDIGITS, "%d", gpsf);
snprintf(maxdepthbuf, MAXCOLDIGITS, "%d", maxdepthf);
snprintf(meandepthbuf, MAXCOLDIGITS, "%d", meandepthf);
snprintf(divemasterbuf, MAXCOLDIGITS, "%d", divemasterf);
snprintf(buddybuf, MAXCOLDIGITS, "%d", buddyf);
snprintf(suitbuf, MAXCOLDIGITS, "%d", suitf);
snprintf(notesbuf, MAXCOLDIGITS, "%d", notesf);
snprintf(weightbuf, MAXCOLDIGITS, "%d", weightf);
snprintf(tagsbuf, MAXCOLDIGITS, "%d", tagsf);
snprintf(separator_index, MAXCOLDIGITS, "%d", sepidx);
snprintf(unit, MAXCOLDIGITS, "%d", units);
snprintf(datefmt, MAXCOLDIGITS, "%d", dateformat);
snprintf(durationfmt, MAXCOLDIGITS, "%d", durationformat);
snprintf(cylsizebuf, MAXCOLDIGITS, "%d", cylsizef);
snprintf(startpresbuf, MAXCOLDIGITS, "%d", startpresf);
snprintf(endpresbuf, MAXCOLDIGITS, "%d", endpresf);
snprintf(o2buf, MAXCOLDIGITS, "%d", o2f);
snprintf(hebuf, MAXCOLDIGITS, "%d", hef);
snprintf(airtempbuf, MAXCOLDIGITS, "%d", airtempf);
snprintf(watertempbuf, MAXCOLDIGITS, "%d", watertempf);
time(&now);
timep = localtime(&now);
strftime(curdate, DATESTR, "%Y%m%d", timep);
@ -1187,60 +1124,11 @@ int parse_manual_file(const char *filename, int sepidx, int units, int dateforma
* is not discarded during the transform, thus prepend time with 1 */
strftime(curtime, TIMESTR, "1%H%M", timep);
params[pnr++] = "numberField";
params[pnr++] = numberbuf;
params[pnr++] = "dateField";
params[pnr++] = datebuf;
params[pnr++] = "timeField";
params[pnr++] = timebuf;
params[pnr++] = "durationField";
params[pnr++] = durationbuf;
params[pnr++] = "locationField";
params[pnr++] = locationbuf;
params[pnr++] = "gpsField";
params[pnr++] = gpsbuf;
params[pnr++] = "maxDepthField";
params[pnr++] = maxdepthbuf;
params[pnr++] = "meanDepthField";
params[pnr++] = meandepthbuf;
params[pnr++] = "divemasterField";
params[pnr++] = divemasterbuf;
params[pnr++] = "buddyField";
params[pnr++] = buddybuf;
params[pnr++] = "suitField";
params[pnr++] = suitbuf;
params[pnr++] = "notesField";
params[pnr++] = notesbuf;
params[pnr++] = "weightField";
params[pnr++] = weightbuf;
params[pnr++] = "tagsField";
params[pnr++] = tagsbuf;
params[pnr++] = "date";
params[pnr++] = curdate;
params[pnr++] = "time";
params[pnr++] = curtime;
params[pnr++] = "separatorIndex";
params[pnr++] = separator_index;
params[pnr++] = "units";
params[pnr++] = unit;
params[pnr++] = "datefmt";
params[pnr++] = datefmt;
params[pnr++] = "durationfmt";
params[pnr++] = durationfmt;
params[pnr++] = "cylindersizeField";
params[pnr++] = cylsizebuf;
params[pnr++] = "startpressureField";
params[pnr++] = startpresbuf;
params[pnr++] = "endpressureField";
params[pnr++] = endpresbuf;
params[pnr++] = "o2Field";
params[pnr++] = o2buf;
params[pnr++] = "heField";
params[pnr++] = hebuf;
params[pnr++] = "airtempField";
params[pnr++] = airtempbuf;
params[pnr++] = "watertempField";
params[pnr++] = watertempbuf;
params[pnr++] = strdup("date");
params[pnr++] = strdup(curdate);
params[pnr++] = strdup("time");
params[pnr++] = strdup(curtime);
params[pnr++] = NULL;
if (filename == NULL)
@ -1253,5 +1141,7 @@ int parse_manual_file(const char *filename, int sepidx, int units, int dateforma
ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
free(mem.buffer);
for (i = 0; i < pnr - 2; ++i)
free(params[i]);
return ret;
}

View file

@ -614,6 +614,15 @@ void DiveLogImportDialog::loadFileContents(int value, whatChanged triggeredBy)
resultModel->setData(resultModel->index(0, i),headers.at(i),Qt::EditRole);
}
char *intdup(int index)
{
char tmpbuf[21];
snprintf(tmpbuf, sizeof(tmpbuf) - 2, "%d", index);
tmpbuf[20] = 0;
return strdup(tmpbuf);
}
void DiveLogImportDialog::on_buttonBox_accepted()
{
QStringList r = resultModel->result();
@ -672,35 +681,63 @@ void DiveLogImportDialog::on_buttonBox_accepted()
}
} else {
for (int i = 0; i < fileNames.size(); ++i) {
if (r.indexOf(tr("Sample time")) < 0)
parse_manual_file(fileNames[i].toUtf8().data(),
ui->CSVSeparator->currentIndex(),
ui->CSVUnits->currentIndex(),
ui->DateFormat->currentIndex(),
ui->DurationFormat->currentIndex(),
r.indexOf(tr("Dive #")),
r.indexOf(tr("Date")),
r.indexOf(tr("Time")),
r.indexOf(tr("Duration")),
r.indexOf(tr("Location")),
r.indexOf(tr("GPS")),
r.indexOf(tr("Max. depth")),
r.indexOf(tr("Avg. depth")),
r.indexOf(tr("Divemaster")),
r.indexOf(tr("Buddy")),
r.indexOf(tr("Suit")),
r.indexOf(tr("Notes")),
r.indexOf(tr("Weight")),
r.indexOf(tr("Tags")),
r.indexOf(tr("Cyl. size")),
r.indexOf(tr("Start pressure")),
r.indexOf(tr("End pressure")),
r.indexOf(tr("O₂")),
r.indexOf(tr("He")),
r.indexOf(tr("Air temp.")),
r.indexOf(tr("Water temp."))
);
else
if (r.indexOf(tr("Sample time")) < 0) {
char *params[55];
int pnr = 0;
params[pnr++] = strdup("numberField");
params[pnr++] = intdup(r.indexOf(tr("Dive #")));
params[pnr++] = strdup("dateField");
params[pnr++] = intdup(r.indexOf(tr("Date")));
params[pnr++] = strdup("timeField");
params[pnr++] = intdup(r.indexOf(tr("Time")));
params[pnr++] = strdup("durationField");
params[pnr++] = intdup(r.indexOf(tr("Duration")));
params[pnr++] = strdup("locationField");
params[pnr++] = intdup(r.indexOf(tr("Location")));
params[pnr++] = strdup("gpsField");
params[pnr++] = intdup(r.indexOf(tr("GPS")));
params[pnr++] = strdup("maxDepthField");
params[pnr++] = intdup(r.indexOf(tr("Max. depth")));
params[pnr++] = strdup("meanDepthField");
params[pnr++] = intdup(r.indexOf(tr("Avg. depth")));
params[pnr++] = strdup("divemasterField");
params[pnr++] = intdup(r.indexOf(tr("Divemaster")));
params[pnr++] = strdup("buddyField");
params[pnr++] = intdup(r.indexOf(tr("Buddy")));
params[pnr++] = strdup("suitField");
params[pnr++] = intdup(r.indexOf(tr("Suit")));
params[pnr++] = strdup("notesField");
params[pnr++] = intdup(r.indexOf(tr("Notes")));
params[pnr++] = strdup("weightField");
params[pnr++] = intdup(r.indexOf(tr("Weight")));
params[pnr++] = strdup("tagsField");
params[pnr++] = intdup(r.indexOf(tr("Tags")));
params[pnr++] = strdup("separatorIndex");
params[pnr++] = intdup(ui->CSVSeparator->currentIndex());
params[pnr++] = strdup("units");
params[pnr++] = intdup(ui->CSVUnits->currentIndex());
params[pnr++] = strdup("datefmt");
params[pnr++] = intdup(ui->DateFormat->currentIndex());
params[pnr++] = strdup("durationfmt");
params[pnr++] = intdup(ui->DurationFormat->currentIndex());
params[pnr++] = strdup("cylindersizeField");
params[pnr++] = intdup(r.indexOf(tr("Cyl. size")));
params[pnr++] = strdup("startpressureField");
params[pnr++] = intdup(r.indexOf(tr("Start pressure")));
params[pnr++] = strdup("endpressureField");
params[pnr++] = intdup(r.indexOf(tr("End pressure")));
params[pnr++] = strdup("o2Field");
params[pnr++] = intdup(r.indexOf(tr("O₂")));
params[pnr++] = strdup("heField");
params[pnr++] = intdup(r.indexOf(tr("He")));
params[pnr++] = strdup("airtempField");
params[pnr++] = intdup(r.indexOf(tr("Air temp.")));
params[pnr++] = strdup("watertempField");
params[pnr++] = intdup(r.indexOf(tr("Water temp.")));
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")),