2017-11-28 19:34:37 +00:00
// SPDX-License-Identifier: GPL-2.0
# ifdef __clang__
// Clang has a bug on zero-initialization of C structs.
# pragma clang diagnostic ignored "-Wmissing-field-initializers"
# endif
2023-12-03 21:59:21 +00:00
# include <stdlib.h>
2020-10-25 12:28:55 +00:00
# include "dive.h"
2019-03-04 22:20:29 +00:00
# include "divesite.h"
2024-03-24 20:03:08 +00:00
# include "errorhelper.h"
2020-10-25 08:14:16 +00:00
# include "gas.h"
2017-11-28 19:34:37 +00:00
# include "parse.h"
2020-10-25 12:28:55 +00:00
# include "sample.h"
# include "subsurface-string.h"
2017-11-28 19:34:37 +00:00
# include "divelist.h"
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
# include "divelog.h"
2017-11-28 19:34:37 +00:00
# include "device.h"
# include "membuffer.h"
# include "gettext.h"
2024-02-29 06:29:11 +00:00
static int cobalt_profile_sample ( void * param , int , char * * data , char * * )
2017-11-28 19:34:37 +00:00
{
2018-10-17 16:45:22 +00:00
struct parser_state * state = ( struct parser_state * ) param ;
2017-11-28 19:34:37 +00:00
2018-10-17 16:45:22 +00:00
sample_start ( state ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 0 ] )
2018-10-17 16:45:22 +00:00
state - > cur_sample - > time . seconds = atoi ( data [ 0 ] ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 1 ] )
2018-10-17 16:45:22 +00:00
state - > cur_sample - > depth . mm = atoi ( data [ 1 ] ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 2 ] )
2024-04-23 07:06:28 +00:00
state - > cur_sample - > temperature . mkelvin = state - > metric ? C_to_mkelvin ( permissive_strtod ( data [ 2 ] , NULL ) ) : F_to_mkelvin ( permissive_strtod ( data [ 2 ] , NULL ) ) ;
2018-10-17 16:45:22 +00:00
sample_end ( state ) ;
2017-11-28 19:34:37 +00:00
return 0 ;
}
2024-02-29 06:29:11 +00:00
static int cobalt_cylinders ( void * param , int , char * * data , char * * )
2017-11-28 19:34:37 +00:00
{
2018-10-17 16:45:22 +00:00
struct parser_state * state = ( struct parser_state * ) param ;
2019-08-04 16:44:57 +00:00
cylinder_t * cyl ;
2017-11-28 19:34:37 +00:00
2019-08-04 16:59:14 +00:00
cyl = cylinder_start ( state ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 0 ] )
2019-08-04 16:44:57 +00:00
cyl - > gasmix . o2 . permille = atoi ( data [ 0 ] ) * 10 ;
2017-11-28 19:34:37 +00:00
if ( data [ 1 ] )
2019-08-04 16:44:57 +00:00
cyl - > gasmix . he . permille = atoi ( data [ 1 ] ) * 10 ;
2017-11-28 19:34:37 +00:00
if ( data [ 2 ] )
2019-08-04 16:44:57 +00:00
cyl - > start . mbar = psi_to_mbar ( atoi ( data [ 2 ] ) ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 3 ] )
2019-08-04 16:44:57 +00:00
cyl - > end . mbar = psi_to_mbar ( atoi ( data [ 3 ] ) ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 4 ] )
2019-08-04 16:44:57 +00:00
cyl - > type . size . mliter = atoi ( data [ 4 ] ) * 100 ;
2017-11-28 19:34:37 +00:00
if ( data [ 5 ] )
2019-08-04 16:44:57 +00:00
cyl - > gas_used . mliter = atoi ( data [ 5 ] ) * 1000 ;
2018-10-17 16:45:22 +00:00
cylinder_end ( state ) ;
2017-11-28 19:34:37 +00:00
return 0 ;
}
2024-02-29 06:29:11 +00:00
static int cobalt_buddies ( void * param , int , char * * data , char * * )
2017-11-28 19:34:37 +00:00
{
2018-10-17 16:45:22 +00:00
struct parser_state * state = ( struct parser_state * ) param ;
2017-11-28 19:34:37 +00:00
if ( data [ 0 ] )
2018-10-17 16:45:22 +00:00
utf8_string ( data [ 0 ] , & state - > cur_dive - > buddy ) ;
2017-11-28 19:34:37 +00:00
return 0 ;
}
/*
* We still need to figure out how to map free text visibility to
* Subsurface star rating .
*/
2024-02-29 06:29:11 +00:00
static int cobalt_visibility ( void * , int , char * * , char * * )
2017-11-28 19:34:37 +00:00
{
return 0 ;
}
2024-02-29 06:29:11 +00:00
static int cobalt_location ( void * param , int , char * * data , char * * )
2017-11-28 19:34:37 +00:00
{
2018-10-22 13:31:43 +00:00
char * * location = ( char * * ) param ;
* location = data [ 0 ] ? strdup ( data [ 0 ] ) : NULL ;
2017-11-28 19:34:37 +00:00
return 0 ;
}
2024-02-29 06:29:11 +00:00
static int cobalt_dive ( void * param , int , char * * data , char * * )
2017-11-28 19:34:37 +00:00
{
int retval = 0 ;
2018-10-17 16:45:22 +00:00
struct parser_state * state = ( struct parser_state * ) param ;
sqlite3 * handle = state - > sql_handle ;
2018-10-22 13:31:43 +00:00
char * location , * location_site ;
2017-11-28 19:34:37 +00:00
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_cylinder_template [ ] = " select FO2,FHe,StartingPressure,EndingPressure,TankSize,TankPressure,TotalConsumption from GasMixes where DiveID=%d and StartingPressure>0 and EndingPressure > 0 group by FO2,FHe " ;
char get_buddy_template [ ] = " select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=4 " ;
char get_visibility_template [ ] = " select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=3 " ;
char get_location_template [ ] = " select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=0 " ;
char get_site_template [ ] = " select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=1 " ;
char get_buffer [ 1024 ] ;
2018-10-17 16:45:22 +00:00
dive_start ( state ) ;
state - > cur_dive - > number = atoi ( data [ 0 ] ) ;
2017-11-28 19:34:37 +00:00
2018-10-17 16:45:22 +00:00
state - > cur_dive - > when = ( time_t ) ( atol ( data [ 1 ] ) ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 4 ] )
2018-10-17 16:45:22 +00:00
utf8_string ( data [ 4 ] , & state - > cur_dive - > notes ) ;
2017-11-28 19:34:37 +00:00
/* 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 .
*/
2018-10-17 16:45:22 +00:00
state - > metric = 0 ;
2017-11-28 19:34:37 +00:00
/* Cobalt stores the pressures, not the depth */
if ( data [ 6 ] )
2018-10-17 16:45:22 +00:00
state - > cur_dive - > dc . maxdepth . mm = atoi ( data [ 6 ] ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 7 ] )
2018-10-17 16:45:22 +00:00
state - > cur_dive - > dc . duration . seconds = atoi ( data [ 7 ] ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 8 ] )
2018-10-17 16:45:22 +00:00
state - > cur_dive - > dc . surface_pressure . mbar = atoi ( data [ 8 ] ) ;
2017-11-28 19:34:37 +00:00
/*
* TODO : the deviceid hash should be calculated here .
*/
2018-10-17 16:45:22 +00:00
settings_start ( state ) ;
dc_settings_start ( state ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 9 ] ) {
2024-02-29 06:56:27 +00:00
utf8_string_std ( data [ 9 ] , & state - > cur_settings . dc . serial_nr ) ;
2018-10-17 16:45:22 +00:00
state - > cur_settings . dc . deviceid = atoi ( data [ 9 ] ) ;
state - > cur_settings . dc . model = strdup ( " Cobalt import " ) ;
2017-11-28 19:34:37 +00:00
}
2018-10-17 16:45:22 +00:00
dc_settings_end ( state ) ;
settings_end ( state ) ;
2017-11-28 19:34:37 +00:00
if ( data [ 9 ] ) {
2018-10-17 16:45:22 +00:00
state - > cur_dive - > dc . deviceid = atoi ( data [ 9 ] ) ;
state - > cur_dive - > dc . model = strdup ( " Cobalt import " ) ;
2017-11-28 19:34:37 +00:00
}
2018-10-17 16:45:22 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_cylinder_template , state - > cur_dive - > number ) ;
2019-10-27 10:50:41 +00:00
retval = sqlite3_exec ( handle , get_buffer , & cobalt_cylinders , state , NULL ) ;
2017-11-28 19:34:37 +00:00
if ( retval ! = SQLITE_OK ) {
2024-03-24 20:03:08 +00:00
report_info ( " Database query cobalt_cylinders failed. " ) ;
2017-11-28 19:34:37 +00:00
return 1 ;
}
2018-10-17 16:45:22 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_buddy_template , state - > cur_dive - > number ) ;
2019-10-27 10:50:41 +00:00
retval = sqlite3_exec ( handle , get_buffer , & cobalt_buddies , state , NULL ) ;
2017-11-28 19:34:37 +00:00
if ( retval ! = SQLITE_OK ) {
2024-03-24 20:03:08 +00:00
report_info ( " Database query cobalt_buddies failed. " ) ;
2017-11-28 19:34:37 +00:00
return 1 ;
}
2018-10-17 16:45:22 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_visibility_template , state - > cur_dive - > number ) ;
2019-10-27 10:50:41 +00:00
retval = sqlite3_exec ( handle , get_buffer , & cobalt_visibility , state , NULL ) ;
2017-11-28 19:34:37 +00:00
if ( retval ! = SQLITE_OK ) {
2024-03-24 20:03:08 +00:00
report_info ( " Database query cobalt_visibility failed. " ) ;
2017-11-28 19:34:37 +00:00
return 1 ;
}
2018-10-17 16:45:22 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_location_template , state - > cur_dive - > number ) ;
2019-10-27 10:50:41 +00:00
retval = sqlite3_exec ( handle , get_buffer , & cobalt_location , & location , NULL ) ;
2017-11-28 19:34:37 +00:00
if ( retval ! = SQLITE_OK ) {
2024-03-24 20:03:08 +00:00
report_info ( " Database query cobalt_location failed. " ) ;
2017-11-28 19:34:37 +00:00
return 1 ;
}
2018-10-17 16:45:22 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_site_template , state - > cur_dive - > number ) ;
2019-10-27 10:50:41 +00:00
retval = sqlite3_exec ( handle , get_buffer , & cobalt_location , & location_site , NULL ) ;
2017-11-28 19:34:37 +00:00
if ( retval ! = SQLITE_OK ) {
2024-03-24 20:03:08 +00:00
report_info ( " Database query cobalt_location (site) failed. " ) ;
2017-11-28 19:34:37 +00:00
return 1 ;
}
2018-10-22 13:31:43 +00:00
if ( location & & location_site ) {
2024-05-04 15:18:08 +00:00
std : : string tmp = std : : string ( location ) + " / " + location_site ;
2022-11-12 07:57:56 +00:00
add_dive_to_dive_site ( state - > cur_dive , find_or_create_dive_site_with_name ( tmp , state - > log - > sites ) ) ;
2018-10-22 13:31:43 +00:00
}
free ( location ) ;
free ( location_site ) ;
2018-10-17 16:45:22 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_profile_template , state - > cur_dive - > number ) ;
2019-10-27 10:50:41 +00:00
retval = sqlite3_exec ( handle , get_buffer , & cobalt_profile_sample , state , NULL ) ;
2017-11-28 19:34:37 +00:00
if ( retval ! = SQLITE_OK ) {
2024-03-24 20:03:08 +00:00
report_info ( " Database query cobalt_profile_sample failed. " ) ;
2017-11-28 19:34:37 +00:00
return 1 ;
}
2018-10-17 16:45:22 +00:00
dive_end ( state ) ;
2017-11-28 19:34:37 +00:00
return SQLITE_OK ;
}
2024-05-04 16:45:55 +00:00
int parse_cobalt_buffer ( sqlite3 * handle , const char * url , const char * , int , struct divelog * log )
2017-11-28 19:34:37 +00:00
{
int retval ;
2018-10-17 16:45:22 +00:00
struct parser_state state ;
2022-11-12 07:57:56 +00:00
state . log = log ;
2018-10-17 16:45:22 +00:00
state . sql_handle = handle ;
2017-11-28 19:34:37 +00:00
char get_dives [ ] = " select Id,strftime('%s',DiveStartTime),LocationId,'buddy','notes',Units,(MaxDepthPressure*10000/SurfacePressure)-10000,DiveMinutes,SurfacePressure,SerialNumber,'model' from Dive where IsViewDeleted = 0 " ;
2019-10-27 10:50:41 +00:00
retval = sqlite3_exec ( handle , get_dives , & cobalt_dive , & state , NULL ) ;
2017-11-28 19:34:37 +00:00
if ( retval ! = SQLITE_OK ) {
2024-03-24 20:03:08 +00:00
report_info ( " Database query failed '%s'. \n " , url ) ;
2017-11-28 19:34:37 +00:00
return 1 ;
}
return 0 ;
}