Add unit support for CSV import

Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Miika Turkia 2014-02-15 10:51:23 +02:00 committed by Dirk Hohndel
parent 31aa93857a
commit cb5ab4bc8e
5 changed files with 72 additions and 6 deletions

2
dive.h
View file

@ -648,7 +648,7 @@ extern int parse_dm4_buffer(sqlite3 *handle, const char *url, const char *buf, i
extern int parse_shearwater_buffer(sqlite3 *handle, 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);

8
file.c
View file

@ -385,17 +385,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;
@ -416,6 +417,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);
@ -440,6 +442,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;

View file

@ -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);

View file

@ -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">

View file

@ -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">