This implements importing of dive profile and temperature graph along
with some meta data from a Cobalt Divelog database.

Signed-off-by: Miika Turkia <[email protected]>
---
 dive.h      |  1 +
 file.c      |  9 ++++++
 parse-xml.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+)

diff --git a/dive.h b/dive.h
index f67c736..719e313 100644
--- a/dive.h
+++ b/dive.h
@@ -626,6 +626,7 @@ extern void set_filename(const char *filename, bool force);
 extern int parse_dm4_buffer(sqlite3 *handle, const char *url, const char *buf, 
int size, struct dive_table *table);
 extern int parse_dm5_buffer(sqlite3 *handle, const char *url, const char *buf, 
int size, struct dive_table *table);
 extern int parse_shearwater_buffer(sqlite3 *handle, const char *url, const 
char *buf, int size, struct dive_table *table);
+extern int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char 
*buf, int size, struct dive_table *table);
 
 extern int parse_file(const char *filename);
 extern int parse_csv_file(const char *filename, int time, int depth, int temp, 
int po2f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int 
sepidx, const char *csvtemplate, int units);
diff --git a/file.c b/file.c
index 04936a5..89f9c1f 100644
--- a/file.c
+++ b/file.c
@@ -165,6 +165,7 @@ static int try_to_open_db(const char *filename, struct 
memblock *mem)
        char dm4_test[] = "select count(*) from sqlite_master where 
type='table' and name='Dive' and sql like '%ProfileBlob%'";
        char dm5_test[] = "select count(*) from sqlite_master where 
type='table' and name='Dive' and sql like '%SampleBlob%'";
        char shearwater_test[] = "select count(*) from sqlite_master where 
type='table' and name='system' and sql like '%dbVersion%'";
+       char cobalt_test[] = "select count(*) from sqlite_master where 
type='table' and name='TrackPoints' and sql like '%DepthPressure%'";
        int retval;
 
        retval = sqlite3_open(filename, &handle);
@@ -198,6 +199,14 @@ static int try_to_open_db(const char *filename, struct 
memblock *mem)
                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);
+               sqlite3_close(handle);
+               return retval;
+       }
+
        sqlite3_close(handle);
 
        return retval;
diff --git a/parse-xml.c b/parse-xml.c
index f1b439e..d892e75 100644
--- a/parse-xml.c
+++ b/parse-xml.c
@@ -2219,6 +2219,22 @@ extern int shearwater_changes(void *handle, int columns, 
char **data, char **col
        return 0;
 }
 
+
+extern int cobalt_profile_sample(void *handle, int columns, char **data, char 
**column)
+{
+       sample_start();
+       if (data[0])
+               cur_sample->time.seconds = atoi(data[0]);
+       if (data[1])
+               cur_sample->depth.mm = atoi(data[1]);
+       if (data[2])
+               cur_sample->temperature.mkelvin = metric ? 
C_to_mkelvin(atof(data[2])) : F_to_mkelvin(atof(data[2]));
+       sample_end();
+
+       return 0;
+}
+
+
 extern int shearwater_profile_sample(void *handle, int columns, char **data, 
char **column)
 {
        sample_start();
@@ -2323,6 +2339,68 @@ extern int shearwater_dive(void *param, int columns, 
char **data, char **column)
 }
 
 
+extern int cobalt_dive(void *param, int columns, char **data, char **column)
+{
+       int retval = 0;
+       sqlite3 *handle = (sqlite3 *)param;
+       char *err = NULL;
+       char get_profile_template[] = "select 
runtime*60,(DepthPressure*10000/SurfacePressure)-10000,p.Temperature from Dive 
AS d JOIN TrackPoints AS p ON d.Id=p.DiveId where d.Id=%d";
+       char get_buffer[1024];
+
+       dive_start();
+       cur_dive->number = atoi(data[0]);
+
+       cur_dive->when = (time_t)(atol(data[1]));
+
+       if (data[2])
+               utf8_string(data[2], &cur_dive->location);
+       if (data[3])
+               utf8_string(data[3], &cur_dive->buddy);
+       if (data[4])
+               utf8_string(data[4], &cur_dive->notes);
+
+       /* data[5] should have information on Units used, but I cannot
+        * parse it at all based on the sample log I have received. The
+        * temperatures in the samples are all Imperial, so let's go by
+        * that.
+        */
+
+       metric = 0;
+
+       /* Cobalt stores the pressures, not the depth */
+       if (data[6])
+               cur_dive->dc.maxdepth.mm = atoi(data[6]) * 10000 / 
atoi(data[8]);
+
+       if (data[7])
+               cur_dive->dc.duration.seconds = atoi(data[7]);
+
+       if (data[8])
+               cur_dive->dc.surface_pressure.mbar = atoi(data[8]);
+       /*
+        * TODO: the deviceid hash should be calculated here.
+        */
+       settings_start();
+       dc_settings_start();
+       if (data[9])
+               utf8_string(data[9], &cur_settings.dc.serial_nr);
+
+       cur_settings.dc.deviceid = 0xffffffff;
+       dc_settings_end();
+       settings_end();
+
+       snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template, 
cur_dive->number);
+       retval = sqlite3_exec(handle, get_buffer, &cobalt_profile_sample, 0, 
&err);
+       if (retval != SQLITE_OK) {
+               fprintf(stderr, "%s", translate("gettextFromC", "Database query 
get_profile_sample failed.\n"));
+               return 1;
+       }
+
+       dive_end();
+
+       return SQLITE_OK;
+}
+
+
 int parse_shearwater_buffer(sqlite3 *handle, const char *url, const char 
*buffer, int size,
                            struct dive_table *table)
 {
@@ -2342,6 +2420,25 @@ int parse_shearwater_buffer(sqlite3 *handle, const char 
*url, const char *buffer
        return 0;
 }
 
+int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *buffer, 
int size,
+                           struct dive_table *table)
+{
+       int retval;
+       char *err = NULL;
+       target_table = table;
+
+       char get_dives[] = "select 
Id,strftime('%s',DiveStartTime),LocationId,'buddy','notes',Units,MaxDepthPressure,DiveMinutes,SurfacePressure,SerialNumber,'model'
 from Dive";
+
+       retval = sqlite3_exec(handle, get_dives, &cobalt_dive, handle, &err);
+
+       if (retval != SQLITE_OK) {
+               fprintf(stderr, translate("gettextFromC", "Database query 
failed '%s'.\n"), url);
+               return 1;
+       }
+
+       return 0;
+}
+
 void parse_xml_init(void)
 {
        LIBXML_TEST_VERSION
-- 
1.9.1

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

Reply via email to