[PATCH] Add unit support for CSV import

Miika Turkia miika.turkia at gmail.com
Sat Feb 15 00:51:23 UTC 2014


Signed-off-by: Miika Turkia <miika.turkia at gmail.com>
---
Anton, can you verify the stopdepth is correct? I have no idea what it
means nor data to test it.
---
 dive.h                        |  2 +-
 file.c                        |  8 ++++++--
 qt-ui/divelogimportdialog.cpp |  1 +
 qt-ui/divelogimportdialog.ui  | 33 +++++++++++++++++++++++++++++++++
 xslt/csv2xml.xslt             | 34 +++++++++++++++++++++++++++++++---
 5 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/dive.h b/dive.h
index 16105e1..e121b61 100644
--- a/dive.h
+++ b/dive.h
@@ -646,7 +646,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, int sepidx, const char *csvtemplate, char **error);
+extern void parse_csv_file(const char *filename, int time, int depth, int temp, int po2f, int cnsf, int stopdepthf, int sepidx, const char *csvtemplate, int units, char **error);
 extern void parse_manual_file(const char *filename, int separator_index, int units, int number, int date, int time, int duration, int location, int gps, int maxdepth, int meandepth, int buddy, int notes, int weight, int tags, char **error);
 
 extern void save_dives(const char *filename);
diff --git a/file.c b/file.c
index 8157e9a..2c6e505 100644
--- a/file.c
+++ b/file.c
@@ -349,17 +349,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, int sepidx, const char *csvtemplate, char **error)
+void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int cnsf, int stopdepthf, int sepidx, const char *csvtemplate, int unitidx, char **error)
 {
 	struct memblock mem;
 	int pnr=0;
-	char *params[19];
+	char *params[21];
 	char timebuf[MAXCOLDIGITS];
 	char depthbuf[MAXCOLDIGITS];
 	char tempbuf[MAXCOLDIGITS];
 	char po2buf[MAXCOLDIGITS];
 	char cnsbuf[MAXCOLDIGITS];
 	char stopdepthbuf[MAXCOLDIGITS];
+	char unitbuf[MAXCOLDIGITS];
 	char separator_index[MAXCOLDIGITS];
 	time_t now;
 	struct tm *timep;
@@ -380,6 +381,7 @@ void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int
 	snprintf(cnsbuf, MAXCOLDIGITS, "%d", cnsf);
 	snprintf(stopdepthbuf, MAXCOLDIGITS, "%d", stopdepthf);
 	snprintf(separator_index, MAXCOLDIGITS, "%d", sepidx);
+	snprintf(unitbuf, MAXCOLDIGITS, "%d", unitidx);
 	time(&now);
 	timep = localtime(&now);
 	strftime(curdate, sizeof(curdate), "%Y%m%d", timep);
@@ -404,6 +406,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++] = "units";
+	params[pnr++] = unitbuf;
 	params[pnr++] = "separatorIndex";
 	params[pnr++] = separator_index;
 	params[pnr++] = NULL;
diff --git a/qt-ui/divelogimportdialog.cpp b/qt-ui/divelogimportdialog.cpp
index 3005d15..36e69d0 100644
--- a/qt-ui/divelogimportdialog.cpp
+++ b/qt-ui/divelogimportdialog.cpp
@@ -66,6 +66,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
 			               VALUE_IF_CHECKED(CSVstopdepth),
 			               ui->CSVSeparator->currentIndex(),
 			               specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv",
+			               ui->CSVUnits->currentIndex(),
 			               &error);
 			if (error != NULL) {
 				MainWindow::instance()->showError(error);
diff --git a/qt-ui/divelogimportdialog.ui b/qt-ui/divelogimportdialog.ui
index 627a12a..e2cf958 100644
--- a/qt-ui/divelogimportdialog.ui
+++ b/qt-ui/divelogimportdialog.ui
@@ -239,6 +239,39 @@
        <zorder>stopdepthCheckBox</zorder>
        <zorder>CSVstopdepth</zorder>
       </widget>
+      <widget class="QGroupBox" name="groupBox_7">
+       <property name="geometry">
+        <rect>
+         <x>210</x>
+         <y>150</y>
+         <width>271</width>
+         <height>80</height>
+        </rect>
+       </property>
+       <property name="title">
+        <string>Units</string>
+       </property>
+       <widget class="QComboBox" name="CSVUnits">
+        <property name="geometry">
+         <rect>
+          <x>20</x>
+          <y>30</y>
+          <width>251</width>
+          <height>29</height>
+         </rect>
+        </property>
+        <item>
+         <property name="text">
+          <string>Metric</string>
+         </property>
+        </item>
+        <item>
+         <property name="text">
+          <string>Imperial</string>
+         </property>
+        </item>
+       </widget>
+      </widget>
      </widget>
      <widget class="QWidget" name="tab_2">
       <attribute name="title">
diff --git a/xslt/csv2xml.xslt b/xslt/csv2xml.xslt
index ee8b755..5ae689a 100644
--- a/xslt/csv2xml.xslt
+++ b/xslt/csv2xml.xslt
@@ -11,6 +11,7 @@
   <xsl:param name="stopdepthField" select="stopdepthField"/>
   <xsl:param name="date" select="date"/>
   <xsl:param name="time" select="time"/>
+  <xsl:param name="units" select="units"/>
   <xsl:param name="separatorIndex" select="separatorIndex"/>
   <xsl:output method="xml" indent="yes"/>
 
@@ -113,19 +114,39 @@
           </xsl:choose>
         </xsl:attribute>
 
-        <xsl:attribute name="depth">
+        <xsl:variable name="depth">
           <xsl:call-template name="getFieldByIndex">
             <xsl:with-param name="index" select="$depthField"/>
             <xsl:with-param name="line" select="$line"/>
           </xsl:call-template>
+        </xsl:variable>
+        <xsl:attribute name="depth">
+          <xsl:choose>
+            <xsl:when test="$units = 0">
+              <xsl:value-of select="$depth"/>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:value-of select="$depth * 0.3048"/>
+            </xsl:otherwise>
+          </xsl:choose>
         </xsl:attribute>
 
         <xsl:if test="$tempField >= 0">
-          <xsl:attribute name="temp">
+          <xsl:variable name="temp">
             <xsl:call-template name="getFieldByIndex">
               <xsl:with-param name="index" select="$tempField"/>
               <xsl:with-param name="line" select="$line"/>
             </xsl:call-template>
+          </xsl:variable>
+          <xsl:attribute name="temp">
+            <xsl:choose>
+              <xsl:when test="$units = 0">
+                <xsl:value-of select="$temp"/>
+              </xsl:when>
+              <xsl:otherwise>
+                <xsl:value-of select="concat(format-number(($temp - 32) * 5 div 9, '0.0'), ' C')"/>
+              </xsl:otherwise>
+            </xsl:choose>
           </xsl:attribute>
         </xsl:if>
 
@@ -164,7 +185,14 @@
             </xsl:call-template>
           </xsl:variable>
           <xsl:attribute name="stopdepth">
-            <xsl:copy-of select="$stopdepth"/>
+            <xsl:choose>
+              <xsl:when test="$units = 0">
+                <xsl:copy-of select="$stopdepth"/>
+              </xsl:when>
+              <xsl:otherwise>
+                <xsl:copy-of select="format-number($stopdepth * 0.3048, '0.00')"/>
+              </xsl:otherwise>
+            </xsl:choose>
           </xsl:attribute>
 
           <xsl:attribute name="in_deco">
-- 
1.8.3.2



More information about the subsurface mailing list