diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-10-17 20:15:23 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-23 18:17:02 -0700 |
commit | d508a16aca91b521719d929cd59db381c44cd7ad (patch) | |
tree | af076eb66fcd4002a01a29a46f8b80d4d4129994 /desktop-widgets | |
parent | b9b51ffd4ebd14e085bef69ee0daf6a1927cc960 (diff) | |
download | subsurface-d508a16aca91b521719d929cd59db381c44cd7ad.tar.gz |
parser: replace params[] code by new xml_params struct
This fixes a load of memory holes, and makes the code
(hopefully) more readable.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets')
-rw-r--r-- | desktop-widgets/divelogimportdialog.cpp | 219 | ||||
-rw-r--r-- | desktop-widgets/divelogimportdialog.h | 5 |
2 files changed, 80 insertions, 144 deletions
diff --git a/desktop-widgets/divelogimportdialog.cpp b/desktop-widgets/divelogimportdialog.cpp index 0cb481397..eccdd2264 100644 --- a/desktop-widgets/divelogimportdialog.cpp +++ b/desktop-widgets/divelogimportdialog.cpp @@ -15,6 +15,7 @@ #include "core/divesite.h" #include "core/trip.h" #include "core/import-csv.h" +#include "core/xmlparams.h" static QString subsurface_mimedata = "subsurface/csvcolumns"; static QString subsurface_index = "subsurface/csvindex"; @@ -801,64 +802,37 @@ void DiveLogImportDialog::loadFileContents(int value, whatChanged triggeredBy) resultModel->setData(resultModel->index(0, i),headers.at(i),Qt::EditRole); } -int DiveLogImportDialog::setup_csv_params(QStringList r, char **params, int pnr) -{ - params[pnr++] = strdup("dateField"); - params[pnr++] = intdup(r.indexOf(tr("Date"))); - params[pnr++] = strdup("datefmt"); - params[pnr++] = intdup(ui->DateFormat->currentIndex()); - params[pnr++] = strdup("starttimeField"); - params[pnr++] = intdup(r.indexOf(tr("Time"))); - params[pnr++] = strdup("numberField"); - params[pnr++] = intdup(r.indexOf(tr("Dive #"))); - 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("heartBeat"); - params[pnr++] = intdup(r.indexOf(tr("Sample heartrate"))); - params[pnr++] = strdup("setpointField"); - params[pnr++] = intdup(r.indexOf(tr("Sample setpoint"))); - params[pnr++] = strdup("visibilityField"); - params[pnr++] = intdup(r.indexOf(tr("Visibility"))); - params[pnr++] = strdup("ratingField"); - params[pnr++] = intdup(r.indexOf(tr("Rating"))); - 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++] = copy_qstring(hw); - } else if (ui->knownImports->currentText().length() > 0) { - params[pnr++] = strdup("hw"); - params[pnr++] = copy_qstring(ui->knownImports->currentText().prepend("\"").append("\"")); - } - params[pnr++] = NULL; - - return pnr; -} -int DiveLogImportDialog::parseTxtHeader(QString fileName, char **params, int pnr) +void DiveLogImportDialog::setup_csv_params(QStringList r, xml_params ¶ms) +{ + xml_params_add_int(¶ms, "dateField", r.indexOf(tr("Date"))); + xml_params_add_int(¶ms, "datefmt", ui->DateFormat->currentIndex()); + xml_params_add_int(¶ms, "starttimeField", r.indexOf(tr("Time"))); + xml_params_add_int(¶ms, "numberField", r.indexOf(tr("Dive #"))); + xml_params_add_int(¶ms, "timeField", r.indexOf(tr("Sample time"))); + xml_params_add_int(¶ms, "depthField", r.indexOf(tr("Sample depth"))); + xml_params_add_int(¶ms, "tempField", r.indexOf(tr("Sample temperature"))); + xml_params_add_int(¶ms, "po2Field", r.indexOf(tr("Sample pO₂"))); + xml_params_add_int(¶ms, "o2sensor1Field", r.indexOf(tr("Sample sensor1 pO₂"))); + xml_params_add_int(¶ms, "o2sensor2Field", r.indexOf(tr("Sample sensor2 pO₂"))); + xml_params_add_int(¶ms, "o2sensor3Field", r.indexOf(tr("Sample sensor3 pO₂"))); + xml_params_add_int(¶ms, "cnsField", r.indexOf(tr("Sample CNS"))); + xml_params_add_int(¶ms, "ndlField", r.indexOf(tr("Sample NDL"))); + xml_params_add_int(¶ms, "ttsField", r.indexOf(tr("Sample TTS"))); + xml_params_add_int(¶ms, "stopdepthField", r.indexOf(tr("Sample stopdepth"))); + xml_params_add_int(¶ms, "pressureField", r.indexOf(tr("Sample pressure"))); + xml_params_add_int(¶ms, "heartBeat", r.indexOf(tr("Sample heartrate"))); + xml_params_add_int(¶ms, "setpointField", r.indexOf(tr("Sample setpoint"))); + xml_params_add_int(¶ms, "visibilityField", r.indexOf(tr("Visibility"))); + xml_params_add_int(¶ms, "ratingField", r.indexOf(tr("Rating"))); + xml_params_add_int(¶ms, "separatorIndex", ui->CSVSeparator->currentIndex()); + xml_params_add_int(¶ms, "units", ui->CSVUnits->currentIndex()); + if (hw.length()) + xml_params_add(¶ms, "hw", qPrintable(hw)); + else if (ui->knownImports->currentText().length() > 0) + xml_params_add(¶ms, "hw", qPrintable(ui->knownImports->currentText().prepend("\"").append("\""))); +} + +void DiveLogImportDialog::parseTxtHeader(QString fileName, xml_params ¶ms) { QFile f(fileName); QString date; @@ -897,11 +871,8 @@ int DiveLogImportDialog::parseTxtHeader(QString fileName, char **params, int pnr } f.close(); - params[pnr++] = strdup("date"); - params[pnr++] = strdup(date.toLatin1()); - params[pnr++] = strdup("time"); - params[pnr++] = strdup(time.toLatin1()); - return pnr; + xml_params_add(¶ms, "date", qPrintable(date)); + xml_params_add(¶ms, "time", qPrintable(time)); } void DiveLogImportDialog::on_buttonBox_accepted() @@ -919,20 +890,17 @@ void DiveLogImportDialog::on_buttonBox_accepted() QPair<QString, QString> pair = poseidonFileNames(fileNames[i]); parse_txt_file(qPrintable(pair.second), qPrintable(pair.first), &table, &trips, &sites); } else { - char *params[50]; - int pnr = 0; + xml_params params; QRegExp apdRe("^.*[/\\][0-9a-zA-Z]*_([0-9]{6})_([0-9]{6})\\.apd"); if (txtLog) { - pnr = parseTxtHeader(fileNames[i], params, pnr); + parseTxtHeader(fileNames[i], params); } else if (apdRe.exactMatch(fileNames[i])) { - params[pnr++] = strdup("date"); - params[pnr++] = strdup("20" + apdRe.cap(1).toLatin1()); - params[pnr++] = strdup("time"); - params[pnr++] = strdup("1" + apdRe.cap(2).toLatin1()); + xml_params_add(¶ms, "date", qPrintable("20" + apdRe.cap(1))); + xml_params_add(¶ms, "time", qPrintable("1" + apdRe.cap(2))); } - pnr = setup_csv_params(r, params, pnr); - parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1, + setup_csv_params(r, params); + parse_csv_file(qPrintable(fileNames[i]), ¶ms, specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv", &table, &trips, &sites, &filter_presets); } @@ -940,82 +908,49 @@ void DiveLogImportDialog::on_buttonBox_accepted() } else { for (int i = 0; i < fileNames.size(); ++i) { if (r.indexOf(tr("Sample time")) < 0) { - char *params[61]; - 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("modeField"); - params[pnr++] = intdup(r.indexOf(tr("Mode"))); - 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++] = strdup("visibilityField"); - params[pnr++] = intdup(r.indexOf(tr("Visibility"))); - params[pnr++] = strdup("ratingField"); - params[pnr++] = intdup(r.indexOf(tr("Rating"))); - params[pnr++] = NULL; - - parse_manual_file(qPrintable(fileNames[i]), params, pnr - 1, &table, &trips, &sites, &filter_presets); + xml_params params; + xml_params_add_int(¶ms, "numberField", r.indexOf(tr("Dive #"))); + xml_params_add_int(¶ms, "dateField", r.indexOf(tr("Date"))); + xml_params_add_int(¶ms, "timeField", r.indexOf(tr("Time"))); + xml_params_add_int(¶ms, "durationField", r.indexOf(tr("Duration"))); + xml_params_add_int(¶ms, "modeField", r.indexOf(tr("Mode"))); + xml_params_add_int(¶ms, "locationField", r.indexOf(tr("Location"))); + xml_params_add_int(¶ms, "gpsField", r.indexOf(tr("GPS"))); + xml_params_add_int(¶ms, "maxDepthField", r.indexOf(tr("Max. depth"))); + xml_params_add_int(¶ms, "meanDepthField", r.indexOf(tr("Avg. depth"))); + xml_params_add_int(¶ms, "divemasterField", r.indexOf(tr("Divemaster"))); + xml_params_add_int(¶ms, "buddyField", r.indexOf(tr("Buddy"))); + xml_params_add_int(¶ms, "suitField", r.indexOf(tr("Suit"))); + xml_params_add_int(¶ms, "notesField", r.indexOf(tr("Notes"))); + xml_params_add_int(¶ms, "weightField", r.indexOf(tr("Weight"))); + xml_params_add_int(¶ms, "tagsField", r.indexOf(tr("Tags"))); + xml_params_add_int(¶ms, "separatorIndex", ui->CSVSeparator->currentIndex()); + xml_params_add_int(¶ms, "units", ui->CSVUnits->currentIndex()); + xml_params_add_int(¶ms, "datefmt", ui->DateFormat->currentIndex()); + xml_params_add_int(¶ms, "durationfmt", ui->DurationFormat->currentIndex()); + xml_params_add_int(¶ms, "cylindersizeField", r.indexOf(tr("Cyl. size"))); + xml_params_add_int(¶ms, "startpressureField", r.indexOf(tr("Start pressure"))); + xml_params_add_int(¶ms, "endpressureField", r.indexOf(tr("End pressure"))); + xml_params_add_int(¶ms, "o2Field", r.indexOf(tr("O₂"))); + xml_params_add_int(¶ms, "heField", r.indexOf(tr("He"))); + xml_params_add_int(¶ms, "airtempField", r.indexOf(tr("Air temp."))); + xml_params_add_int(¶ms, "watertempField", r.indexOf(tr("Water temp."))); + xml_params_add_int(¶ms, "visibilityField", r.indexOf(tr("Visibility"))); + xml_params_add_int(¶ms, "ratingField", r.indexOf(tr("Rating"))); + + parse_manual_file(qPrintable(fileNames[i]), ¶ms, &table, &trips, &sites, &filter_presets); } else { - char *params[53]; - int pnr = 0; + xml_params params; QRegExp apdRe("^.*[/\\][0-9a-zA-Z]*_([0-9]{6})_([0-9]{6})\\.apd"); if (txtLog) { - pnr = parseTxtHeader(fileNames[i], params, pnr); + parseTxtHeader(fileNames[i], params); } else if (apdRe.exactMatch(fileNames[i])) { - params[pnr++] = strdup("date"); - params[pnr++] = strdup("20" + apdRe.cap(1).toLatin1()); - params[pnr++] = strdup("time"); - params[pnr++] = strdup("1" + apdRe.cap(2).toLatin1()); + xml_params_add(¶ms, "date", qPrintable("20" + apdRe.cap(1))); + xml_params_add(¶ms, "time", qPrintable("1" + apdRe.cap(2))); } - pnr = setup_csv_params(r, params, pnr); - parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1, + setup_csv_params(r, params); + parse_csv_file(qPrintable(fileNames[i]), ¶ms, specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv", &table, &trips, &sites, &filter_presets); } diff --git a/desktop-widgets/divelogimportdialog.h b/desktop-widgets/divelogimportdialog.h index d64cbc656..9c75dc0f0 100644 --- a/desktop-widgets/divelogimportdialog.h +++ b/desktop-widgets/divelogimportdialog.h @@ -13,6 +13,7 @@ #include "core/dive.h" #include "core/divelist.h" +struct xml_params; namespace Ui { class DiveLogImportDialog; } @@ -87,8 +88,8 @@ 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); - int parseTxtHeader(QString fileName, char **params, int pnr); + void setup_csv_params(QStringList r, xml_params ¶ms); + void parseTxtHeader(QString fileName, xml_params ¶ms); private: bool selector; |