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 <[email protected]>
---
 dive.h                        |  2 +-
 file.c                        | 28 +++++++------
 qt-ui/divelogimportdialog.cpp | 97 ++++++++++++++++++++++++++-----------------
 qt-ui/divelogimportdialog.h   |  1 +
 4 files changed, 76 insertions(+), 52 deletions(-)

diff --git a/dive.h b/dive.h
index 2585555..308a231 100644
--- a/dive.h
+++ b/dive.h
@@ -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);
diff --git a/file.c b/file.c
index 5ea6bb2..cd3d3ca 100644
--- a/file.c
+++ b/file.c
@@ -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,19 +938,23 @@ 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);
+       if (filename == NULL)
+               return report_error("No CSV filename");
 
-       ret = init_csv_file_parsing(params, &now, timep, timef, depthf, tempf, 
po2f, o2sensor1f, o2sensor2f, o2sensor3f, cnsf, ndlf, ttsf, stopdepthf, 
pressuref, setpointf, sepidx, csvtemplate, unitidx);
+       time(&now);
+       timep = localtime(&now);
 
-       if (strlen(hw)) {
-               params[ret++] = "hw";
-               params[ret++] = strdup(hw);
-               params[ret++] = NULL;
-       }
+       strftime(tmpbuf, MAXCOLDIGITS, "%Y%m%d", timep);
+       params[pnr++] = "date";
+       params[pnr++] = strdup(tmpbuf);
 
-       if (filename == NULL)
-               return report_error("No CSV filename");
+       /* 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))
diff --git a/qt-ui/divelogimportdialog.cpp b/qt-ui/divelogimportdialog.cpp
index 39bcc34..12e3666 100644
--- a/qt-ui/divelogimportdialog.cpp
+++ b/qt-ui/divelogimportdialog.cpp
@@ -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");
+                       }
                }
        }
 
diff --git a/qt-ui/divelogimportdialog.h b/qt-ui/divelogimportdialog.h
index 9840c94..03bb140 100644
--- a/qt-ui/divelogimportdialog.h
+++ b/qt-ui/divelogimportdialog.h
@@ -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;
-- 
2.1.4

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to