From b5d3476b0ba881699c604a3d97c5801b8bb99a08 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 4 Dec 2013 15:19:28 -0800 Subject: [PATCH] Allow comma separated CSV files The separator selector in the CSV import dialog was unused. This passes the value into the xslt and adds ',' as possible value. I'm sure this could be done much better (pass the actual character instead of the index), but I couldn't get that to work and this does seem to do the trick. Also added a test dive to test this feature. Fixes #321 Signed-off-by: Dirk Hohndel --- dive.h | 2 +- dives/TestComma.csv | 27 +++++++++++++++++++++++++++ file.c | 8 ++++++-- qt-ui/csvimportdialog.cpp | 2 ++ xslt/csv2xml.xslt | 8 +++++++- 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 dives/TestComma.csv diff --git a/dive.h b/dive.h index 7f3a55a99..0ee3a1aa6 100644 --- a/dive.h +++ b/dive.h @@ -626,7 +626,7 @@ extern void set_filename(const char *filename, bool force); extern int parse_dm4_buffer(const char *url, const char *buf, int size, struct dive_table *table, char **error); extern void parse_file(const char *filename, char **error); -extern void parse_csv_file(const char *filename, int time, int depth, int temp, int po2f, int cnsf, int stopdepthf, char **error); +extern void parse_csv_file(const char *filename, int time, int depth, int temp, int po2f, int cnsf, int stopdepthf, int sepidx, char **error); extern void save_dives(const char *filename); extern void save_dives_logic(const char *filename, bool select_only); diff --git a/dives/TestComma.csv b/dives/TestComma.csv new file mode 100644 index 000000000..2294a7001 --- /dev/null +++ b/dives/TestComma.csv @@ -0,0 +1,27 @@ +Dive Time (s),Depth (m),PPO2 - Setpoint (Bar),PPO2 - C1 Cell 1 (Bar),PPO2 - C1 Cell 2 (Bar),PPO2 - C1 Cell 3 (Bar),PPO2 - C1 Measured (Bar),PPO2 - C2 Cell 1 (Bar),PPO2 - C2 Cell 2 (Bar),PPO2 - C2 Cell 3 (Bar),PPO2 - C2 Measured (Bar),C1 Battery 1 (Volts),C1 Battery 2 (Volts),C2 Battery 1 (Volts),C2 Battery 2 (Volts),Ambient Temp. (Celcius),OTU Exposure (%),CNS Exposure (%),Deco Ceiling (m),Gradient Factor +0,0.0,0,0,0,0,0,0,0,0,0,0,0,0,0,19.00,0,0,0,0 +60,2.5,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +120,4.5,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +180,5.0,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +240,5.5,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +300,5.0,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +360,5.5,0,0,0,0,0,0,0,0,0,0,0,0,0,17.00,0,0,0,0 +420,7.0,0,0,0,0,0,0,0,0,0,0,0,0,0,17.00,0,0,0,0 +480,9.0,0,0,0,0,0,0,0,0,0,0,0,0,0,17.00,0,0,0,0 +540,11.5,0,0,0,0,0,0,0,0,0,0,0,0,0,16.00,0,0,0,0 +600,11.5,0,0,0,0,0,0,0,0,0,0,0,0,0,16.00,0,0,0,0 +660,12.5,0,0,0,0,0,0,0,0,0,0,0,0,0,16.00,0,0,0,0 +720,13.5,0,0,0,0,0,0,0,0,0,0,0,0,0,16.00,0,0,0,0 +780,16.0,0,0,0,0,0,0,0,0,0,0,0,0,0,15.00,0,0,0,0 +840,17.0,0,0,0,0,0,0,0,0,0,0,0,0,0,15.00,0,0,0,0 +900,18.0,0,0,0,0,0,0,0,0,0,0,0,0,0,15.00,0,0,0,0 +960,18.5,0,0,0,0,0,0,0,0,0,0,0,0,0,15.00,0,0,0,0 +1020,20.0,0,0,0,0,0,0,0,0,0,0,0,0,0,15.00,0,0,0,0 +1080,18.5,0,0,0,0,0,0,0,0,0,0,0,0,0,15.00,0,0,0,0 +1140,16.0,0,0,0,0,0,0,0,0,0,0,0,0,0,16.00,0,0,0,0 +1200,10.5,0,0,0,0,0,0,0,0,0,0,0,0,0,17.00,0,0,0,0 +1260,5.0,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +1320,4.0,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +1380,2.0,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +1440,1.5,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 +1460,0.0,0,0,0,0,0,0,0,0,0,0,0,0,0,18.00,0,0,0,0 diff --git a/file.c b/file.c index 5f9b3d90a..aaf14f3d9 100644 --- a/file.c +++ b/file.c @@ -327,17 +327,18 @@ void parse_file(const char *filename, char **error) #define MAXCOLDIGITS 3 #define MAXCOLS 100 -void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int cnsf, int stopdepthf, char **error) +void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int cnsf, int stopdepthf, int sepidx, char **error) { struct memblock mem; int pnr=0; - char *params[17]; + char *params[19]; char timebuf[MAXCOLDIGITS]; char depthbuf[MAXCOLDIGITS]; char tempbuf[MAXCOLDIGITS]; char po2buf[MAXCOLDIGITS]; char cnsbuf[MAXCOLDIGITS]; char stopdepthbuf[MAXCOLDIGITS]; + char separator_index[MAXCOLDIGITS]; time_t now; struct tm *timep; char curdate[9]; @@ -356,6 +357,7 @@ void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int snprintf(po2buf, MAXCOLDIGITS, "%d", po2f); snprintf(cnsbuf, MAXCOLDIGITS, "%d", cnsf); snprintf(stopdepthbuf, MAXCOLDIGITS, "%d", stopdepthf); + snprintf(separator_index, MAXCOLDIGITS, "%d", sepidx); time(&now); timep = localtime(&now); strftime(curdate, sizeof(curdate), "%Y%m%d", timep); @@ -380,6 +382,8 @@ void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int params[pnr++] = curdate; params[pnr++] = "time"; params[pnr++] = curtime; + params[pnr++] = "separatorIndex"; + params[pnr++] = separator_index; params[pnr++] = NULL; if (filename == NULL) diff --git a/qt-ui/csvimportdialog.cpp b/qt-ui/csvimportdialog.cpp index 91b9ddd40..b3392f421 100644 --- a/qt-ui/csvimportdialog.cpp +++ b/qt-ui/csvimportdialog.cpp @@ -22,6 +22,7 @@ CSVImportDialog::CSVImportDialog(QWidget *parent) : ui->knownImports->addItem(CSVApps[i].name); ui->CSVSeparator->addItem("Tab"); + ui->CSVSeparator->addItem(","); ui->knownImports->setCurrentIndex(1); ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); @@ -52,6 +53,7 @@ void CSVImportDialog::on_buttonBox_accepted() VALUE_IF_CHECKED(CSVpo2), VALUE_IF_CHECKED(CSVcns), VALUE_IF_CHECKED(CSVstopdepth), + ui->CSVSeparator->currentIndex(), &error); if (error != NULL) { diff --git a/xslt/csv2xml.xslt b/xslt/csv2xml.xslt index fda0b6724..7ddbda92d 100644 --- a/xslt/csv2xml.xslt +++ b/xslt/csv2xml.xslt @@ -11,11 +11,17 @@ + - + + + + , + +