2012-01-26 21:00:45 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-27 01:43:33 +00:00
|
|
|
#include <string.h>
|
2012-01-26 21:00:45 +00:00
|
|
|
#include <errno.h>
|
2013-10-06 15:55:58 +00:00
|
|
|
#include "gettext.h"
|
2013-05-11 19:33:46 +00:00
|
|
|
#include <zip.h>
|
2013-10-19 05:17:13 +00:00
|
|
|
#include <time.h>
|
2012-01-26 21:00:45 +00:00
|
|
|
|
|
|
|
#include "dive.h"
|
2012-01-27 20:43:40 +00:00
|
|
|
#include "file.h"
|
2015-06-13 15:01:06 +00:00
|
|
|
#include "git-access.h"
|
2015-06-13 16:08:39 +00:00
|
|
|
#include "qthelperfromc.h"
|
2012-01-26 21:00:45 +00:00
|
|
|
|
2014-11-19 21:14:22 +00:00
|
|
|
/* For SAMPLE_* */
|
|
|
|
#include <libdivecomputer/parser.h>
|
|
|
|
|
2015-02-18 05:58:34 +00:00
|
|
|
/* to check XSLT version number */
|
|
|
|
#include <libxslt/xsltconfig.h>
|
|
|
|
|
2012-08-24 22:39:00 +00:00
|
|
|
/* Crazy windows sh*t */
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2013-03-14 03:37:38 +00:00
|
|
|
int readfile(const char *filename, struct memblock *mem)
|
2012-01-26 21:00:45 +00:00
|
|
|
{
|
2012-07-12 22:28:47 +00:00
|
|
|
int ret, fd;
|
2012-01-26 21:00:45 +00:00
|
|
|
struct stat st;
|
2012-01-27 18:56:36 +00:00
|
|
|
char *buf;
|
2012-01-26 21:00:45 +00:00
|
|
|
|
|
|
|
mem->buffer = NULL;
|
|
|
|
mem->size = 0;
|
|
|
|
|
2013-12-19 13:00:51 +00:00
|
|
|
fd = subsurface_open(filename, O_RDONLY | O_BINARY, 0);
|
2012-01-26 21:00:45 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
ret = fstat(fd, &st);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
ret = -EINVAL;
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
goto out;
|
|
|
|
ret = 0;
|
|
|
|
if (!st.st_size)
|
|
|
|
goto out;
|
2014-02-28 04:09:57 +00:00
|
|
|
buf = malloc(st.st_size + 1);
|
2012-01-26 21:00:45 +00:00
|
|
|
ret = -1;
|
|
|
|
errno = ENOMEM;
|
2012-01-27 18:56:36 +00:00
|
|
|
if (!buf)
|
2012-01-26 21:00:45 +00:00
|
|
|
goto out;
|
2012-01-27 18:56:36 +00:00
|
|
|
mem->buffer = buf;
|
2012-01-26 21:00:45 +00:00
|
|
|
mem->size = st.st_size;
|
2012-01-27 18:56:36 +00:00
|
|
|
ret = read(fd, buf, mem->size);
|
2012-01-26 21:00:45 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto free;
|
2012-01-27 18:56:36 +00:00
|
|
|
buf[ret] = 0;
|
2012-01-26 21:00:45 +00:00
|
|
|
if (ret == mem->size)
|
|
|
|
goto out;
|
|
|
|
errno = EIO;
|
|
|
|
ret = -1;
|
|
|
|
free:
|
|
|
|
free(mem->buffer);
|
|
|
|
mem->buffer = NULL;
|
|
|
|
mem->size = 0;
|
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-27 01:43:33 +00:00
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
static void zip_read(struct zip_file *file, const char *filename)
|
2012-01-27 01:43:33 +00:00
|
|
|
{
|
|
|
|
int size = 1024, n, read = 0;
|
|
|
|
char *mem = malloc(size);
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
while ((n = zip_fread(file, mem + read, size - read)) > 0) {
|
2012-01-27 01:43:33 +00:00
|
|
|
read += n;
|
|
|
|
size = read * 3 / 2;
|
|
|
|
mem = realloc(mem, size);
|
|
|
|
}
|
2013-03-17 05:12:23 +00:00
|
|
|
mem[read] = 0;
|
2014-12-08 19:26:03 +00:00
|
|
|
(void) parse_xml_buffer(filename, mem, read, &dive_table, NULL);
|
2012-01-27 01:43:33 +00:00
|
|
|
free(mem);
|
|
|
|
}
|
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
static int try_to_open_zip(const char *filename, struct memblock *mem)
|
2012-01-27 01:43:33 +00:00
|
|
|
{
|
|
|
|
int success = 0;
|
2012-01-27 18:56:36 +00:00
|
|
|
/* Grr. libzip needs to re-open the file, it can't take a buffer */
|
2013-12-19 13:00:51 +00:00
|
|
|
struct zip *zip = subsurface_zip_open_readonly(filename, ZIP_CHECKCONS, NULL);
|
2012-01-27 01:43:33 +00:00
|
|
|
|
|
|
|
if (zip) {
|
|
|
|
int index;
|
2014-02-28 04:09:57 +00:00
|
|
|
for (index = 0;; index++) {
|
2012-01-27 01:43:33 +00:00
|
|
|
struct zip_file *file = zip_fopen_index(zip, index, 0);
|
|
|
|
if (!file)
|
|
|
|
break;
|
2014-10-13 18:31:01 +00:00
|
|
|
/* skip parsing the divelogs.de pictures */
|
|
|
|
if (strstr(zip_get_name(zip, index, 0), "pictures/"))
|
|
|
|
continue;
|
2014-03-14 18:26:07 +00:00
|
|
|
zip_read(file, filename);
|
2012-01-27 01:43:33 +00:00
|
|
|
zip_fclose(file);
|
|
|
|
success++;
|
|
|
|
}
|
2013-12-19 13:00:51 +00:00
|
|
|
subsurface_zip_close(zip);
|
2012-01-27 01:43:33 +00:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, const char *tag)
|
2013-09-29 12:44:38 +00:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
2014-10-28 09:14:00 +00:00
|
|
|
if (mem->size == 0 && readfile(filename, mem) < 0)
|
2014-03-14 18:26:07 +00:00
|
|
|
return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);
|
2013-09-29 12:44:38 +00:00
|
|
|
|
|
|
|
/* Surround the CSV file content with XML tags to enable XSLT
|
|
|
|
* parsing
|
2014-01-16 20:50:14 +00:00
|
|
|
*
|
|
|
|
* Tag markers take: strlen("<></>") = 5
|
2013-09-29 12:44:38 +00:00
|
|
|
*/
|
2015-01-24 15:03:11 +00:00
|
|
|
buf = realloc(mem->buffer, mem->size + 7 + strlen(tag) * 2);
|
2013-09-29 12:44:38 +00:00
|
|
|
if (buf != NULL) {
|
2014-01-16 20:50:14 +00:00
|
|
|
char *starttag = NULL;
|
|
|
|
char *endtag = NULL;
|
|
|
|
|
|
|
|
starttag = malloc(3 + strlen(tag));
|
2015-01-24 15:03:11 +00:00
|
|
|
endtag = malloc(5 + strlen(tag));
|
2014-01-16 20:50:14 +00:00
|
|
|
|
|
|
|
if (starttag == NULL || endtag == NULL) {
|
2014-03-06 22:19:42 +00:00
|
|
|
/* this is fairly silly - so the malloc fails, but we strdup the error?
|
|
|
|
* let's complete the silliness by freeing the two pointers in case one malloc succeeded
|
|
|
|
* and the other one failed - this will make static analysis tools happy */
|
|
|
|
free(starttag);
|
|
|
|
free(endtag);
|
|
|
|
free(buf);
|
2014-03-14 18:26:07 +00:00
|
|
|
return report_error("Memory allocation failed in %s", __func__);
|
2014-01-16 20:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(starttag, "<%s>", tag);
|
2015-01-24 15:03:11 +00:00
|
|
|
sprintf(endtag, "\n</%s>", tag);
|
2014-01-16 20:50:14 +00:00
|
|
|
|
|
|
|
memmove(buf + 2 + strlen(tag), buf, mem->size);
|
|
|
|
memcpy(buf, starttag, 2 + strlen(tag));
|
2015-01-24 15:03:11 +00:00
|
|
|
memcpy(buf + mem->size + 2 + strlen(tag), endtag, 5 + strlen(tag));
|
|
|
|
mem->size += (6 + 2 * strlen(tag));
|
2013-12-11 20:21:49 +00:00
|
|
|
mem->buffer = buf;
|
2014-01-16 20:50:14 +00:00
|
|
|
|
|
|
|
free(starttag);
|
|
|
|
free(endtag);
|
2013-12-10 23:53:31 +00:00
|
|
|
} else {
|
|
|
|
free(mem->buffer);
|
2014-03-14 18:26:07 +00:00
|
|
|
return report_error("realloc failed in %s", __func__);
|
2013-12-10 23:53:31 +00:00
|
|
|
}
|
2013-09-29 12:44:38 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-15 06:36:50 +00:00
|
|
|
int db_test_func(void *param, int columns, char **data, char **column)
|
|
|
|
{
|
|
|
|
return *data[0] == '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
static int try_to_open_db(const char *filename, struct memblock *mem)
|
2013-03-05 05:10:39 +00:00
|
|
|
{
|
2014-02-15 06:36:49 +00:00
|
|
|
sqlite3 *handle;
|
2014-02-15 06:36:50 +00:00
|
|
|
char dm4_test[] = "select count(*) from sqlite_master where type='table' and name='Dive' and sql like '%ProfileBlob%'";
|
2014-11-15 15:34:20 +00:00
|
|
|
char dm5_test[] = "select count(*) from sqlite_master where type='table' and name='Dive' and sql like '%SampleBlob%'";
|
2014-02-15 06:36:50 +00:00
|
|
|
char shearwater_test[] = "select count(*) from sqlite_master where type='table' and name='system' and sql like '%dbVersion%'";
|
2014-12-20 16:19:43 +00:00
|
|
|
char cobalt_test[] = "select count(*) from sqlite_master where type='table' and name='TrackPoints' and sql like '%DepthPressure%'";
|
2015-07-12 17:46:48 +00:00
|
|
|
char divinglog_test[] = "select count(*) from sqlite_master where type='table' and name='DBInfo' and sql like '%PrgName%'";
|
2014-02-15 06:36:49 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = sqlite3_open(filename, &handle);
|
|
|
|
|
|
|
|
if (retval) {
|
2015-07-02 18:22:22 +00:00
|
|
|
fprintf(stderr, "Database connection failed '%s'.\n", filename);
|
2014-02-15 06:36:49 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-15 15:34:20 +00:00
|
|
|
/* Testing if DB schema resembles Suunto DM5 database format */
|
|
|
|
retval = sqlite3_exec(handle, dm5_test, &db_test_func, 0, NULL);
|
|
|
|
if (!retval) {
|
|
|
|
retval = parse_dm5_buffer(handle, filename, mem->buffer, mem->size, &dive_table);
|
|
|
|
sqlite3_close(handle);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-02-15 06:36:50 +00:00
|
|
|
/* Testing if DB schema resembles Suunto DM4 database format */
|
|
|
|
retval = sqlite3_exec(handle, dm4_test, &db_test_func, 0, NULL);
|
|
|
|
if (!retval) {
|
2014-03-14 18:26:07 +00:00
|
|
|
retval = parse_dm4_buffer(handle, filename, mem->buffer, mem->size, &dive_table);
|
2014-02-15 06:36:50 +00:00
|
|
|
sqlite3_close(handle);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Testing if DB schema resembles Shearwater database format */
|
|
|
|
retval = sqlite3_exec(handle, shearwater_test, &db_test_func, 0, NULL);
|
|
|
|
if (!retval) {
|
2014-03-14 18:26:07 +00:00
|
|
|
retval = parse_shearwater_buffer(handle, filename, mem->buffer, mem->size, &dive_table);
|
2014-12-20 16:19:43 +00:00
|
|
|
sqlite3_close(handle);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Testing if DB schema resembles Atomic Cobalt database format */
|
|
|
|
retval = sqlite3_exec(handle, cobalt_test, &db_test_func, 0, NULL);
|
|
|
|
if (!retval) {
|
|
|
|
retval = parse_cobalt_buffer(handle, filename, mem->buffer, mem->size, &dive_table);
|
2014-02-15 06:36:50 +00:00
|
|
|
sqlite3_close(handle);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-07-12 17:46:48 +00:00
|
|
|
/* Testing if DB schema resembles Divinglog database format */
|
|
|
|
retval = sqlite3_exec(handle, divinglog_test, &db_test_func, 0, NULL);
|
|
|
|
if (!retval) {
|
|
|
|
retval = parse_divinglog_buffer(handle, filename, mem->buffer, mem->size, &dive_table);
|
|
|
|
sqlite3_close(handle);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-02-15 06:36:49 +00:00
|
|
|
sqlite3_close(handle);
|
|
|
|
|
|
|
|
return retval;
|
2013-03-05 05:10:39 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 13:44:26 +00:00
|
|
|
timestamp_t parse_date(const char *date)
|
2012-06-20 03:07:42 +00:00
|
|
|
{
|
|
|
|
int hour, min, sec;
|
|
|
|
struct tm tm;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
memset(&tm, 0, sizeof(tm));
|
|
|
|
tm.tm_mday = strtol(date, &p, 10);
|
|
|
|
if (tm.tm_mday < 1 || tm.tm_mday > 31)
|
|
|
|
return 0;
|
|
|
|
for (tm.tm_mon = 0; tm.tm_mon < 12; tm.tm_mon++) {
|
|
|
|
if (!memcmp(p, monthname(tm.tm_mon), 3))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tm.tm_mon > 11)
|
|
|
|
return 0;
|
2014-02-28 04:09:57 +00:00
|
|
|
date = p + 3;
|
2012-06-20 03:07:42 +00:00
|
|
|
tm.tm_year = strtol(date, &p, 10);
|
|
|
|
if (date == p)
|
|
|
|
return 0;
|
|
|
|
if (tm.tm_year < 70)
|
|
|
|
tm.tm_year += 2000;
|
|
|
|
if (tm.tm_year < 100)
|
|
|
|
tm.tm_year += 1900;
|
|
|
|
if (sscanf(p, "%d:%d:%d", &hour, &min, &sec) != 3)
|
|
|
|
return 0;
|
|
|
|
tm.tm_hour = hour;
|
|
|
|
tm.tm_min = min;
|
|
|
|
tm.tm_sec = sec;
|
|
|
|
return utc_mktime(&tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum csv_format {
|
2014-02-28 04:09:57 +00:00
|
|
|
CSV_DEPTH,
|
|
|
|
CSV_TEMP,
|
2014-05-28 06:55:46 +00:00
|
|
|
CSV_PRESSURE,
|
|
|
|
POSEIDON_DEPTH,
|
|
|
|
POSEIDON_TEMP,
|
|
|
|
POSEIDON_SETPOINT,
|
|
|
|
POSEIDON_SENSOR1,
|
|
|
|
POSEIDON_SENSOR2,
|
|
|
|
POSEIDON_PRESSURE,
|
2014-11-19 21:14:19 +00:00
|
|
|
POSEIDON_O2CYLINDER,
|
2014-11-19 21:14:20 +00:00
|
|
|
POSEIDON_NDL,
|
|
|
|
POSEIDON_CEILING
|
2012-06-20 03:07:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void add_sample_data(struct sample *sample, enum csv_format type, double val)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case CSV_DEPTH:
|
|
|
|
sample->depth.mm = feet_to_mm(val);
|
|
|
|
break;
|
|
|
|
case CSV_TEMP:
|
|
|
|
sample->temperature.mkelvin = F_to_mkelvin(val);
|
|
|
|
break;
|
|
|
|
case CSV_PRESSURE:
|
2014-02-28 04:09:57 +00:00
|
|
|
sample->cylinderpressure.mbar = psi_to_mbar(val * 4);
|
2012-06-20 03:07:42 +00:00
|
|
|
break;
|
2014-05-28 06:55:46 +00:00
|
|
|
case POSEIDON_DEPTH:
|
|
|
|
sample->depth.mm = val * 0.5 *1000;
|
|
|
|
break;
|
|
|
|
case POSEIDON_TEMP:
|
|
|
|
sample->temperature.mkelvin = C_to_mkelvin(val * 0.2);
|
|
|
|
break;
|
|
|
|
case POSEIDON_SETPOINT:
|
|
|
|
sample->setpoint.mbar = val * 10;
|
|
|
|
break;
|
|
|
|
case POSEIDON_SENSOR1:
|
|
|
|
sample->o2sensor[0].mbar = val * 10;
|
|
|
|
break;
|
|
|
|
case POSEIDON_SENSOR2:
|
|
|
|
sample->o2sensor[1].mbar = val * 10;
|
|
|
|
break;
|
|
|
|
case POSEIDON_PRESSURE:
|
|
|
|
sample->cylinderpressure.mbar = val * 1000;
|
|
|
|
break;
|
2014-11-17 11:25:00 +00:00
|
|
|
case POSEIDON_O2CYLINDER:
|
|
|
|
sample->o2cylinderpressure.mbar = val * 1000;
|
2014-05-28 06:55:46 +00:00
|
|
|
break;
|
2014-11-19 21:14:19 +00:00
|
|
|
case POSEIDON_NDL:
|
|
|
|
sample->ndl.seconds = val * 60;
|
|
|
|
break;
|
2014-11-19 21:14:20 +00:00
|
|
|
case POSEIDON_CEILING:
|
|
|
|
sample->stopdepth.mm = val * 1000;
|
|
|
|
break;
|
2012-06-20 03:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cochran comma-separated values: depth in feet, temperature in F, pressure in psi.
|
|
|
|
*
|
|
|
|
* They start with eight comma-separated fields like:
|
|
|
|
*
|
|
|
|
* filename: {C:\Analyst4\can\T036785.can},{C:\Analyst4\can\K031892.can}
|
|
|
|
* divenr: %d
|
|
|
|
* datetime: {03Sep11 16:37:22},{15Dec11 18:27:02}
|
|
|
|
* ??: 1
|
|
|
|
* serialnr??: {CCI134},{CCI207}
|
|
|
|
* computer??: {GeminiII},{CommanderIII}
|
|
|
|
* computer??: {GeminiII},{CommanderIII}
|
|
|
|
* ??: 1
|
|
|
|
*
|
|
|
|
* Followed by the data values (all comma-separated, all one long line).
|
|
|
|
*/
|
|
|
|
static int try_to_open_csv(const char *filename, struct memblock *mem, enum csv_format type)
|
|
|
|
{
|
|
|
|
char *p = mem->buffer;
|
|
|
|
char *header[8];
|
|
|
|
int i, time;
|
2012-09-20 00:35:52 +00:00
|
|
|
timestamp_t date;
|
2012-06-20 03:07:42 +00:00
|
|
|
struct dive *dive;
|
2013-02-09 15:41:15 +00:00
|
|
|
struct divecomputer *dc;
|
2012-06-20 03:07:42 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
header[i] = p;
|
|
|
|
p = strchr(p, ',');
|
|
|
|
if (!p)
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
date = parse_date(header[2]);
|
|
|
|
if (!date)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dive = alloc_dive();
|
|
|
|
dive->when = date;
|
|
|
|
dive->number = atoi(header[1]);
|
2013-02-09 15:41:15 +00:00
|
|
|
dc = &dive->dc;
|
2012-06-20 03:07:42 +00:00
|
|
|
|
|
|
|
time = 0;
|
|
|
|
for (;;) {
|
|
|
|
char *end;
|
|
|
|
double val;
|
|
|
|
struct sample *sample;
|
|
|
|
|
|
|
|
errno = 0;
|
2014-02-28 04:09:57 +00:00
|
|
|
val = strtod(p, &end); // FIXME == localization issue
|
2012-06-20 03:07:42 +00:00
|
|
|
if (end == p)
|
|
|
|
break;
|
|
|
|
if (errno)
|
|
|
|
break;
|
|
|
|
|
2013-02-09 15:41:15 +00:00
|
|
|
sample = prepare_sample(dc);
|
2012-06-20 03:07:42 +00:00
|
|
|
sample->time.seconds = time;
|
|
|
|
add_sample_data(sample, type, val);
|
2013-02-09 15:41:15 +00:00
|
|
|
finish_sample(dc);
|
2012-06-20 03:07:42 +00:00
|
|
|
|
|
|
|
time++;
|
2013-02-09 15:41:15 +00:00
|
|
|
dc->duration.seconds = time;
|
2012-06-20 03:07:42 +00:00
|
|
|
if (*end != ',')
|
|
|
|
break;
|
2014-02-28 04:09:57 +00:00
|
|
|
p = end + 1;
|
2012-06-20 03:07:42 +00:00
|
|
|
}
|
|
|
|
record_dive(dive);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem)
|
2012-01-27 01:43:33 +00:00
|
|
|
{
|
2014-12-08 21:09:34 +00:00
|
|
|
// hack to be able to provide a comment for the translated string
|
|
|
|
static char *csv_warning = QT_TRANSLATE_NOOP3("gettextFromC",
|
|
|
|
"Cannot open CSV file %s; please use Import log file dialog",
|
|
|
|
"'Import log file' should be the same text as corresponding label in Import menu");
|
|
|
|
|
2013-09-17 18:23:54 +00:00
|
|
|
/* Suunto Dive Manager files: SDE, ZIP; divelogs.de files: DLD */
|
|
|
|
if (!strcasecmp(fmt, "SDE") || !strcasecmp(fmt, "ZIP") || !strcasecmp(fmt, "DLD"))
|
2014-03-14 18:26:07 +00:00
|
|
|
return try_to_open_zip(filename, mem);
|
2012-01-27 01:43:33 +00:00
|
|
|
|
2013-09-29 12:44:38 +00:00
|
|
|
/* CSV files */
|
2014-01-15 13:59:25 +00:00
|
|
|
if (!strcasecmp(fmt, "CSV"))
|
2014-12-08 21:09:34 +00:00
|
|
|
return report_error(translate("gettextFromC", csv_warning), filename);
|
2012-01-27 20:43:40 +00:00
|
|
|
/* Truly nasty intentionally obfuscated Cochran Anal software */
|
|
|
|
if (!strcasecmp(fmt, "CAN"))
|
2014-03-14 18:26:07 +00:00
|
|
|
return try_to_open_cochran(filename, mem);
|
2012-06-20 03:07:42 +00:00
|
|
|
/* Cochran export comma-separated-value files */
|
|
|
|
if (!strcasecmp(fmt, "DPT"))
|
|
|
|
return try_to_open_csv(filename, mem, CSV_DEPTH);
|
2014-11-07 16:30:44 +00:00
|
|
|
if (!strcasecmp(fmt, "LVD"))
|
|
|
|
return try_to_open_liquivision(filename, mem);
|
2012-06-20 03:07:42 +00:00
|
|
|
if (!strcasecmp(fmt, "TMP"))
|
|
|
|
return try_to_open_csv(filename, mem, CSV_TEMP);
|
|
|
|
if (!strcasecmp(fmt, "HP1"))
|
|
|
|
return try_to_open_csv(filename, mem, CSV_PRESSURE);
|
|
|
|
|
2012-01-27 01:43:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:26:03 +00:00
|
|
|
static int parse_file_buffer(const char *filename, struct memblock *mem)
|
2012-01-27 18:56:36 +00:00
|
|
|
{
|
2014-12-08 19:26:03 +00:00
|
|
|
int ret;
|
2012-01-27 18:56:36 +00:00
|
|
|
char *fmt = strrchr(filename, '.');
|
2014-12-08 19:26:03 +00:00
|
|
|
if (fmt && (ret = open_by_filename(filename, fmt + 1, mem)) != 0)
|
|
|
|
return ret;
|
2012-01-27 18:56:36 +00:00
|
|
|
|
2013-12-09 06:42:51 +00:00
|
|
|
if (!mem->size || !mem->buffer)
|
2014-12-08 19:26:03 +00:00
|
|
|
return report_error("Out of memory parsing file %s\n", filename);
|
2013-12-09 06:42:51 +00:00
|
|
|
|
2014-12-08 19:26:03 +00:00
|
|
|
return parse_xml_buffer(filename, mem->buffer, mem->size, &dive_table, NULL);
|
2012-01-27 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
int parse_file(const char *filename)
|
2012-01-26 21:00:45 +00:00
|
|
|
{
|
2014-03-12 21:12:58 +00:00
|
|
|
struct git_repository *git;
|
|
|
|
const char *branch;
|
2012-01-26 21:00:45 +00:00
|
|
|
struct memblock mem;
|
2013-03-05 05:10:39 +00:00
|
|
|
char *fmt;
|
2014-12-08 19:26:03 +00:00
|
|
|
int ret;
|
2012-01-26 21:00:45 +00:00
|
|
|
|
2015-06-12 19:32:12 +00:00
|
|
|
git = is_git_repository(filename, &branch, NULL);
|
2015-06-16 13:27:12 +00:00
|
|
|
if (prefs.cloud_git_url &&
|
|
|
|
strstr(filename, prefs.cloud_git_url)
|
|
|
|
&& git == dummy_git_repository)
|
2015-06-13 20:16:08 +00:00
|
|
|
/* opening the cloud storage repository failed for some reason
|
|
|
|
* give up here and don't send errors about git repositories */
|
|
|
|
return 0;
|
|
|
|
|
2014-03-12 21:12:58 +00:00
|
|
|
if (git && !git_load_dives(git, branch))
|
2014-03-14 18:26:07 +00:00
|
|
|
return 0;
|
2014-03-12 21:12:58 +00:00
|
|
|
|
2012-01-26 21:00:45 +00:00
|
|
|
if (readfile(filename, &mem) < 0) {
|
2015-06-13 16:08:39 +00:00
|
|
|
/* we don't want to display an error if this was the default file or the cloud storage */
|
|
|
|
if ((prefs.default_filename && !strcmp(filename, prefs.default_filename)) ||
|
|
|
|
isCloudUrl(filename))
|
2014-03-14 18:26:07 +00:00
|
|
|
return 0;
|
2012-11-10 14:32:06 +00:00
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);
|
2012-01-26 21:00:45 +00:00
|
|
|
}
|
|
|
|
|
2013-03-05 05:10:39 +00:00
|
|
|
fmt = strrchr(filename, '.');
|
2015-07-12 17:46:47 +00:00
|
|
|
if (fmt && (!strcasecmp(fmt + 1, "DB") || !strcasecmp(fmt + 1, "BAK") || !strcasecmp(fmt + 1, "SQL"))) {
|
2014-03-14 18:26:07 +00:00
|
|
|
if (!try_to_open_db(filename, &mem)) {
|
2013-03-05 05:10:39 +00:00
|
|
|
free(mem.buffer);
|
2014-03-14 18:26:07 +00:00
|
|
|
return 0;
|
2013-03-05 05:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 20:10:44 +00:00
|
|
|
/* Divesoft Freedom */
|
|
|
|
if (fmt && (!strcasecmp(fmt + 1, "DLF"))) {
|
|
|
|
if (!parse_dlf_buffer(mem.buffer, mem.size)) {
|
|
|
|
free(mem.buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
Import Datatrak/WLog files
Sequentially parses a file, expected to be a Datatrak/WLog divelog, and
converts the dive info into Subsurface's dive structure.
As my first DC, back in 90s, was an Aladin Air X, the obvious choice of log
software was DTrak (Win version). After using it for some time we moved to WLog
(shareware software more user friendly than Dtrak, printing capable, and still
better, it runs under wine, which, as linux user, was definitive for me). Then,
some years later, my last Aladin died and I moved to an OSTC, forcing me to
look for a software that support this DC.
I found JDivelog which was capable of import Dtrak logs and used it for some
time until discovered Subsurface existence and devoted to it.
The fact was that importing Dtrak dives in JDivelog and then re-importing them
in Subsurface caused a significant data loss (mainly in the profile events and
alarms) and weird location of some other info in the dive notes (mostly tag
items in the original Dtrak software). This situation can't actually be solved
with tools like divelogs.de which causes similar if no greater data loss.
Although this won't be a core feature for Subsurface, I expect it can be useful
for some other divers as has been for me.
Comments and issues:
Datatrak/Wlog files include a lot of diving data which are not directly
supported in Subsurface, in these cases we choose mostly to use "tags".
The lack of some important info in Datatrak archives (e.g. tank's initial
pressure) forces us to do some arbitrary assumptions (e.g. initial pressure =
200 bar).
There might be archives coming directly from old DOS days, as first versions
of Datatrak run on that OS; they were coded CP437 or CP850, while dive logs
coming from Win versions seems to be coded CP1252. Finally, Wlog seems to use a
mixed confusing style. Program directly converts some of the old encoded chars
to iso8859 but is expected there be some issues with non alphabetic chars, e.g.
"ª".
There are two text fields: "Other activities" and "Dive notes", both limited to
256 char size. We have merged them in Subsurface's "Dive Notes" although the
first one could be "tagged", but we're unsure that the user had filled it in
a tag friendly way.
WLog adds some information to the dive and lets the user to write more than
256 chars notes. This is achieved, while keeping compatibility with DTrak
divelogs, by adding a complementary file named equally as the .log file and
with .add extension where all this info is stored. We have, still, not worked
with this complementary files.
This work is based on the paper referenced in butracker #194 which has some
errors (e.g. beginning of log and beginning of dive are changed) and a lot of
bytes of unknown meaning. Example.log shows, at least, one more byte than those
referred in the paper for the O2 Aladin computer, this could be a byte referred
to the use of SCR but the lack of an OC dive with O2 computer makes impossible
for us to compare.
The only way we have figured out to distinguish a priori between SCR and non
SCR dives with O2 computers is that the dives are tagged with a "rebreather"
tag. Obviously this is not a very trusty way of doing things. In SCR dives,
the O2% in mix means, probably, the maximum O2% in the circuit, not the O2%
of the EAN mix in the tanks, which would be unknown in this case.
The list of DCs related in bug #194 paper seems incomplete, we have added
one or two from WLog and discarded those which are known to exist but whose
model is unknown, grouping them under the imaginative name of "unknown". The
list can easily be increased in the future if we ever know the models
identifiers.
BTW, in Example.log, 0x00 identifier is used for some DC dives and from my own
divelogs is inferred that 0x00 is used for manually entered dives, this could
easily be an error in Example.log coming from a preproduction DC model.
Example.log which is shipped in datatrak package is included in dives
directory for testing pourposes.
[Dirk Hohndel: some small cleanups, merged with latest master, support
divesites, remove the pointless memset() before free() calls
add to cmake build]
Signed-off-by: Salvador Cuñat <salvador.cunat@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-11-05 18:38:27 +00:00
|
|
|
/* DataTrak/Wlog */
|
2015-03-10 22:23:14 +00:00
|
|
|
if (fmt && !strcasecmp(fmt + 1, "LOG")) {
|
Import Datatrak/WLog files
Sequentially parses a file, expected to be a Datatrak/WLog divelog, and
converts the dive info into Subsurface's dive structure.
As my first DC, back in 90s, was an Aladin Air X, the obvious choice of log
software was DTrak (Win version). After using it for some time we moved to WLog
(shareware software more user friendly than Dtrak, printing capable, and still
better, it runs under wine, which, as linux user, was definitive for me). Then,
some years later, my last Aladin died and I moved to an OSTC, forcing me to
look for a software that support this DC.
I found JDivelog which was capable of import Dtrak logs and used it for some
time until discovered Subsurface existence and devoted to it.
The fact was that importing Dtrak dives in JDivelog and then re-importing them
in Subsurface caused a significant data loss (mainly in the profile events and
alarms) and weird location of some other info in the dive notes (mostly tag
items in the original Dtrak software). This situation can't actually be solved
with tools like divelogs.de which causes similar if no greater data loss.
Although this won't be a core feature for Subsurface, I expect it can be useful
for some other divers as has been for me.
Comments and issues:
Datatrak/Wlog files include a lot of diving data which are not directly
supported in Subsurface, in these cases we choose mostly to use "tags".
The lack of some important info in Datatrak archives (e.g. tank's initial
pressure) forces us to do some arbitrary assumptions (e.g. initial pressure =
200 bar).
There might be archives coming directly from old DOS days, as first versions
of Datatrak run on that OS; they were coded CP437 or CP850, while dive logs
coming from Win versions seems to be coded CP1252. Finally, Wlog seems to use a
mixed confusing style. Program directly converts some of the old encoded chars
to iso8859 but is expected there be some issues with non alphabetic chars, e.g.
"ª".
There are two text fields: "Other activities" and "Dive notes", both limited to
256 char size. We have merged them in Subsurface's "Dive Notes" although the
first one could be "tagged", but we're unsure that the user had filled it in
a tag friendly way.
WLog adds some information to the dive and lets the user to write more than
256 chars notes. This is achieved, while keeping compatibility with DTrak
divelogs, by adding a complementary file named equally as the .log file and
with .add extension where all this info is stored. We have, still, not worked
with this complementary files.
This work is based on the paper referenced in butracker #194 which has some
errors (e.g. beginning of log and beginning of dive are changed) and a lot of
bytes of unknown meaning. Example.log shows, at least, one more byte than those
referred in the paper for the O2 Aladin computer, this could be a byte referred
to the use of SCR but the lack of an OC dive with O2 computer makes impossible
for us to compare.
The only way we have figured out to distinguish a priori between SCR and non
SCR dives with O2 computers is that the dives are tagged with a "rebreather"
tag. Obviously this is not a very trusty way of doing things. In SCR dives,
the O2% in mix means, probably, the maximum O2% in the circuit, not the O2%
of the EAN mix in the tanks, which would be unknown in this case.
The list of DCs related in bug #194 paper seems incomplete, we have added
one or two from WLog and discarded those which are known to exist but whose
model is unknown, grouping them under the imaginative name of "unknown". The
list can easily be increased in the future if we ever know the models
identifiers.
BTW, in Example.log, 0x00 identifier is used for some DC dives and from my own
divelogs is inferred that 0x00 is used for manually entered dives, this could
easily be an error in Example.log coming from a preproduction DC model.
Example.log which is shipped in datatrak package is included in dives
directory for testing pourposes.
[Dirk Hohndel: some small cleanups, merged with latest master, support
divesites, remove the pointless memset() before free() calls
add to cmake build]
Signed-off-by: Salvador Cuñat <salvador.cunat@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-11-05 18:38:27 +00:00
|
|
|
datatrak_import(filename, &dive_table);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-03 23:07:59 +00:00
|
|
|
/* OSTCtools */
|
|
|
|
if (fmt && (!strcasecmp(fmt + 1, "DIVE"))) {
|
|
|
|
ostctools_import(filename, &dive_table);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:26:03 +00:00
|
|
|
ret = parse_file_buffer(filename, &mem);
|
2012-01-26 21:00:45 +00:00
|
|
|
free(mem.buffer);
|
2014-12-08 19:26:03 +00:00
|
|
|
return ret;
|
2012-01-26 21:00:45 +00:00
|
|
|
}
|
2013-10-16 19:05:19 +00:00
|
|
|
|
2014-05-28 06:55:46 +00:00
|
|
|
#define MATCH(buffer, pattern) \
|
|
|
|
memcmp(buffer, pattern, strlen(pattern))
|
|
|
|
|
|
|
|
char *parse_mkvi_value(const char *haystack, const char *needle)
|
|
|
|
{
|
|
|
|
char *lineptr, *valueptr, *endptr, *ret = NULL;
|
|
|
|
|
|
|
|
if ((lineptr = strstr(haystack, needle)) != NULL) {
|
|
|
|
if ((valueptr = strstr(lineptr, ": ")) != NULL) {
|
|
|
|
valueptr += 2;
|
|
|
|
}
|
|
|
|
if ((endptr = strstr(lineptr, "\n")) != NULL) {
|
2014-11-15 10:19:12 +00:00
|
|
|
char terminator = '\n';
|
|
|
|
if (*(endptr - 1) == '\r') {
|
|
|
|
--endptr;
|
|
|
|
terminator = '\r';
|
|
|
|
}
|
2014-05-28 06:55:46 +00:00
|
|
|
*endptr = 0;
|
2015-06-22 04:43:38 +00:00
|
|
|
ret = copy_string(valueptr);
|
2014-11-15 10:19:12 +00:00
|
|
|
*endptr = terminator;
|
2014-05-28 06:55:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-09 12:03:45 +00:00
|
|
|
char *next_mkvi_key(const char *haystack)
|
|
|
|
{
|
|
|
|
char *valueptr, *endptr, *ret = NULL;
|
|
|
|
|
|
|
|
if ((valueptr = strstr(haystack, "\n")) != NULL) {
|
|
|
|
valueptr += 1;
|
2015-06-22 04:43:38 +00:00
|
|
|
if ((endptr = strstr(valueptr, ": ")) != NULL) {
|
|
|
|
*endptr = 0;
|
|
|
|
ret = strdup(valueptr);
|
|
|
|
*endptr = ':';
|
|
|
|
}
|
2014-11-09 12:03:45 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-28 06:55:46 +00:00
|
|
|
int parse_txt_file(const char *filename, const char *csv)
|
|
|
|
{
|
|
|
|
struct memblock memtxt, memcsv;
|
|
|
|
|
|
|
|
if (readfile(filename, &memtxt) < 0) {
|
|
|
|
return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MkVI stores some information in .txt file but the whole profile and events are stored in .csv file. First
|
|
|
|
* make sure the input .txt looks like proper MkVI file, then start parsing the .csv.
|
|
|
|
*/
|
|
|
|
if (MATCH(memtxt.buffer, "MkVI_Config") == 0) {
|
2014-10-31 21:06:37 +00:00
|
|
|
int d, m, y, he;
|
2014-05-28 06:55:46 +00:00
|
|
|
int hh = 0, mm = 0, ss = 0;
|
2014-11-19 21:14:19 +00:00
|
|
|
int prev_depth = 0, cur_sampletime = 0, prev_setpoint = -1, prev_ndl = -1;
|
|
|
|
bool has_depth = false, has_setpoint = false, has_ndl = false;
|
2014-11-09 12:03:45 +00:00
|
|
|
char *lineptr, *key, *value;
|
2014-11-17 11:25:00 +00:00
|
|
|
int o2cylinder_pressure = 0, cylinder_pressure = 0, cur_cylinder_index = 0;
|
2014-05-28 06:55:46 +00:00
|
|
|
|
|
|
|
struct dive *dive;
|
|
|
|
struct divecomputer *dc;
|
|
|
|
struct tm cur_tm;
|
|
|
|
|
2015-06-22 03:24:07 +00:00
|
|
|
value = parse_mkvi_value(memtxt.buffer, "Dive started at");
|
|
|
|
if (sscanf(value, "%d-%d-%d %d:%d:%d", &y, &m, &d, &hh, &mm, &ss) != 6) {
|
|
|
|
free(value);
|
2014-05-28 06:55:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-06-22 03:24:07 +00:00
|
|
|
free(value);
|
2014-05-28 06:55:46 +00:00
|
|
|
cur_tm.tm_year = y;
|
|
|
|
cur_tm.tm_mon = m - 1;
|
|
|
|
cur_tm.tm_mday = d;
|
|
|
|
cur_tm.tm_hour = hh;
|
|
|
|
cur_tm.tm_min = mm;
|
|
|
|
cur_tm.tm_sec = ss;
|
|
|
|
|
|
|
|
dive = alloc_dive();
|
|
|
|
dive->when = utc_mktime(&cur_tm);;
|
|
|
|
dive->dc.model = strdup("Poseidon MkVI Discovery");
|
2014-11-09 12:17:29 +00:00
|
|
|
value = parse_mkvi_value(memtxt.buffer, "Rig Serial number");
|
|
|
|
dive->dc.deviceid = atoi(value);
|
|
|
|
free(value);
|
2015-01-10 23:01:15 +00:00
|
|
|
dive->dc.divemode = CCR;
|
2014-10-30 20:15:16 +00:00
|
|
|
dive->dc.no_o2sensors = 2;
|
2014-05-28 06:55:46 +00:00
|
|
|
|
2014-11-09 13:36:27 +00:00
|
|
|
dive->cylinder[cur_cylinder_index].cylinder_use = OXYGEN;
|
2014-05-28 06:55:46 +00:00
|
|
|
dive->cylinder[cur_cylinder_index].type.size.mliter = 3000;
|
|
|
|
dive->cylinder[cur_cylinder_index].type.workingpressure.mbar = 200000;
|
|
|
|
dive->cylinder[cur_cylinder_index].type.description = strdup("3l Mk6");
|
2014-10-31 21:06:37 +00:00
|
|
|
dive->cylinder[cur_cylinder_index].gasmix.o2.permille = 1000;
|
2014-05-28 06:55:46 +00:00
|
|
|
cur_cylinder_index++;
|
|
|
|
|
2014-11-09 13:36:27 +00:00
|
|
|
dive->cylinder[cur_cylinder_index].cylinder_use = DILUENT;
|
2014-05-28 06:55:46 +00:00
|
|
|
dive->cylinder[cur_cylinder_index].type.size.mliter = 3000;
|
|
|
|
dive->cylinder[cur_cylinder_index].type.workingpressure.mbar = 200000;
|
|
|
|
dive->cylinder[cur_cylinder_index].type.description = strdup("3l Mk6");
|
2014-11-09 12:17:29 +00:00
|
|
|
value = parse_mkvi_value(memtxt.buffer, "Helium percentage");
|
|
|
|
he = atoi(value);
|
|
|
|
free(value);
|
|
|
|
value = parse_mkvi_value(memtxt.buffer, "Nitrogen percentage");
|
|
|
|
dive->cylinder[cur_cylinder_index].gasmix.o2.permille = (100 - atoi(value) - he) * 10;
|
|
|
|
free(value);
|
2014-10-31 21:06:37 +00:00
|
|
|
dive->cylinder[cur_cylinder_index].gasmix.he.permille = he * 10;
|
2014-05-28 06:55:46 +00:00
|
|
|
cur_cylinder_index++;
|
|
|
|
|
2014-10-31 21:06:38 +00:00
|
|
|
lineptr = strstr(memtxt.buffer, "Dive started at");
|
2014-11-09 12:03:45 +00:00
|
|
|
while (lineptr && *lineptr && (lineptr = strchr(lineptr, '\n')) && ++lineptr) {
|
|
|
|
key = next_mkvi_key(lineptr);
|
|
|
|
if (!key)
|
|
|
|
break;
|
|
|
|
value = parse_mkvi_value(lineptr, key);
|
|
|
|
if (!value) {
|
|
|
|
free(key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
add_extra_data(&dive->dc, key, value);
|
|
|
|
free(key);
|
|
|
|
free(value);
|
|
|
|
}
|
2014-05-28 06:55:46 +00:00
|
|
|
dc = &dive->dc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read samples from the CSV file. A sample contains all the lines with same timestamp. The CSV file has
|
|
|
|
* the following format:
|
|
|
|
*
|
|
|
|
* timestamp, type, value
|
|
|
|
*
|
|
|
|
* And following fields are of interest to us:
|
|
|
|
*
|
|
|
|
* 6 sensor1
|
|
|
|
* 7 sensor2
|
|
|
|
* 8 depth
|
|
|
|
* 13 o2 tank pressure
|
|
|
|
* 14 diluent tank pressure
|
|
|
|
* 20 o2 setpoint
|
|
|
|
* 39 water temp
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (readfile(csv, &memcsv) < 0) {
|
2015-06-22 03:24:07 +00:00
|
|
|
free(dive);
|
2014-05-28 06:55:46 +00:00
|
|
|
return report_error(translate("gettextFromC", "Poseidon import failed: unable to read '%s'"), csv);
|
|
|
|
}
|
|
|
|
lineptr = memcsv.buffer;
|
|
|
|
for (;;) {
|
|
|
|
struct sample *sample;
|
|
|
|
int type;
|
|
|
|
int value;
|
|
|
|
int sampletime;
|
2014-11-19 21:14:22 +00:00
|
|
|
int gaschange = 0;
|
2014-05-28 06:55:46 +00:00
|
|
|
|
|
|
|
/* Collect all the information for one sample */
|
|
|
|
sscanf(lineptr, "%d,%d,%d", &cur_sampletime, &type, &value);
|
|
|
|
|
|
|
|
has_depth = false;
|
2014-10-27 16:19:54 +00:00
|
|
|
has_setpoint = false;
|
2014-11-19 21:14:19 +00:00
|
|
|
has_ndl = false;
|
2014-05-28 06:55:46 +00:00
|
|
|
sample = prepare_sample(dc);
|
|
|
|
sample->time.seconds = cur_sampletime;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int i = sscanf(lineptr, "%d,%d,%d", &sampletime, &type, &value);
|
|
|
|
switch (i) {
|
|
|
|
case 3:
|
|
|
|
switch (type) {
|
2014-11-19 21:14:21 +00:00
|
|
|
case 0:
|
2014-11-20 20:02:37 +00:00
|
|
|
//Mouth piece position event: 0=OC, 1=CC, 2=UN, 3=NC
|
2014-11-19 21:14:21 +00:00
|
|
|
switch (value) {
|
|
|
|
case 0:
|
|
|
|
add_event(dc, cur_sampletime, 0, 0, 0,
|
2014-11-20 20:02:37 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Mouth piece position OC"));
|
2014-11-19 21:14:21 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
add_event(dc, cur_sampletime, 0, 0, 0,
|
2014-11-20 20:02:37 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Mouth piece position CC"));
|
2014-11-19 21:14:21 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
add_event(dc, cur_sampletime, 0, 0, 0,
|
2014-11-20 20:02:37 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Mouth piece position unknown"));
|
2014-11-19 21:14:21 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
add_event(dc, cur_sampletime, 0, 0, 0,
|
2014-11-20 20:02:37 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Mouth piece position not connected"));
|
2014-11-19 21:14:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-23 00:35:34 +00:00
|
|
|
break;
|
2014-11-19 21:14:23 +00:00
|
|
|
case 3:
|
|
|
|
//Power Off event
|
|
|
|
add_event(dc, cur_sampletime, 0, 0, 0,
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Power off"));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
//Battery State of Charge in %
|
|
|
|
#ifdef SAMPLE_EVENT_BATTERY
|
|
|
|
add_event(dc, cur_sampletime, SAMPLE_EVENT_BATTERY, 0,
|
|
|
|
value, QT_TRANSLATE_NOOP("gettextFromC", "battery"));
|
|
|
|
#endif
|
2014-11-19 21:14:21 +00:00
|
|
|
break;
|
2014-05-28 06:55:46 +00:00
|
|
|
case 6:
|
2014-11-19 21:14:24 +00:00
|
|
|
//PO2 Cell 1 Average
|
2014-05-28 06:55:46 +00:00
|
|
|
add_sample_data(sample, POSEIDON_SENSOR1, value);
|
|
|
|
break;
|
|
|
|
case 7:
|
2014-11-19 21:14:24 +00:00
|
|
|
//PO2 Cell 2 Average
|
2014-05-28 06:55:46 +00:00
|
|
|
add_sample_data(sample, POSEIDON_SENSOR2, value);
|
|
|
|
break;
|
|
|
|
case 8:
|
2014-11-19 21:14:24 +00:00
|
|
|
//Depth * 2
|
2014-05-28 06:55:46 +00:00
|
|
|
has_depth = true;
|
|
|
|
prev_depth = value;
|
|
|
|
add_sample_data(sample, POSEIDON_DEPTH, value);
|
|
|
|
break;
|
2014-11-19 21:14:24 +00:00
|
|
|
//9 Max Depth * 2
|
|
|
|
//10 Ascent/Descent Rate * 2
|
2014-11-19 21:14:23 +00:00
|
|
|
case 11:
|
|
|
|
//Ascent Rate Alert >10 m/s
|
|
|
|
add_event(dc, cur_sampletime, SAMPLE_EVENT_ASCENT, 0, 0,
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "ascent"));
|
|
|
|
break;
|
2014-05-28 06:55:46 +00:00
|
|
|
case 13:
|
2014-11-19 21:14:24 +00:00
|
|
|
//O2 Tank Pressure
|
2014-11-17 11:25:00 +00:00
|
|
|
add_sample_data(sample, POSEIDON_O2CYLINDER, value);
|
|
|
|
if (!o2cylinder_pressure) {
|
2014-11-17 19:04:36 +00:00
|
|
|
dive->cylinder[0].sample_start.mbar = value * 1000;
|
2014-11-17 11:25:00 +00:00
|
|
|
o2cylinder_pressure = value;
|
|
|
|
} else
|
|
|
|
o2cylinder_pressure = value;
|
|
|
|
break;
|
|
|
|
case 14:
|
2014-11-19 21:14:24 +00:00
|
|
|
//Diluent Tank Pressure
|
2014-05-28 06:55:46 +00:00
|
|
|
add_sample_data(sample, POSEIDON_PRESSURE, value);
|
2014-11-01 08:06:46 +00:00
|
|
|
if (!cylinder_pressure) {
|
2014-11-17 19:04:36 +00:00
|
|
|
dive->cylinder[1].sample_start.mbar = value * 1000;
|
2014-11-01 08:06:46 +00:00
|
|
|
cylinder_pressure = value;
|
|
|
|
} else
|
|
|
|
cylinder_pressure = value;
|
2014-05-28 06:55:46 +00:00
|
|
|
break;
|
2014-11-19 21:14:24 +00:00
|
|
|
//16 Remaining dive time #1?
|
|
|
|
//17 related to O2 injection
|
2014-05-28 06:55:46 +00:00
|
|
|
case 20:
|
2014-11-19 21:14:24 +00:00
|
|
|
//PO2 Setpoint
|
2014-10-27 16:19:54 +00:00
|
|
|
has_setpoint = true;
|
|
|
|
prev_setpoint = value;
|
2014-05-28 06:55:46 +00:00
|
|
|
add_sample_data(sample, POSEIDON_SETPOINT, value);
|
|
|
|
break;
|
2014-11-19 21:14:23 +00:00
|
|
|
case 22:
|
|
|
|
//End of O2 calibration Event: 0 = OK, 2 = Failed, rest of dive setpoint 1.0
|
|
|
|
if (value == 2)
|
|
|
|
add_event(dc, cur_sampletime, 0, SAMPLE_FLAGS_END, 0,
|
2014-11-25 15:47:26 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "O₂ calibration failed"));
|
2014-11-19 21:14:23 +00:00
|
|
|
add_event(dc, cur_sampletime, 0, SAMPLE_FLAGS_END, 0,
|
2014-11-25 15:47:26 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "O₂ calibration"));
|
2014-11-19 21:14:23 +00:00
|
|
|
break;
|
2014-11-19 21:14:20 +00:00
|
|
|
case 25:
|
|
|
|
//25 Max Ascent depth
|
|
|
|
add_sample_data(sample, POSEIDON_CEILING, value);
|
|
|
|
break;
|
2014-11-19 21:14:23 +00:00
|
|
|
case 31:
|
|
|
|
//Start of O2 calibration Event
|
|
|
|
add_event(dc, cur_sampletime, 0, SAMPLE_FLAGS_BEGIN, 0,
|
2014-11-25 15:47:26 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "O₂ calibration"));
|
2014-11-19 21:14:23 +00:00
|
|
|
break;
|
2014-11-19 21:14:19 +00:00
|
|
|
case 37:
|
|
|
|
//Remaining dive time #2?
|
|
|
|
has_ndl = true;
|
|
|
|
prev_ndl = value;
|
|
|
|
add_sample_data(sample, POSEIDON_NDL, value);
|
|
|
|
break;
|
2014-05-28 06:55:46 +00:00
|
|
|
case 39:
|
2014-11-19 21:14:24 +00:00
|
|
|
// Water Temperature in Celcius
|
2014-05-28 06:55:46 +00:00
|
|
|
add_sample_data(sample, POSEIDON_TEMP, value);
|
|
|
|
break;
|
2014-11-19 21:14:22 +00:00
|
|
|
case 85:
|
|
|
|
//He diluent part in %
|
2014-11-20 20:08:13 +00:00
|
|
|
gaschange += value << 16;
|
2014-11-19 21:14:22 +00:00
|
|
|
break;
|
|
|
|
case 86:
|
|
|
|
//O2 diluent part in %
|
2014-11-20 20:08:13 +00:00
|
|
|
gaschange += value;
|
2014-11-19 21:14:22 +00:00
|
|
|
break;
|
2014-11-19 21:14:24 +00:00
|
|
|
//239 Unknown, maybe PO2 at sensor validation?
|
|
|
|
//240 Unknown, maybe PO2 at sensor validation?
|
|
|
|
//247 Unknown, maybe PO2 Cell 1 during pressure test
|
|
|
|
//248 Unknown, maybe PO2 Cell 2 during pressure test
|
|
|
|
//250 PO2 Cell 1
|
|
|
|
//251 PO2 Cell 2
|
2014-05-28 06:55:46 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} /* sample types */
|
|
|
|
break;
|
|
|
|
case EOF:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Unable to parse input: %s\n", lineptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lineptr = strchr(lineptr, '\n');
|
|
|
|
if (!lineptr || !*lineptr)
|
|
|
|
break;
|
|
|
|
lineptr++;
|
|
|
|
|
|
|
|
/* Grabbing next sample time */
|
|
|
|
sscanf(lineptr, "%d,%d,%d", &cur_sampletime, &type, &value);
|
|
|
|
} while (sampletime == cur_sampletime);
|
|
|
|
|
2014-11-19 21:14:22 +00:00
|
|
|
if (gaschange)
|
|
|
|
add_event(dc, cur_sampletime, SAMPLE_EVENT_GASCHANGE2, 0, gaschange,
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "gaschange"));
|
2014-05-28 06:55:46 +00:00
|
|
|
if (!has_depth)
|
|
|
|
add_sample_data(sample, POSEIDON_DEPTH, prev_depth);
|
2015-01-05 06:00:20 +00:00
|
|
|
if (!has_setpoint && prev_setpoint >= 0)
|
2014-10-27 16:19:54 +00:00
|
|
|
add_sample_data(sample, POSEIDON_SETPOINT, prev_setpoint);
|
2015-01-05 06:00:20 +00:00
|
|
|
if (!has_ndl && prev_ndl >= 0)
|
2014-11-19 21:14:19 +00:00
|
|
|
add_sample_data(sample, POSEIDON_NDL, prev_ndl);
|
2014-11-01 08:06:46 +00:00
|
|
|
if (cylinder_pressure)
|
2014-11-17 11:25:00 +00:00
|
|
|
dive->cylinder[1].sample_end.mbar = cylinder_pressure * 1000;
|
|
|
|
if (o2cylinder_pressure)
|
|
|
|
dive->cylinder[0].sample_end.mbar = o2cylinder_pressure * 1000;
|
2014-05-28 06:55:46 +00:00
|
|
|
finish_sample(dc);
|
|
|
|
|
|
|
|
if (!lineptr || !*lineptr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
record_dive(dive);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return report_error(translate("gettextFromC", "No matching DC found for file '%s'"), csv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:05:19 +00:00
|
|
|
#define MAXCOLDIGITS 3
|
|
|
|
#define MAXCOLS 100
|
2014-10-28 09:14:00 +00:00
|
|
|
#define DATESTR 9
|
|
|
|
#define TIMESTR 6
|
2015-06-27 21:38:10 +00:00
|
|
|
void init_csv_file_parsing(char **params, char *timebuf, char *depthbuf, char *tempbuf, char *po2buf, char *o2sensor1buf, char *o2sensor2buf, char *o2sensor3buf, char *cnsbuf, char *ndlbuf, char *ttsbuf, char *stopdepthbuf, char *pressurebuf, char *unitbuf, char *separator_index, time_t *now, struct tm *timep, char *curdate, char *curtime, 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 sepidx, const char *csvtemplate, int unitidx)
|
2013-10-16 19:05:19 +00:00
|
|
|
{
|
2014-02-28 04:09:57 +00:00
|
|
|
int pnr = 0;
|
2013-10-16 19:05:19 +00:00
|
|
|
|
2013-10-19 05:17:13 +00:00
|
|
|
snprintf(timebuf, MAXCOLDIGITS, "%d", timef);
|
|
|
|
snprintf(depthbuf, MAXCOLDIGITS, "%d", depthf);
|
|
|
|
snprintf(tempbuf, MAXCOLDIGITS, "%d", tempf);
|
2013-11-21 22:48:40 +00:00
|
|
|
snprintf(po2buf, MAXCOLDIGITS, "%d", po2f);
|
2015-06-27 21:38:10 +00:00
|
|
|
snprintf(o2sensor1buf, MAXCOLDIGITS, "%d", o2sensor1f);
|
|
|
|
snprintf(o2sensor2buf, MAXCOLDIGITS, "%d", o2sensor2f);
|
|
|
|
snprintf(o2sensor3buf, MAXCOLDIGITS, "%d", o2sensor3f);
|
2013-11-21 22:48:41 +00:00
|
|
|
snprintf(cnsbuf, MAXCOLDIGITS, "%d", cnsf);
|
2014-07-09 20:13:37 +00:00
|
|
|
snprintf(ndlbuf, MAXCOLDIGITS, "%d", ndlf);
|
2014-07-09 20:13:38 +00:00
|
|
|
snprintf(ttsbuf, MAXCOLDIGITS, "%d", ttsf);
|
2013-11-21 22:48:42 +00:00
|
|
|
snprintf(stopdepthbuf, MAXCOLDIGITS, "%d", stopdepthf);
|
2014-07-10 18:54:18 +00:00
|
|
|
snprintf(pressurebuf, MAXCOLDIGITS, "%d", pressuref);
|
2013-12-04 23:19:28 +00:00
|
|
|
snprintf(separator_index, MAXCOLDIGITS, "%d", sepidx);
|
2014-02-15 08:51:23 +00:00
|
|
|
snprintf(unitbuf, MAXCOLDIGITS, "%d", unitidx);
|
2014-10-28 09:14:00 +00:00
|
|
|
time(now);
|
|
|
|
timep = localtime(now);
|
|
|
|
strftime(curdate, DATESTR, "%Y%m%d", timep);
|
2013-10-19 05:17:13 +00:00
|
|
|
|
|
|
|
/* As the parameter is numeric, we need to ensure that the leading zero
|
|
|
|
* is not discarded during the transform, thus prepend time with 1 */
|
2014-10-28 09:14:00 +00:00
|
|
|
strftime(curtime, TIMESTR, "1%H%M", timep);
|
2013-10-16 19:05:19 +00:00
|
|
|
|
2013-11-21 22:48:39 +00:00
|
|
|
params[pnr++] = "timeField";
|
|
|
|
params[pnr++] = timebuf;
|
|
|
|
params[pnr++] = "depthField";
|
|
|
|
params[pnr++] = depthbuf;
|
|
|
|
params[pnr++] = "tempField";
|
|
|
|
params[pnr++] = tempbuf;
|
2013-11-21 22:48:40 +00:00
|
|
|
params[pnr++] = "po2Field";
|
|
|
|
params[pnr++] = po2buf;
|
2015-06-27 21:38:10 +00:00
|
|
|
params[pnr++] = "o2sensor1Field";
|
|
|
|
params[pnr++] = o2sensor1buf;
|
|
|
|
params[pnr++] = "o2sensor2Field";
|
|
|
|
params[pnr++] = o2sensor2buf;
|
|
|
|
params[pnr++] = "o2sensor3Field";
|
|
|
|
params[pnr++] = o2sensor3buf;
|
2013-11-21 22:48:41 +00:00
|
|
|
params[pnr++] = "cnsField";
|
|
|
|
params[pnr++] = cnsbuf;
|
2014-07-09 20:13:37 +00:00
|
|
|
params[pnr++] = "ndlField";
|
|
|
|
params[pnr++] = ndlbuf;
|
2014-07-09 20:13:38 +00:00
|
|
|
params[pnr++] = "ttsField";
|
|
|
|
params[pnr++] = ttsbuf;
|
2013-11-21 22:48:42 +00:00
|
|
|
params[pnr++] = "stopdepthField";
|
|
|
|
params[pnr++] = stopdepthbuf;
|
2014-07-10 18:54:18 +00:00
|
|
|
params[pnr++] = "pressureField";
|
|
|
|
params[pnr++] = pressurebuf;
|
2013-11-21 22:48:39 +00:00
|
|
|
params[pnr++] = "date";
|
|
|
|
params[pnr++] = curdate;
|
|
|
|
params[pnr++] = "time";
|
|
|
|
params[pnr++] = curtime;
|
2014-02-15 08:51:23 +00:00
|
|
|
params[pnr++] = "units";
|
|
|
|
params[pnr++] = unitbuf;
|
2013-12-04 23:19:28 +00:00
|
|
|
params[pnr++] = "separatorIndex";
|
|
|
|
params[pnr++] = separator_index;
|
2013-11-21 22:48:39 +00:00
|
|
|
params[pnr++] = NULL;
|
2014-10-28 09:14:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 21:38:10 +00:00
|
|
|
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 sepidx, const char *csvtemplate, int unitidx)
|
2014-10-28 09:14:00 +00:00
|
|
|
{
|
2014-12-08 19:26:03 +00:00
|
|
|
int ret;
|
2014-10-28 09:14:00 +00:00
|
|
|
struct memblock mem;
|
2015-06-27 21:38:10 +00:00
|
|
|
char *params[33];
|
2014-10-28 09:14:00 +00:00
|
|
|
char timebuf[MAXCOLDIGITS];
|
|
|
|
char depthbuf[MAXCOLDIGITS];
|
|
|
|
char tempbuf[MAXCOLDIGITS];
|
|
|
|
char po2buf[MAXCOLDIGITS];
|
2015-06-27 21:38:10 +00:00
|
|
|
char o2sensor1buf[MAXCOLDIGITS];
|
|
|
|
char o2sensor2buf[MAXCOLDIGITS];
|
|
|
|
char o2sensor3buf[MAXCOLDIGITS];
|
2014-10-28 09:14:00 +00:00
|
|
|
char cnsbuf[MAXCOLDIGITS];
|
|
|
|
char ndlbuf[MAXCOLDIGITS];
|
|
|
|
char ttsbuf[MAXCOLDIGITS];
|
|
|
|
char stopdepthbuf[MAXCOLDIGITS];
|
|
|
|
char pressurebuf[MAXCOLDIGITS];
|
|
|
|
char unitbuf[MAXCOLDIGITS];
|
|
|
|
char separator_index[MAXCOLDIGITS];
|
|
|
|
time_t now;
|
2014-11-27 19:55:23 +00:00
|
|
|
struct tm *timep = NULL;
|
2014-10-28 09:14:00 +00:00
|
|
|
char curdate[DATESTR];
|
|
|
|
char curtime[TIMESTR];
|
2015-01-21 18:54:03 +00:00
|
|
|
int previous;
|
2014-10-28 09:14:00 +00:00
|
|
|
|
2015-02-08 20:37:38 +00:00
|
|
|
/* Increase the limits for recursion and variables on XSLT
|
|
|
|
* parsing */
|
|
|
|
xsltMaxDepth = 30000;
|
2015-02-18 05:58:34 +00:00
|
|
|
#if LIBXSLT_VERSION > 10126
|
2015-02-08 20:37:38 +00:00
|
|
|
xsltMaxVars = 150000;
|
2015-02-18 05:58:34 +00:00
|
|
|
#endif
|
2015-02-08 20:37:38 +00:00
|
|
|
|
2015-06-27 21:38:10 +00:00
|
|
|
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)
|
2014-10-28 09:14:00 +00:00
|
|
|
return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
|
|
|
|
|
2015-06-27 21:38:10 +00:00
|
|
|
init_csv_file_parsing(params, timebuf, depthbuf, tempbuf, po2buf, o2sensor1buf, o2sensor2buf, o2sensor3buf, cnsbuf, ndlbuf, ttsbuf, stopdepthbuf, pressurebuf, unitbuf, separator_index, &now, timep, curdate, curtime, timef, depthf, tempf, po2f, o2sensor1f, o2sensor2f, o2sensor3f, cnsf, ndlf, ttsf, stopdepthf, pressuref, sepidx, csvtemplate, unitidx);
|
2014-10-28 09:14:00 +00:00
|
|
|
|
|
|
|
if (filename == NULL)
|
|
|
|
return report_error("No CSV filename");
|
|
|
|
|
|
|
|
mem.size = 0;
|
|
|
|
if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
|
|
|
|
return -1;
|
|
|
|
|
2015-01-21 18:54:03 +00:00
|
|
|
previous = dive_table.nr;
|
2014-12-08 19:26:03 +00:00
|
|
|
ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
|
2015-01-21 18:54:03 +00:00
|
|
|
|
2014-10-28 09:14:00 +00:00
|
|
|
free(mem.buffer);
|
2014-12-08 19:26:03 +00:00
|
|
|
return ret;
|
2014-10-28 09:14:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 21:38:10 +00:00
|
|
|
#define SBPARAMS 35
|
|
|
|
int parse_seabear_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 sepidx, const char *csvtemplate, int unitidx, const char *delta)
|
2014-10-28 09:14:00 +00:00
|
|
|
{
|
2014-12-08 19:26:03 +00:00
|
|
|
int ret;
|
2014-11-08 13:11:07 +00:00
|
|
|
struct memblock mem;
|
2015-03-22 15:13:47 +00:00
|
|
|
char *params[SBPARAMS];
|
2014-10-28 09:14:00 +00:00
|
|
|
char timebuf[MAXCOLDIGITS];
|
|
|
|
char depthbuf[MAXCOLDIGITS];
|
|
|
|
char tempbuf[MAXCOLDIGITS];
|
|
|
|
char po2buf[MAXCOLDIGITS];
|
2015-06-27 21:38:10 +00:00
|
|
|
char o2sensor1buf[MAXCOLDIGITS];
|
|
|
|
char o2sensor2buf[MAXCOLDIGITS];
|
|
|
|
char o2sensor3buf[MAXCOLDIGITS];
|
2014-10-28 09:14:00 +00:00
|
|
|
char cnsbuf[MAXCOLDIGITS];
|
|
|
|
char ndlbuf[MAXCOLDIGITS];
|
|
|
|
char ttsbuf[MAXCOLDIGITS];
|
|
|
|
char stopdepthbuf[MAXCOLDIGITS];
|
|
|
|
char pressurebuf[MAXCOLDIGITS];
|
|
|
|
char unitbuf[MAXCOLDIGITS];
|
|
|
|
char separator_index[MAXCOLDIGITS];
|
2015-03-22 15:13:47 +00:00
|
|
|
char deltabuf[MAXCOLDIGITS];
|
2014-10-28 09:14:00 +00:00
|
|
|
time_t now;
|
2014-11-27 19:55:23 +00:00
|
|
|
struct tm *timep = NULL;
|
2014-10-28 09:14:00 +00:00
|
|
|
char curdate[DATESTR];
|
|
|
|
char curtime[TIMESTR];
|
|
|
|
char *ptr, *ptr_old = NULL;
|
2015-03-22 15:13:46 +00:00
|
|
|
char *NL = NULL;
|
2014-10-28 09:14:00 +00:00
|
|
|
|
2015-06-27 21:38:10 +00:00
|
|
|
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)
|
2014-10-28 09:14:00 +00:00
|
|
|
return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
|
|
|
|
|
2015-06-27 21:38:10 +00:00
|
|
|
init_csv_file_parsing(params, timebuf, depthbuf, tempbuf, po2buf, o2sensor1buf, o2sensor2buf, o2sensor3buf, cnsbuf, ndlbuf, ttsbuf, stopdepthbuf, pressurebuf, unitbuf, separator_index, &now, timep, curdate, curtime, timef, depthf, tempf, po2f, o2sensor1f, o2sensor2f, o2sensor3f, cnsf, ndlf, ttsf, stopdepthf, pressuref, sepidx, csvtemplate, unitidx);
|
2013-10-16 19:05:19 +00:00
|
|
|
|
|
|
|
if (filename == NULL)
|
2014-03-14 18:26:07 +00:00
|
|
|
return report_error("No CSV filename");
|
2013-10-16 19:05:19 +00:00
|
|
|
|
2014-10-28 09:14:00 +00:00
|
|
|
if (readfile(filename, &mem) < 0)
|
|
|
|
return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);
|
|
|
|
|
|
|
|
/* Determine NL (new line) character and the start of CSV data */
|
|
|
|
ptr = mem.buffer;
|
2014-10-28 14:43:33 +00:00
|
|
|
while ((ptr = strstr(ptr, "\r\n\r\n")) != NULL) {
|
2014-10-28 09:14:00 +00:00
|
|
|
ptr_old = ptr;
|
|
|
|
ptr += 1;
|
|
|
|
NL = "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ptr_old) {
|
2014-10-28 14:43:33 +00:00
|
|
|
while ((ptr = strstr(ptr, "\n\n")) != NULL) {
|
2014-10-28 09:14:00 +00:00
|
|
|
ptr_old = ptr;
|
|
|
|
ptr += 1;
|
2015-03-30 18:57:12 +00:00
|
|
|
NL = "\n";
|
2014-10-28 09:14:00 +00:00
|
|
|
}
|
|
|
|
ptr_old += 2;
|
|
|
|
} else
|
|
|
|
ptr_old += 4;
|
|
|
|
|
2015-03-22 15:13:46 +00:00
|
|
|
/*
|
|
|
|
* If file does not contain empty lines, it is not a valid
|
|
|
|
* Seabear CSV file.
|
|
|
|
*/
|
|
|
|
if (NL == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2014-10-28 09:14:00 +00:00
|
|
|
/*
|
|
|
|
* On my current sample of Seabear DC log file, the date is
|
|
|
|
* without any identifier. Thus we must search for the previous
|
|
|
|
* line and step through from there.
|
|
|
|
*/
|
|
|
|
ptr = strstr(mem.buffer, "Serial number:");
|
|
|
|
if (ptr)
|
|
|
|
ptr = strstr(ptr, NL);
|
|
|
|
|
|
|
|
/* Write date and time values to params array */
|
|
|
|
if (ptr) {
|
|
|
|
ptr += strlen(NL) + 2;
|
|
|
|
memcpy(params[19], ptr, 4);
|
|
|
|
memcpy(params[19] + 4, ptr + 5, 2);
|
|
|
|
memcpy(params[19] + 6, ptr + 8, 2);
|
|
|
|
params[19][8] = 0;
|
|
|
|
|
|
|
|
params[21][0] = '1';
|
|
|
|
memcpy(params[21] + 1, ptr + 11, 2);
|
|
|
|
memcpy(params[21] + 3, ptr + 14, 2);
|
|
|
|
params[21][5] = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-22 15:13:47 +00:00
|
|
|
snprintf(deltabuf, MAXCOLDIGITS, "%s", delta);
|
|
|
|
params[SBPARAMS - 3] = "delta";
|
|
|
|
params[SBPARAMS - 2] = deltabuf;
|
|
|
|
params[SBPARAMS - 1] = NULL;
|
|
|
|
|
2014-10-28 09:14:00 +00:00
|
|
|
/* Move the CSV data to the start of mem buffer */
|
|
|
|
memmove(mem.buffer, ptr_old, mem.size - (ptr_old - (char*)mem.buffer));
|
|
|
|
mem.size = (int)mem.size - (ptr_old - (char*)mem.buffer);
|
|
|
|
|
2014-03-14 18:26:07 +00:00
|
|
|
if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
|
|
|
|
return -1;
|
2013-10-16 19:05:19 +00:00
|
|
|
|
2014-12-08 19:26:03 +00:00
|
|
|
ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
|
2013-10-16 19:05:19 +00:00
|
|
|
free(mem.buffer);
|
2014-12-08 19:26:03 +00:00
|
|
|
return ret;
|
2013-10-16 19:05:19 +00:00
|
|
|
}
|
2014-01-25 07:49:23 +00:00
|
|
|
|
2015-03-15 01:05:42 +00:00
|
|
|
int parse_manual_file(const char *filename, int sepidx, int units, int dateformat, int durationformat,
|
|
|
|
int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf,
|
2015-04-24 12:39:20 +00:00
|
|
|
int divemasterf, int buddyf, int suitf, int notesf, int weightf, int tagsf, int cylsizef, int startpresf, int endpresf,
|
2015-03-15 01:05:42 +00:00
|
|
|
int o2f, int hef, int airtempf, int watertempf)
|
2014-01-25 07:49:23 +00:00
|
|
|
{
|
2015-03-15 01:05:42 +00:00
|
|
|
if (verbose > 4) {
|
|
|
|
fprintf(stderr, "filename %s, sepidx %d, units %d, dateformat %d, durationformat %d\n", filename, sepidx, units, dateformat, durationformat);
|
|
|
|
fprintf(stderr, "numberf %d, datef %d, timef %d, durationf %d, locationf %d, gpsf %d, maxdepthf %d, meandepthf %d\n", numberf, datef, timef, durationf, locationf, gpsf, maxdepthf, meandepthf);
|
2015-04-24 12:39:20 +00:00
|
|
|
fprintf(stderr, "divemasterf %d, buddyf %d, suitf %d, notesf %d, weightf %d, tagsf %d, cylsizef %d, startpresf %d, endpresf %d\n", divemasterf, buddyf, suitf, notesf, weightf, tagsf, cylsizef, startpresf, endpresf);
|
2015-03-15 01:05:42 +00:00
|
|
|
fprintf(stderr, "o2f %d, hef %d, airtempf %d, watertempf %d\n", o2f, hef, airtempf, watertempf);
|
|
|
|
}
|
2014-01-25 07:49:23 +00:00
|
|
|
struct memblock mem;
|
2014-02-28 04:09:57 +00:00
|
|
|
int pnr = 0;
|
2015-04-24 12:39:20 +00:00
|
|
|
char *params[55];
|
2014-01-25 07:49:23 +00:00
|
|
|
char numberbuf[MAXCOLDIGITS];
|
|
|
|
char datebuf[MAXCOLDIGITS];
|
|
|
|
char timebuf[MAXCOLDIGITS];
|
|
|
|
char durationbuf[MAXCOLDIGITS];
|
|
|
|
char locationbuf[MAXCOLDIGITS];
|
|
|
|
char gpsbuf[MAXCOLDIGITS];
|
|
|
|
char maxdepthbuf[MAXCOLDIGITS];
|
|
|
|
char meandepthbuf[MAXCOLDIGITS];
|
2015-01-07 04:19:45 +00:00
|
|
|
char divemasterbuf[MAXCOLDIGITS];
|
2014-01-25 07:49:23 +00:00
|
|
|
char buddybuf[MAXCOLDIGITS];
|
2015-04-24 12:39:20 +00:00
|
|
|
char suitbuf[MAXCOLDIGITS];
|
2014-01-25 07:49:23 +00:00
|
|
|
char notesbuf[MAXCOLDIGITS];
|
|
|
|
char weightbuf[MAXCOLDIGITS];
|
|
|
|
char tagsbuf[MAXCOLDIGITS];
|
|
|
|
char separator_index[MAXCOLDIGITS];
|
|
|
|
char unit[MAXCOLDIGITS];
|
2014-11-25 17:47:04 +00:00
|
|
|
char datefmt[MAXCOLDIGITS];
|
2014-12-31 16:27:30 +00:00
|
|
|
char durationfmt[MAXCOLDIGITS];
|
2014-12-20 19:23:34 +00:00
|
|
|
char cylsizebuf[MAXCOLDIGITS];
|
|
|
|
char startpresbuf[MAXCOLDIGITS];
|
|
|
|
char endpresbuf[MAXCOLDIGITS];
|
|
|
|
char o2buf[MAXCOLDIGITS];
|
|
|
|
char hebuf[MAXCOLDIGITS];
|
|
|
|
char airtempbuf[MAXCOLDIGITS];
|
|
|
|
char watertempbuf[MAXCOLDIGITS];
|
2014-01-25 07:49:23 +00:00
|
|
|
time_t now;
|
|
|
|
struct tm *timep;
|
|
|
|
char curdate[9];
|
|
|
|
char curtime[6];
|
2014-12-08 19:26:03 +00:00
|
|
|
int ret;
|
2014-01-25 07:49:23 +00:00
|
|
|
|
2015-04-24 12:39:20 +00:00
|
|
|
if (numberf >= MAXCOLS || datef >= MAXCOLS || timef >= MAXCOLS || durationf >= MAXCOLS || locationf >= MAXCOLS || gpsf >= MAXCOLS || maxdepthf >= MAXCOLS || meandepthf >= MAXCOLS || buddyf >= MAXCOLS || suitf >= MAXCOLS || notesf >= MAXCOLS || weightf >= MAXCOLS || tagsf >= MAXCOLS || cylsizef >= MAXCOLS || startpresf >= MAXCOLS || endpresf >= MAXCOLS || o2f >= MAXCOLS || hef >= MAXCOLS || airtempf >= MAXCOLS || watertempf >= MAXCOLS)
|
2014-03-14 18:26:07 +00:00
|
|
|
return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
|
2014-01-25 07:49:23 +00:00
|
|
|
|
|
|
|
snprintf(numberbuf, MAXCOLDIGITS, "%d", numberf);
|
|
|
|
snprintf(datebuf, MAXCOLDIGITS, "%d", datef);
|
|
|
|
snprintf(timebuf, MAXCOLDIGITS, "%d", timef);
|
|
|
|
snprintf(durationbuf, MAXCOLDIGITS, "%d", durationf);
|
|
|
|
snprintf(locationbuf, MAXCOLDIGITS, "%d", locationf);
|
|
|
|
snprintf(gpsbuf, MAXCOLDIGITS, "%d", gpsf);
|
|
|
|
snprintf(maxdepthbuf, MAXCOLDIGITS, "%d", maxdepthf);
|
|
|
|
snprintf(meandepthbuf, MAXCOLDIGITS, "%d", meandepthf);
|
2015-01-07 04:19:45 +00:00
|
|
|
snprintf(divemasterbuf, MAXCOLDIGITS, "%d", divemasterf);
|
2014-01-25 07:49:23 +00:00
|
|
|
snprintf(buddybuf, MAXCOLDIGITS, "%d", buddyf);
|
2015-04-24 12:39:20 +00:00
|
|
|
snprintf(suitbuf, MAXCOLDIGITS, "%d", suitf);
|
2014-01-25 07:49:23 +00:00
|
|
|
snprintf(notesbuf, MAXCOLDIGITS, "%d", notesf);
|
|
|
|
snprintf(weightbuf, MAXCOLDIGITS, "%d", weightf);
|
|
|
|
snprintf(tagsbuf, MAXCOLDIGITS, "%d", tagsf);
|
|
|
|
snprintf(separator_index, MAXCOLDIGITS, "%d", sepidx);
|
|
|
|
snprintf(unit, MAXCOLDIGITS, "%d", units);
|
2014-11-25 17:47:04 +00:00
|
|
|
snprintf(datefmt, MAXCOLDIGITS, "%d", dateformat);
|
2014-12-31 16:27:30 +00:00
|
|
|
snprintf(durationfmt, MAXCOLDIGITS, "%d", durationformat);
|
2014-12-20 19:23:34 +00:00
|
|
|
snprintf(cylsizebuf, MAXCOLDIGITS, "%d", cylsizef);
|
|
|
|
snprintf(startpresbuf, MAXCOLDIGITS, "%d", startpresf);
|
|
|
|
snprintf(endpresbuf, MAXCOLDIGITS, "%d", endpresf);
|
|
|
|
snprintf(o2buf, MAXCOLDIGITS, "%d", o2f);
|
|
|
|
snprintf(hebuf, MAXCOLDIGITS, "%d", hef);
|
|
|
|
snprintf(airtempbuf, MAXCOLDIGITS, "%d", airtempf);
|
|
|
|
snprintf(watertempbuf, MAXCOLDIGITS, "%d", watertempf);
|
2014-01-25 07:49:23 +00:00
|
|
|
time(&now);
|
|
|
|
timep = localtime(&now);
|
2014-10-28 09:14:00 +00:00
|
|
|
strftime(curdate, DATESTR, "%Y%m%d", timep);
|
2014-01-25 07:49:23 +00:00
|
|
|
|
|
|
|
/* As the parameter is numeric, we need to ensure that the leading zero
|
|
|
|
* is not discarded during the transform, thus prepend time with 1 */
|
2014-10-28 09:14:00 +00:00
|
|
|
strftime(curtime, TIMESTR, "1%H%M", timep);
|
2014-01-25 07:49:23 +00:00
|
|
|
|
|
|
|
params[pnr++] = "numberField";
|
|
|
|
params[pnr++] = numberbuf;
|
|
|
|
params[pnr++] = "dateField";
|
|
|
|
params[pnr++] = datebuf;
|
|
|
|
params[pnr++] = "timeField";
|
|
|
|
params[pnr++] = timebuf;
|
|
|
|
params[pnr++] = "durationField";
|
|
|
|
params[pnr++] = durationbuf;
|
|
|
|
params[pnr++] = "locationField";
|
|
|
|
params[pnr++] = locationbuf;
|
|
|
|
params[pnr++] = "gpsField";
|
|
|
|
params[pnr++] = gpsbuf;
|
|
|
|
params[pnr++] = "maxDepthField";
|
|
|
|
params[pnr++] = maxdepthbuf;
|
|
|
|
params[pnr++] = "meanDepthField";
|
|
|
|
params[pnr++] = meandepthbuf;
|
2015-01-07 04:19:45 +00:00
|
|
|
params[pnr++] = "divemasterField";
|
|
|
|
params[pnr++] = divemasterbuf;
|
2014-01-25 07:49:23 +00:00
|
|
|
params[pnr++] = "buddyField";
|
|
|
|
params[pnr++] = buddybuf;
|
2015-04-24 12:39:20 +00:00
|
|
|
params[pnr++] = "suitField";
|
|
|
|
params[pnr++] = suitbuf;
|
2014-01-25 07:49:23 +00:00
|
|
|
params[pnr++] = "notesField";
|
|
|
|
params[pnr++] = notesbuf;
|
|
|
|
params[pnr++] = "weightField";
|
|
|
|
params[pnr++] = weightbuf;
|
|
|
|
params[pnr++] = "tagsField";
|
|
|
|
params[pnr++] = tagsbuf;
|
|
|
|
params[pnr++] = "date";
|
|
|
|
params[pnr++] = curdate;
|
|
|
|
params[pnr++] = "time";
|
|
|
|
params[pnr++] = curtime;
|
|
|
|
params[pnr++] = "separatorIndex";
|
|
|
|
params[pnr++] = separator_index;
|
|
|
|
params[pnr++] = "units";
|
|
|
|
params[pnr++] = unit;
|
2014-11-25 17:47:04 +00:00
|
|
|
params[pnr++] = "datefmt";
|
|
|
|
params[pnr++] = datefmt;
|
2014-12-31 16:27:30 +00:00
|
|
|
params[pnr++] = "durationfmt";
|
|
|
|
params[pnr++] = durationfmt;
|
2014-12-20 19:23:34 +00:00
|
|
|
params[pnr++] = "cylindersizeField";
|
|
|
|
params[pnr++] = cylsizebuf;
|
|
|
|
params[pnr++] = "startpressureField";
|
|
|
|
params[pnr++] = startpresbuf;
|
|
|
|
params[pnr++] = "endpressureField";
|
|
|
|
params[pnr++] = endpresbuf;
|
|
|
|
params[pnr++] = "o2Field";
|
|
|
|
params[pnr++] = o2buf;
|
|
|
|
params[pnr++] = "heField";
|
|
|
|
params[pnr++] = hebuf;
|
|
|
|
params[pnr++] = "airtempField";
|
|
|
|
params[pnr++] = airtempbuf;
|
|
|
|
params[pnr++] = "watertempField";
|
|
|
|
params[pnr++] = watertempbuf;
|
2014-01-25 07:49:23 +00:00
|
|
|
params[pnr++] = NULL;
|
|
|
|
|
|
|
|
if (filename == NULL)
|
2014-03-14 18:26:07 +00:00
|
|
|
return report_error("No manual CSV filename");
|
2014-01-25 07:49:23 +00:00
|
|
|
|
2014-10-28 09:14:00 +00:00
|
|
|
mem.size = 0;
|
2014-03-14 18:26:07 +00:00
|
|
|
if (try_to_xslt_open_csv(filename, &mem, "manualCSV"))
|
|
|
|
return -1;
|
2014-01-25 07:49:23 +00:00
|
|
|
|
2014-12-08 19:26:03 +00:00
|
|
|
ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
|
2015-03-14 23:24:24 +00:00
|
|
|
|
2014-01-25 07:49:23 +00:00
|
|
|
free(mem.buffer);
|
2014-12-08 19:26:03 +00:00
|
|
|
return ret;
|
2014-01-25 07:49:23 +00:00
|
|
|
}
|