2017-04-27 18:18:03 +00:00
// SPDX-License-Identifier: GPL-2.0
2017-03-11 20:08:31 +00:00
# ifdef __clang__
2016-03-09 18:18:48 +00:00
// Clang has a bug on zero-initialization of C structs.
# pragma clang diagnostic ignored "-Wmissing-field-initializers"
2017-03-11 20:08:31 +00:00
# endif
2016-03-09 18:18:48 +00:00
2011-08-28 23:58:26 +00:00
# include <stdio.h>
2011-08-30 00:51:54 +00:00
# include <ctype.h>
# include <string.h>
2011-08-30 23:23:47 +00:00
# include <stdlib.h>
# include <errno.h>
2011-11-05 20:51:37 +00:00
# include <unistd.h>
2013-01-31 00:08:36 +00:00
# include <assert.h>
2011-10-03 04:59:54 +00:00
# define __USE_XOPEN
2011-08-30 22:22:48 +00:00
# include <time.h>
2011-08-28 23:58:26 +00:00
# include <libxml/parser.h>
2013-03-15 17:02:14 +00:00
# include <libxml/parserInternals.h>
2011-08-28 23:58:26 +00:00
# include <libxml/tree.h>
2011-11-05 10:39:17 +00:00
# include <libxslt/transform.h>
2014-07-12 12:51:03 +00:00
# include <libdivecomputer/parser.h>
2013-10-06 15:55:58 +00:00
# include "gettext.h"
2011-08-31 01:23:59 +00:00
# include "dive.h"
2015-06-20 13:45:12 +00:00
# include "divelist.h"
Assemble the actual Suunto serial number
It turns out that the serial number returned by libdivecomputer isn't
really the serial number as interpreted by the vendor. Those tend to be
strings, but libdivecomputer gives us a 32bit number.
Some experimenting showed that for the Suunto devies tested the serial
number is encoded in that 32bit number:
It so happens that the Suunto serial number strings are strings that have
all numbers, but they aren't *one* number. They are four bytes
representing two numbers each, and the "23500027" string is actually the
four bytes 23 50 00 27 (0x17 0x32 0x00 0x1b). And libdivecomputer has
incorrectly parsed those four bytes as one number, not as the encoded
serial number string it is. So the value 389152795 is actually hex
0x1732001b, which is 0x17 0x32 0x00 0x1b, which is - 23 50 00 27.
This should be done by libdivecomputer, but hey, in the meantime this at
least shows the concept. And helps test the XML save/restore code.
It depends on the two patches that create the whole "device.c"
infrastructure, of course. With this, my dive file ends up having the
settings section look like this:
<divecomputerid model='Suunto Vyper Air' deviceid='d4629110'
serial='01201094' firmware='1.1.22'/>
<divecomputerid model='Suunto HelO2' deviceid='995dd566'
serial='23500027' firmware='1.0.4'/>
where the format of the firmware version is something I guessed at,
but it was the obvious choice (again, it's byte-based, I'm ignoring
the high byte that is zero for both of my Suuntos).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-10 00:14:21 +00:00
# include "device.h"
2014-05-28 06:55:46 +00:00
# include "membuffer.h"
2011-08-30 23:42:05 +00:00
2016-03-25 08:21:45 +00:00
int verbose , quit , force_root ;
2014-02-15 06:36:50 +00:00
int metric = 1 ;
2015-02-13 07:35:52 +00:00
int last_xml_version = - 1 ;
2015-07-12 17:47:00 +00:00
int diveid = - 1 ;
2011-08-30 20:48:05 +00:00
2014-03-14 18:26:07 +00:00
static xmlDoc * test_xslt_transforms ( xmlDoc * doc , const char * * params ) ;
2013-02-22 19:20:46 +00:00
2013-01-31 00:08:36 +00:00
/* the dive table holds the overall dive list; target table points at
* the table we are currently filling */
2011-08-31 01:40:25 +00:00
struct dive_table dive_table ;
2013-01-31 00:08:36 +00:00
struct dive_table * target_table = NULL ;
2013-02-22 19:20:46 +00:00
2014-11-16 23:11:18 +00:00
/* Trim a character string by removing leading and trailing white space characters.
* Parameter : a pointer to a null - terminated character string ( buffer ) ;
* Return value : length of the trimmed string , excluding the terminal 0x0 byte
* The original pointer ( buffer ) remains valid after this function has been called
* and points to the trimmed string */
int trimspace ( char * buffer ) {
int i , size , start , end ;
size = strlen ( buffer ) ;
for ( start = 0 ; isspace ( buffer [ start ] ) ; start + + )
if ( start > = size ) return 0 ; // Find 1st character following leading whitespace
for ( end = size - 1 ; isspace ( buffer [ end ] ) ; end - - ) // Find last character before trailing whitespace
if ( end < = 0 ) return 0 ;
for ( i = start ; i < = end ; i + + ) // Move the nonspace characters to the start of the string
buffer [ i - start ] = buffer [ i ] ;
size = end - start + 1 ;
buffer [ size ] = 0x0 ; // then terminate the string
return size ; // return string length
}
2015-01-09 22:35:31 +00:00
/*
* Clear a dive_table
*/
void clear_table ( struct dive_table * table )
{
for ( int i = 0 ; i < table - > nr ; i + + )
free ( table - > dives [ i ] ) ;
table - > nr = 0 ;
}
2011-08-31 01:40:25 +00:00
/*
* Add a dive into the dive_table array
*/
2015-01-09 22:35:31 +00:00
void record_dive_to_table ( struct dive * dive , struct dive_table * table )
2011-08-31 00:18:33 +00:00
{
2013-01-31 00:08:36 +00:00
assert ( table ! = NULL ) ;
2015-10-02 01:13:48 +00:00
struct dive * * dives = grow_dive_table ( table ) ;
int nr = table - > nr ;
2011-08-31 00:18:33 +00:00
2011-09-03 20:19:26 +00:00
dives [ nr ] = fixup_dive ( dive ) ;
2014-02-16 23:42:56 +00:00
table - > nr = nr + 1 ;
2011-08-31 00:18:33 +00:00
}
2013-01-31 00:08:36 +00:00
void record_dive ( struct dive * dive )
2012-04-03 02:19:01 +00:00
{
2013-01-31 00:08:36 +00:00
record_dive_to_table ( dive , & dive_table ) ;
2012-04-03 02:19:01 +00:00
}
2011-09-01 18:22:05 +00:00
static void start_match ( const char * type , const char * name , char * buffer )
2011-08-30 20:58:19 +00:00
{
2011-09-01 18:22:05 +00:00
if ( verbose > 2 )
printf ( " Matching %s '%s' (%s) \n " ,
2014-02-16 23:42:56 +00:00
type , name , buffer ) ;
2011-08-30 20:58:19 +00:00
}
2011-09-01 18:22:05 +00:00
static void nonmatch ( const char * type , const char * name , char * buffer )
2011-08-30 20:58:19 +00:00
{
2011-09-01 18:22:05 +00:00
if ( verbose > 1 )
printf ( " Unable to match %s '%s' (%s) \n " ,
2014-02-16 23:42:56 +00:00
type , name , buffer ) ;
2011-08-30 20:58:19 +00:00
}
2011-08-30 22:22:48 +00:00
typedef void ( * matchfn_t ) ( char * buffer , void * ) ;
2011-08-30 20:48:05 +00:00
2011-09-01 18:22:05 +00:00
static int match ( const char * pattern , int plen ,
2013-11-02 19:00:16 +00:00
const char * name ,
2011-09-01 18:22:05 +00:00
matchfn_t fn , char * buf , void * data )
2011-08-30 20:48:05 +00:00
{
2013-11-02 19:00:16 +00:00
switch ( name [ plen ] ) {
2014-02-16 23:42:56 +00:00
case ' \0 ' :
case ' . ' :
2013-11-02 19:00:16 +00:00
break ;
default :
2011-09-01 18:22:05 +00:00
return 0 ;
2013-11-02 19:00:16 +00:00
}
if ( memcmp ( pattern , name , plen ) )
2011-08-30 22:22:48 +00:00
return 0 ;
fn ( buf , data ) ;
return 1 ;
2011-08-30 20:48:05 +00:00
}
2011-09-07 02:07:17 +00:00
2013-01-11 01:26:10 +00:00
struct units xml_parsing_units ;
2012-12-10 17:20:57 +00:00
const struct units SI_units = SI_UNITS ;
const struct units IMPERIAL_units = IMPERIAL_UNITS ;
2011-09-07 02:07:17 +00:00
2011-08-30 20:48:05 +00:00
/*
* Dive info as it is being built up . .
*/
2014-08-17 18:26:21 +00:00
# define MAX_EVENT_NAME 128
2012-11-25 02:50:21 +00:00
static struct divecomputer * cur_dc ;
2012-09-20 03:42:11 +00:00
static struct dive * cur_dive ;
2015-02-12 05:46:02 +00:00
static struct dive_site * cur_dive_site ;
2015-06-10 18:52:18 +00:00
degrees_t cur_latitude , cur_longitude ;
2012-09-20 03:42:11 +00:00
static dive_trip_t * cur_trip = NULL ;
2012-01-05 16:16:08 +00:00
static struct sample * cur_sample ;
2014-06-09 00:42:15 +00:00
static struct picture * cur_picture ;
2014-08-17 18:26:21 +00:00
static union {
struct event event ;
char allocation [ sizeof ( struct event ) + MAX_EVENT_NAME ] ;
} event_allocation = { . event . deleted = 1 } ;
# define cur_event event_allocation.event
2012-12-26 21:47:54 +00:00
static struct {
2014-02-16 23:42:56 +00:00
struct {
const char * model ;
uint32_t deviceid ;
const char * nickname , * serial_nr , * firmware ;
} dc ;
2012-12-26 21:47:54 +00:00
} cur_settings ;
2014-01-15 18:54:41 +00:00
static bool in_settings = false ;
2014-04-11 06:17:35 +00:00
static bool in_userid = false ;
2012-01-05 16:16:08 +00:00
static struct tm cur_tm ;
2012-03-24 04:07:53 +00:00
static int cur_cylinder_index , cur_ws_index ;
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
static int lastcylinderindex , next_o2_sensor ;
static int o2pressure_sensor ;
2014-11-06 18:34:19 +00:00
static struct extra_data cur_extra_data ;
2011-08-30 22:22:48 +00:00
2013-02-22 16:52:35 +00:00
/*
* If we don ' t have an explicit dive computer ,
* we use the implicit one that every dive has . .
*/
static struct divecomputer * get_dc ( void )
{
2014-02-16 23:42:56 +00:00
return cur_dc ? : & cur_dive - > dc ;
2013-02-22 16:52:35 +00:00
}
2011-09-05 20:45:14 +00:00
static enum import_source {
UNKNOWN ,
LIBDIVECOMPUTER ,
2011-09-05 21:29:08 +00:00
DIVINGLOG ,
2011-09-07 00:01:28 +00:00
UDDF ,
2015-06-09 19:50:17 +00:00
SSRF_WS ,
2011-09-05 20:45:14 +00:00
} import_source ;
2014-10-27 03:37:12 +00:00
static void divedate ( const char * buffer , timestamp_t * when )
2011-08-30 22:22:48 +00:00
{
2014-02-16 23:42:56 +00:00
int d , m , y ;
int hh , mm , ss ;
2013-01-29 05:07:52 +00:00
2014-02-16 23:42:56 +00:00
hh = 0 ;
mm = 0 ;
ss = 0 ;
2013-01-29 05:07:52 +00:00
if ( sscanf ( buffer , " %d.%d.%d %d:%d:%d " , & d , & m , & y , & hh , & mm , & ss ) > = 3 ) {
/* This is ok, and we got at least the date */
} else if ( sscanf ( buffer , " %d-%d-%d %d:%d:%d " , & y , & m , & d , & hh , & mm , & ss ) > = 3 ) {
/* This is also ok */
2011-09-02 00:13:39 +00:00
} else {
fprintf ( stderr , " Unable to parse date '%s' \n " , buffer ) ;
2013-01-29 05:07:52 +00:00
return ;
2011-08-30 22:22:48 +00:00
}
2013-01-29 05:07:52 +00:00
cur_tm . tm_year = y ;
2014-02-16 23:42:56 +00:00
cur_tm . tm_mon = m - 1 ;
2013-01-29 05:07:52 +00:00
cur_tm . tm_mday = d ;
cur_tm . tm_hour = hh ;
cur_tm . tm_min = mm ;
cur_tm . tm_sec = ss ;
* when = utc_mktime ( & cur_tm ) ;
2011-08-30 22:22:48 +00:00
}
2014-10-27 03:37:12 +00:00
static void divetime ( const char * buffer , timestamp_t * when )
2011-08-30 22:22:48 +00:00
{
2014-02-16 23:42:56 +00:00
int h , m , s = 0 ;
2011-08-30 22:22:48 +00:00
if ( sscanf ( buffer , " %d:%d:%d " , & h , & m , & s ) > = 2 ) {
2012-01-05 16:16:08 +00:00
cur_tm . tm_hour = h ;
cur_tm . tm_min = m ;
cur_tm . tm_sec = s ;
2013-01-29 05:07:52 +00:00
* when = utc_mktime ( & cur_tm ) ;
2011-08-30 22:22:48 +00:00
}
}
2011-08-30 23:59:03 +00:00
/* Libdivecomputer: "2011-03-20 10:22:38" */
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void divedatetime ( char * buffer , timestamp_t * when )
2011-08-30 23:59:03 +00:00
{
2014-02-16 23:42:56 +00:00
int y , m , d ;
int hr , min , sec ;
2011-08-30 23:59:03 +00:00
if ( sscanf ( buffer , " %d-%d-%d %d:%d:%d " ,
2014-02-16 23:42:56 +00:00
& y , & m , & d , & hr , & min , & sec ) = = 6 ) {
2012-01-05 16:16:08 +00:00
cur_tm . tm_year = y ;
2014-02-16 23:42:56 +00:00
cur_tm . tm_mon = m - 1 ;
2012-01-05 16:16:08 +00:00
cur_tm . tm_mday = d ;
cur_tm . tm_hour = hr ;
cur_tm . tm_min = min ;
cur_tm . tm_sec = sec ;
* when = utc_mktime ( & cur_tm ) ;
2011-08-30 23:59:03 +00:00
}
}
2014-02-16 23:42:56 +00:00
enum ParseState {
FINDSTART ,
FINDEND
} ;
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void divetags ( char * buffer , struct tag_entry * * tags )
2013-04-09 20:06:30 +00:00
{
2013-11-02 01:12:42 +00:00
int i = 0 , start = 0 , end = 0 ;
enum ParseState state = FINDEND ;
2013-12-13 00:12:04 +00:00
int len = buffer ? strlen ( buffer ) : 0 ;
2014-02-16 23:42:56 +00:00
while ( i < len ) {
2013-11-02 01:12:42 +00:00
if ( buffer [ i ] = = ' , ' ) {
if ( state = = FINDSTART ) {
/* Detect empty tags */
} else if ( state = = FINDEND ) {
/* Found end of tag */
2013-12-13 00:12:04 +00:00
if ( i > 0 & & buffer [ i - 1 ] ! = ' \\ ' ) {
2014-02-16 23:42:56 +00:00
buffer [ i ] = ' \0 ' ;
state = FINDSTART ;
taglist_add_tag ( tags , buffer + start ) ;
2013-11-02 01:12:42 +00:00
} else {
2014-02-16 23:42:56 +00:00
state = FINDSTART ;
2013-04-09 20:06:30 +00:00
}
}
2013-11-02 01:12:42 +00:00
} else if ( buffer [ i ] = = ' ' ) {
/* Handled */
} else {
/* Found start of tag */
if ( state = = FINDSTART ) {
state = FINDEND ;
start = i ;
} else if ( state = = FINDEND ) {
2013-12-13 00:12:04 +00:00
end = i ;
2013-11-02 01:12:42 +00:00
}
2013-04-09 20:06:30 +00:00
}
2013-11-02 01:12:42 +00:00
i + + ;
2013-12-11 20:21:52 +00:00
}
if ( state = = FINDEND ) {
if ( end < start )
2013-12-13 00:12:04 +00:00
end = len - 1 ;
if ( len > 0 ) {
buffer [ end + 1 ] = ' \0 ' ;
taglist_add_tag ( tags , buffer + start ) ;
2013-12-11 20:21:52 +00:00
}
}
2013-04-09 20:06:30 +00:00
}
2011-08-30 23:23:47 +00:00
enum number_type {
NEITHER ,
FLOAT
} ;
2014-01-08 06:51:22 +00:00
static enum number_type parse_float ( const char * buffer , double * res , const char * * endp )
2011-08-30 23:23:47 +00:00
{
2013-02-23 00:18:39 +00:00
double val ;
2014-01-15 18:54:41 +00:00
static bool first_time = true ;
2011-08-30 23:23:47 +00:00
2013-01-23 20:09:29 +00:00
errno = 0 ;
2013-10-05 07:29:09 +00:00
val = ascii_strtod ( buffer , endp ) ;
2013-02-23 00:18:39 +00:00
if ( errno | | * endp = = buffer )
return NEITHER ;
2013-03-07 19:43:51 +00:00
if ( * * endp = = ' , ' ) {
2017-03-11 16:52:38 +00:00
if ( IS_FP_SAME ( val , rint ( val ) ) ) {
2013-03-07 19:43:51 +00:00
/* we really want to send an error if this is a Subsurface native file
* as this is likely indication of a bug - but right now we don ' t have
* that information available */
if ( first_time ) {
fprintf ( stderr , " Floating point value with decimal comma (%s)? \n " , buffer ) ;
2014-01-15 18:54:41 +00:00
first_time = false ;
2013-03-07 19:43:51 +00:00
}
2014-01-08 06:51:22 +00:00
/* Try again in permissive mode*/
val = strtod_flags ( buffer , endp , 0 ) ;
2013-03-07 19:43:51 +00:00
}
}
2013-02-23 00:18:39 +00:00
* res = val ;
return FLOAT ;
}
union int_or_float {
double fp ;
} ;
2011-08-30 23:23:47 +00:00
2013-02-23 00:18:39 +00:00
static enum number_type integer_or_float ( char * buffer , union int_or_float * res )
{
2014-01-08 06:51:22 +00:00
const char * end ;
2013-02-23 00:18:39 +00:00
return parse_float ( buffer , & res - > fp , & end ) ;
2011-08-30 23:23:47 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void pressure ( char * buffer , pressure_t * pressure )
2011-08-30 23:23:47 +00:00
{
2013-12-20 17:37:56 +00:00
double mbar = 0.0 ;
2011-08-30 23:23:47 +00:00
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2011-09-02 21:06:26 +00:00
/* Just ignore zero values */
if ( ! val . fp )
break ;
2013-01-11 01:26:10 +00:00
switch ( xml_parsing_units . pressure ) {
2011-09-07 00:01:28 +00:00
case PASCAL :
mbar = val . fp / 100 ;
break ;
2011-09-02 20:59:39 +00:00
case BAR :
/* Assume mbar, but if it's really small, it's bar */
mbar = val . fp ;
2014-07-17 09:00:06 +00:00
if ( fabs ( mbar ) < 5000 )
2011-09-02 20:59:39 +00:00
mbar = mbar * 1000 ;
break ;
case PSI :
2013-11-26 22:11:30 +00:00
mbar = psi_to_mbar ( val . fp ) ;
2011-08-30 23:23:47 +00:00
break ;
}
2014-07-17 09:00:06 +00:00
if ( fabs ( mbar ) > 5 & & fabs ( mbar ) < 5000000 ) {
2017-03-08 06:41:41 +00:00
pressure - > mbar = lrint ( mbar ) ;
2011-08-30 23:23:47 +00:00
break ;
}
2011-09-02 20:59:39 +00:00
/* fallthrough */
2011-08-30 23:23:47 +00:00
default :
printf ( " Strange pressure reading %s \n " , buffer ) ;
}
}
2014-11-16 22:11:34 +00:00
static void cylinder_use ( char * buffer , enum cylinderuse * cyl_use )
{
Start cleaning up sensor indexing for multiple sensors
This is a very timid start at making us actually use multiple sensors
without the magical special case for just CCR oxygen tracking.
It mainly does:
- turn the "sample->sensor" index into an array of two indexes, to
match the pressures themselves.
- get rid of dive->{oxygen_cylinder_index,diluent_cylinder_index},
since a CCR dive should now simply set the sample->sensor[] indices
correctly instead.
- in a couple of places, start actually looping over the sensors rather
than special-case the O2 case (although often the small "loops" are
just unrolled, since it's just two cases.
but in many cases we still end up only covering the zero sensor case,
because the CCR O2 sensor code coverage was fairly limited.
It's entirely possible (even likely) that this migth break some existing
case: it tries to be a fairly direct ("stupid") translation of the old
code, but unlike the preparatory patch this does actually does change
some semantics.
For example, right now the git loader code assumes that if the git save
data contains a o2pressure entry, it just hardcodes the O2 sensor index
to 1.
In fact, one issue is going to simply be that our file formats do not
have that multiple sensor format, but instead had very clearly encoded
things as being the CCR O2 pressure sensor.
But this is hopefully close to usable, and I will need feedback (and
maybe test cases) from people who have existing CCR dives with pressure
data.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 02:49:45 +00:00
if ( trimspace ( buffer ) ) {
int use = cylinderuse_from_text ( buffer ) ;
* cyl_use = use ;
if ( use = = OXYGEN )
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
o2pressure_sensor = cur_cylinder_index ;
Start cleaning up sensor indexing for multiple sensors
This is a very timid start at making us actually use multiple sensors
without the magical special case for just CCR oxygen tracking.
It mainly does:
- turn the "sample->sensor" index into an array of two indexes, to
match the pressures themselves.
- get rid of dive->{oxygen_cylinder_index,diluent_cylinder_index},
since a CCR dive should now simply set the sample->sensor[] indices
correctly instead.
- in a couple of places, start actually looping over the sensors rather
than special-case the O2 case (although often the small "loops" are
just unrolled, since it's just two cases.
but in many cases we still end up only covering the zero sensor case,
because the CCR O2 sensor code coverage was fairly limited.
It's entirely possible (even likely) that this migth break some existing
case: it tries to be a fairly direct ("stupid") translation of the old
code, but unlike the preparatory patch this does actually does change
some semantics.
For example, right now the git loader code assumes that if the git save
data contains a o2pressure entry, it just hardcodes the O2 sensor index
to 1.
In fact, one issue is going to simply be that our file formats do not
have that multiple sensor format, but instead had very clearly encoded
things as being the CCR O2 pressure sensor.
But this is hopefully close to usable, and I will need feedback (and
maybe test cases) from people who have existing CCR dives with pressure
data.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 02:49:45 +00:00
}
2014-11-16 22:11:34 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void salinity ( char * buffer , int * salinity )
2012-11-12 19:57:49 +00:00
{
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2017-03-08 06:41:41 +00:00
* salinity = lrint ( val . fp * 10.0 ) ;
2012-11-12 19:57:49 +00:00
break ;
default :
printf ( " Strange salinity reading %s \n " , buffer ) ;
}
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void depth ( char * buffer , depth_t * depth )
2011-08-30 23:23:47 +00:00
{
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2013-01-11 01:26:10 +00:00
switch ( xml_parsing_units . length ) {
2011-09-02 20:59:39 +00:00
case METERS :
2017-03-08 06:41:41 +00:00
depth - > mm = lrint ( val . fp * 1000 ) ;
2011-09-02 20:59:39 +00:00
break ;
case FEET :
2013-11-26 22:11:30 +00:00
depth - > mm = feet_to_mm ( val . fp ) ;
2011-09-02 20:59:39 +00:00
break ;
}
2011-08-30 23:23:47 +00:00
break ;
default :
printf ( " Strange depth reading %s \n " , buffer ) ;
}
}
2014-11-06 18:34:19 +00:00
static void extra_data_start ( void )
{
memset ( & cur_extra_data , 0 , sizeof ( struct extra_data ) ) ;
}
static void extra_data_end ( void )
{
// don't save partial structures - we must have both key and value
if ( cur_extra_data . key & & cur_extra_data . value )
2017-07-22 15:32:51 +00:00
add_extra_data ( get_dc ( ) , cur_extra_data . key , cur_extra_data . value ) ;
2014-11-06 18:34:19 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void weight ( char * buffer , weight_t * weight )
2011-12-24 03:41:16 +00:00
{
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2013-01-11 01:26:10 +00:00
switch ( xml_parsing_units . weight ) {
2011-12-24 03:41:16 +00:00
case KG :
2017-03-08 06:41:41 +00:00
weight - > grams = lrint ( val . fp * 1000 ) ;
2011-12-24 03:41:16 +00:00
break ;
case LBS :
2013-11-26 22:11:30 +00:00
weight - > grams = lbs_to_grams ( val . fp ) ;
2011-12-24 03:41:16 +00:00
break ;
}
break ;
default :
2012-11-24 10:12:16 +00:00
printf ( " Strange weight reading %s \n " , buffer ) ;
2011-12-24 03:41:16 +00:00
}
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void temperature ( char * buffer , temperature_t * temperature )
2011-08-30 23:23:47 +00:00
{
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2013-01-11 01:26:10 +00:00
switch ( xml_parsing_units . temperature ) {
2011-09-07 00:01:28 +00:00
case KELVIN :
2017-03-09 16:07:30 +00:00
temperature - > mkelvin = lrint ( val . fp * 1000 ) ;
2011-09-07 00:01:28 +00:00
break ;
2011-09-02 20:59:39 +00:00
case CELSIUS :
2013-11-26 22:11:30 +00:00
temperature - > mkelvin = C_to_mkelvin ( val . fp ) ;
2011-08-30 23:23:47 +00:00
break ;
2011-09-02 20:59:39 +00:00
case FAHRENHEIT :
2013-11-26 22:11:30 +00:00
temperature - > mkelvin = F_to_mkelvin ( val . fp ) ;
2011-08-30 23:23:47 +00:00
break ;
}
break ;
default :
printf ( " Strange temperature reading %s \n " , buffer ) ;
}
2013-03-08 19:52:10 +00:00
/* temperatures outside -40C .. +70C should be ignored */
if ( temperature - > mkelvin < ZERO_C_IN_MKELVIN - 40000 | |
2014-02-16 23:42:56 +00:00
temperature - > mkelvin > ZERO_C_IN_MKELVIN + 70000 )
2013-03-08 19:52:10 +00:00
temperature - > mkelvin = 0 ;
2011-08-30 23:23:47 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void sampletime ( char * buffer , duration_t * time )
2011-08-30 23:23:47 +00:00
{
2011-08-31 00:45:03 +00:00
int i ;
int min , sec ;
2011-08-30 23:23:47 +00:00
2011-08-31 00:45:03 +00:00
i = sscanf ( buffer , " %d:%d " , & min , & sec ) ;
switch ( i ) {
case 1 :
sec = min ;
min = 0 ;
/* fallthrough */
case 2 :
2014-02-16 23:42:56 +00:00
time - > seconds = sec + min * 60 ;
2011-08-30 23:23:47 +00:00
break ;
default :
printf ( " Strange sample time reading %s \n " , buffer ) ;
}
}
2014-07-08 19:29:06 +00:00
static void offsettime ( char * buffer , offset_t * time )
{
duration_t uoffset ;
int sign = 1 ;
if ( * buffer = = ' - ' ) {
sign = - 1 ;
buffer + + ;
}
/* yes, this could indeed fail if we have an offset > 34yrs
* - too bad */
sampletime ( buffer , & uoffset ) ;
time - > seconds = sign * uoffset . seconds ;
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void duration ( char * buffer , duration_t * time )
2011-08-31 00:45:03 +00:00
{
2013-02-25 22:19:16 +00:00
/* DivingLog 5.08 (and maybe other versions) appear to sometimes
* store the dive time as 44.00 instead of 44 : 00 ;
* This attempts to parse this in a fairly robust way */
2014-02-16 23:42:56 +00:00
if ( ! strchr ( buffer , ' : ' ) & & strchr ( buffer , ' . ' ) ) {
2013-02-25 22:19:16 +00:00
char * mybuffer = strdup ( buffer ) ;
2014-02-16 23:42:56 +00:00
char * dot = strchr ( mybuffer , ' . ' ) ;
2013-02-25 22:19:16 +00:00
* dot = ' : ' ;
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
sampletime ( mybuffer , time ) ;
2013-12-10 23:53:30 +00:00
free ( mybuffer ) ;
2013-02-25 22:19:16 +00:00
} else {
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
sampletime ( buffer , time ) ;
2013-02-25 22:19:16 +00:00
}
2011-08-31 00:45:03 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void percent ( char * buffer , fraction_t * fraction )
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
{
2013-02-23 00:18:39 +00:00
double val ;
2014-01-08 06:51:22 +00:00
const char * end ;
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
2013-02-23 00:18:39 +00:00
switch ( parse_float ( buffer , & val , & end ) ) {
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
case FLOAT :
2013-02-23 00:18:39 +00:00
/* Turn fractions into percent unless explicit.. */
if ( val < = 1.0 ) {
2013-10-05 07:29:09 +00:00
while ( isspace ( * end ) )
2013-02-23 00:18:39 +00:00
end + + ;
if ( * end ! = ' % ' )
val * = 100 ;
}
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
2013-02-23 00:18:39 +00:00
/* Then turn percent into our integer permille format */
if ( val > = 0 & & val < = 100.0 ) {
2017-03-08 06:41:41 +00:00
fraction - > permille = lrint ( val * 10 ) ;
2013-02-23 00:18:39 +00:00
break ;
}
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
default :
2014-02-16 23:42:56 +00:00
printf ( translate ( " gettextFromC " , " Strange percentage reading %s \n " ) , buffer ) ;
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
break ;
}
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void gasmix ( char * buffer , fraction_t * fraction )
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
{
2011-09-01 20:46:24 +00:00
/* libdivecomputer does negative percentages. */
if ( * buffer = = ' - ' )
return ;
2012-01-05 16:16:08 +00:00
if ( cur_cylinder_index < MAX_CYLINDERS )
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
percent ( buffer , fraction ) ;
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void gasmix_nitrogen ( char * buffer , struct gasmix * gasmix )
2011-09-01 23:26:11 +00:00
{
2016-03-07 19:40:12 +00:00
( void ) buffer ;
( void ) gasmix ;
2011-09-01 23:26:11 +00:00
/* Ignore n2 percentages. There's no value in them. */
}
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void cylindersize ( char * buffer , volume_t * volume )
2011-09-04 03:31:18 +00:00
{
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2017-03-08 06:41:41 +00:00
volume - > mliter = lrint ( val . fp * 1000 ) ;
2011-09-04 03:31:18 +00:00
break ;
default :
printf ( " Strange volume reading %s \n " , buffer ) ;
break ;
}
}
2011-09-02 02:56:04 +00:00
static void utf8_string ( char * buffer , void * _res )
{
2014-06-12 02:02:20 +00:00
char * * res = _res ;
2014-06-11 17:48:48 +00:00
int size ;
size = trimspace ( buffer ) ;
2014-06-12 02:02:20 +00:00
if ( size )
* res = strdup ( buffer ) ;
2011-09-02 02:56:04 +00:00
}
2014-08-17 18:26:21 +00:00
static void event_name ( char * buffer , char * name )
{
int size = trimspace ( buffer ) ;
if ( size > = MAX_EVENT_NAME )
size = MAX_EVENT_NAME - 1 ;
memcpy ( name , buffer , size ) ;
name [ size ] = 0 ;
}
2015-09-03 20:25:00 +00:00
// We don't use gauge as a mode, and pscr doesn't exist as a libdc divemode
const char * libdc_divemode_text [ ] = { " oc " , " cc " , " pscr " , " freedive " , " gauge " } ;
2014-06-11 17:48:48 +00:00
/* Extract the dive computer type from the xml text buffer */
2014-11-16 23:11:18 +00:00
static void get_dc_type ( char * buffer , enum dive_comp_type * dct )
2014-06-11 17:48:48 +00:00
{
2014-11-16 23:11:18 +00:00
if ( trimspace ( buffer ) ) {
for ( enum dive_comp_type i = 0 ; i < NUM_DC_TYPE ; i + + ) {
2015-01-10 23:01:15 +00:00
if ( strcmp ( buffer , divemode_text [ i ] ) = = 0 )
2014-11-16 23:11:18 +00:00
* dct = i ;
2015-09-03 20:25:00 +00:00
else if ( strcmp ( buffer , libdc_divemode_text [ i ] ) = = 0 )
* dct = i ;
2014-11-16 23:11:18 +00:00
}
}
}
2014-06-11 17:48:48 +00:00
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
# define MATCH(pattern, fn, dest) ({ \
/* Silly type compatibility test */ \
if ( 0 ) ( fn ) ( " test " , dest ) ; \
match ( pattern , strlen ( pattern ) , name , ( matchfn_t ) ( fn ) , buf , dest ) ; } )
2011-09-01 18:22:05 +00:00
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void get_index ( char * buffer , int * i )
2011-09-02 22:01:53 +00:00
{
* i = atoi ( buffer ) ;
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void get_uint8 ( char * buffer , uint8_t * i )
{
* i = atoi ( buffer ) ;
}
static void get_bearing ( char * buffer , bearing_t * bearing )
{
bearing - > degrees = atoi ( buffer ) ;
}
static void get_rating ( char * buffer , int * i )
2013-01-29 21:30:02 +00:00
{
int j = atoi ( buffer ) ;
if ( j > = 0 & & j < = 5 ) {
* i = j ;
}
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void double_to_o2pressure ( char * buffer , o2pressure_t * i )
2012-12-08 04:08:29 +00:00
{
2017-03-08 06:41:41 +00:00
i - > mbar = lrint ( ascii_strtod ( buffer , NULL ) * 1000.0 ) ;
2012-12-08 04:08:29 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void hex_value ( char * buffer , uint32_t * i )
2012-11-25 19:44:27 +00:00
{
2013-11-22 21:31:52 +00:00
* i = strtoul ( buffer , NULL , 16 ) ;
2012-11-25 19:44:27 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void get_tripflag ( char * buffer , tripflag_t * tf )
2012-08-22 05:04:24 +00:00
{
2012-11-26 22:52:07 +00:00
* tf = strcmp ( buffer , " NOTRIP " ) ? TF_NONE : NO_TRIP ;
2012-08-22 05:04:24 +00:00
}
2011-09-05 21:29:08 +00:00
/*
* Divinglog is crazy . The temperatures are in celsius . EXCEPT
* for the sample temperatures , that are in Fahrenheit .
* WTF ?
2011-09-11 20:16:23 +00:00
*
* Oh , and I think Diving Log * internally * probably kept them
* in celsius , because I ' m seeing entries like
*
* < Temp > 32.0 < / Temp >
*
* in there . Which is freezing , aka 0 degC . I bet the " 0 " is
* what Diving Log uses for " no temperature " .
*
* So throw away crap like that .
2012-10-10 09:14:55 +00:00
*
* It gets worse . Sometimes the sample temperatures are in
* Celsius , which apparently happens if you are in a SI
* locale . So we now do :
*
* - temperatures < 32.0 = = Celsius
* - temperature = = 32.0 - > garbage , it ' s a missing temperature ( zero converted from C to F )
* - temperatures > 32.0 = = Fahrenheit
2011-09-05 21:29:08 +00:00
*/
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void fahrenheit ( char * buffer , temperature_t * temperature )
2011-09-05 21:29:08 +00:00
{
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2014-01-21 21:03:08 +00:00
if ( IS_FP_SAME ( val . fp , 32.0 ) )
2011-09-11 20:16:23 +00:00
break ;
2012-10-10 09:14:55 +00:00
if ( val . fp < 32.0 )
temperature - > mkelvin = C_to_mkelvin ( val . fp ) ;
else
temperature - > mkelvin = F_to_mkelvin ( val . fp ) ;
2011-09-05 21:29:08 +00:00
break ;
default :
fprintf ( stderr , " Crazy Diving Log temperature reading %s \n " , buffer ) ;
}
}
2011-09-11 19:24:57 +00:00
/*
* Did I mention how bat - shit crazy divinglog is ? The sample
* pressures are in PSI . But the tank working pressure is in
* bar . WTF ^ 2 ?
*
2011-09-15 16:43:14 +00:00
* Crazy stuff like this is why subsurface has everything in
2011-09-11 19:24:57 +00:00
* these inconvenient typed structures , and you have to say
* " pressure->mbar " to get the actual value . Exactly so that
* you can never have unit confusion .
2012-10-10 09:14:55 +00:00
*
* It gets worse : sometimes apparently the pressures are in
* bar , sometimes in psi . Dirk suspects that this may be a
* DivingLog Uemis importer bug , and that they are always
* supposed to be in bar , but that the importer got the
* sample importing wrong .
*
* Sadly , there ' s no way to really tell . So I think we just
* have to have some arbitrary cut - off point where we assume
* that smaller values mean bar . . Not good .
2011-09-11 19:24:57 +00:00
*/
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void psi_or_bar ( char * buffer , pressure_t * pressure )
2011-09-11 19:24:57 +00:00
{
union int_or_float val ;
switch ( integer_or_float ( buffer , & val ) ) {
case FLOAT :
2012-10-10 09:14:55 +00:00
if ( val . fp > 400 )
pressure - > mbar = psi_to_mbar ( val . fp ) ;
else
2017-03-08 06:41:41 +00:00
pressure - > mbar = lrint ( val . fp * 1000 ) ;
2011-09-11 19:24:57 +00:00
break ;
default :
fprintf ( stderr , " Crazy Diving Log PSI reading %s \n " , buffer ) ;
}
}
2013-11-02 19:00:16 +00:00
static int divinglog_fill_sample ( struct sample * sample , const char * name , char * buf )
2011-09-05 21:29:08 +00:00
{
2014-02-16 23:42:56 +00:00
return MATCH ( " time.p " , sampletime , & sample - > time ) | |
MATCH ( " depth.p " , depth , & sample - > depth ) | |
MATCH ( " temp.p " , fahrenheit , & sample - > temperature ) | |
2017-07-20 21:39:02 +00:00
MATCH ( " press1.p " , psi_or_bar , & sample - > pressure [ 0 ] ) | |
2014-02-16 23:42:56 +00:00
0 ;
2011-09-05 21:29:08 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void uddf_gasswitch ( char * buffer , struct sample * sample )
2013-02-22 16:52:35 +00:00
{
int idx = atoi ( buffer ) ;
int seconds = sample - > time . seconds ;
struct dive * dive = cur_dive ;
struct divecomputer * dc = get_dc ( ) ;
add_gas_switch_event ( dive , dc , seconds , idx ) ;
}
2013-11-02 19:00:16 +00:00
static int uddf_fill_sample ( struct sample * sample , const char * name , char * buf )
2011-09-07 00:33:52 +00:00
{
2014-02-16 23:42:56 +00:00
return MATCH ( " divetime " , sampletime , & sample - > time ) | |
MATCH ( " depth " , depth , & sample - > depth ) | |
MATCH ( " temperature " , temperature , & sample - > temperature ) | |
2017-07-20 21:39:02 +00:00
MATCH ( " tankpressure " , pressure , & sample - > pressure [ 0 ] ) | |
2014-02-16 23:42:56 +00:00
MATCH ( " ref.switchmix " , uddf_gasswitch , sample ) | |
0 ;
2011-09-07 00:33:52 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void eventtime ( char * buffer , duration_t * duration )
2011-09-23 01:02:54 +00:00
{
sampletime ( buffer , duration ) ;
2012-01-05 16:16:08 +00:00
if ( cur_sample )
duration - > seconds + = cur_sample - > time . seconds ;
2011-09-23 01:02:54 +00:00
}
2013-01-02 01:29:38 +00:00
static void try_to_match_autogroup ( const char * name , char * buf )
{
int autogroupvalue ;
start_match ( " autogroup " , name , buf ) ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " state.autogroup " , get_index , & autogroupvalue ) ) {
2013-01-02 01:29:38 +00:00
set_autogroup ( autogroupvalue ) ;
return ;
}
nonmatch ( " autogroup " , name , buf ) ;
}
2014-06-11 17:48:48 +00:00
void add_gas_switch_event ( struct dive * dive , struct divecomputer * dc , int seconds , int idx )
{
2015-04-21 06:22:48 +00:00
/* sanity check so we don't crash */
if ( idx < 0 | | idx > = MAX_CYLINDERS )
return ;
2014-08-17 18:26:21 +00:00
/* The gas switch event format is insane for historical reasons */
struct gasmix * mix = & dive - > cylinder [ idx ] . gasmix ;
int o2 = get_o2 ( mix ) ;
int he = get_he ( mix ) ;
struct event * ev ;
2014-06-11 17:48:48 +00:00
int value ;
o2 = ( o2 + 5 ) / 10 ;
he = ( he + 5 ) / 10 ;
value = o2 + ( he < < 16 ) ;
2014-08-17 18:26:21 +00:00
ev = add_event ( dc , seconds , he ? SAMPLE_EVENT_GASCHANGE2 : SAMPLE_EVENT_GASCHANGE , 0 , value , " gaschange " ) ;
if ( ev ) {
ev - > gas . index = idx ;
ev - > gas . mix = * mix ;
}
2014-06-11 17:48:48 +00:00
}
static void get_cylinderindex ( char * buffer , uint8_t * i )
{
* i = atoi ( buffer ) ;
if ( lastcylinderindex ! = * i ) {
add_gas_switch_event ( cur_dive , get_dc ( ) , cur_sample - > time . seconds , * i ) ;
lastcylinderindex = * i ;
}
}
static void get_sensor ( char * buffer , uint8_t * i )
{
* i = atoi ( buffer ) ;
}
2015-09-03 20:25:00 +00:00
static void parse_libdc_deco ( char * buffer , struct sample * s )
{
if ( strcmp ( buffer , " deco " ) = = 0 ) {
s - > in_deco = true ;
} else if ( strcmp ( buffer , " ndl " ) = = 0 ) {
s - > in_deco = false ;
// The time wasn't stoptime, it was ndl
s - > ndl = s - > stoptime ;
s - > stoptime . seconds = 0 ;
}
}
2012-12-26 21:47:54 +00:00
static void try_to_fill_dc_settings ( const char * name , char * buf )
{
start_match ( " divecomputerid " , name , buf ) ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " model.divecomputerid " , utf8_string , & cur_settings . dc . model ) )
2012-12-26 21:47:54 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " deviceid.divecomputerid " , hex_value , & cur_settings . dc . deviceid ) )
2012-12-26 21:47:54 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " nickname.divecomputerid " , utf8_string , & cur_settings . dc . nickname ) )
2012-12-26 21:47:54 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " serial.divecomputerid " , utf8_string , & cur_settings . dc . serial_nr ) )
Assemble the actual Suunto serial number
It turns out that the serial number returned by libdivecomputer isn't
really the serial number as interpreted by the vendor. Those tend to be
strings, but libdivecomputer gives us a 32bit number.
Some experimenting showed that for the Suunto devies tested the serial
number is encoded in that 32bit number:
It so happens that the Suunto serial number strings are strings that have
all numbers, but they aren't *one* number. They are four bytes
representing two numbers each, and the "23500027" string is actually the
four bytes 23 50 00 27 (0x17 0x32 0x00 0x1b). And libdivecomputer has
incorrectly parsed those four bytes as one number, not as the encoded
serial number string it is. So the value 389152795 is actually hex
0x1732001b, which is 0x17 0x32 0x00 0x1b, which is - 23 50 00 27.
This should be done by libdivecomputer, but hey, in the meantime this at
least shows the concept. And helps test the XML save/restore code.
It depends on the two patches that create the whole "device.c"
infrastructure, of course. With this, my dive file ends up having the
settings section look like this:
<divecomputerid model='Suunto Vyper Air' deviceid='d4629110'
serial='01201094' firmware='1.1.22'/>
<divecomputerid model='Suunto HelO2' deviceid='995dd566'
serial='23500027' firmware='1.0.4'/>
where the format of the firmware version is something I guessed at,
but it was the obvious choice (again, it's byte-based, I'm ignoring
the high byte that is zero for both of my Suuntos).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-10 00:14:21 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " firmware.divecomputerid " , utf8_string , & cur_settings . dc . firmware ) )
Assemble the actual Suunto serial number
It turns out that the serial number returned by libdivecomputer isn't
really the serial number as interpreted by the vendor. Those tend to be
strings, but libdivecomputer gives us a 32bit number.
Some experimenting showed that for the Suunto devies tested the serial
number is encoded in that 32bit number:
It so happens that the Suunto serial number strings are strings that have
all numbers, but they aren't *one* number. They are four bytes
representing two numbers each, and the "23500027" string is actually the
four bytes 23 50 00 27 (0x17 0x32 0x00 0x1b). And libdivecomputer has
incorrectly parsed those four bytes as one number, not as the encoded
serial number string it is. So the value 389152795 is actually hex
0x1732001b, which is 0x17 0x32 0x00 0x1b, which is - 23 50 00 27.
This should be done by libdivecomputer, but hey, in the meantime this at
least shows the concept. And helps test the XML save/restore code.
It depends on the two patches that create the whole "device.c"
infrastructure, of course. With this, my dive file ends up having the
settings section look like this:
<divecomputerid model='Suunto Vyper Air' deviceid='d4629110'
serial='01201094' firmware='1.1.22'/>
<divecomputerid model='Suunto HelO2' deviceid='995dd566'
serial='23500027' firmware='1.0.4'/>
where the format of the firmware version is something I guessed at,
but it was the obvious choice (again, it's byte-based, I'm ignoring
the high byte that is zero for both of my Suuntos).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-10 00:14:21 +00:00
return ;
2012-12-26 21:47:54 +00:00
nonmatch ( " divecomputerid " , name , buf ) ;
}
2011-09-23 01:02:54 +00:00
static void try_to_fill_event ( const char * name , char * buf )
{
start_match ( " event " , name , buf ) ;
2014-08-17 18:26:21 +00:00
if ( MATCH ( " event " , event_name , cur_event . name ) )
2011-09-23 01:02:54 +00:00
return ;
2014-08-17 18:26:21 +00:00
if ( MATCH ( " name " , event_name , cur_event . name ) )
2011-09-23 01:02:54 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " time " , eventtime , & cur_event . time ) )
2011-09-23 01:02:54 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " type " , get_index , & cur_event . type ) )
2011-09-23 01:02:54 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " flags " , get_index , & cur_event . flags ) )
2011-09-23 01:02:54 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " value " , get_index , & cur_event . value ) )
2011-10-01 04:55:51 +00:00
return ;
2014-08-17 18:26:21 +00:00
if ( MATCH ( " cylinder " , get_index , & cur_event . gas . index ) ) {
/* We add one to indicate that we got an actual cylinder index value */
cur_event . gas . index + + ;
return ;
}
if ( MATCH ( " o2 " , percent , & cur_event . gas . mix . o2 ) )
return ;
if ( MATCH ( " he " , percent , & cur_event . gas . mix . he ) )
return ;
2011-09-23 01:02:54 +00:00
nonmatch ( " event " , name , buf ) ;
}
2013-11-02 19:00:16 +00:00
static int match_dc_data_fields ( struct divecomputer * dc , const char * name , char * buf )
2013-01-23 18:25:31 +00:00
{
2013-11-02 19:00:16 +00:00
if ( MATCH ( " maxdepth " , depth , & dc - > maxdepth ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " meandepth " , depth , & dc - > meandepth ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " max.depth " , depth , & dc - > maxdepth ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " mean.depth " , depth , & dc - > meandepth ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " duration " , duration , & dc - > duration ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " divetime " , duration , & dc - > duration ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " divetimesec " , duration , & dc - > duration ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " surfacetime " , duration , & dc - > surfacetime ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " airtemp " , temperature , & dc - > airtemp ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " watertemp " , temperature , & dc - > watertemp ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " air.temperature " , temperature , & dc - > airtemp ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " water.temperature " , temperature , & dc - > watertemp ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " pressure.surface " , pressure , & dc - > surface_pressure ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " salinity.water " , salinity , & dc - > salinity ) )
2013-01-23 18:25:31 +00:00
return 1 ;
2014-11-06 18:34:19 +00:00
if ( MATCH ( " key.extradata " , utf8_string , & cur_extra_data . key ) )
return 1 ;
if ( MATCH ( " value.extradata " , utf8_string , & cur_extra_data . value ) )
return 1 ;
2015-09-03 20:25:00 +00:00
if ( MATCH ( " divemode " , get_dc_type , & dc - > divemode ) )
return 1 ;
if ( MATCH ( " salinity " , salinity , & dc - > salinity ) )
return 1 ;
if ( MATCH ( " atmospheric " , pressure , & dc - > surface_pressure ) )
return 1 ;
2013-01-23 18:25:31 +00:00
return 0 ;
}
2012-11-25 02:50:21 +00:00
/* We're in the top-level dive xml. Try to convert whatever value to a dive value */
static void try_to_fill_dc ( struct divecomputer * dc , const char * name , char * buf )
{
2016-06-21 04:07:36 +00:00
unsigned int deviceid ;
2012-11-25 02:50:21 +00:00
start_match ( " divecomputer " , name , buf ) ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " date " , divedate , & dc - > when ) )
2012-11-25 02:50:21 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " time " , divetime , & dc - > when ) )
2012-11-25 02:50:21 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " model " , utf8_string , & dc - > model ) )
2012-11-25 19:44:27 +00:00
return ;
2016-06-21 04:07:36 +00:00
if ( MATCH ( " deviceid " , hex_value , & deviceid ) ) {
set_dc_deviceid ( dc , deviceid ) ;
2012-11-25 02:50:21 +00:00
return ;
2016-06-21 04:07:36 +00:00
}
2013-11-02 19:00:16 +00:00
if ( MATCH ( " diveid " , hex_value , & dc - > diveid ) )
2012-11-25 02:50:21 +00:00
return ;
2015-01-10 23:01:15 +00:00
if ( MATCH ( " dctype " , get_dc_type , & dc - > divemode ) )
2014-06-11 17:48:48 +00:00
return ;
if ( MATCH ( " no_o2sensors " , get_sensor , & dc - > no_o2sensors ) )
return ;
2013-11-02 19:00:16 +00:00
if ( match_dc_data_fields ( dc , name , buf ) )
2013-01-23 18:25:31 +00:00
return ;
2012-11-25 02:50:21 +00:00
nonmatch ( " divecomputer " , name , buf ) ;
}
2011-08-30 22:22:48 +00:00
/* We're in samples - try to convert the random xml value to something useful */
static void try_to_fill_sample ( struct sample * sample , const char * name , char * buf )
{
2012-12-31 02:11:01 +00:00
int in_deco ;
Add support for loading and saving multiple pressure samples
This does both the XML and the git save format, because the changes
really are the same, even if the actual format differs in some details.
See how the two "save_samples()" routines both do the same basic setup,
for example.
This is fairly straightforward, with the possible exception of the odd
sensor = sample->sensor[0];
default in the git pressure loading code.
That line just means that if we do *not* have an explicit cylinder index
for the pressure reading, we will always end up filling in the new
pressure as the first pressure (because the cylinder index will match the
first sensor slot).
So that makes the "add_sample_pressure()" case always do the same thing it
used to do for the legacy case: fill in the first slot. The actual sensor
index may later change, since the legacy format has a "sensor=X" key value
pair that sets the sensor, but it will also use the first sensor slot,
making it all do exactly what it used to do.
And on the other hand, if we're loading new-style data with cylinder
pressure and sensor index together, we just end up using the new semantics
for add_sample_pressure(), which tries to keep the same slot for the same
sensor, but does the right thing if we already have other pressure values.
The XML code has no such issues at all, since it can't share the cases
anyway, and we need to have different node names for the different sensor
values and cannot just have multiple "pressure" entries. Have I mentioned
how much I despise XML lately?
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-26 02:10:03 +00:00
pressure_t p ;
2011-08-30 23:23:47 +00:00
2011-09-01 18:22:05 +00:00
start_match ( " sample " , name , buf ) ;
2017-07-20 21:39:02 +00:00
if ( MATCH ( " pressure.sample " , pressure , & sample - > pressure [ 0 ] ) )
2011-08-30 23:23:47 +00:00
return ;
2017-07-20 21:39:02 +00:00
if ( MATCH ( " cylpress.sample " , pressure , & sample - > pressure [ 0 ] ) )
2011-08-30 23:23:47 +00:00
return ;
2017-07-20 21:39:02 +00:00
if ( MATCH ( " pdiluent.sample " , pressure , & sample - > pressure [ 0 ] ) )
2014-11-17 11:25:00 +00:00
return ;
2017-07-20 21:39:02 +00:00
if ( MATCH ( " o2pressure.sample " , pressure , & sample - > pressure [ 1 ] ) )
2014-08-30 15:46:47 +00:00
return ;
Add support for loading and saving multiple pressure samples
This does both the XML and the git save format, because the changes
really are the same, even if the actual format differs in some details.
See how the two "save_samples()" routines both do the same basic setup,
for example.
This is fairly straightforward, with the possible exception of the odd
sensor = sample->sensor[0];
default in the git pressure loading code.
That line just means that if we do *not* have an explicit cylinder index
for the pressure reading, we will always end up filling in the new
pressure as the first pressure (because the cylinder index will match the
first sensor slot).
So that makes the "add_sample_pressure()" case always do the same thing it
used to do for the legacy case: fill in the first slot. The actual sensor
index may later change, since the legacy format has a "sensor=X" key value
pair that sets the sensor, but it will also use the first sensor slot,
making it all do exactly what it used to do.
And on the other hand, if we're loading new-style data with cylinder
pressure and sensor index together, we just end up using the new semantics
for add_sample_pressure(), which tries to keep the same slot for the same
sensor, but does the right thing if we already have other pressure values.
The XML code has no such issues at all, since it can't share the cases
anyway, and we need to have different node names for the different sensor
values and cannot just have multiple "pressure" entries. Have I mentioned
how much I despise XML lately?
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-26 02:10:03 +00:00
/* Christ, this is ugly */
if ( MATCH ( " pressure0.sample " , pressure , & p ) ) {
add_sample_pressure ( sample , 0 , p . mbar ) ;
return ;
}
if ( MATCH ( " pressure1.sample " , pressure , & p ) ) {
add_sample_pressure ( sample , 1 , p . mbar ) ;
return ;
}
if ( MATCH ( " pressure2.sample " , pressure , & p ) ) {
add_sample_pressure ( sample , 2 , p . mbar ) ;
return ;
}
if ( MATCH ( " pressure3.sample " , pressure , & p ) ) {
add_sample_pressure ( sample , 3 , p . mbar ) ;
return ;
}
if ( MATCH ( " pressure4.sample " , pressure , & p ) ) {
add_sample_pressure ( sample , 4 , p . mbar ) ;
return ;
}
Start cleaning up sensor indexing for multiple sensors
This is a very timid start at making us actually use multiple sensors
without the magical special case for just CCR oxygen tracking.
It mainly does:
- turn the "sample->sensor" index into an array of two indexes, to
match the pressures themselves.
- get rid of dive->{oxygen_cylinder_index,diluent_cylinder_index},
since a CCR dive should now simply set the sample->sensor[] indices
correctly instead.
- in a couple of places, start actually looping over the sensors rather
than special-case the O2 case (although often the small "loops" are
just unrolled, since it's just two cases.
but in many cases we still end up only covering the zero sensor case,
because the CCR O2 sensor code coverage was fairly limited.
It's entirely possible (even likely) that this migth break some existing
case: it tries to be a fairly direct ("stupid") translation of the old
code, but unlike the preparatory patch this does actually does change
some semantics.
For example, right now the git loader code assumes that if the git save
data contains a o2pressure entry, it just hardcodes the O2 sensor index
to 1.
In fact, one issue is going to simply be that our file formats do not
have that multiple sensor format, but instead had very clearly encoded
things as being the CCR O2 pressure sensor.
But this is hopefully close to usable, and I will need feedback (and
maybe test cases) from people who have existing CCR dives with pressure
data.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 02:49:45 +00:00
if ( MATCH ( " cylinderindex.sample " , get_cylinderindex , & sample - > sensor [ 0 ] ) )
First step in cleaning up cylinder pressure sensor logic
This clarifies/changes the meaning of our "cylinderindex" entry in our
samples. It has been rather confused, because different dive computers
have done things differently, and the naming really hasn't helped.
There are two totally different - and independent - cylinder "indexes":
- the pressure sensor index, which indicates which cylinder the sensor
data is from.
- the "active cylinder" index, which indicates which cylinder we actually
breathe from.
These two values really are totally independent, and have nothing
what-so-ever to do with each other. The sensor index may well be fixed:
many dive computers only support a single pressure sensor (whether
wireless or wired), and the sensor index is thus always zero.
Other dive computers may support multiple pressure sensors, and the gas
switch event may - or may not - indicate that the sensor changed too. A
dive computer might give the sensor data for *all* cylinders it can read,
regardless of which one is the one we're actively breathing. In fact, some
dive computers might give sensor data for not just *your* cylinder, but
your buddies.
This patch renames "cylinderindex" in the samples as "sensor", making it
quite clear that it's about which sensor index the pressure data in the
sample is about.
The way we figure out which is the currently active gas is with an
explicit has change event. If a computer (like the Uemis Zurich) joins the
two concepts together, then a sensor change should also create a gas
switch event. This patch also changes the Uemis importer to do that.
Finally, it should be noted that the plot info works totally separately
from the sample data, and is about what we actually *display*, not about
the sample pressures etc. In the plot info, the "cylinderindex" does in
fact mean the currently active cylinder, and while it is initially set to
match the sensor information from the samples, we then walk the gas change
events and fix it up - and if the active cylinder differs from the sensor
cylinder, we clear the sensor data.
[Dirk Hohndel: this conflicted with some of my recent changes - I think
I merged things correctly...]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-31 04:00:51 +00:00
return ;
Start cleaning up sensor indexing for multiple sensors
This is a very timid start at making us actually use multiple sensors
without the magical special case for just CCR oxygen tracking.
It mainly does:
- turn the "sample->sensor" index into an array of two indexes, to
match the pressures themselves.
- get rid of dive->{oxygen_cylinder_index,diluent_cylinder_index},
since a CCR dive should now simply set the sample->sensor[] indices
correctly instead.
- in a couple of places, start actually looping over the sensors rather
than special-case the O2 case (although often the small "loops" are
just unrolled, since it's just two cases.
but in many cases we still end up only covering the zero sensor case,
because the CCR O2 sensor code coverage was fairly limited.
It's entirely possible (even likely) that this migth break some existing
case: it tries to be a fairly direct ("stupid") translation of the old
code, but unlike the preparatory patch this does actually does change
some semantics.
For example, right now the git loader code assumes that if the git save
data contains a o2pressure entry, it just hardcodes the O2 sensor index
to 1.
In fact, one issue is going to simply be that our file formats do not
have that multiple sensor format, but instead had very clearly encoded
things as being the CCR O2 pressure sensor.
But this is hopefully close to usable, and I will need feedback (and
maybe test cases) from people who have existing CCR dives with pressure
data.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 02:49:45 +00:00
if ( MATCH ( " sensor.sample " , get_sensor , & sample - > sensor [ 0 ] ) )
2011-10-19 17:06:11 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " depth.sample " , depth , & sample - > depth ) )
2011-08-30 23:23:47 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " temp.sample " , temperature , & sample - > temperature ) )
2011-09-01 23:41:10 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " temperature.sample " , temperature , & sample - > temperature ) )
2011-08-30 23:23:47 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " sampletime.sample " , sampletime , & sample - > time ) )
2011-08-30 23:23:47 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " time.sample " , sampletime , & sample - > time ) )
2011-08-30 23:23:47 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " ndl.sample " , sampletime , & sample - > ndl ) )
2012-12-01 21:02:30 +00:00
return ;
2014-07-09 20:13:36 +00:00
if ( MATCH ( " tts.sample " , sampletime , & sample - > tts ) )
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " in_deco.sample " , get_index , & in_deco ) ) {
2012-12-31 02:11:01 +00:00
sample - > in_deco = ( in_deco = = 1 ) ;
return ;
}
2013-11-02 19:00:16 +00:00
if ( MATCH ( " stoptime.sample " , sampletime , & sample - > stoptime ) )
2012-12-01 21:02:30 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " stopdepth.sample " , depth , & sample - > stopdepth ) )
2012-12-01 21:02:30 +00:00
return ;
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
if ( MATCH ( " cns.sample " , get_uint8 , & sample - > cns ) )
2012-12-11 21:40:07 +00:00
return ;
2015-07-22 15:20:39 +00:00
if ( MATCH ( " rbt.sample " , sampletime , & sample - > rbt ) )
return ;
2014-10-11 07:49:48 +00:00
if ( MATCH ( " sensor1.sample " , double_to_o2pressure , & sample - > o2sensor [ 0 ] ) ) // CCR O2 sensor data
return ;
if ( MATCH ( " sensor2.sample " , double_to_o2pressure , & sample - > o2sensor [ 1 ] ) )
return ;
if ( MATCH ( " sensor3.sample " , double_to_o2pressure , & sample - > o2sensor [ 2 ] ) ) // up to 3 CCR sensors
return ;
2015-02-14 05:07:22 +00:00
if ( MATCH ( " po2.sample " , double_to_o2pressure , & sample - > setpoint ) )
2012-12-11 21:40:07 +00:00
return ;
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
if ( MATCH ( " heartbeat " , get_uint8 , & sample - > heartbeat ) )
2014-01-17 22:00:28 +00:00
return ;
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
if ( MATCH ( " bearing " , get_bearing , & sample - > bearing ) )
2014-01-17 22:00:28 +00:00
return ;
2015-09-03 20:25:00 +00:00
if ( MATCH ( " setpoint.sample " , double_to_o2pressure , & sample - > setpoint ) )
return ;
if ( MATCH ( " ppo2.sample " , double_to_o2pressure , & sample - > o2sensor [ next_o2_sensor ] ) ) {
next_o2_sensor + + ;
return ;
}
if ( MATCH ( " deco.sample " , parse_libdc_deco , sample ) )
return ;
if ( MATCH ( " time.deco " , sampletime , & sample - > stoptime ) )
return ;
if ( MATCH ( " depth.deco " , depth , & sample - > stopdepth ) )
return ;
2011-08-30 23:23:47 +00:00
2011-09-05 20:45:14 +00:00
switch ( import_source ) {
2011-09-05 21:29:08 +00:00
case DIVINGLOG :
2013-11-02 19:00:16 +00:00
if ( divinglog_fill_sample ( sample , name , buf ) )
2011-09-05 21:29:08 +00:00
return ;
break ;
2011-09-07 00:33:52 +00:00
case UDDF :
2013-11-02 19:00:16 +00:00
if ( uddf_fill_sample ( sample , name , buf ) )
2011-09-07 00:33:52 +00:00
return ;
break ;
2011-09-05 20:45:14 +00:00
default :
break ;
2011-09-02 18:32:48 +00:00
}
2011-09-01 18:22:05 +00:00
nonmatch ( " sample " , name , buf ) ;
2011-08-30 22:22:48 +00:00
}
2014-04-11 06:17:35 +00:00
void try_to_fill_userid ( const char * name , char * buf )
{
2016-03-07 19:40:12 +00:00
( void ) name ;
2014-04-17 14:34:21 +00:00
if ( prefs . save_userid_local )
2014-04-11 06:17:35 +00:00
set_userid ( buf ) ;
}
2011-09-05 21:29:08 +00:00
static const char * country , * city ;
2015-02-12 05:46:02 +00:00
static void divinglog_place ( char * place , uint32_t * uuid )
2011-09-05 21:29:08 +00:00
{
2015-02-15 18:25:21 +00:00
char buffer [ 1024 ] ;
2011-09-05 21:29:08 +00:00
2015-02-12 05:46:02 +00:00
snprintf ( buffer , sizeof ( buffer ) ,
" %s%s%s%s%s " ,
place ,
city ? " , " : " " ,
city ? city : " " ,
country ? " , " : " " ,
country ? country : " " ) ;
2015-02-13 08:04:14 +00:00
* uuid = get_dive_site_uuid_by_name ( buffer , NULL ) ;
2015-02-12 05:46:02 +00:00
if ( * uuid = = 0 )
2015-08-24 17:37:18 +00:00
* uuid = create_dive_site ( buffer , cur_dive - > when ) ;
2011-09-05 21:29:08 +00:00
city = NULL ;
country = NULL ;
}
2013-11-02 19:00:16 +00:00
static int divinglog_dive_match ( struct dive * dive , const char * name , char * buf )
{
2014-02-16 23:42:56 +00:00
return MATCH ( " divedate " , divedate , & dive - > when ) | |
MATCH ( " entrytime " , divetime , & dive - > when ) | |
MATCH ( " divetime " , duration , & dive - > dc . duration ) | |
MATCH ( " depth " , depth , & dive - > dc . maxdepth ) | |
MATCH ( " depthavg " , depth , & dive - > dc . meandepth ) | |
MATCH ( " tanktype " , utf8_string , & dive - > cylinder [ 0 ] . type . description ) | |
MATCH ( " tanksize " , cylindersize , & dive - > cylinder [ 0 ] . type . size ) | |
MATCH ( " presw " , pressure , & dive - > cylinder [ 0 ] . type . workingpressure ) | |
MATCH ( " press " , pressure , & dive - > cylinder [ 0 ] . start ) | |
MATCH ( " prese " , pressure , & dive - > cylinder [ 0 ] . end ) | |
MATCH ( " comments " , utf8_string , & dive - > notes ) | |
MATCH ( " names.buddy " , utf8_string , & dive - > buddy ) | |
MATCH ( " name.country " , utf8_string , & country ) | |
MATCH ( " name.city " , utf8_string , & city ) | |
2015-02-12 05:46:02 +00:00
MATCH ( " name.place " , divinglog_place , & dive - > dive_site_uuid ) | |
2014-02-16 23:42:56 +00:00
0 ;
2011-09-05 21:29:08 +00:00
}
2011-09-07 00:33:52 +00:00
/*
* Uddf specifies ISO 8601 time format .
*
* There are many variations on that . This handles the useful cases .
*/
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void uddf_datetime ( char * buffer , timestamp_t * when )
2011-09-07 00:33:52 +00:00
{
char c ;
2014-02-16 23:42:56 +00:00
int y , m , d , hh , mm , ss ;
2011-09-07 00:33:52 +00:00
struct tm tm = { 0 } ;
int i ;
i = sscanf ( buffer , " %d-%d-%d%c%d:%d:%d " , & y , & m , & d , & c , & hh , & mm , & ss ) ;
if ( i = = 7 )
goto success ;
ss = 0 ;
if ( i = = 6 )
goto success ;
i = sscanf ( buffer , " %04d%02d%02d%c%02d%02d%02d " , & y , & m , & d , & c , & hh , & mm , & ss ) ;
if ( i = = 7 )
goto success ;
ss = 0 ;
if ( i = = 6 )
goto success ;
bad_date :
printf ( " Bad date time %s \n " , buffer ) ;
return ;
success :
if ( c ! = ' T ' & & c ! = ' ' )
goto bad_date ;
tm . tm_year = y ;
tm . tm_mon = m - 1 ;
tm . tm_mday = d ;
tm . tm_hour = hh ;
tm . tm_min = mm ;
tm . tm_sec = ss ;
* when = utc_mktime ( & tm ) ;
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
# define uddf_datedata(name, offset) \
static void uddf_ # # name ( char * buffer , timestamp_t * when ) \
{ \
cur_tm . tm_ # # name = atoi ( buffer ) + offset ; \
* when = utc_mktime ( & cur_tm ) ; \
2014-02-16 23:42:56 +00:00
}
2013-02-22 16:52:35 +00:00
uddf_datedata ( year , 0 )
uddf_datedata ( mon , - 1 )
uddf_datedata ( mday , 0 )
uddf_datedata ( hour , 0 )
uddf_datedata ( min , 0 )
2013-11-02 19:00:16 +00:00
static int uddf_dive_match ( struct dive * dive , const char * name , char * buf )
2011-09-07 00:33:52 +00:00
{
2014-02-16 23:42:56 +00:00
return MATCH ( " datetime " , uddf_datetime , & dive - > when ) | |
MATCH ( " diveduration " , duration , & dive - > dc . duration ) | |
MATCH ( " greatestdepth " , depth , & dive - > dc . maxdepth ) | |
MATCH ( " year.date " , uddf_year , & dive - > when ) | |
MATCH ( " month.date " , uddf_mon , & dive - > when ) | |
MATCH ( " day.date " , uddf_mday , & dive - > when ) | |
MATCH ( " hour.time " , uddf_hour , & dive - > when ) | |
MATCH ( " minute.time " , uddf_min , & dive - > when ) | |
0 ;
2011-09-07 00:33:52 +00:00
}
2012-12-05 17:59:52 +00:00
/*
* This parses " floating point " into micro - degrees .
* We don ' t do exponentials etc , if somebody does
2015-09-04 23:03:14 +00:00
* GPS locations in that format , they are insane .
2012-12-05 17:59:52 +00:00
*/
2014-03-09 19:19:41 +00:00
degrees_t parse_degrees ( char * buf , char * * end )
2012-12-05 17:59:52 +00:00
{
int sign = 1 , decimals = 6 , value = 0 ;
degrees_t ret ;
2013-10-05 07:29:09 +00:00
while ( isspace ( * buf ) )
2012-12-05 17:59:52 +00:00
buf + + ;
switch ( * buf ) {
case ' - ' :
sign = - 1 ;
2014-02-16 23:42:56 +00:00
/* fallthrough */
2012-12-05 17:59:52 +00:00
case ' + ' :
buf + + ;
}
while ( isdigit ( * buf ) ) {
2014-02-16 23:42:56 +00:00
value = 10 * value + * buf - ' 0 ' ;
2012-12-05 17:59:52 +00:00
buf + + ;
}
/* Get the first six decimals if they exist */
if ( * buf = = ' . ' )
buf + + ;
do {
value * = 10 ;
if ( isdigit ( * buf ) ) {
value + = * buf - ' 0 ' ;
buf + + ;
}
} while ( - - decimals ) ;
/* Rounding */
switch ( * buf ) {
case ' 5 ' . . . ' 9 ' :
value + + ;
}
while ( isdigit ( * buf ) )
buf + + ;
* end = buf ;
ret . udeg = value * sign ;
return ret ;
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void gps_lat ( char * buffer , struct dive * dive )
2013-01-23 19:53:42 +00:00
{
char * end ;
2015-02-13 05:26:43 +00:00
degrees_t latitude = parse_degrees ( buffer , & end ) ;
struct dive_site * ds = get_dive_site_for_dive ( dive ) ;
if ( ! ds ) {
2015-08-24 17:37:18 +00:00
dive - > dive_site_uuid = create_dive_site_with_gps ( NULL , latitude , ( degrees_t ) { 0 } , dive - > when ) ;
2015-02-13 05:26:43 +00:00
} else {
if ( ds - > latitude . udeg & & ds - > latitude . udeg ! = latitude . udeg )
fprintf ( stderr , " Oops, changing the latitude of existing dive site id %8x name %s; not good \n " , ds - > uuid , ds - > name ? : " (unknown) " ) ;
ds - > latitude = latitude ;
}
2013-01-23 19:53:42 +00:00
}
Make parse-xml callbacks be type-safe
.. and fix the type breakage brought in by commit eaf6d564874a ("CCR code:
Change to sample structure")
The XML parsing callbacks pass a "void *" around, because the helper
function that matches the XML node names ("match()") does so for all the
different dive/sample/dc member nodes that all have different types.
But that also hid the fact that it very much depended on the various types
being regular "int" etc, rather than the denser types that were introduced
so that the CCR data wouldn't expand memory use excessively. As a result,
XML loading would overwrite other members, and possibly even the
allocation, when it wrote an "int" value to something that only was a
8-bit allocation.
I left the "utf8_string()" without type checking - so it still uses
"void *_res" for the result type, with the cast happening inside the
function.
That's because the result destination ends up being a bit mixed-up wrt
"const char **" and just plain "char **". Note that the thing we modify
itself isn't const (it's not "char *const *"), but the pointer, but we
basically sometimes assign a "const char *", and sometimes a "char *".
I considered making two different versions of the callback, but it just
wasn't worth it. So "utf8_string()" users still aren't type-checked, and
you'd better give it a pointer to something that is some kind of "char *"
This patch doesn't really change the calling convention of the matching
function itself, but it makes the wrapper macro ("MATCH()") take a
properly type-checked function pointer instead (with a dummy call to do
type checking), and then casts the pointer to the "void *" type for the
actual real call.
The function pointer call is not really portable (although it works on
all sane architectures, particularly since the cast only changes one
argument from one type of pointer to another), and to make matters worse
uses the gcc statement-expression extension. But all the compilers we use
seem to support that gcc'ism, so in practice this gives us type-safety
with no downsides.
(If we ever want to use MSVC to compile subsurface, I suspect we'll have
to ifdef out the statement expression use and not type-check things. Or
perhaps re-write the thing as a ternary expression instead, or something).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-07 21:41:07 +00:00
static void gps_long ( char * buffer , struct dive * dive )
2013-01-23 19:53:42 +00:00
{
char * end ;
2015-02-13 05:26:43 +00:00
degrees_t longitude = parse_degrees ( buffer , & end ) ;
struct dive_site * ds = get_dive_site_for_dive ( dive ) ;
if ( ! ds ) {
2015-08-24 17:37:18 +00:00
dive - > dive_site_uuid = create_dive_site_with_gps ( NULL , ( degrees_t ) { 0 } , longitude , dive - > when ) ;
2015-02-13 05:26:43 +00:00
} else {
if ( ds - > longitude . udeg & & ds - > longitude . udeg ! = longitude . udeg )
fprintf ( stderr , " Oops, changing the longitude of existing dive site id %8x name %s; not good \n " , ds - > uuid , ds - > name ? : " (unknown) " ) ;
ds - > longitude = longitude ;
}
2013-01-23 19:53:42 +00:00
}
2015-02-12 05:46:02 +00:00
static void gps_location ( char * buffer , struct dive_site * ds )
2011-09-16 01:16:07 +00:00
{
2012-11-18 17:55:41 +00:00
char * end ;
2011-09-16 01:16:07 +00:00
2015-02-12 05:46:02 +00:00
ds - > latitude = parse_degrees ( buffer , & end ) ;
ds - > longitude = parse_degrees ( end , & end ) ;
}
2015-02-15 02:00:35 +00:00
/* this is in qthelper.cpp, so including the .h file is a pain */
extern const char * printGPSCoords ( int lat , int lon ) ;
2015-02-12 05:46:02 +00:00
static void gps_in_dive ( char * buffer , struct dive * dive )
{
char * end ;
2015-02-13 08:04:14 +00:00
struct dive_site * ds = NULL ;
2015-02-12 05:46:02 +00:00
degrees_t latitude = parse_degrees ( buffer , & end ) ;
degrees_t longitude = parse_degrees ( end , & end ) ;
uint32_t uuid = dive - > dive_site_uuid ;
if ( uuid = = 0 ) {
2015-06-10 18:52:18 +00:00
// check if we have a dive site within 20 meters of that gps fix
uuid = get_dive_site_uuid_by_gps_proximity ( latitude , longitude , 20 , & ds ) ;
2015-02-13 08:04:14 +00:00
if ( ds ) {
2015-06-10 18:52:18 +00:00
// found a site nearby; in case it turns out this one had a different name let's
// remember the original coordinates so we can create the correct dive site later
cur_latitude = latitude ;
cur_longitude = longitude ;
2015-02-13 08:04:14 +00:00
dive - > dive_site_uuid = uuid ;
} else {
2015-08-24 17:37:18 +00:00
dive - > dive_site_uuid = create_dive_site_with_gps ( " " , latitude , longitude , dive - > when ) ;
2015-02-15 04:01:33 +00:00
ds = get_dive_site_by_uuid ( dive - > dive_site_uuid ) ;
2015-02-13 08:04:14 +00:00
}
2015-02-12 05:46:02 +00:00
} else {
2015-02-15 04:01:33 +00:00
ds = get_dive_site_by_uuid ( uuid ) ;
2015-02-12 05:46:02 +00:00
if ( dive_site_has_gps_location ( ds ) & &
( latitude . udeg ! = 0 | | longitude . udeg ! = 0 ) & &
( ds - > latitude . udeg ! = latitude . udeg | | ds - > longitude . udeg ! = longitude . udeg ) ) {
// Houston, we have a problem
fprintf ( stderr , " dive site uuid in dive, but gps location (%10.6f/%10.6f) different from dive location (%10.6f/%10.6f) \n " ,
ds - > latitude . udeg / 1000000.0 , ds - > longitude . udeg / 1000000.0 ,
latitude . udeg / 1000000.0 , longitude . udeg / 1000000.0 ) ;
2015-06-22 03:24:07 +00:00
const char * coords = printGPSCoords ( latitude . udeg , longitude . udeg ) ;
2015-09-04 23:03:14 +00:00
ds - > notes = add_to_string ( ds - > notes , translate ( " gettextFromC " , " multiple GPS locations for this dive site; also %s \n " ) , coords ) ;
2015-06-22 03:24:07 +00:00
free ( ( void * ) coords ) ;
2015-02-12 05:46:02 +00:00
} else {
ds - > latitude = latitude ;
ds - > longitude = longitude ;
}
}
}
2015-06-09 19:50:17 +00:00
static void add_dive_site ( char * ds_name , struct dive * dive )
2015-02-12 05:46:02 +00:00
{
2015-06-17 18:42:15 +00:00
static int suffix = 1 ;
2015-06-09 19:50:17 +00:00
char * buffer = ds_name ;
2015-06-22 03:24:07 +00:00
char * to_free = NULL ;
2015-02-13 08:04:14 +00:00
int size = trimspace ( buffer ) ;
2015-02-12 05:46:02 +00:00
if ( size ) {
2015-02-13 08:04:14 +00:00
uint32_t uuid = dive - > dive_site_uuid ;
struct dive_site * ds = get_dive_site_by_uuid ( uuid ) ;
if ( uuid & & ! ds ) {
// that's strange - we have a uuid but it doesn't exist - let's just ignore it
fprintf ( stderr , " dive contains a non-existing dive site uuid %x \n " , dive - > dive_site_uuid ) ;
uuid = 0 ;
}
2015-06-09 19:50:17 +00:00
if ( ! uuid ) {
2015-02-13 08:04:14 +00:00
// if the dive doesn't have a uuid, check if there's already a dive site by this name
uuid = get_dive_site_uuid_by_name ( buffer , & ds ) ;
2015-06-09 19:50:17 +00:00
if ( uuid & & import_source = = SSRF_WS ) {
// when downloading GPS fixes from the Subsurface webservice we will often
// get a lot of dives with identical names (the autogenerated fixes).
// So in this case modify the name to make it unique
int name_size = strlen ( buffer ) + 10 ; // 8 digits - enough for 100 million sites
2015-06-22 03:24:07 +00:00
to_free = buffer = malloc ( name_size ) ;
2015-06-09 19:50:17 +00:00
do {
suffix + + ;
snprintf ( buffer , name_size , " %s %8d " , ds_name , suffix ) ;
} while ( get_dive_site_uuid_by_name ( buffer , NULL ) ! = 0 ) ;
ds = NULL ;
}
}
2015-02-13 08:04:14 +00:00
if ( ds ) {
// we have a uuid, let's hope there isn't a different name
if ( same_string ( ds - > name , " " ) ) {
ds - > name = copy_string ( buffer ) ;
} else if ( ! same_string ( ds - > name , buffer ) ) {
2015-06-10 18:52:18 +00:00
// if it's not the same name, it's not the same dive site
2015-08-30 17:10:07 +00:00
// but wait, we could have gotten this one based on GPS coords and could
// have had two different names for the same site... so let's search the other
// way around
uint32_t exact_match_uuid = get_dive_site_uuid_by_gps_and_name ( buffer , ds - > latitude , ds - > longitude ) ;
if ( exact_match_uuid ) {
dive - > dive_site_uuid = exact_match_uuid ;
2015-06-10 18:52:18 +00:00
} else {
2015-08-30 17:10:07 +00:00
dive - > dive_site_uuid = create_dive_site ( buffer , dive - > when ) ;
struct dive_site * newds = get_dive_site_by_uuid ( dive - > dive_site_uuid ) ;
if ( cur_latitude . udeg | | cur_longitude . udeg ) {
// we started this uuid with GPS data, so lets use those
newds - > latitude = cur_latitude ;
newds - > longitude = cur_longitude ;
} else {
newds - > latitude = ds - > latitude ;
newds - > longitude = ds - > longitude ;
}
newds - > notes = add_to_string ( newds - > notes , translate ( " gettextFromC " , " additional name for site: %s \n " ) , ds - > name ) ;
2015-06-10 18:52:18 +00:00
}
2015-02-14 09:33:56 +00:00
} else {
// add the existing dive site to the current dive
dive - > dive_site_uuid = uuid ;
2015-02-12 05:46:02 +00:00
}
} else {
2015-08-24 17:37:18 +00:00
dive - > dive_site_uuid = create_dive_site ( buffer , dive - > when ) ;
2015-02-12 05:46:02 +00:00
}
}
2015-06-22 03:24:07 +00:00
free ( to_free ) ;
2011-09-16 01:16:07 +00:00
}
2014-06-09 00:42:15 +00:00
static void gps_picture_location ( char * buffer , struct picture * pic )
{
char * end ;
pic - > latitude = parse_degrees ( buffer , & end ) ;
pic - > longitude = parse_degrees ( end , & end ) ;
}
2011-08-30 22:22:48 +00:00
/* We're in the top-level dive xml. Try to convert whatever value to a dive value */
2012-11-24 02:05:38 +00:00
static void try_to_fill_dive ( struct dive * dive , const char * name , char * buf )
2011-08-30 22:22:48 +00:00
{
2011-09-01 18:22:05 +00:00
start_match ( " dive " , name , buf ) ;
2011-09-05 21:29:08 +00:00
switch ( import_source ) {
case DIVINGLOG :
2013-11-02 19:00:16 +00:00
if ( divinglog_dive_match ( dive , name , buf ) )
2011-09-05 21:29:08 +00:00
return ;
break ;
2011-09-07 00:33:52 +00:00
case UDDF :
2013-11-02 19:00:16 +00:00
if ( uddf_dive_match ( dive , name , buf ) )
2011-09-07 00:33:52 +00:00
return ;
break ;
2011-09-05 21:29:08 +00:00
default :
break ;
}
2015-02-12 05:46:02 +00:00
if ( MATCH ( " divesiteid " , hex_value , & dive - > dive_site_uuid ) )
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " number " , get_index , & dive - > number ) )
2011-09-11 18:36:33 +00:00
return ;
Get rid of crazy empty tag_list element at the start
So this is totally unrelated to the git repository format, except for
the fact that I noticed it while writing the git saving code.
The subsurface divetag list handling is being stupid, and has a
initial dummy entry at the head of the list for no good reason.
I say "no good reason", because there *is* a reason for it: it allows
code to avoid the special case of empty list and adding entries to
before the first entry etc etc. But that reason is a really *bad*
reason, because it's valid only because people don't understand basic
list manipulation and pointers to pointers.
So get rid of the dummy element, and do things right instead - by
passing a *pointer* to the list, instead of the list. And then when
traversing the list and looking for a place to insert things, don't go
to the next entry - just update the "pointer to pointer" to point to
the address of the next entry. Each entry in a C linked list is no
different than the list itself, so you can use the pointer to the
pointer to the next entry as a pointer to the list.
This is a pet peeve of mine. The real beauty of pointers can never be
understood unless you understand the indirection they allow. People
who grew up with Pascal and were corrupted by that mindset are
mentally stunted. Niklaus Wirth has a lot to answer for!
But never fear. You too can overcome that mental limitation, it just
needs some brain exercise. Reading this patch may help. In particular,
contemplate the new "taglist_add_divetag()".
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-10 17:18:13 +00:00
if ( MATCH ( " tags " , divetags , & dive - > tag_list ) )
2013-04-09 15:54:36 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " tripflag " , get_tripflag , & dive - > tripflag ) )
2012-08-22 05:04:24 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " date " , divedate , & dive - > when ) )
2011-08-30 22:22:48 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " time " , divetime , & dive - > when ) )
2011-08-30 22:22:48 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " datetime " , divedatetime , & dive - > when ) )
2011-08-30 23:59:03 +00:00
return ;
2013-01-23 18:25:31 +00:00
/*
* Legacy format note : per - dive depths and duration get saved
* in the first dive computer entry
*/
2013-11-02 19:00:16 +00:00
if ( match_dc_data_fields ( & dive - > dc , name , buf ) )
2012-11-12 19:57:49 +00:00
return ;
2013-01-23 18:25:31 +00:00
2014-06-09 00:42:15 +00:00
if ( MATCH ( " filename.picture " , utf8_string , & cur_picture - > filename ) )
return ;
2014-07-08 19:29:06 +00:00
if ( MATCH ( " offset.picture " , offsettime , & cur_picture - > offset ) )
2014-06-09 00:42:15 +00:00
return ;
if ( MATCH ( " gps.picture " , gps_picture_location , cur_picture ) )
return ;
2015-02-26 13:39:42 +00:00
if ( MATCH ( " hash.picture " , utf8_string , & cur_picture - > hash ) )
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " cylinderstartpressure " , pressure , & dive - > cylinder [ 0 ] . start ) )
2011-08-31 00:45:03 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " cylinderendpressure " , pressure , & dive - > cylinder [ 0 ] . end ) )
2011-08-31 00:45:03 +00:00
return ;
2015-02-12 05:46:02 +00:00
if ( MATCH ( " gps " , gps_in_dive , dive ) )
2011-09-16 01:16:07 +00:00
return ;
2015-02-12 05:46:02 +00:00
if ( MATCH ( " Place " , gps_in_dive , dive ) )
2013-02-25 07:20:58 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " latitude " , gps_lat , dive ) )
2013-01-23 19:53:42 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " sitelat " , gps_lat , dive ) )
2013-01-29 05:11:01 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " lat " , gps_lat , dive ) )
2013-02-25 07:20:58 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " longitude " , gps_long , dive ) )
2013-01-23 19:53:42 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " sitelon " , gps_long , dive ) )
2013-01-29 05:11:01 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " lon " , gps_long , dive ) )
2013-02-25 07:20:58 +00:00
return ;
2015-02-12 05:46:02 +00:00
if ( MATCH ( " location " , add_dive_site , dive ) )
2011-09-02 02:56:04 +00:00
return ;
2015-02-12 05:46:02 +00:00
if ( MATCH ( " name.dive " , add_dive_site , dive ) )
2013-01-23 19:53:42 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " suit " , utf8_string , & dive - > suit ) )
2012-08-14 23:07:25 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " divesuit " , utf8_string , & dive - > suit ) )
2012-08-18 03:22:37 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " notes " , utf8_string , & dive - > notes ) )
2011-09-02 02:56:04 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " divemaster " , utf8_string , & dive - > divemaster ) )
2011-09-13 21:58:06 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " buddy " , utf8_string , & dive - > buddy ) )
2011-09-13 21:58:06 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " rating.dive " , get_rating , & dive - > rating ) )
2011-12-07 19:58:16 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " visibility.dive " , get_rating , & dive - > visibility ) )
2012-10-28 22:49:02 +00:00
return ;
2015-11-03 02:03:01 +00:00
if ( cur_ws_index < MAX_WEIGHTSYSTEMS ) {
if ( MATCH ( " description.weightsystem " , utf8_string , & dive - > weightsystem [ cur_ws_index ] . description ) )
return ;
if ( MATCH ( " weight.weightsystem " , weight , & dive - > weightsystem [ cur_ws_index ] . weight ) )
return ;
if ( MATCH ( " weight " , weight , & dive - > weightsystem [ cur_ws_index ] . weight ) )
return ;
}
if ( cur_cylinder_index < MAX_CYLINDERS ) {
if ( MATCH ( " size.cylinder " , cylindersize , & dive - > cylinder [ cur_cylinder_index ] . type . size ) )
return ;
if ( MATCH ( " workpressure.cylinder " , pressure , & dive - > cylinder [ cur_cylinder_index ] . type . workingpressure ) )
return ;
if ( MATCH ( " description.cylinder " , utf8_string , & dive - > cylinder [ cur_cylinder_index ] . type . description ) )
return ;
if ( MATCH ( " start.cylinder " , pressure , & dive - > cylinder [ cur_cylinder_index ] . start ) )
return ;
if ( MATCH ( " end.cylinder " , pressure , & dive - > cylinder [ cur_cylinder_index ] . end ) )
return ;
if ( MATCH ( " use.cylinder " , cylinder_use , & dive - > cylinder [ cur_cylinder_index ] . cylinder_use ) )
return ;
if ( MATCH ( " o2 " , gasmix , & dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 ) )
return ;
if ( MATCH ( " o2percent " , gasmix , & dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 ) )
return ;
if ( MATCH ( " n2 " , gasmix_nitrogen , & dive - > cylinder [ cur_cylinder_index ] . gasmix ) )
return ;
if ( MATCH ( " he " , gasmix , & dive - > cylinder [ cur_cylinder_index ] . gasmix . he ) )
return ;
}
2013-11-02 19:00:16 +00:00
if ( MATCH ( " air.divetemperature " , temperature , & dive - > airtemp ) )
2013-02-14 17:44:18 +00:00
return ;
2014-06-03 23:01:48 +00:00
if ( MATCH ( " water.divetemperature " , temperature , & dive - > watertemp ) )
return ;
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
2011-09-01 18:22:05 +00:00
nonmatch ( " dive " , name , buf ) ;
2011-08-30 22:22:48 +00:00
}
2011-08-30 20:48:05 +00:00
2012-08-22 05:04:24 +00:00
/* We're in the top-level trip xml. Try to convert whatever value to a trip value */
2012-09-20 03:42:11 +00:00
static void try_to_fill_trip ( dive_trip_t * * dive_trip_p , const char * name , char * buf )
2012-08-22 05:04:24 +00:00
{
start_match ( " trip " , name , buf ) ;
2012-09-20 03:42:11 +00:00
dive_trip_t * dive_trip = * dive_trip_p ;
2012-08-22 05:04:24 +00:00
2013-11-02 19:00:16 +00:00
if ( MATCH ( " date " , divedate , & dive_trip - > when ) )
2012-08-30 00:24:15 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " time " , divetime , & dive_trip - > when ) )
2012-08-22 05:04:24 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " location " , utf8_string , & dive_trip - > location ) )
2012-08-22 05:04:24 +00:00
return ;
2013-11-02 19:00:16 +00:00
if ( MATCH ( " notes " , utf8_string , & dive_trip - > notes ) )
2012-08-22 05:04:24 +00:00
return ;
nonmatch ( " trip " , name , buf ) ;
}
2015-02-12 05:46:02 +00:00
/* We're processing a divesite entry - try to fill the components */
static void try_to_fill_dive_site ( struct dive_site * * ds_p , const char * name , char * buf )
{
start_match ( " divesite " , name , buf ) ;
struct dive_site * ds = * ds_p ;
2015-07-01 19:30:33 +00:00
if ( ds - > taxonomy . category = = NULL )
ds - > taxonomy . category = alloc_taxonomy ( ) ;
2015-02-12 05:46:02 +00:00
if ( MATCH ( " uuid " , hex_value , & ds - > uuid ) )
return ;
if ( MATCH ( " name " , utf8_string , & ds - > name ) )
return ;
if ( MATCH ( " description " , utf8_string , & ds - > description ) )
return ;
if ( MATCH ( " notes " , utf8_string , & ds - > notes ) )
return ;
if ( MATCH ( " gps " , gps_location , ds ) )
return ;
2015-07-01 19:30:33 +00:00
if ( MATCH ( " cat.geo " , get_index , ( int * ) & ds - > taxonomy . category [ ds - > taxonomy . nr ] . category ) )
return ;
if ( MATCH ( " origin.geo " , get_index , ( int * ) & ds - > taxonomy . category [ ds - > taxonomy . nr ] . origin ) )
return ;
if ( MATCH ( " value.geo " , utf8_string , & ds - > taxonomy . category [ ds - > taxonomy . nr ] . value ) ) {
2015-07-02 17:21:35 +00:00
if ( ds - > taxonomy . nr < TC_NR_CATEGORIES )
2015-07-01 19:30:33 +00:00
ds - > taxonomy . nr + + ;
return ;
}
2015-02-12 05:46:02 +00:00
nonmatch ( " divesite " , name , buf ) ;
}
2011-08-30 04:32:27 +00:00
/*
2012-09-16 01:32:15 +00:00
* While in some formats file boundaries are dive boundaries , in many
* others ( as for example in our native format ) there are
2011-08-30 04:32:27 +00:00
* multiple dives per file , so there can be other events too that
* trigger a " new dive " marker and you may get some nesting due
* to that . Just ignore nesting levels .
2012-09-16 01:32:15 +00:00
* On the flipside it is possible that we start an XML file that ends
* up having no dives in it at all - don ' t create a bogus empty dive
* for those . It ' s not entirely clear what is the minimum set of data
* to make a dive valid , but if it has no location , no date and no
* samples I ' m pretty sure it ' s useless .
2011-08-30 04:32:27 +00:00
*/
2013-10-05 07:29:09 +00:00
static bool is_dive ( void )
2012-09-16 01:32:15 +00:00
{
return ( cur_dive & &
2015-02-12 05:46:02 +00:00
( cur_dive - > dive_site_uuid | | cur_dive - > when | | cur_dive - > dc . samples ) ) ;
2012-09-16 01:32:15 +00:00
}
2012-12-19 20:36:56 +00:00
static void reset_dc_info ( struct divecomputer * dc )
{
2016-03-07 19:40:12 +00:00
/* WARN: reset dc info does't touch the dc? */
( void ) dc ;
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
lastcylinderindex = 0 ;
2012-12-19 20:36:56 +00:00
}
2012-12-26 21:47:54 +00:00
static void reset_dc_settings ( void )
{
free ( ( void * ) cur_settings . dc . model ) ;
free ( ( void * ) cur_settings . dc . nickname ) ;
Assemble the actual Suunto serial number
It turns out that the serial number returned by libdivecomputer isn't
really the serial number as interpreted by the vendor. Those tend to be
strings, but libdivecomputer gives us a 32bit number.
Some experimenting showed that for the Suunto devies tested the serial
number is encoded in that 32bit number:
It so happens that the Suunto serial number strings are strings that have
all numbers, but they aren't *one* number. They are four bytes
representing two numbers each, and the "23500027" string is actually the
four bytes 23 50 00 27 (0x17 0x32 0x00 0x1b). And libdivecomputer has
incorrectly parsed those four bytes as one number, not as the encoded
serial number string it is. So the value 389152795 is actually hex
0x1732001b, which is 0x17 0x32 0x00 0x1b, which is - 23 50 00 27.
This should be done by libdivecomputer, but hey, in the meantime this at
least shows the concept. And helps test the XML save/restore code.
It depends on the two patches that create the whole "device.c"
infrastructure, of course. With this, my dive file ends up having the
settings section look like this:
<divecomputerid model='Suunto Vyper Air' deviceid='d4629110'
serial='01201094' firmware='1.1.22'/>
<divecomputerid model='Suunto HelO2' deviceid='995dd566'
serial='23500027' firmware='1.0.4'/>
where the format of the firmware version is something I guessed at,
but it was the obvious choice (again, it's byte-based, I'm ignoring
the high byte that is zero for both of my Suuntos).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-10 00:14:21 +00:00
free ( ( void * ) cur_settings . dc . serial_nr ) ;
free ( ( void * ) cur_settings . dc . firmware ) ;
2012-12-26 21:47:54 +00:00
cur_settings . dc . model = NULL ;
cur_settings . dc . nickname = NULL ;
Assemble the actual Suunto serial number
It turns out that the serial number returned by libdivecomputer isn't
really the serial number as interpreted by the vendor. Those tend to be
strings, but libdivecomputer gives us a 32bit number.
Some experimenting showed that for the Suunto devies tested the serial
number is encoded in that 32bit number:
It so happens that the Suunto serial number strings are strings that have
all numbers, but they aren't *one* number. They are four bytes
representing two numbers each, and the "23500027" string is actually the
four bytes 23 50 00 27 (0x17 0x32 0x00 0x1b). And libdivecomputer has
incorrectly parsed those four bytes as one number, not as the encoded
serial number string it is. So the value 389152795 is actually hex
0x1732001b, which is 0x17 0x32 0x00 0x1b, which is - 23 50 00 27.
This should be done by libdivecomputer, but hey, in the meantime this at
least shows the concept. And helps test the XML save/restore code.
It depends on the two patches that create the whole "device.c"
infrastructure, of course. With this, my dive file ends up having the
settings section look like this:
<divecomputerid model='Suunto Vyper Air' deviceid='d4629110'
serial='01201094' firmware='1.1.22'/>
<divecomputerid model='Suunto HelO2' deviceid='995dd566'
serial='23500027' firmware='1.0.4'/>
where the format of the firmware version is something I guessed at,
but it was the obvious choice (again, it's byte-based, I'm ignoring
the high byte that is zero for both of my Suuntos).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-10 00:14:21 +00:00
cur_settings . dc . serial_nr = NULL ;
cur_settings . dc . firmware = NULL ;
2012-12-26 21:47:54 +00:00
cur_settings . dc . deviceid = 0 ;
}
2013-01-02 01:29:38 +00:00
static void settings_start ( void )
2012-12-26 21:47:54 +00:00
{
2014-01-15 18:54:41 +00:00
in_settings = true ;
2013-01-02 01:29:38 +00:00
}
static void settings_end ( void )
{
2014-01-15 18:54:41 +00:00
in_settings = false ;
2013-01-02 01:29:38 +00:00
}
static void dc_settings_start ( void )
{
2012-12-26 21:47:54 +00:00
reset_dc_settings ( ) ;
}
static void dc_settings_end ( void )
{
2013-06-17 22:58:26 +00:00
create_device_node ( cur_settings . dc . model , cur_settings . dc . deviceid , cur_settings . dc . serial_nr ,
cur_settings . dc . firmware , cur_settings . dc . nickname ) ;
2012-12-26 21:47:54 +00:00
reset_dc_settings ( ) ;
}
2015-02-12 05:46:02 +00:00
static void dive_site_start ( void )
{
if ( cur_dive_site )
return ;
cur_dive_site = calloc ( 1 , sizeof ( struct dive_site ) ) ;
}
static void dive_site_end ( void )
{
if ( ! cur_dive_site )
return ;
2017-02-19 22:11:37 +00:00
if ( cur_dive_site - > taxonomy . nr = = 0 ) {
free ( cur_dive_site - > taxonomy . category ) ;
cur_dive_site - > taxonomy . category = NULL ;
}
2015-02-12 05:46:02 +00:00
if ( cur_dive_site - > uuid ) {
2017-02-19 22:11:37 +00:00
struct dive_site * ds = alloc_or_get_dive_site ( cur_dive_site - > uuid ) ;
merge_dive_site ( ds , cur_dive_site ) ;
2015-07-01 19:30:33 +00:00
2015-02-13 20:44:42 +00:00
if ( verbose > 3 )
printf ( " completed dive site uuid %x8 name {%s} \n " , ds - > uuid , ds - > name ) ;
2015-02-12 05:46:02 +00:00
}
2015-07-13 14:09:55 +00:00
free_taxonomy ( & cur_dive_site - > taxonomy ) ;
2015-02-12 05:46:02 +00:00
free ( cur_dive_site ) ;
cur_dive_site = NULL ;
}
// now we need to add the code to parse the parts of the divesite enry
2011-08-30 04:32:27 +00:00
static void dive_start ( void )
{
2012-01-05 16:16:08 +00:00
if ( cur_dive )
2011-08-30 22:22:48 +00:00
return ;
2012-01-05 16:16:08 +00:00
cur_dive = alloc_dive ( ) ;
2012-12-19 20:36:56 +00:00
reset_dc_info ( & cur_dive - > dc ) ;
2012-01-05 16:16:08 +00:00
memset ( & cur_tm , 0 , sizeof ( cur_tm ) ) ;
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-09-30 19:36:18 +00:00
if ( cur_trip ) {
2012-11-10 18:51:03 +00:00
add_dive_to_trip ( cur_dive , cur_trip ) ;
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-09-30 19:36:18 +00:00
cur_dive - > tripflag = IN_TRIP ;
}
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
o2pressure_sensor = 1 ;
2011-08-30 04:32:27 +00:00
}
static void dive_end ( void )
{
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-09-30 19:36:18 +00:00
if ( ! cur_dive )
2011-08-30 20:48:05 +00:00
return ;
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-09-30 19:36:18 +00:00
if ( ! is_dive ( ) )
free ( cur_dive ) ;
else
2013-01-31 00:08:36 +00:00
record_dive_to_table ( cur_dive , target_table ) ;
2012-01-05 16:16:08 +00:00
cur_dive = NULL ;
2012-11-25 02:50:21 +00:00
cur_dc = NULL ;
2015-06-10 18:52:18 +00:00
cur_latitude . udeg = 0 ;
cur_longitude . udeg = 0 ;
2012-01-05 16:16:08 +00:00
cur_cylinder_index = 0 ;
2012-03-24 04:07:53 +00:00
cur_ws_index = 0 ;
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
}
2012-08-22 05:04:24 +00:00
static void trip_start ( void )
{
if ( cur_trip )
return ;
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-09-30 19:36:18 +00:00
dive_end ( ) ;
2014-02-10 15:04:37 +00:00
cur_trip = calloc ( 1 , sizeof ( dive_trip_t ) ) ;
2012-08-22 05:04:24 +00:00
memset ( & cur_tm , 0 , sizeof ( cur_tm ) ) ;
}
static void trip_end ( void )
{
if ( ! cur_trip )
return ;
2012-09-05 20:54:22 +00:00
insert_trip ( & cur_trip ) ;
2012-08-22 05:04:24 +00:00
cur_trip = NULL ;
}
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
static void event_start ( void )
{
2012-01-05 16:16:08 +00:00
memset ( & cur_event , 0 , sizeof ( cur_event ) ) ;
2014-08-17 18:26:21 +00:00
cur_event . deleted = 0 ; /* Active */
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
}
static void event_end ( void )
{
2012-11-25 02:50:21 +00:00
struct divecomputer * dc = get_dc ( ) ;
2016-05-31 18:38:47 +00:00
if ( cur_event . type = = 123 ) {
struct picture * pic = alloc_picture ( ) ;
pic - > filename = strdup ( cur_event . name ) ;
/* theoretically this could fail - but we didn't support multi year offsets */
pic - > offset . seconds = cur_event . time . seconds ;
dive_add_picture ( cur_dive , pic ) ;
} else {
struct event * ev ;
/* At some point gas change events did not have any type. Thus we need to add
* one on import , if we encounter the type one missing .
*/
if ( cur_event . type = = 0 & & strcmp ( cur_event . name , " gaschange " ) = = 0 )
cur_event . type = cur_event . value > > 16 > 0 ? SAMPLE_EVENT_GASCHANGE2 : SAMPLE_EVENT_GASCHANGE ;
ev = add_event ( dc , cur_event . time . seconds ,
cur_event . type , cur_event . flags ,
cur_event . value , cur_event . name ) ;
/*
* Older logs might mark the dive to be CCR by having an " SP change " event at time 0 : 00. Better
* to mark them being CCR on import so no need for special treatments elsewhere on the code .
*/
2016-06-14 15:12:57 +00:00
if ( ev & & cur_event . time . seconds = = 0 & & cur_event . type = = SAMPLE_EVENT_PO2 & & cur_event . value & & dc - > divemode = = OC ) {
2016-05-31 18:38:47 +00:00
dc - > divemode = CCR ;
}
2016-01-30 10:57:19 +00:00
2016-05-31 18:38:47 +00:00
if ( ev & & event_is_gaschange ( ev ) ) {
/* See try_to_fill_event() on why the filled-in index is one too big */
ev - > gas . index = cur_event . gas . index - 1 ;
if ( cur_event . gas . mix . o2 . permille | | cur_event . gas . mix . he . permille )
ev - > gas . mix = cur_event . gas . mix ;
2014-06-09 16:21:26 +00:00
}
2012-12-24 01:51:39 +00:00
}
2014-08-17 18:26:21 +00:00
cur_event . deleted = 1 ; /* No longer active */
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
}
2014-06-09 00:42:15 +00:00
static void picture_start ( void )
{
cur_picture = alloc_picture ( ) ;
}
static void picture_end ( void )
{
dive_add_picture ( cur_dive , cur_picture ) ;
cur_picture = NULL ;
}
2011-09-04 03:31:18 +00:00
static void cylinder_start ( void )
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
{
}
2011-09-04 03:31:18 +00:00
static void cylinder_end ( void )
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
{
2012-01-05 16:16:08 +00:00
cur_cylinder_index + + ;
2011-08-30 04:32:27 +00:00
}
2011-12-24 03:41:16 +00:00
static void ws_start ( void )
{
}
static void ws_end ( void )
{
2012-03-24 04:07:53 +00:00
cur_ws_index + + ;
2011-12-24 03:41:16 +00:00
}
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
/*
* By default the sample data does not change unless the
* save - file gives an explicit new value . So we copy the
* data from the previous sample if one exists , and then
* the parsing will update it as necessary .
*
* There are a few exceptions , like the sample pressure :
* missing sample pressure doesn ' t mean " same as last
* time " , but " interpolate " . We clear those ones
* explicitly .
*
* NOTE ! We default sensor use to 0 , 1 respetively for
* the two sensors , but for CCR dives with explicit
* OXYGEN bottles we set the secondary sensor to that .
* Then the primary sensor will be either the first
* or the second cylinder depending on what isn ' t an
* oxygen cylinder .
*/
2011-08-30 04:32:27 +00:00
static void sample_start ( void )
{
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
struct divecomputer * dc = get_dc ( ) ;
struct sample * sample = prepare_sample ( dc ) ;
if ( sample ! = dc - > sample ) {
memcpy ( sample , sample - 1 , sizeof ( struct sample ) ) ;
sample - > pressure [ 0 ] . mbar = 0 ;
sample - > pressure [ 1 ] . mbar = 0 ;
} else {
sample - > sensor [ 0 ] = ! o2pressure_sensor ;
sample - > sensor [ 1 ] = o2pressure_sensor ;
}
cur_sample = sample ;
2015-09-03 20:25:00 +00:00
next_o2_sensor = 0 ;
2011-08-30 04:32:27 +00:00
}
static void sample_end ( void )
{
2012-01-05 16:16:08 +00:00
if ( ! cur_dive )
2011-08-30 20:48:05 +00:00
return ;
2011-08-31 21:36:53 +00:00
2012-11-25 02:50:21 +00:00
finish_sample ( get_dc ( ) ) ;
2012-01-05 16:16:08 +00:00
cur_sample = NULL ;
2011-08-30 04:32:27 +00:00
}
2012-11-25 02:50:21 +00:00
static void divecomputer_start ( void )
{
struct divecomputer * dc ;
/* Start from the previous dive computer */
dc = & cur_dive - > dc ;
while ( dc - > next )
dc = dc - > next ;
/* Did we already fill that in? */
2012-11-25 19:44:27 +00:00
if ( dc - > samples | | dc - > model | | dc - > when ) {
2012-11-25 02:50:21 +00:00
struct divecomputer * newdc = calloc ( 1 , sizeof ( * newdc ) ) ;
if ( newdc ) {
dc - > next = newdc ;
dc = newdc ;
}
}
/* .. this is the one we'll use */
cur_dc = dc ;
2012-12-19 20:36:56 +00:00
reset_dc_info ( dc ) ;
2012-11-25 02:50:21 +00:00
}
static void divecomputer_end ( void )
{
if ( ! cur_dc - > when )
cur_dc - > when = cur_dive - > when ;
cur_dc = NULL ;
}
2014-04-11 06:17:35 +00:00
static void userid_start ( void )
{
in_userid = true ;
2016-08-10 16:27:03 +00:00
//if the xml contains userid, keep saving it.
// don't call the prefs method here as we don't wanna
// actually change the preferences, this is temporary and
// will be reverted when the file finishes.
prefs . save_userid_local = true ;
2014-04-11 06:17:35 +00:00
}
static void userid_stop ( void )
{
in_userid = false ;
}
2015-02-25 09:05:37 +00:00
static bool entry ( const char * name , char * buf )
2011-08-30 04:32:27 +00:00
{
2015-02-13 07:35:52 +00:00
if ( ! strncmp ( name , " version.program " , sizeof ( " version.program " ) - 1 ) | |
2015-02-25 09:05:37 +00:00
! strncmp ( name , " version.divelog " , sizeof ( " version.divelog " ) - 1 ) ) {
2015-02-13 07:35:52 +00:00
last_xml_version = atoi ( buf ) ;
2015-06-20 13:45:12 +00:00
report_datafile_version ( last_xml_version ) ;
2015-02-25 09:05:37 +00:00
}
2014-04-11 06:17:35 +00:00
if ( in_userid ) {
try_to_fill_userid ( name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
2014-04-11 06:17:35 +00:00
}
2012-12-26 21:47:54 +00:00
if ( in_settings ) {
try_to_fill_dc_settings ( name , buf ) ;
2013-01-02 01:29:38 +00:00
try_to_match_autogroup ( name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
2012-12-26 21:47:54 +00:00
}
2015-02-12 05:46:02 +00:00
if ( cur_dive_site ) {
try_to_fill_dive_site ( & cur_dive_site , name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
2015-02-12 05:46:02 +00:00
}
2014-08-17 18:26:21 +00:00
if ( ! cur_event . deleted ) {
2011-09-23 01:02:54 +00:00
try_to_fill_event ( name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
2011-09-23 01:02:54 +00:00
}
2012-01-05 16:16:08 +00:00
if ( cur_sample ) {
try_to_fill_sample ( cur_sample , name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
2011-08-30 20:48:05 +00:00
}
2012-11-25 02:50:21 +00:00
if ( cur_dc ) {
try_to_fill_dc ( cur_dc , name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
2012-11-25 02:50:21 +00:00
}
2012-01-05 16:16:08 +00:00
if ( cur_dive ) {
2012-11-24 02:05:38 +00:00
try_to_fill_dive ( cur_dive , name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
2011-08-30 20:48:05 +00:00
}
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-09-30 19:36:18 +00:00
if ( cur_trip ) {
try_to_fill_trip ( & cur_trip , name , buf ) ;
2015-02-25 09:05:37 +00:00
return true ;
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-09-30 19:36:18 +00:00
}
2015-02-25 09:05:37 +00:00
return true ;
2011-08-30 04:32:27 +00:00
}
2011-08-30 00:51:54 +00:00
static const char * nodename ( xmlNode * node , char * buf , int len )
2011-08-28 23:58:26 +00:00
{
2013-11-02 19:00:16 +00:00
int levels = 2 ;
char * p = buf ;
2015-06-22 04:43:38 +00:00
if ( ! node | | ( node - > type ! = XML_CDATA_SECTION_NODE & & ! node - > name ) ) {
2011-08-30 21:38:39 +00:00
return " root " ;
2014-10-28 09:13:59 +00:00
}
2011-08-30 00:51:54 +00:00
2015-05-28 12:59:08 +00:00
if ( node - > type = = XML_CDATA_SECTION_NODE | | ( node - > parent & & ! strcmp ( ( const char * ) node - > name , " text " ) ) )
2013-11-02 19:00:16 +00:00
node = node - > parent ;
/* Make sure it's always NUL-terminated */
p [ - - len ] = 0 ;
2011-08-30 00:51:54 +00:00
2014-02-16 23:42:56 +00:00
for ( ; ; ) {
2015-05-28 12:59:08 +00:00
const char * name = ( const char * ) node - > name ;
2013-11-02 19:00:16 +00:00
char c ;
while ( ( c = * name + + ) ! = 0 ) {
/* Cheaper 'tolower()' for ASCII */
c = ( c > = ' A ' & & c < = ' Z ' ) ? c - ' A ' + ' a ' : c ;
* p + + = c ;
2011-08-30 00:51:54 +00:00
if ( ! - - len )
return buf ;
}
2013-11-02 19:00:16 +00:00
* p = 0 ;
2011-08-30 00:51:54 +00:00
node = node - > parent ;
if ( ! node | | ! node - > name )
return buf ;
2013-11-02 19:00:16 +00:00
* p + + = ' . ' ;
2011-08-30 00:51:54 +00:00
if ( ! - - len )
return buf ;
2013-11-02 19:00:16 +00:00
if ( ! - - levels )
return buf ;
2011-08-30 00:51:54 +00:00
}
}
2013-11-02 19:00:16 +00:00
# define MAXNAME 32
2011-08-30 00:51:54 +00:00
2015-02-25 09:05:37 +00:00
static bool visit_one_node ( xmlNode * node )
2011-08-30 00:51:54 +00:00
{
2015-05-28 12:59:08 +00:00
xmlChar * content ;
2013-11-28 20:13:02 +00:00
static char buffer [ MAXNAME ] ;
2011-08-30 00:51:54 +00:00
const char * name ;
content = node - > content ;
2012-12-28 16:18:23 +00:00
if ( ! content | | xmlIsBlankNode ( node ) )
2015-02-25 09:05:37 +00:00
return true ;
2011-08-30 00:51:54 +00:00
name = nodename ( node , buffer , sizeof ( buffer ) ) ;
2011-08-28 23:58:26 +00:00
2015-05-28 12:59:08 +00:00
return entry ( name , ( char * ) content ) ;
2011-08-28 23:58:26 +00:00
}
2015-02-25 09:05:37 +00:00
static bool traverse ( xmlNode * root ) ;
2011-09-01 18:22:05 +00:00
2015-02-25 09:05:37 +00:00
static bool traverse_properties ( xmlNode * node )
2011-09-01 18:22:05 +00:00
{
xmlAttr * p ;
2015-02-25 09:05:37 +00:00
bool ret = true ;
2011-09-01 18:22:05 +00:00
for ( p = node - > properties ; p ; p = p - > next )
2015-02-25 09:05:37 +00:00
if ( ( ret = traverse ( p - > children ) ) = = false )
break ;
return ret ;
2011-09-01 18:22:05 +00:00
}
2015-02-25 09:05:37 +00:00
static bool visit ( xmlNode * n )
2011-09-01 18:22:05 +00:00
{
2015-02-25 09:05:37 +00:00
return visit_one_node ( n ) & & traverse_properties ( n ) & & traverse ( n - > children ) ;
2011-09-01 18:22:05 +00:00
}
2011-09-05 21:29:08 +00:00
static void DivingLog_importer ( void )
{
import_source = DIVINGLOG ;
/*
* Diving Log units are really strange .
*
* Temperatures are in C , except in samples ,
* when they are in Fahrenheit . Depths are in
2011-09-11 19:24:57 +00:00
* meters , an dpressure is in PSI in the samples ,
* but in bar when it comes to working pressure .
*
* Crazy f * % ^ morons .
2011-09-05 21:29:08 +00:00
*/
2013-01-11 01:26:10 +00:00
xml_parsing_units = SI_units ;
2011-09-05 21:29:08 +00:00
}
2011-09-07 00:01:28 +00:00
static void uddf_importer ( void )
{
import_source = UDDF ;
2013-01-11 01:26:10 +00:00
xml_parsing_units = SI_units ;
xml_parsing_units . pressure = PASCAL ;
xml_parsing_units . temperature = KELVIN ;
2011-09-07 00:01:28 +00:00
}
2015-06-09 19:50:17 +00:00
static void subsurface_webservice ( void )
{
import_source = SSRF_WS ;
}
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
/*
* I ' m sure this could be done as some fancy DTD rules .
* It ' s just not worth the headache .
*/
static struct nesting {
const char * name ;
void ( * start ) ( void ) , ( * end ) ( void ) ;
} nesting [ ] = {
2014-02-16 23:42:56 +00:00
{ " divecomputerid " , dc_settings_start , dc_settings_end } ,
{ " settings " , settings_start , settings_end } ,
2015-02-12 05:46:02 +00:00
{ " site " , dive_site_start , dive_site_end } ,
2014-02-16 23:42:56 +00:00
{ " dive " , dive_start , dive_end } ,
{ " Dive " , dive_start , dive_end } ,
{ " trip " , trip_start , trip_end } ,
{ " sample " , sample_start , sample_end } ,
{ " waypoint " , sample_start , sample_end } ,
{ " SAMPLE " , sample_start , sample_end } ,
{ " reading " , sample_start , sample_end } ,
{ " event " , event_start , event_end } ,
{ " mix " , cylinder_start , cylinder_end } ,
{ " gasmix " , cylinder_start , cylinder_end } ,
{ " cylinder " , cylinder_start , cylinder_end } ,
{ " weightsystem " , ws_start , ws_end } ,
{ " divecomputer " , divecomputer_start , divecomputer_end } ,
{ " P " , sample_start , sample_end } ,
2014-04-11 06:17:35 +00:00
{ " userid " , userid_start , userid_stop } ,
2014-06-09 00:42:15 +00:00
{ " picture " , picture_start , picture_end } ,
2014-11-06 18:34:19 +00:00
{ " extradata " , extra_data_start , extra_data_end } ,
2014-02-16 23:42:56 +00:00
/* Import type recognition */
{ " Divinglog " , DivingLog_importer } ,
{ " uddf " , uddf_importer } ,
2015-06-09 19:50:17 +00:00
{ " output " , subsurface_webservice } ,
2014-02-16 23:42:56 +00:00
{ NULL , }
} ;
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
2015-02-25 09:05:37 +00:00
static bool traverse ( xmlNode * root )
2011-08-28 23:58:26 +00:00
{
xmlNode * n ;
2015-02-25 09:05:37 +00:00
bool ret = true ;
2011-08-28 23:58:26 +00:00
2011-09-01 18:22:05 +00:00
for ( n = root ; n ; n = n - > next ) {
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
struct nesting * rule = nesting ;
2011-08-30 04:32:27 +00:00
parse-xml: allow XML nodes with empty tag names
They happen for CDATA content, where libxml2 turns the CDATA fields into
a child of the parent entry, but without a name.
Now, of course, any sane person would just want to use the CDATA as the
string value of the parent itself, but libxml2 probably does this
insanity for a reason. And the reason is probably that some misguided
people want to *write* XML using libxml2, and then the stupid child node
actually acts as a "now I want you to write this data as CDATA".
Whatever the reason, let's just ignore it. We will just traverse such a
nameless child and be happy, and we'll give the nameless child the name
of the parent. Our XML node matching logic will then never see this
insane nameless child at all, and doesn't have to care.
Our whole XML parsing rule-of-thumb is to take the whole "be strict in
what you output, but generous in what you accept" to its logical
conclusion. Because we will literally accept almost anything, in any
format. You can mix tags or attributes wildly, and youc an use CDATA or
not as you see fit. We just don't care.
We're the honeybadger of the divelog world.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-29 05:12:32 +00:00
if ( ! n - > name ) {
2015-02-25 09:05:37 +00:00
if ( ( ret = visit ( n ) ) = = false )
break ;
parse-xml: allow XML nodes with empty tag names
They happen for CDATA content, where libxml2 turns the CDATA fields into
a child of the parent entry, but without a name.
Now, of course, any sane person would just want to use the CDATA as the
string value of the parent itself, but libxml2 probably does this
insanity for a reason. And the reason is probably that some misguided
people want to *write* XML using libxml2, and then the stupid child node
actually acts as a "now I want you to write this data as CDATA".
Whatever the reason, let's just ignore it. We will just traverse such a
nameless child and be happy, and we'll give the nameless child the name
of the parent. Our XML node matching logic will then never see this
insane nameless child at all, and doesn't have to care.
Our whole XML parsing rule-of-thumb is to take the whole "be strict in
what you output, but generous in what you accept" to its logical
conclusion. Because we will literally accept almost anything, in any
format. You can mix tags or attributes wildly, and youc an use CDATA or
not as you see fit. We just don't care.
We're the honeybadger of the divelog world.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-29 05:12:32 +00:00
continue ;
}
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
do {
2015-05-28 12:59:08 +00:00
if ( ! strcmp ( rule - > name , ( const char * ) n - > name ) )
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
break ;
rule + + ;
} while ( rule - > name ) ;
2011-08-30 04:32:27 +00:00
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
if ( rule - > start )
rule - > start ( ) ;
2015-02-25 09:05:37 +00:00
if ( ( ret = visit ( n ) ) = = false )
break ;
Start parsing gas mixes
The suunto xml is just completely crazy. What's the helium percentage
companion to "o2pct"? Would it be "hepct"? No. It's "hepct_0".
Ok, so they didn't number the first o2pct, which could be seen as sane:
that's the only mix value that should always exist. And they clearly
started their indexing with 0. So with multiple mixes, you'd then
expect "o2pct_1" and "hepct_1", right?
Wrong! Because XML people are crazy, the second O2 mix percentage is
obviously "o2pct_2". So the O2 percentages are one-based, with an
implicit one. But the He percentages are zero-based with an explicit
zero. So the second mix is "o2pct_2" and "hepct_1".
I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company. No need to ask. Vodka explains everything. LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane, and uses a
'gasmix' node. Of course, now we have so many different XML nesting
nodes to check that I just made it an array of different noces. That
also allows me to mark the suunto case, so that we only do the "check
for crazy alcoholic xml entries" when it's a suunto file.
The "type of file" thing is probably a good idea for deciding on default
units too. Some day.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-01 20:32:52 +00:00
if ( rule - > end )
rule - > end ( ) ;
2011-08-28 23:58:26 +00:00
}
2015-02-25 09:05:37 +00:00
return ret ;
2011-08-28 23:58:26 +00:00
}
2011-09-02 18:32:48 +00:00
/* Per-file reset */
static void reset_all ( void )
{
/*
* We reset the units for each file . You ' d think it was
* a per - dive property , but I ' m not going to trust people
* to do per - dive setup . If the xml does have per - dive
* data within one file , we might have to reset it per
* dive for that format .
*/
2013-01-11 01:26:10 +00:00
xml_parsing_units = SI_units ;
2011-09-05 20:45:14 +00:00
import_source = UNKNOWN ;
2011-09-02 18:32:48 +00:00
}
2013-03-15 17:02:14 +00:00
/* divelog.de sends us xml files that claim to be iso-8859-1
* but once we decode the HTML encoded characters they turn
* into UTF - 8 instead . So skip the incorrect encoding
* declaration and decode the HTML encoded characters */
const char * preprocess_divelog_de ( const char * buffer )
{
char * ret = strstr ( buffer , " <DIVELOGSDATA> " ) ;
if ( ret ) {
xmlParserCtxtPtr ctx ;
char buf [ ] = " " ;
2016-03-10 02:26:59 +00:00
size_t i ;
2013-03-26 19:59:22 +00:00
for ( i = 0 ; i < strlen ( ret ) ; + + i )
if ( ! isascii ( ret [ i ] ) )
return buffer ;
2013-03-15 17:02:14 +00:00
ctx = xmlCreateMemoryParserCtxt ( buf , sizeof ( buf ) ) ;
2015-05-28 12:59:08 +00:00
ret = ( char * ) xmlStringLenDecodeEntities ( ctx , ( xmlChar * ) ret , strlen ( ret ) , XML_SUBSTITUTE_REF , 0 , 0 , 0 ) ;
2013-03-15 17:02:14 +00:00
return ret ;
}
return buffer ;
}
2014-12-08 19:26:03 +00:00
int parse_xml_buffer ( const char * url , const char * buffer , int size ,
2014-03-14 18:26:07 +00:00
struct dive_table * table , const char * * params )
2011-08-28 23:58:26 +00:00
{
2016-03-07 19:40:12 +00:00
( void ) size ;
2011-08-28 23:58:26 +00:00
xmlDoc * doc ;
2013-03-15 17:02:14 +00:00
const char * res = preprocess_divelog_de ( buffer ) ;
2015-02-25 09:05:37 +00:00
int ret = 0 ;
2011-08-28 23:58:26 +00:00
2013-01-31 00:08:36 +00:00
target_table = table ;
2013-03-15 17:02:14 +00:00
doc = xmlReadMemory ( res , strlen ( res ) , url , NULL , 0 ) ;
2017-04-16 15:12:36 +00:00
if ( ! doc )
doc = xmlReadMemory ( res , strlen ( res ) , url , " latin1 " , 0 ) ;
2013-03-17 05:12:23 +00:00
if ( res ! = buffer )
free ( ( char * ) res ) ;
2014-12-08 19:26:03 +00:00
if ( ! doc )
return report_error ( translate ( " gettextFromC " , " Failed to parse '%s' " ) , url ) ;
2016-08-10 16:27:03 +00:00
prefs . save_userid_local = false ;
2011-09-02 18:32:48 +00:00
reset_all ( ) ;
2011-08-30 04:32:27 +00:00
dive_start ( ) ;
2014-03-14 18:26:07 +00:00
doc = test_xslt_transforms ( doc , params ) ;
2015-02-25 09:05:37 +00:00
if ( ! traverse ( xmlDocGetRootElement ( doc ) ) ) {
// we decided to give up on parsing... why?
ret = - 1 ;
}
2011-08-30 04:32:27 +00:00
dive_end ( ) ;
2011-08-28 23:58:26 +00:00
xmlFreeDoc ( doc ) ;
2015-02-25 09:05:37 +00:00
return ret ;
2011-08-28 23:58:26 +00:00
}
2014-05-28 06:55:46 +00:00
void parse_mkvi_buffer ( struct membuffer * txt , struct membuffer * csv , const char * starttime )
{
2016-03-07 19:40:12 +00:00
( void ) csv ;
( void ) txt ;
2014-05-28 06:55:46 +00:00
dive_start ( ) ;
divedate ( starttime , & cur_dive - > when ) ;
dive_end ( ) ;
}
2013-03-05 05:10:39 +00:00
extern int dm4_events ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2013-03-05 05:10:39 +00:00
event_start ( ) ;
2014-01-16 04:50:56 +00:00
if ( data [ 1 ] )
2013-03-05 05:10:39 +00:00
cur_event . time . seconds = atoi ( data [ 1 ] ) ;
2014-01-16 04:50:56 +00:00
if ( data [ 2 ] ) {
2013-03-05 05:10:39 +00:00
switch ( atoi ( data [ 2 ] ) ) {
2014-02-16 23:42:56 +00:00
case 1 :
/* 1 Mandatory Safety Stop */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " safety stop (mandatory) " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 3 :
/* 3 Deco */
/* What is Subsurface's term for going to
2013-03-05 05:10:39 +00:00
* deco ? */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " deco " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 4 :
/* 4 Ascent warning */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " ascent " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 5 :
/* 5 Ceiling broken */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " violation " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 6 :
/* 6 Mandatory safety stop ceiling error */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " violation " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 7 :
/* 7 Below deco floor */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " below floor " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 8 :
/* 8 Dive time alarm */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " divetime " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 9 :
/* 9 Depth alarm */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " maxdepth " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 10 :
/* 10 OLF 80% */
case 11 :
/* 11 OLF 100% */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " OLF " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 12 :
2014-06-22 14:41:44 +00:00
/* 12 High pO₂ */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " PO2 " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 13 :
/* 13 Air time */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " airtime " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 17 :
/* 17 Ascent warning */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " ascent " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 18 :
/* 18 Ceiling error */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " ceiling " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 19 :
/* 19 Surfaced */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " surface " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 20 :
/* 20 Deco */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " deco " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 22 :
2015-02-17 20:28:27 +00:00
case 32 :
2014-02-16 23:42:56 +00:00
/* 22 Mandatory safety stop violation */
2015-02-17 20:28:27 +00:00
/* 32 Deep stop violation */
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " violation " ) ;
2014-02-16 23:42:56 +00:00
break ;
2015-02-17 20:28:27 +00:00
case 30 :
/* Tissue level warning */
strcpy ( cur_event . name , " tissue warning " ) ;
break ;
case 37 :
/* Tank pressure alarm */
strcpy ( cur_event . name , " tank pressure " ) ;
break ;
2014-02-16 23:42:56 +00:00
case 257 :
/* 257 Dive active */
2015-05-19 16:22:36 +00:00
/* This seems to be given after surface when
* descending again . */
strcpy ( cur_event . name , " surface " ) ;
2014-02-16 23:42:56 +00:00
break ;
case 258 :
/* 258 Bookmark */
if ( data [ 3 ] ) {
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " heading " ) ;
2014-02-16 23:42:56 +00:00
cur_event . value = atoi ( data [ 3 ] ) ;
} else {
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " bookmark " ) ;
2014-02-16 23:42:56 +00:00
}
break ;
2015-02-17 20:28:27 +00:00
case 259 :
/* Deep stop */
strcpy ( cur_event . name , " Deep stop " ) ;
break ;
case 260 :
/* Deep stop */
strcpy ( cur_event . name , " Deep stop cleared " ) ;
break ;
case 266 :
/* Mandatory safety stop activated */
strcpy ( cur_event . name , " safety stop (mandatory) " ) ;
break ;
case 267 :
/* Mandatory safety stop deactivated */
/* DM5 shows this only on event list, not on the
* profile so skipping as well for now */
break ;
2014-02-16 23:42:56 +00:00
default :
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " unknown " ) ;
2014-02-16 23:42:56 +00:00
cur_event . value = atoi ( data [ 2 ] ) ;
break ;
2013-03-05 05:10:39 +00:00
}
}
event_end ( ) ;
return 0 ;
}
2015-02-19 21:02:31 +00:00
extern int dm5_cylinders ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2015-02-19 21:02:31 +00:00
cylinder_start ( ) ;
if ( data [ 7 ] & & atoi ( data [ 7 ] ) > 0 & & atoi ( data [ 7 ] ) < 350000 )
cur_dive - > cylinder [ cur_cylinder_index ] . start . mbar = atoi ( data [ 7 ] ) ;
if ( data [ 8 ] & & atoi ( data [ 8 ] ) > 0 & & atoi ( data [ 8 ] ) < 350000 )
cur_dive - > cylinder [ cur_cylinder_index ] . end . mbar = ( atoi ( data [ 8 ] ) ) ;
2015-02-20 03:47:06 +00:00
if ( data [ 6 ] ) {
2015-02-19 21:02:32 +00:00
/* DM5 shows tank size of 12 liters when the actual
* value is 0 ( and using metric units ) . So we just use
* the same 12 liters when size is not available */
if ( atof ( data [ 6 ] ) = = 0.0 & & cur_dive - > cylinder [ cur_cylinder_index ] . start . mbar )
cur_dive - > cylinder [ cur_cylinder_index ] . type . size . mliter = 12000 ;
else
2017-03-09 16:07:30 +00:00
cur_dive - > cylinder [ cur_cylinder_index ] . type . size . mliter = lrint ( ( atof ( data [ 6 ] ) ) * 1000 ) ;
2015-02-20 03:47:06 +00:00
}
2015-02-19 21:02:31 +00:00
if ( data [ 2 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 . permille = atoi ( data [ 2 ] ) * 10 ;
if ( data [ 3 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . he . permille = atoi ( data [ 3 ] ) * 10 ;
cylinder_end ( ) ;
2015-02-20 03:51:12 +00:00
return 0 ;
2015-02-19 21:02:31 +00:00
}
2015-02-21 11:15:03 +00:00
extern int dm5_gaschange ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2015-02-21 11:15:03 +00:00
event_start ( ) ;
if ( data [ 0 ] )
cur_event . time . seconds = atoi ( data [ 0 ] ) ;
if ( data [ 1 ] ) {
strcpy ( cur_event . name , " gaschange " ) ;
2017-03-09 16:07:30 +00:00
cur_event . value = lrint ( atof ( data [ 1 ] ) ) ;
2015-02-21 11:15:03 +00:00
}
2017-06-13 08:25:14 +00:00
/* He part of the mix */
if ( data [ 2 ] )
cur_event . value + = lrint ( atof ( data [ 2 ] ) ) < < 16 ;
2015-02-21 11:15:03 +00:00
event_end ( ) ;
return 0 ;
}
2015-02-19 21:02:31 +00:00
2014-01-09 21:14:50 +00:00
extern int dm4_tags ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2014-01-16 04:50:56 +00:00
if ( data [ 0 ] )
Get rid of crazy empty tag_list element at the start
So this is totally unrelated to the git repository format, except for
the fact that I noticed it while writing the git saving code.
The subsurface divetag list handling is being stupid, and has a
initial dummy entry at the head of the list for no good reason.
I say "no good reason", because there *is* a reason for it: it allows
code to avoid the special case of empty list and adding entries to
before the first entry etc etc. But that reason is a really *bad*
reason, because it's valid only because people don't understand basic
list manipulation and pointers to pointers.
So get rid of the dummy element, and do things right instead - by
passing a *pointer* to the list, instead of the list. And then when
traversing the list and looking for a place to insert things, don't go
to the next entry - just update the "pointer to pointer" to point to
the address of the next entry. Each entry in a C linked list is no
different than the list itself, so you can use the pointer to the
pointer to the next entry as a pointer to the list.
This is a pet peeve of mine. The real beauty of pointers can never be
understood unless you understand the indirection they allow. People
who grew up with Pascal and were corrupted by that mindset are
mentally stunted. Niklaus Wirth has a lot to answer for!
But never fear. You too can overcome that mental limitation, it just
needs some brain exercise. Reading this patch may help. In particular,
contemplate the new "taglist_add_divetag()".
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-10 17:18:13 +00:00
taglist_add_tag ( & cur_dive - > tag_list , data [ 0 ] ) ;
2014-01-09 21:14:50 +00:00
return 0 ;
}
2013-03-05 05:10:39 +00:00
extern int dm4_dive ( void * param , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) columns ;
( void ) column ;
2016-03-10 02:26:59 +00:00
unsigned int i ;
int interval , retval = 0 ;
2013-03-05 05:10:39 +00:00
sqlite3 * handle = ( sqlite3 * ) param ;
float * profileBlob ;
unsigned char * tempBlob ;
int * pressureBlob ;
char * err = NULL ;
char get_events_template [ ] = " select * from Mark where DiveId = %d " ;
2014-01-09 21:14:50 +00:00
char get_tags_template [ ] = " select Text from DiveTag where DiveId = %d " ;
2013-03-05 05:10:39 +00:00
char get_events [ 64 ] ;
dive_start ( ) ;
cur_dive - > number = atoi ( data [ 0 ] ) ;
2013-05-15 05:07:41 +00:00
cur_dive - > when = ( time_t ) ( atol ( data [ 1 ] ) ) ;
2013-03-05 05:10:39 +00:00
if ( data [ 2 ] )
utf8_string ( data [ 2 ] , & cur_dive - > notes ) ;
/*
* DM4 stores Duration and DiveTime . It looks like DiveTime is
* 10 to 60 seconds shorter than Duration . However , I have no
* idea what is the difference and which one should be used .
* Duration = data [ 3 ]
* DiveTime = data [ 15 ]
*/
2013-05-15 01:34:54 +00:00
if ( data [ 3 ] )
cur_dive - > duration . seconds = atoi ( data [ 3 ] ) ;
2013-03-05 05:10:39 +00:00
if ( data [ 15 ] )
2013-05-15 01:34:54 +00:00
cur_dive - > dc . duration . seconds = atoi ( data [ 15 ] ) ;
2013-03-05 05:10:39 +00:00
/*
* TODO : the deviceid hash should be calculated here .
*/
settings_start ( ) ;
dc_settings_start ( ) ;
if ( data [ 4 ] )
utf8_string ( data [ 4 ] , & cur_settings . dc . serial_nr ) ;
if ( data [ 5 ] )
utf8_string ( data [ 5 ] , & cur_settings . dc . model ) ;
cur_settings . dc . deviceid = 0xffffffff ;
dc_settings_end ( ) ;
settings_end ( ) ;
if ( data [ 6 ] )
2017-03-09 16:07:30 +00:00
cur_dive - > dc . maxdepth . mm = lrint ( atof ( data [ 6 ] ) * 1000 ) ;
2013-03-05 05:10:39 +00:00
if ( data [ 8 ] )
2013-11-26 22:11:30 +00:00
cur_dive - > dc . airtemp . mkelvin = C_to_mkelvin ( atoi ( data [ 8 ] ) ) ;
2013-03-05 05:10:39 +00:00
if ( data [ 9 ] )
2014-02-16 23:42:56 +00:00
cur_dive - > dc . watertemp . mkelvin = C_to_mkelvin ( atoi ( data [ 9 ] ) ) ;
2013-03-05 05:10:39 +00:00
/*
* TODO : handle multiple cylinders
*/
cylinder_start ( ) ;
2013-03-08 19:44:10 +00:00
if ( data [ 22 ] & & atoi ( data [ 22 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . start . mbar = atoi ( data [ 22 ] ) ;
else if ( data [ 10 ] & & atoi ( data [ 10 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . start . mbar = atoi ( data [ 10 ] ) ;
if ( data [ 23 ] & & atoi ( data [ 23 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . end . mbar = ( atoi ( data [ 23 ] ) ) ;
if ( data [ 11 ] & & atoi ( data [ 11 ] ) > 0 )
2013-03-05 05:10:39 +00:00
cur_dive - > cylinder [ cur_cylinder_index ] . end . mbar = ( atoi ( data [ 11 ] ) ) ;
if ( data [ 12 ] )
2017-03-09 16:07:30 +00:00
cur_dive - > cylinder [ cur_cylinder_index ] . type . size . mliter = lrint ( ( atof ( data [ 12 ] ) ) * 1000 ) ;
2013-03-05 05:10:39 +00:00
if ( data [ 13 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . type . workingpressure . mbar = ( atoi ( data [ 13 ] ) ) ;
if ( data [ 20 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 . permille = atoi ( data [ 20 ] ) * 10 ;
if ( data [ 21 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . he . permille = atoi ( data [ 21 ] ) * 10 ;
cylinder_end ( ) ;
if ( data [ 14 ] )
2013-05-15 01:34:54 +00:00
cur_dive - > dc . surface_pressure . mbar = ( atoi ( data [ 14 ] ) * 1000 ) ;
2013-03-05 05:10:39 +00:00
interval = data [ 16 ] ? atoi ( data [ 16 ] ) : 0 ;
profileBlob = ( float * ) data [ 17 ] ;
tempBlob = ( unsigned char * ) data [ 18 ] ;
pressureBlob = ( int * ) data [ 19 ] ;
2014-02-16 23:42:56 +00:00
for ( i = 0 ; interval & & i * interval < cur_dive - > duration . seconds ; i + + ) {
2013-03-05 05:10:39 +00:00
sample_start ( ) ;
cur_sample - > time . seconds = i * interval ;
if ( profileBlob )
2017-03-09 16:07:30 +00:00
cur_sample - > depth . mm = lrintf ( profileBlob [ i ] * 1000.0f ) ;
2013-03-05 05:10:39 +00:00
else
2013-05-15 01:34:54 +00:00
cur_sample - > depth . mm = cur_dive - > dc . maxdepth . mm ;
2013-03-05 05:10:39 +00:00
2014-01-09 21:14:49 +00:00
if ( data [ 18 ] & & data [ 18 ] [ 0 ] )
2013-11-26 22:11:30 +00:00
cur_sample - > temperature . mkelvin = C_to_mkelvin ( tempBlob [ i ] ) ;
2013-03-07 04:40:28 +00:00
if ( data [ 19 ] & & data [ 19 ] [ 0 ] )
2017-07-20 21:39:02 +00:00
cur_sample - > pressure [ 0 ] . mbar = pressureBlob [ i ] ;
2013-03-05 05:10:39 +00:00
sample_end ( ) ;
}
snprintf ( get_events , sizeof ( get_events ) - 1 , get_events_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_events , & dm4_events , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query dm4_events failed. \n " ) ;
2013-03-05 05:10:39 +00:00
return 1 ;
}
2014-01-09 21:14:50 +00:00
snprintf ( get_events , sizeof ( get_events ) - 1 , get_tags_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_events , & dm4_tags , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query dm4_tags failed. \n " ) ;
2014-01-09 21:14:50 +00:00
return 1 ;
}
2013-03-05 05:10:39 +00:00
dive_end ( ) ;
/*
for ( i = 0 ; i < columns ; + + i ) {
fprintf ( stderr , " %s \t " , column [ i ] ) ;
}
fprintf ( stderr , " \n " ) ;
for ( i = 0 ; i < columns ; + + i ) {
fprintf ( stderr , " %s \t " , data [ i ] ) ;
}
fprintf ( stderr , " \n " ) ;
//exit(0);
*/
return SQLITE_OK ;
}
2014-11-15 15:34:20 +00:00
extern int dm5_dive ( void * param , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) columns ;
( void ) column ;
2016-03-10 02:26:59 +00:00
unsigned int i ;
int interval , retval = 0 , block_size ;
2014-11-15 15:34:20 +00:00
sqlite3 * handle = ( sqlite3 * ) param ;
unsigned const char * sampleBlob ;
char * err = NULL ;
char get_events_template [ ] = " select * from Mark where DiveId = %d " ;
char get_tags_template [ ] = " select Text from DiveTag where DiveId = %d " ;
2015-02-19 21:02:31 +00:00
char get_cylinders_template [ ] = " select * from DiveMixture where DiveId = %d " ;
2015-02-21 11:15:03 +00:00
char get_gaschange_template [ ] = " select GasChangeTime,Oxygen,Helium from DiveGasChange join DiveMixture on DiveGasChange.DiveMixtureId=DiveMixture.DiveMixtureId where DiveId = %d " ;
char get_events [ 512 ] ;
2014-11-15 15:34:20 +00:00
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 - > notes ) ;
if ( data [ 3 ] )
cur_dive - > duration . seconds = atoi ( data [ 3 ] ) ;
if ( data [ 15 ] )
cur_dive - > dc . duration . seconds = atoi ( data [ 15 ] ) ;
/*
* TODO : the deviceid hash should be calculated here .
*/
settings_start ( ) ;
dc_settings_start ( ) ;
2015-02-21 18:55:06 +00:00
if ( data [ 4 ] ) {
2014-11-15 15:34:20 +00:00
utf8_string ( data [ 4 ] , & cur_settings . dc . serial_nr ) ;
2015-02-21 18:55:06 +00:00
cur_settings . dc . deviceid = atoi ( data [ 4 ] ) ;
}
2014-11-15 15:34:20 +00:00
if ( data [ 5 ] )
utf8_string ( data [ 5 ] , & cur_settings . dc . model ) ;
dc_settings_end ( ) ;
settings_end ( ) ;
if ( data [ 6 ] )
2017-03-09 16:07:30 +00:00
cur_dive - > dc . maxdepth . mm = lrint ( atof ( data [ 6 ] ) * 1000 ) ;
2014-11-15 15:34:20 +00:00
if ( data [ 8 ] )
cur_dive - > dc . airtemp . mkelvin = C_to_mkelvin ( atoi ( data [ 8 ] ) ) ;
if ( data [ 9 ] )
cur_dive - > dc . watertemp . mkelvin = C_to_mkelvin ( atoi ( data [ 9 ] ) ) ;
2015-02-21 18:55:06 +00:00
if ( data [ 4 ] ) {
cur_dive - > dc . deviceid = atoi ( data [ 4 ] ) ;
}
if ( data [ 5 ] )
utf8_string ( data [ 5 ] , & cur_dive - > dc . model ) ;
2015-02-19 21:02:31 +00:00
snprintf ( get_events , sizeof ( get_events ) - 1 , get_cylinders_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_events , & dm5_cylinders , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query dm5_cylinders failed. \n " ) ;
2015-02-19 21:02:31 +00:00
return 1 ;
}
2014-11-15 15:34:20 +00:00
if ( data [ 14 ] )
2015-02-20 18:35:44 +00:00
cur_dive - > dc . surface_pressure . mbar = ( atoi ( data [ 14 ] ) / 100 ) ;
2014-11-15 15:34:20 +00:00
interval = data [ 16 ] ? atoi ( data [ 16 ] ) : 0 ;
2014-12-04 16:53:05 +00:00
sampleBlob = ( unsigned const char * ) data [ 24 ] ;
2015-06-02 03:33:51 +00:00
if ( sampleBlob ) {
switch ( sampleBlob [ 0 ] ) {
case 2 :
block_size = 19 ;
break ;
case 3 :
block_size = 23 ;
break ;
2017-04-20 14:36:49 +00:00
case 4 :
block_size = 26 ;
break ;
2015-06-02 03:33:51 +00:00
default :
block_size = 16 ;
break ;
}
}
2014-12-03 18:47:40 +00:00
for ( i = 0 ; interval & & sampleBlob & & i * interval < cur_dive - > duration . seconds ; i + + ) {
2015-06-02 03:33:51 +00:00
float * depth = ( float * ) & sampleBlob [ i * block_size + 3 ] ;
int32_t temp = ( sampleBlob [ i * block_size + 10 ] < < 8 ) + sampleBlob [ i * block_size + 11 ] ;
2015-08-04 14:49:17 +00:00
int32_t pressure = ( sampleBlob [ i * block_size + 9 ] < < 16 ) + ( sampleBlob [ i * block_size + 8 ] < < 8 ) + sampleBlob [ i * block_size + 7 ] ;
2014-11-15 15:34:20 +00:00
sample_start ( ) ;
cur_sample - > time . seconds = i * interval ;
2017-03-09 16:07:30 +00:00
cur_sample - > depth . mm = lrintf ( depth [ 0 ] * 1000.0f ) ;
2014-11-16 07:48:17 +00:00
/*
* Limit temperatures and cylinder pressures to somewhat
* sensible values
*/
if ( temp > = - 10 & & temp < 50 )
cur_sample - > temperature . mkelvin = C_to_mkelvin ( temp ) ;
if ( pressure > = 0 & & pressure < 350000 )
2017-07-20 21:39:02 +00:00
cur_sample - > pressure [ 0 ] . mbar = pressure ;
2014-11-15 15:34:20 +00:00
sample_end ( ) ;
}
2014-12-03 18:47:41 +00:00
/*
* Log was converted from DM4 , thus we need to parse the profile
* from DM4 format
*/
if ( i = = 0 ) {
float * profileBlob ;
unsigned char * tempBlob ;
int * pressureBlob ;
profileBlob = ( float * ) data [ 17 ] ;
tempBlob = ( unsigned char * ) data [ 18 ] ;
pressureBlob = ( int * ) data [ 19 ] ;
for ( i = 0 ; interval & & i * interval < cur_dive - > duration . seconds ; i + + ) {
sample_start ( ) ;
cur_sample - > time . seconds = i * interval ;
if ( profileBlob )
2017-03-09 16:07:30 +00:00
cur_sample - > depth . mm = lrintf ( profileBlob [ i ] * 1000.0f ) ;
2014-12-03 18:47:41 +00:00
else
cur_sample - > depth . mm = cur_dive - > dc . maxdepth . mm ;
if ( data [ 18 ] & & data [ 18 ] [ 0 ] )
cur_sample - > temperature . mkelvin = C_to_mkelvin ( tempBlob [ i ] ) ;
if ( data [ 19 ] & & data [ 19 ] [ 0 ] )
2017-07-20 21:39:02 +00:00
cur_sample - > pressure [ 0 ] . mbar = pressureBlob [ i ] ;
2014-12-03 18:47:41 +00:00
sample_end ( ) ;
}
}
2015-02-21 11:15:03 +00:00
snprintf ( get_events , sizeof ( get_events ) - 1 , get_gaschange_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_events , & dm5_gaschange , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query dm5_gaschange failed. \n " ) ;
2015-02-21 11:15:03 +00:00
return 1 ;
}
2014-11-15 15:34:20 +00:00
snprintf ( get_events , sizeof ( get_events ) - 1 , get_events_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_events , & dm4_events , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query dm4_events failed. \n " ) ;
2014-11-15 15:34:20 +00:00
return 1 ;
}
snprintf ( get_events , sizeof ( get_events ) - 1 , get_tags_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_events , & dm4_tags , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query dm4_tags failed. \n " ) ;
2014-11-15 15:34:20 +00:00
return 1 ;
}
dive_end ( ) ;
return SQLITE_OK ;
}
2014-02-15 06:36:50 +00:00
int parse_dm4_buffer ( sqlite3 * handle , const char * url , const char * buffer , int size ,
2014-03-14 18:26:07 +00:00
struct dive_table * table )
2013-03-05 05:10:39 +00:00
{
2016-03-07 19:40:12 +00:00
( void ) buffer ;
( void ) size ;
2013-03-05 05:10:39 +00:00
int retval ;
char * err = NULL ;
target_table = table ;
2013-05-09 17:57:39 +00:00
/* StartTime is converted from Suunto's nano seconds to standard
* time . We also need epoch , not seconds since year 1. */
2013-05-15 05:07:41 +00:00
char get_dives [ ] = " select D.DiveId,StartTime/10000000-62135596800,Note,Duration,SourceSerialNumber,Source,MaxDepth,SampleInterval,StartTemperature,BottomTemperature,D.StartPressure,D.EndPressure,Size,CylinderWorkPressure,SurfacePressure,DiveTime,SampleInterval,ProfileBlob,TemperatureBlob,PressureBlob,Oxygen,Helium,MIX.StartPressure,MIX.EndPressure FROM Dive AS D JOIN DiveMixture AS MIX ON D.DiveId=MIX.DiveId " ;
2013-03-05 05:10:39 +00:00
retval = sqlite3_exec ( handle , get_dives , & dm4_dive , handle , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " Database query failed '%s'. \n " , url ) ;
2013-03-05 05:10:39 +00:00
return 1 ;
}
return 0 ;
}
2014-11-15 15:34:20 +00:00
int parse_dm5_buffer ( sqlite3 * handle , const char * url , const char * buffer , int size ,
struct dive_table * table )
{
2016-03-07 19:40:12 +00:00
( void ) buffer ;
( void ) size ;
2014-11-15 15:34:20 +00:00
int retval ;
char * err = NULL ;
target_table = table ;
/* StartTime is converted from Suunto's nano seconds to standard
* time . We also need epoch , not seconds since year 1. */
2015-02-22 06:20:20 +00:00
char get_dives [ ] = " select DiveId,StartTime/10000000-62135596800,Note,Duration,coalesce(SourceSerialNumber,SerialNumber),Source,MaxDepth,SampleInterval,StartTemperature,BottomTemperature,StartPressure,EndPressure,'','',SurfacePressure,DiveTime,SampleInterval,ProfileBlob,TemperatureBlob,PressureBlob,'','','','',SampleBlob FROM Dive where Deleted is null " ;
2014-11-15 15:34:20 +00:00
retval = sqlite3_exec ( handle , get_dives , & dm5_dive , handle , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " Database query failed '%s'. \n " , url ) ;
2014-11-15 15:34:20 +00:00
return 1 ;
}
return 0 ;
}
2014-02-15 06:36:51 +00:00
extern int shearwater_cylinders ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2014-02-15 06:36:51 +00:00
cylinder_start ( ) ;
if ( data [ 0 ] )
2017-03-09 16:07:30 +00:00
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 . permille = lrint ( atof ( data [ 0 ] ) * 1000 ) ;
2014-02-15 06:36:51 +00:00
if ( data [ 1 ] )
2017-03-09 16:07:30 +00:00
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . he . permille = lrint ( atof ( data [ 1 ] ) * 1000 ) ;
2014-02-15 06:36:51 +00:00
cylinder_end ( ) ;
return 0 ;
}
extern int shearwater_changes ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2014-02-15 06:36:51 +00:00
event_start ( ) ;
if ( data [ 0 ] )
cur_event . time . seconds = atoi ( data [ 0 ] ) ;
if ( data [ 1 ] ) {
2014-08-17 18:26:21 +00:00
strcpy ( cur_event . name , " gaschange " ) ;
2017-03-09 16:07:30 +00:00
cur_event . value = lrint ( atof ( data [ 1 ] ) * 100 ) ;
2014-02-15 06:36:51 +00:00
}
event_end ( ) ;
return 0 ;
}
2014-12-20 16:19:43 +00:00
extern int cobalt_profile_sample ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2014-12-20 16:19:43 +00:00
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 ;
}
2014-02-15 06:36:50 +00:00
extern int shearwater_profile_sample ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2014-02-15 06:36:50 +00:00
sample_start ( ) ;
if ( data [ 0 ] )
cur_sample - > time . seconds = atoi ( data [ 0 ] ) ;
if ( data [ 1 ] )
2017-03-09 16:07:30 +00:00
cur_sample - > depth . mm = metric ? lrint ( atof ( data [ 1 ] ) * 1000 ) : feet_to_mm ( atof ( data [ 1 ] ) ) ;
2014-02-15 06:36:50 +00:00
if ( data [ 2 ] )
cur_sample - > temperature . mkelvin = metric ? C_to_mkelvin ( atof ( data [ 2 ] ) ) : F_to_mkelvin ( atof ( data [ 2 ] ) ) ;
2014-10-22 09:27:10 +00:00
if ( data [ 3 ] ) {
2017-03-09 16:07:30 +00:00
cur_sample - > setpoint . mbar = lrint ( atof ( data [ 3 ] ) * 1000 ) ;
2015-01-10 23:01:15 +00:00
cur_dive - > dc . divemode = CCR ;
2014-10-22 09:27:10 +00:00
}
2014-02-15 06:36:51 +00:00
if ( data [ 4 ] )
cur_sample - > ndl . seconds = atoi ( data [ 4 ] ) * 60 ;
if ( data [ 5 ] )
cur_sample - > cns = atoi ( data [ 5 ] ) ;
if ( data [ 6 ] )
2014-02-16 23:42:56 +00:00
cur_sample - > stopdepth . mm = metric ? atoi ( data [ 6 ] ) * 1000 : feet_to_mm ( atoi ( data [ 6 ] ) ) ;
2014-02-15 06:36:50 +00:00
/* We don't actually have data[3], but it should appear in the
* SQL query at some point .
if ( data [ 3 ] )
2017-07-20 21:39:02 +00:00
cur_sample - > pressure [ 0 ] . mbar = metric ? atoi ( data [ 3 ] ) * 1000 : psi_to_mbar ( atoi ( data [ 3 ] ) ) ;
2014-02-15 06:36:50 +00:00
*/
sample_end ( ) ;
return 0 ;
}
2017-08-22 18:01:06 +00:00
extern int shearwater_ai_profile_sample ( void * handle , int columns , char * * data , char * * column )
{
( void ) handle ;
( void ) columns ;
( void ) column ;
sample_start ( ) ;
if ( data [ 0 ] )
cur_sample - > time . seconds = atoi ( data [ 0 ] ) ;
if ( data [ 1 ] )
cur_sample - > depth . mm = metric ? lrint ( atof ( data [ 1 ] ) * 1000 ) : feet_to_mm ( atof ( data [ 1 ] ) ) ;
if ( data [ 2 ] )
cur_sample - > temperature . mkelvin = metric ? C_to_mkelvin ( atof ( data [ 2 ] ) ) : F_to_mkelvin ( atof ( data [ 2 ] ) ) ;
if ( data [ 3 ] ) {
cur_sample - > setpoint . mbar = lrint ( atof ( data [ 3 ] ) * 1000 ) ;
cur_dive - > dc . divemode = CCR ;
}
if ( data [ 4 ] )
cur_sample - > ndl . seconds = atoi ( data [ 4 ] ) * 60 ;
if ( data [ 5 ] )
cur_sample - > cns = atoi ( data [ 5 ] ) ;
if ( data [ 6 ] )
cur_sample - > stopdepth . mm = metric ? atoi ( data [ 6 ] ) * 1000 : feet_to_mm ( atoi ( data [ 6 ] ) ) ;
/* Weird unit conversion but seems to produce correct results */
if ( data [ 7 ] ) {
cur_sample - > pressure [ 0 ] . mbar = psi_to_mbar ( atoi ( data [ 7 ] ) ) * 2 ;
}
if ( data [ 8 ] )
cur_sample - > pressure [ 1 ] . mbar = psi_to_mbar ( atoi ( data [ 8 ] ) ) * 2 ;
sample_end ( ) ;
return 0 ;
}
2014-02-15 06:36:50 +00:00
extern int shearwater_dive ( void * param , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) columns ;
( void ) column ;
2014-02-16 01:45:22 +00:00
int retval = 0 ;
2014-02-15 06:36:50 +00:00
sqlite3 * handle = ( sqlite3 * ) param ;
char * err = NULL ;
2017-08-22 15:37:39 +00:00
char get_profile_template [ ] = " select currentTime,currentDepth,waterTemp,averagePPO2,currentNdl,CNSPercent,decoCeiling from dive_log_records AS r join dive_logs as l on r.diveLogId=l.diveId where diveLogId = %d " ;
2017-08-22 18:01:06 +00:00
char get_profile_template_ai [ ] = " select currentTime,currentDepth,waterTemp,averagePPO2,currentNdl,CNSPercent,decoCeiling,aiSensor0_PressurePSI,aiSensor1_PressurePSI from dive_log_records AS r join dive_logs as l on r.diveLogId=l.diveId where number = %d " ;
2014-02-15 06:36:51 +00:00
char get_cylinder_template [ ] = " select fractionO2,fractionHe from dive_log_records where diveLogId = %d group by fractionO2,fractionHe " ;
char get_changes_template [ ] = " select a.currentTime,a.fractionO2,a.fractionHe from dive_log_records as a,dive_log_records as b where a.diveLogId = %d and b.diveLogId = %d and (a.id - 1) = b.id and (a.fractionO2 != b.fractionO2 or a.fractionHe != b.fractionHe) union select min(currentTime),fractionO2,fractionHe from dive_log_records " ;
char get_buffer [ 1024 ] ;
2014-02-15 06:36:50 +00:00
dive_start ( ) ;
cur_dive - > number = atoi ( data [ 0 ] ) ;
cur_dive - > when = ( time_t ) ( atol ( data [ 1 ] ) ) ;
if ( data [ 2 ] )
2015-02-12 05:46:02 +00:00
add_dive_site ( data [ 2 ] , cur_dive ) ;
2014-02-15 06:36:50 +00:00
if ( data [ 3 ] )
utf8_string ( data [ 3 ] , & cur_dive - > buddy ) ;
if ( data [ 4 ] )
utf8_string ( data [ 4 ] , & cur_dive - > notes ) ;
metric = atoi ( data [ 5 ] ) = = 1 ? 0 : 1 ;
/* TODO: verify that metric calculation is correct */
if ( data [ 6 ] )
2017-03-09 16:07:30 +00:00
cur_dive - > dc . maxdepth . mm = metric ? lrint ( atof ( data [ 6 ] ) * 1000 ) : feet_to_mm ( atof ( data [ 6 ] ) ) ;
2014-02-15 06:36:50 +00:00
if ( data [ 7 ] )
cur_dive - > dc . duration . seconds = atoi ( data [ 7 ] ) * 60 ;
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 ) ;
2016-05-27 19:50:58 +00:00
if ( data [ 10 ] ) {
switch ( atoi ( data [ 10 ] ) ) {
2016-05-28 06:37:40 +00:00
case 2 :
2017-08-22 18:21:09 +00:00
cur_settings . dc . model = strdup ( " Shearwater Petrel/Perdix " ) ;
2016-05-28 06:37:40 +00:00
break ;
case 4 :
cur_settings . dc . model = strdup ( " Shearwater Predator " ) ;
break ;
2016-05-27 19:50:58 +00:00
default :
cur_settings . dc . model = strdup ( " Shearwater import " ) ;
break ;
}
}
2014-02-15 06:36:50 +00:00
2016-05-27 19:50:57 +00:00
cur_settings . dc . deviceid = atoi ( data [ 9 ] ) ;
2014-02-15 06:36:50 +00:00
dc_settings_end ( ) ;
settings_end ( ) ;
2016-05-27 19:50:58 +00:00
if ( data [ 10 ] ) {
switch ( atoi ( data [ 10 ] ) ) {
2016-05-28 06:37:40 +00:00
case 2 :
2017-08-22 18:21:09 +00:00
cur_dive - > dc . model = strdup ( " Shearwater Petrel/Perdix " ) ;
2016-05-28 06:37:40 +00:00
break ;
case 4 :
cur_dive - > dc . model = strdup ( " Shearwater Predator " ) ;
break ;
2016-05-27 19:50:58 +00:00
default :
cur_dive - > dc . model = strdup ( " Shearwater import " ) ;
break ;
}
}
2014-02-15 06:36:51 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_cylinder_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & shearwater_cylinders , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query shearwater_cylinders failed. \n " ) ;
2014-02-15 06:36:51 +00:00
return 1 ;
}
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_changes_template , cur_dive - > number , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & shearwater_changes , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query shearwater_changes failed. \n " ) ;
2014-02-15 06:36:51 +00:00
return 1 ;
}
2017-08-22 18:01:06 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_profile_template_ai , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & shearwater_ai_profile_sample , 0 , & err ) ;
2014-02-15 06:36:50 +00:00
if ( retval ! = SQLITE_OK ) {
2017-08-22 18:01:06 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_profile_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & shearwater_profile_sample , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
fprintf ( stderr , " %s " , " Database query shearwater_profile_sample failed. \n " ) ;
return 1 ;
}
2014-02-15 06:36:50 +00:00
}
dive_end ( ) ;
return SQLITE_OK ;
}
2014-12-20 16:19:44 +00:00
extern int cobalt_cylinders ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2014-12-20 16:19:44 +00:00
cylinder_start ( ) ;
if ( data [ 0 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 . permille = atoi ( data [ 0 ] ) * 10 ;
if ( data [ 1 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . he . permille = atoi ( data [ 1 ] ) * 10 ;
if ( data [ 2 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . start . mbar = psi_to_mbar ( atoi ( data [ 2 ] ) ) ;
if ( data [ 3 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . end . mbar = psi_to_mbar ( atoi ( data [ 3 ] ) ) ;
if ( data [ 4 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . type . size . mliter = atoi ( data [ 4 ] ) * 100 ;
if ( data [ 5 ] )
cur_dive - > cylinder [ cur_cylinder_index ] . gas_used . mliter = atoi ( data [ 5 ] ) * 1000 ;
cylinder_end ( ) ;
return 0 ;
}
2014-12-20 16:19:46 +00:00
extern int cobalt_buddies ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2014-12-20 16:19:46 +00:00
if ( data [ 0 ] )
utf8_string ( data [ 0 ] , & cur_dive - > buddy ) ;
return 0 ;
}
2014-12-20 16:19:47 +00:00
/*
* We still need to figure out how to map free text visibility to
* Subsurface star rating .
*/
extern int cobalt_visibility ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
( void ) data ;
2014-12-20 16:19:47 +00:00
return 0 ;
}
2014-12-20 16:19:49 +00:00
extern int cobalt_location ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2015-02-12 05:46:02 +00:00
static char * location = NULL ;
2014-12-20 16:19:49 +00:00
if ( data [ 0 ] ) {
2015-02-12 05:46:02 +00:00
if ( location ) {
char * tmp = malloc ( strlen ( location ) + strlen ( data [ 0 ] ) + 4 ) ;
2014-12-20 16:19:49 +00:00
if ( ! tmp )
return - 1 ;
2015-02-12 05:46:02 +00:00
sprintf ( tmp , " %s / %s " , location , data [ 0 ] ) ;
free ( location ) ;
location = NULL ;
2015-08-24 17:37:18 +00:00
cur_dive - > dive_site_uuid = find_or_create_dive_site_with_name ( tmp , cur_dive - > when ) ;
2015-03-23 18:19:39 +00:00
free ( tmp ) ;
2014-12-20 16:19:49 +00:00
} else {
2015-02-12 05:46:02 +00:00
location = strdup ( data [ 0 ] ) ;
2014-12-20 16:19:49 +00:00
}
}
return 0 ;
}
2014-02-15 06:36:50 +00:00
2014-12-20 16:19:43 +00:00
extern int cobalt_dive ( void * param , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) columns ;
( void ) column ;
2014-12-20 16:19:43 +00:00
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 " ;
2017-04-11 18:06:37 +00:00
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 " ;
2014-12-20 16:19:46 +00:00
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 " ;
2014-12-20 16:19:47 +00:00
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 " ;
2014-12-20 16:19:49 +00:00
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 " ;
2014-12-20 16:19:43 +00:00
char get_buffer [ 1024 ] ;
dive_start ( ) ;
cur_dive - > number = atoi ( data [ 0 ] ) ;
cur_dive - > when = ( time_t ) ( atol ( data [ 1 ] ) ) ;
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 ] )
2014-12-20 16:19:45 +00:00
cur_dive - > dc . maxdepth . mm = atoi ( data [ 6 ] ) ;
2014-12-20 16:19:43 +00:00
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 ( ) ;
2015-02-22 17:57:34 +00:00
if ( data [ 9 ] ) {
2014-12-20 16:19:43 +00:00
utf8_string ( data [ 9 ] , & cur_settings . dc . serial_nr ) ;
2015-02-22 17:57:34 +00:00
cur_settings . dc . deviceid = atoi ( data [ 9 ] ) ;
cur_settings . dc . model = strdup ( " Cobalt import " ) ;
}
2014-12-20 16:19:43 +00:00
dc_settings_end ( ) ;
settings_end ( ) ;
2015-02-22 17:57:34 +00:00
if ( data [ 9 ] ) {
cur_dive - > dc . deviceid = atoi ( data [ 9 ] ) ;
cur_dive - > dc . model = strdup ( " Cobalt import " ) ;
}
2014-12-20 16:19:44 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_cylinder_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & cobalt_cylinders , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query cobalt_cylinders failed. \n " ) ;
2014-12-20 16:19:44 +00:00
return 1 ;
}
2014-12-20 16:19:46 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_buddy_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & cobalt_buddies , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query cobalt_buddies failed. \n " ) ;
2014-12-20 16:19:46 +00:00
return 1 ;
}
2014-12-20 16:19:47 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_visibility_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & cobalt_visibility , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query cobalt_visibility failed. \n " ) ;
2014-12-20 16:19:47 +00:00
return 1 ;
}
2014-12-20 16:19:49 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_location_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & cobalt_location , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query cobalt_location failed. \n " ) ;
2014-12-20 16:19:49 +00:00
return 1 ;
}
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_site_template , cur_dive - > number ) ;
retval = sqlite3_exec ( handle , get_buffer , & cobalt_location , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query cobalt_location (site) failed. \n " ) ;
2014-12-20 16:19:49 +00:00
return 1 ;
}
2014-12-20 16:19:43 +00:00
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 ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " %s " , " Database query cobalt_profile_sample failed. \n " ) ;
2014-12-20 16:19:43 +00:00
return 1 ;
}
dive_end ( ) ;
return SQLITE_OK ;
}
2014-02-15 06:36:50 +00:00
int parse_shearwater_buffer ( sqlite3 * handle , const char * url , const char * buffer , int size ,
2014-03-14 18:26:07 +00:00
struct dive_table * table )
2014-02-15 06:36:50 +00:00
{
2016-03-07 19:40:12 +00:00
( void ) buffer ;
( void ) size ;
2014-02-15 06:36:50 +00:00
int retval ;
char * err = NULL ;
target_table = table ;
2017-08-22 15:37:39 +00:00
char get_dives [ ] = " select l.number,timestamp,location||' / '||site,buddy,notes,imperialUnits,maxDepth,maxTime,startSurfacePressure,computerSerial,computerModel FROM dive_info AS i JOIN dive_logs AS l ON i.diveId=l.diveId " ;
2014-02-15 06:36:50 +00:00
retval = sqlite3_exec ( handle , get_dives , & shearwater_dive , handle , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " Database query failed '%s'. \n " , url ) ;
2014-02-15 06:36:50 +00:00
return 1 ;
}
return 0 ;
}
2014-12-20 16:19:43 +00:00
int parse_cobalt_buffer ( sqlite3 * handle , const char * url , const char * buffer , int size ,
struct dive_table * table )
{
2016-03-07 19:40:12 +00:00
( void ) buffer ;
( void ) size ;
2014-12-20 16:19:43 +00:00
int retval ;
char * err = NULL ;
target_table = table ;
2015-02-22 17:57:33 +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 " ;
2014-12-20 16:19:43 +00:00
retval = sqlite3_exec ( handle , get_dives , & cobalt_dive , handle , & err ) ;
if ( retval ! = SQLITE_OK ) {
2015-07-02 18:22:22 +00:00
fprintf ( stderr , " Database query failed '%s'. \n " , url ) ;
2014-12-20 16:19:43 +00:00
return 1 ;
}
return 0 ;
}
2015-07-12 17:46:58 +00:00
extern int divinglog_cylinder ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2015-07-12 17:46:58 +00:00
short dbl = 1 ;
//char get_cylinder_template[] = "select TankID,TankSize,PresS,PresE,PresW,O2,He,DblTank from Tank where LogID = %d";
2015-08-02 15:36:38 +00:00
/*
* Divinglog might have more cylinders than what we support . So
* better to ignore those .
*/
if ( cur_cylinder_index > = MAX_CYLINDERS )
return 0 ;
2015-07-12 17:46:58 +00:00
if ( data [ 7 ] & & atoi ( data [ 7 ] ) > 0 )
dbl = 2 ;
cylinder_start ( ) ;
/*
* Assuming that we have to double the cylinder size , if double
* is set
*/
if ( data [ 1 ] & & atoi ( data [ 1 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . type . size . mliter = atol ( data [ 1 ] ) * 1000 * dbl ;
if ( data [ 2 ] & & atoi ( data [ 2 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . start . mbar = atol ( data [ 2 ] ) * 1000 ;
if ( data [ 3 ] & & atoi ( data [ 3 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . end . mbar = atol ( data [ 3 ] ) * 1000 ;
if ( data [ 4 ] & & atoi ( data [ 4 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . type . workingpressure . mbar = atol ( data [ 4 ] ) * 1000 ;
if ( data [ 5 ] & & atoi ( data [ 5 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 . permille = atol ( data [ 5 ] ) * 10 ;
if ( data [ 6 ] & & atoi ( data [ 6 ] ) > 0 )
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . he . permille = atol ( data [ 6 ] ) * 10 ;
cylinder_end ( ) ;
return 0 ;
}
2015-07-12 17:46:51 +00:00
extern int divinglog_profile ( void * handle , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) handle ;
( void ) columns ;
( void ) column ;
2015-07-12 17:46:51 +00:00
int sinterval = 0 ;
2015-07-12 17:46:57 +00:00
unsigned long i , len , lenprofile2 = 0 ;
2015-07-17 14:04:51 +00:00
char * ptr , temp [ 4 ] , pres [ 5 ] , hbeat [ 4 ] , stop [ 4 ] , stime [ 4 ] , ndl [ 4 ] , ppo2_1 [ 4 ] , ppo2_2 [ 4 ] , ppo2_3 [ 4 ] , cns [ 5 ] , setpoint [ 3 ] ;
2015-07-12 17:47:02 +00:00
short oldcyl = - 1 ;
2015-07-12 17:46:51 +00:00
/* We do not have samples */
if ( ! data [ 1 ] )
return 0 ;
if ( data [ 0 ] )
sinterval = atoi ( data [ 0 ] ) ;
/*
* Profile
*
* DDDDDCRASWEE
* D : Depth ( in meter with two decimals )
* C : Deco ( 1 = yes , 0 = no )
* R : RBT ( Remaining Bottom Time warning )
* A : Ascent warning
* S : Decostop ignored
* W : Work warning
* E : Extra info ( different for every computer )
*
* Example : 004500010000
* 4.5 m , no deco , no RBT warning , ascanding too fast , no decostop ignored , no work , no extra info
2015-07-12 17:46:57 +00:00
*
*
* Profile2
*
* TTTFFFFIRRR
*
* T : Temperature ( in ° C with one decimal )
* F : Tank pressure 1 ( in bar with one decimal )
* I : Tank ID ( 0 , 1 , 2 . . . 9 )
* R : RBT ( in min )
*
* Example : 25518051099
* 25.5 ° C , 180.5 bar , Tank 1 , 99 min RBT
2015-07-17 14:04:49 +00:00
*
2015-07-12 17:46:51 +00:00
*/
len = strlen ( data [ 1 ] ) ;
2015-07-12 17:46:57 +00:00
if ( data [ 2 ] )
lenprofile2 = strlen ( data [ 2 ] ) ;
2015-07-12 17:46:51 +00:00
for ( i = 0 , ptr = data [ 1 ] ; i * 12 < len ; + + i ) {
sample_start ( ) ;
cur_sample - > time . seconds = sinterval * i ;
2015-07-12 17:46:52 +00:00
cur_sample - > in_deco = ptr [ 5 ] - ' 0 ' ? true : false ;
2015-07-12 17:46:51 +00:00
ptr [ 5 ] = 0 ;
cur_sample - > depth . mm = atoi ( ptr ) * 10 ;
2015-07-12 17:46:57 +00:00
if ( i * 11 < lenprofile2 ) {
memcpy ( temp , & data [ 2 ] [ i * 11 ] , 3 ) ;
cur_sample - > temperature . mkelvin = C_to_mkelvin ( atoi ( temp ) / 10 ) ;
}
2015-07-12 17:47:01 +00:00
if ( data [ 2 ] ) {
memcpy ( pres , & data [ 2 ] [ i * 11 + 3 ] , 4 ) ;
2017-07-20 21:39:02 +00:00
cur_sample - > pressure [ 0 ] . mbar = atoi ( pres ) * 100 ;
2015-07-12 17:47:01 +00:00
}
2015-07-13 16:13:54 +00:00
if ( data [ 3 ] & & strlen ( data [ 3 ] ) ) {
memcpy ( hbeat , & data [ 3 ] [ i * 14 + 8 ] , 3 ) ;
cur_sample - > heartbeat = atoi ( hbeat ) ;
}
2015-07-13 16:13:56 +00:00
if ( data [ 4 ] & & strlen ( data [ 4 ] ) ) {
memcpy ( stop , & data [ 4 ] [ i * 9 + 6 ] , 3 ) ;
cur_sample - > stopdepth . mm = atoi ( stop ) * 1000 ;
2015-07-13 16:13:57 +00:00
2015-07-13 16:13:58 +00:00
memcpy ( stime , & data [ 4 ] [ i * 9 + 3 ] , 3 ) ;
cur_sample - > stoptime . seconds = atoi ( stime ) * 60 ;
2015-07-13 16:13:57 +00:00
/*
* Following value is NDL when not in deco , and
* either 0 or TTS when in deco .
*/
memcpy ( ndl , & data [ 4 ] [ i * 9 + 0 ] , 3 ) ;
if ( cur_sample - > in_deco = = false )
cur_sample - > ndl . seconds = atoi ( ndl ) * 60 ;
else if ( atoi ( ndl ) )
cur_sample - > tts . seconds = atoi ( ndl ) * 60 ;
if ( cur_sample - > in_deco = = true )
cur_sample - > ndl . seconds = 0 ;
2015-07-13 16:13:56 +00:00
}
2015-07-17 14:04:49 +00:00
/*
* AAABBBCCCOOOONNNNSS
*
* A = ppO2 cell 1 ( measured )
* B = ppO2 cell 2 ( measured )
* C = ppO2 cell 3 ( measured )
* O = OTU
* N = CNS
* S = Setpoint
*
* Example : 1121131141548026411
* 1.12 bar , 1.13 bar , 1.14 bar , OTU = 154.8 , CNS = 26.4 , Setpoint = 1.1
*/
if ( data [ 5 ] & & strlen ( data [ 5 ] ) ) {
memcpy ( ppo2_1 , & data [ 5 ] [ i * 19 + 0 ] , 3 ) ;
memcpy ( ppo2_2 , & data [ 5 ] [ i * 19 + 3 ] , 3 ) ;
memcpy ( ppo2_3 , & data [ 5 ] [ i * 19 + 6 ] , 3 ) ;
2015-07-17 14:04:50 +00:00
memcpy ( cns , & data [ 5 ] [ i * 19 + 13 ] , 4 ) ;
2015-07-17 14:04:51 +00:00
memcpy ( setpoint , & data [ 5 ] [ i * 19 + 17 ] , 2 ) ;
2015-07-17 14:04:49 +00:00
if ( atoi ( ppo2_1 ) > 0 )
cur_sample - > o2sensor [ 0 ] . mbar = atoi ( ppo2_1 ) * 100 ;
if ( atoi ( ppo2_2 ) > 0 )
cur_sample - > o2sensor [ 1 ] . mbar = atoi ( ppo2_2 ) * 100 ;
if ( atoi ( ppo2_3 ) > 0 )
cur_sample - > o2sensor [ 2 ] . mbar = atoi ( ppo2_3 ) * 100 ;
2015-07-17 14:04:50 +00:00
if ( atoi ( cns ) > 0 )
2017-03-08 06:41:41 +00:00
cur_sample - > cns = lrintf ( atoi ( cns ) / 10.0f ) ;
2015-07-17 14:04:51 +00:00
if ( atoi ( setpoint ) > 0 )
cur_sample - > setpoint . mbar = atoi ( setpoint ) * 100 ;
2015-07-17 14:04:49 +00:00
}
2015-07-17 14:04:52 +00:00
/*
2017-07-24 03:41:10 +00:00
* Count the number of o2 sensors
2015-07-17 14:04:52 +00:00
*/
2017-07-23 05:54:08 +00:00
if ( ! cur_dive - > dc . no_o2sensors & & ( cur_sample - > o2sensor [ 0 ] . mbar | | cur_sample - > o2sensor [ 0 ] . mbar | | cur_sample - > o2sensor [ 0 ] . mbar ) ) {
2015-07-17 14:04:52 +00:00
cur_dive - > dc . no_o2sensors = cur_sample - > o2sensor [ 0 ] . mbar ? 1 : 0 +
cur_sample - > o2sensor [ 1 ] . mbar ? 1 : 0 +
cur_sample - > o2sensor [ 2 ] . mbar ? 1 : 0 ;
}
2015-07-12 17:46:51 +00:00
ptr + = 12 ;
sample_end ( ) ;
}
2015-07-12 17:46:53 +00:00
for ( i = 0 , ptr = data [ 1 ] ; i * 12 < len ; + + i ) {
/* Remaining bottom time warning */
if ( ptr [ 6 ] - ' 0 ' ) {
event_start ( ) ;
cur_event . time . seconds = sinterval * i ;
strcpy ( cur_event . name , " rbt " ) ;
event_end ( ) ;
2015-07-12 17:46:54 +00:00
}
/* Ascent warning */
if ( ptr [ 7 ] - ' 0 ' ) {
event_start ( ) ;
cur_event . time . seconds = sinterval * i ;
strcpy ( cur_event . name , " ascent " ) ;
event_end ( ) ;
2015-07-12 17:46:55 +00:00
}
/* Deco stop ignored */
if ( ptr [ 8 ] - ' 0 ' ) {
event_start ( ) ;
cur_event . time . seconds = sinterval * i ;
strcpy ( cur_event . name , " violation " ) ;
event_end ( ) ;
2015-07-12 17:46:56 +00:00
}
/* Workload warning */
if ( ptr [ 9 ] - ' 0 ' ) {
event_start ( ) ;
cur_event . time . seconds = sinterval * i ;
strcpy ( cur_event . name , " workload " ) ;
event_end ( ) ;
2015-07-12 17:46:53 +00:00
}
ptr + = 12 ;
}
2015-07-12 17:47:02 +00:00
for ( i = 0 ; i * 11 < lenprofile2 ; + + i ) {
short tank = data [ 2 ] [ i * 11 + 7 ] - ' 0 ' ;
if ( oldcyl ! = tank ) {
struct gasmix * mix = & cur_dive - > cylinder [ tank ] . gasmix ;
int o2 = get_o2 ( mix ) ;
int he = get_he ( mix ) ;
event_start ( ) ;
cur_event . time . seconds = sinterval * i ;
strcpy ( cur_event . name , " gaschange " ) ;
o2 = ( o2 + 5 ) / 10 ;
he = ( he + 5 ) / 10 ;
cur_event . value = o2 + ( he < < 16 ) ;
event_end ( ) ;
oldcyl = tank ;
}
}
2015-07-12 17:46:51 +00:00
return 0 ;
}
2015-07-12 17:46:48 +00:00
extern int divinglog_dive ( void * param , int columns , char * * data , char * * column )
{
2016-03-07 19:40:12 +00:00
( void ) columns ;
( void ) column ;
2015-07-12 17:47:00 +00:00
int retval = 0 ;
2015-07-12 17:46:48 +00:00
sqlite3 * handle = ( sqlite3 * ) param ;
char * err = NULL ;
2015-07-17 14:04:49 +00:00
char get_profile_template [ ] = " select ProfileInt,Profile,Profile2,Profile3,Profile4,Profile5 from Logbook where ID = %d " ;
2015-07-12 17:47:00 +00:00
char get_cylinder0_template [ ] = " select 0,TankSize,PresS,PresE,PresW,O2,He,DblTank from Logbook where ID = %d " ;
2015-07-12 17:46:58 +00:00
char get_cylinder_template [ ] = " select TankID,TankSize,PresS,PresE,PresW,O2,He,DblTank from Tank where LogID = %d order by TankID " ;
2015-07-12 17:46:51 +00:00
char get_buffer [ 1024 ] ;
2015-07-12 17:46:48 +00:00
dive_start ( ) ;
2015-07-12 17:46:58 +00:00
diveid = atoi ( data [ 13 ] ) ;
2015-07-12 17:46:48 +00:00
cur_dive - > number = atoi ( data [ 0 ] ) ;
cur_dive - > when = ( time_t ) ( atol ( data [ 1 ] ) ) ;
if ( data [ 2 ] )
2015-08-24 17:37:18 +00:00
cur_dive - > dive_site_uuid = find_or_create_dive_site_with_name ( data [ 2 ] , cur_dive - > when ) ;
2015-07-12 17:46:48 +00:00
if ( data [ 3 ] )
utf8_string ( data [ 3 ] , & cur_dive - > buddy ) ;
if ( data [ 4 ] )
utf8_string ( data [ 4 ] , & cur_dive - > notes ) ;
if ( data [ 5 ] )
2017-03-09 16:07:30 +00:00
cur_dive - > dc . maxdepth . mm = lrint ( atof ( data [ 5 ] ) * 1000 ) ;
2015-07-12 17:46:48 +00:00
if ( data [ 6 ] )
cur_dive - > dc . duration . seconds = atoi ( data [ 6 ] ) * 60 ;
2015-07-12 17:46:50 +00:00
if ( data [ 7 ] )
utf8_string ( data [ 7 ] , & cur_dive - > divemaster ) ;
if ( data [ 8 ] )
cur_dive - > airtemp . mkelvin = C_to_mkelvin ( atol ( data [ 8 ] ) ) ;
if ( data [ 9 ] )
cur_dive - > watertemp . mkelvin = C_to_mkelvin ( atol ( data [ 9 ] ) ) ;
if ( data [ 10 ] ) {
cur_dive - > weightsystem [ 0 ] . weight . grams = atol ( data [ 10 ] ) * 1000 ;
cur_dive - > weightsystem [ 0 ] . description = strdup ( translate ( " gettextFromC " , " unknown " ) ) ;
}
if ( data [ 11 ] )
cur_dive - > suit = strdup ( data [ 11 ] ) ;
2017-07-23 18:58:36 +00:00
/* Divinglog has following visibility options: good, medium, bad */
if ( data [ 14 ] ) {
switch ( data [ 14 ] [ 0 ] ) {
case ' 0 ' :
break ;
case ' 1 ' :
cur_dive - > visibility = 5 ;
break ;
case ' 2 ' :
cur_dive - > visibility = 3 ;
break ;
case ' 3 ' :
cur_dive - > visibility = 1 ;
break ;
default :
break ;
}
}
2017-07-23 05:42:58 +00:00
2015-07-12 17:46:48 +00:00
settings_start ( ) ;
dc_settings_start ( ) ;
2015-07-12 17:46:50 +00:00
if ( data [ 12 ] ) {
cur_dive - > dc . model = strdup ( data [ 12 ] ) ;
} else {
cur_settings . dc . model = strdup ( " Divinglog import " ) ;
}
2015-07-12 17:46:48 +00:00
2015-07-12 17:47:00 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_cylinder0_template , diveid ) ;
retval = sqlite3_exec ( handle , get_buffer , & divinglog_cylinder , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
fprintf ( stderr , " %s " , " Database query divinglog_cylinder0 failed. \n " ) ;
return 1 ;
}
2015-07-12 17:46:58 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_cylinder_template , diveid ) ;
retval = sqlite3_exec ( handle , get_buffer , & divinglog_cylinder , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
fprintf ( stderr , " %s " , " Database query divinglog_cylinder failed. \n " ) ;
return 1 ;
}
2017-07-23 18:54:41 +00:00
if ( data [ 15 ] ) {
switch ( data [ 15 ] [ 0 ] ) {
/* OC */
case ' 0 ' :
break ;
case ' 1 ' :
cur_dive - > dc . divemode = PSCR ;
break ;
case ' 2 ' :
cur_dive - > dc . divemode = CCR ;
break ;
}
}
2015-07-12 17:46:58 +00:00
2015-07-12 17:46:48 +00:00
dc_settings_end ( ) ;
settings_end ( ) ;
2015-07-12 17:46:50 +00:00
if ( data [ 12 ] ) {
cur_dive - > dc . model = strdup ( data [ 12 ] ) ;
} else {
cur_dive - > dc . model = strdup ( " Divinglog import " ) ;
}
2015-07-12 17:46:51 +00:00
2015-07-12 17:46:59 +00:00
snprintf ( get_buffer , sizeof ( get_buffer ) - 1 , get_profile_template , diveid ) ;
2015-07-12 17:46:51 +00:00
retval = sqlite3_exec ( handle , get_buffer , & divinglog_profile , 0 , & err ) ;
if ( retval ! = SQLITE_OK ) {
fprintf ( stderr , " %s " , " Database query divinglog_profile failed. \n " ) ;
return 1 ;
}
2015-07-12 17:46:48 +00:00
dive_end ( ) ;
return SQLITE_OK ;
}
int parse_divinglog_buffer ( sqlite3 * handle , const char * url , const char * buffer , int size ,
struct dive_table * table )
{
2016-03-07 19:40:12 +00:00
( void ) buffer ;
( void ) size ;
2015-07-12 17:46:48 +00:00
int retval ;
char * err = NULL ;
target_table = table ;
2017-07-23 18:54:41 +00:00
char get_dives [ ] = " select Number,strftime('%s',Divedate || ' ' || ifnull(Entrytime,'00:00')),Country || ' - ' || City || ' - ' || Place,Buddy,Comments,Depth,Divetime,Divemaster,Airtemp,Watertemp,Weight,Divesuit,Computer,ID,Visibility,SupplyType from Logbook where UUID not in (select UUID from DeletedRecords) " ;
2015-07-12 17:46:48 +00:00
retval = sqlite3_exec ( handle , get_dives , & divinglog_dive , handle , & err ) ;
if ( retval ! = SQLITE_OK ) {
fprintf ( stderr , " Database query failed '%s'. \n " , url ) ;
return 1 ;
}
return 0 ;
}
2015-10-03 14:29:40 +00:00
/*
2015-10-04 12:03:49 +00:00
* Parse a unsigned 32 - bit integer in little - endian mode ,
2015-10-03 14:29:40 +00:00
* that is seconds since Jan 1 , 2000.
*/
static timestamp_t parse_dlf_timestamp ( unsigned char * buffer )
{
timestamp_t offset ;
2015-10-04 12:03:49 +00:00
offset = buffer [ 3 ] ;
2015-10-03 14:29:40 +00:00
offset = ( offset < < 8 ) + buffer [ 2 ] ;
offset = ( offset < < 8 ) + buffer [ 1 ] ;
offset = ( offset < < 8 ) + buffer [ 0 ] ;
// Jan 1, 2000 is 946684800 seconds after Jan 1, 1970, which is
// the Unix epoch date that "timestamp_t" uses.
return offset + 946684800 ;
}
2014-12-28 22:38:41 +00:00
int parse_dlf_buffer ( unsigned char * buffer , size_t size )
2014-12-27 20:10:44 +00:00
{
2014-12-28 22:38:41 +00:00
unsigned char * ptr = buffer ;
2014-12-29 22:43:30 +00:00
unsigned char event ;
bool found ;
2014-12-28 22:38:42 +00:00
unsigned int time = 0 ;
2014-12-28 07:58:51 +00:00
int i ;
2014-12-28 22:38:43 +00:00
char serial [ 6 ] ;
2014-12-27 20:10:44 +00:00
2014-12-28 11:26:38 +00:00
target_table = & dive_table ;
2014-12-29 15:18:20 +00:00
// Check for the correct file magic
if ( ptr [ 0 ] ! = ' D ' | | ptr [ 1 ] ! = ' i ' | | ptr [ 2 ] ! = ' v ' | | ptr [ 3 ] ! = ' E ' )
return - 1 ;
2014-12-27 20:10:44 +00:00
dive_start ( ) ;
2014-12-28 22:38:43 +00:00
divecomputer_start ( ) ;
cur_dc - > model = strdup ( " DLF import " ) ;
// (ptr[7] << 8) + ptr[6] Is "Serial"
snprintf ( serial , sizeof ( serial ) , " %d " , ( ptr [ 7 ] < < 8 ) + ptr [ 6 ] ) ;
cur_dc - > serial = strdup ( serial ) ;
2015-10-03 14:29:40 +00:00
cur_dc - > when = parse_dlf_timestamp ( ptr + 8 ) ;
2014-12-28 22:38:43 +00:00
cur_dive - > when = cur_dc - > when ;
cur_dc - > duration . seconds = ( ( ptr [ 14 ] & 0xFE ) < < 16 ) + ( ptr [ 13 ] < < 8 ) + ptr [ 12 ] ;
2015-01-23 23:29:33 +00:00
// ptr[14] >> 1 is scrubber used in %
// 3 bit dive type
switch ( ( ptr [ 15 ] & 0x30 ) > > 3 ) {
case 0 : // unknown
case 1 :
2015-01-10 23:01:15 +00:00
cur_dc - > divemode = OC ;
2015-01-23 23:29:33 +00:00
break ;
case 2 :
2015-01-10 23:01:15 +00:00
cur_dc - > divemode = CCR ;
2015-01-23 23:29:33 +00:00
break ;
case 3 :
2015-01-10 23:01:15 +00:00
cur_dc - > divemode = CCR ; // mCCR
2015-01-23 23:29:33 +00:00
break ;
case 4 :
2015-01-21 07:52:28 +00:00
cur_dc - > divemode = FREEDIVE ;
2015-01-23 23:29:33 +00:00
break ;
case 5 :
2015-01-10 23:01:15 +00:00
cur_dc - > divemode = OC ; // Gauge
2015-01-23 23:29:33 +00:00
break ;
case 6 :
2015-01-10 23:01:15 +00:00
cur_dc - > divemode = PSCR ; // ASCR
2015-01-23 23:29:33 +00:00
break ;
case 7 :
2015-01-10 23:01:15 +00:00
cur_dc - > divemode = PSCR ;
2015-01-23 23:29:33 +00:00
break ;
}
2014-12-28 22:38:43 +00:00
cur_dc - > maxdepth . mm = ( ( ptr [ 21 ] < < 8 ) + ptr [ 20 ] ) * 10 ;
2015-01-23 23:29:34 +00:00
cur_dc - > surface_pressure . mbar = ( ( ptr [ 25 ] < < 8 ) + ptr [ 24 ] ) / 10 ;
2014-12-28 22:38:43 +00:00
/* Done with parsing what we know about the dive header */
ptr + = 32 ;
2014-12-27 20:10:44 +00:00
2015-01-06 12:23:31 +00:00
// We're going to interpret ppO2 saved as a sensor value in these modes.
if ( cur_dc - > divemode = = CCR | | cur_dc - > divemode = = PSCR )
cur_dc - > no_o2sensors = 1 ;
2017-08-07 18:39:33 +00:00
for ( ; ptr < buffer + size ; ptr + = 16 ) {
2014-12-28 22:38:42 +00:00
time = ( ( ptr [ 0 ] > > 4 ) & 0x0f ) +
( ( ptr [ 1 ] < < 4 ) & 0xff0 ) +
2016-04-25 18:23:22 +00:00
( ( ptr [ 2 ] < < 12 ) & 0x1f000 ) ;
2014-12-27 20:10:44 +00:00
event = ptr [ 0 ] & 0x0f ;
2014-12-29 22:43:30 +00:00
switch ( event ) {
case 0 :
/* Regular sample */
sample_start ( ) ;
cur_sample - > time . seconds = time ;
2015-01-05 21:59:17 +00:00
cur_sample - > depth . mm = ( ( ptr [ 5 ] < < 8 ) + ptr [ 4 ] ) * 10 ;
2015-01-05 21:59:18 +00:00
// Crazy precision on these stored values...
// Only store value if we're in CCR/PSCR mode,
// because we rather calculate ppo2 our selfs.
2015-01-10 23:01:15 +00:00
if ( cur_dc - > divemode = = CCR | | cur_dc - > divemode = = PSCR )
2015-01-05 21:59:18 +00:00
cur_sample - > o2sensor [ 0 ] . mbar = ( ( ptr [ 7 ] < < 8 ) + ptr [ 6 ] ) / 10 ;
2017-08-07 21:42:03 +00:00
// In some test files, ndl / tts / temp is bogus if this bits are 1
// flag bits in ptr[11] & 0xF0 is probably involved to,
if ( ( ptr [ 2 ] > > 5 ) ! = 1 ) {
// NDL in minutes, 10 bit
cur_sample - > ndl . seconds = ( ( ( ptr [ 9 ] & 0x03 ) < < 8 ) + ptr [ 8 ] ) * 60 ;
// TTS in minutes, 10 bit
cur_sample - > tts . seconds = ( ( ( ptr [ 10 ] & 0x0F ) < < 6 ) + ( ptr [ 9 ] > > 2 ) ) * 60 ;
// Temperature in 1/10 C, 10 bit signed
cur_sample - > temperature . mkelvin = ( ( ptr [ 11 ] & 0x20 ) ? - 1 : 1 ) * ( ( ( ptr [ 11 ] & 0x1F ) < < 4 ) + ( ptr [ 10 ] > > 4 ) ) * 100 + ZERO_C_IN_MKELVIN ;
}
2015-01-05 21:59:21 +00:00
cur_sample - > stopdepth . mm = ( ( ptr [ 13 ] < < 8 ) + ptr [ 12 ] ) * 10 ;
2015-01-23 23:29:37 +00:00
if ( cur_sample - > stopdepth . mm )
cur_sample - > in_deco = true ;
//ptr[14] is helium content, always zero?
2017-08-07 21:42:03 +00:00
//ptr[15] is setpoint, what the computer thinks you should aim for?
2014-12-29 22:43:30 +00:00
sample_end ( ) ;
break ;
2015-01-23 16:15:09 +00:00
case 1 : /* dive event */
case 2 : /* automatic parameter change */
case 3 : /* diver error */
case 4 : /* internal error */
case 5 : /* device activity log */
2014-12-29 22:43:30 +00:00
event_start ( ) ;
cur_event . time . seconds = time ;
switch ( ptr [ 4 ] ) {
2014-12-29 22:43:33 +00:00
case 1 :
strcpy ( cur_event . name , " Setpoint Manual " ) ;
2017-08-07 18:40:57 +00:00
cur_event . value = ptr [ 6 ] ;
sample_start ( ) ;
cur_sample - > setpoint . mbar = ptr [ 6 ] * 10 ;
sample_end ( ) ;
2014-12-29 22:43:33 +00:00
break ;
case 2 :
strcpy ( cur_event . name , " Setpoint Auto " ) ;
2017-08-07 18:40:57 +00:00
cur_event . value = ptr [ 6 ] ;
sample_start ( ) ;
cur_sample - > setpoint . mbar = ptr [ 6 ] * 10 ;
sample_end ( ) ;
2014-12-29 22:43:33 +00:00
switch ( ptr [ 7 ] ) {
case 0 :
strcat ( cur_event . name , " Manual " ) ;
break ;
case 1 :
strcat ( cur_event . name , " Auto Start " ) ;
break ;
case 2 :
strcat ( cur_event . name , " Auto Hypox " ) ;
break ;
case 3 :
strcat ( cur_event . name , " Auto Timeout " ) ;
break ;
case 4 :
strcat ( cur_event . name , " Auto Ascent " ) ;
break ;
case 5 :
strcat ( cur_event . name , " Auto Stall " ) ;
break ;
case 6 :
strcat ( cur_event . name , " Auto SP Low " ) ;
break ;
default :
break ;
}
break ;
case 3 :
2015-01-23 16:15:08 +00:00
// obsolete
2014-12-29 22:43:33 +00:00
strcpy ( cur_event . name , " OC " ) ;
break ;
case 4 :
2015-01-23 16:15:08 +00:00
// obsolete
strcpy ( cur_event . name , " CCR " ) ;
2014-12-29 22:43:33 +00:00
break ;
2014-12-28 07:58:51 +00:00
case 5 :
strcpy ( cur_event . name , " gaschange " ) ;
2014-12-29 22:43:31 +00:00
cur_event . type = SAMPLE_EVENT_GASCHANGE2 ;
cur_event . value = ptr [ 7 ] < < 8 ^ ptr [ 6 ] ;
2014-12-28 07:58:51 +00:00
found = false ;
for ( i = 0 ; i < cur_cylinder_index ; + + i ) {
2015-06-22 05:00:21 +00:00
if ( cur_dive - > cylinder [ i ] . gasmix . o2 . permille = = ptr [ 6 ] * 10 & & cur_dive - > cylinder [ i ] . gasmix . he . permille = = ptr [ 7 ] * 10 ) {
2014-12-28 07:58:51 +00:00
found = true ;
break ;
2015-06-22 05:00:21 +00:00
}
2014-12-28 07:58:51 +00:00
}
if ( ! found ) {
cylinder_start ( ) ;
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . o2 . permille = ptr [ 6 ] * 10 ;
cur_dive - > cylinder [ cur_cylinder_index ] . gasmix . he . permille = ptr [ 7 ] * 10 ;
cylinder_end ( ) ;
2014-12-30 16:05:51 +00:00
cur_event . gas . index = cur_cylinder_index ;
} else {
cur_event . gas . index = i ;
2014-12-28 07:58:51 +00:00
}
break ;
2014-12-28 22:38:44 +00:00
case 6 :
2014-12-29 22:43:33 +00:00
strcpy ( cur_event . name , " Start " ) ;
break ;
case 7 :
strcpy ( cur_event . name , " Too Fast " ) ;
break ;
case 8 :
strcpy ( cur_event . name , " Above Ceiling " ) ;
break ;
case 9 :
strcpy ( cur_event . name , " Toxic " ) ;
break ;
case 10 :
strcpy ( cur_event . name , " Hypox " ) ;
break ;
case 11 :
strcpy ( cur_event . name , " Critical " ) ;
break ;
case 12 :
strcpy ( cur_event . name , " Sensor Disabled " ) ;
break ;
case 13 :
strcpy ( cur_event . name , " Sensor Enabled " ) ;
break ;
case 14 :
strcpy ( cur_event . name , " O2 Backup " ) ;
break ;
case 15 :
strcpy ( cur_event . name , " Peer Down " ) ;
break ;
case 16 :
strcpy ( cur_event . name , " HS Down " ) ;
break ;
case 17 :
strcpy ( cur_event . name , " Inconsistent " ) ;
break ;
case 18 :
2015-01-23 16:15:08 +00:00
// key pressed - probably not
// interesting to view on profile
2014-12-29 22:43:33 +00:00
break ;
case 19 :
2015-01-23 16:15:08 +00:00
// obsolete
strcpy ( cur_event . name , " SCR " ) ;
2014-12-29 22:43:33 +00:00
break ;
case 20 :
strcpy ( cur_event . name , " Above Stop " ) ;
break ;
case 21 :
strcpy ( cur_event . name , " Safety Miss " ) ;
break ;
case 22 :
strcpy ( cur_event . name , " Fatal " ) ;
break ;
case 23 :
2017-08-07 18:41:43 +00:00
strcpy ( cur_event . name , " gaschange " ) ;
cur_event . type = SAMPLE_EVENT_GASCHANGE2 ;
cur_event . value = ptr [ 7 ] < < 8 ^ ptr [ 6 ] ;
event_end ( ) ;
2014-12-29 22:43:33 +00:00
break ;
case 24 :
strcpy ( cur_event . name , " gaschange " ) ;
cur_event . type = SAMPLE_EVENT_GASCHANGE2 ;
cur_event . value = ptr [ 7 ] < < 8 ^ ptr [ 6 ] ;
event_end ( ) ;
// This is both a mode change and a gas change event
// so we encode it as two separate events.
event_start ( ) ;
strcpy ( cur_event . name , " Change Mode " ) ;
switch ( ptr [ 8 ] ) {
case 1 :
strcat ( cur_event . name , " : OC " ) ;
break ;
case 2 :
strcat ( cur_event . name , " : CCR " ) ;
break ;
case 3 :
strcat ( cur_event . name , " : mCCR " ) ;
break ;
case 4 :
strcat ( cur_event . name , " : Free " ) ;
break ;
case 5 :
strcat ( cur_event . name , " : Gauge " ) ;
break ;
case 6 :
strcat ( cur_event . name , " : ASCR " ) ;
break ;
case 7 :
strcat ( cur_event . name , " : PSCR " ) ;
break ;
default :
break ;
}
event_end ( ) ;
break ;
2015-01-23 16:15:08 +00:00
case 25 :
strcpy ( cur_event . name , " CCR O2 solenoid opened/closed " ) ;
break ;
case 26 :
strcpy ( cur_event . name , " User mark " ) ;
break ;
2014-12-29 22:43:33 +00:00
case 27 :
2015-01-23 23:29:35 +00:00
snprintf ( cur_event . name , MAX_EVENT_NAME , " %sGF Switch (%d/%d) " , ptr [ 6 ] ? " Bailout, " : " " , ptr [ 7 ] , ptr [ 8 ] ) ;
2014-12-29 22:43:33 +00:00
break ;
case 28 :
strcpy ( cur_event . name , " Peer Up " ) ;
break ;
case 29 :
strcpy ( cur_event . name , " HS Up " ) ;
break ;
case 30 :
2015-01-23 23:29:36 +00:00
snprintf ( cur_event . name , MAX_EVENT_NAME , " CNS %d%% " , ptr [ 6 ] ) ;
2014-12-28 22:38:44 +00:00
break ;
2014-12-28 07:58:51 +00:00
default :
2014-12-29 22:43:33 +00:00
// No values above 30 had any description
2014-12-29 22:43:30 +00:00
break ;
2014-12-28 07:58:51 +00:00
}
2014-12-29 22:43:30 +00:00
event_end ( ) ;
break ;
2015-01-23 16:15:09 +00:00
case 6 :
/* device configuration */
break ;
2014-12-29 22:43:32 +00:00
case 7 :
2015-01-23 16:15:09 +00:00
/* measure record */
2017-08-07 22:00:36 +00:00
switch ( ptr [ 2 ] > > 5 ) {
case 1 :
/* Measure Battery */
//printf("B1: %dmV %d% B2: %dmV %d%\n", (ptr[5] << 8) + ptr[4], (ptr[7] << 8) + ptr[6], (ptr[9] << 8) + ptr[8], (ptr[11] << 8) + ptr[10]);
case 3 :
/* Measure Oxygen */
//printf("o2 cells(0.01 mV): %d %d %d %d\n", (ptr[5] << 8) + ptr[4], (ptr[7] << 8) + ptr[6], (ptr[9] << 8) + ptr[8], (ptr[11] << 8) + ptr[10]);
break ;
case 4 :
/* Measure GPS */
cur_latitude . udeg = ( int ) ( ( ptr [ 7 ] < < 24 ) + ( ptr [ 6 ] < < 16 ) + ( ptr [ 5 ] < < 8 ) + ( ptr [ 4 ] < < 0 ) ) ;
cur_longitude . udeg = ( int ) ( ( ptr [ 11 ] < < 24 ) + ( ptr [ 10 ] < < 16 ) + ( ptr [ 9 ] < < 8 ) + ( ptr [ 8 ] < < 0 ) ) ;
cur_dive - > dive_site_uuid = create_dive_site_with_gps ( NULL , cur_latitude , cur_longitude , cur_dive - > when ) ;
printf ( " gps: %s \n " , printGPSCoords ( cur_latitude . udeg , cur_longitude . udeg ) ) ;
break ;
default :
break ;
}
2014-12-29 22:43:32 +00:00
break ;
2014-12-29 22:43:30 +00:00
default :
/* Unknown... */
break ;
2014-12-27 20:10:44 +00:00
}
}
2014-12-28 22:38:43 +00:00
divecomputer_end ( ) ;
2014-12-27 20:10:44 +00:00
dive_end ( ) ;
2014-12-28 14:51:28 +00:00
return 0 ;
2014-12-27 20:10:44 +00:00
}
2011-08-31 01:40:25 +00:00
void parse_xml_init ( void )
2011-08-30 23:28:59 +00:00
{
2011-08-28 23:58:26 +00:00
LIBXML_TEST_VERSION
}
2011-11-05 10:39:17 +00:00
2012-09-18 15:33:55 +00:00
void parse_xml_exit ( void )
{
xmlCleanupParser ( ) ;
}
2011-11-16 05:12:43 +00:00
static struct xslt_files {
const char * root ;
const char * file ;
2014-02-08 12:00:54 +00:00
const char * attribute ;
2011-11-16 05:12:43 +00:00
} xslt_files [ ] = {
2014-02-16 23:42:56 +00:00
{ " SUUNTO " , " SuuntoSDM.xslt " , NULL } ,
{ " Dive " , " SuuntoDM4.xslt " , " xmlns " } ,
{ " Dive " , " shearwater.xslt " , " version " } ,
{ " JDiveLog " , " jdivelog2subsurface.xslt " , NULL } ,
{ " dives " , " MacDive.xslt " , NULL } ,
{ " DIVELOGSDATA " , " divelogs.xslt " , NULL } ,
{ " uddf " , " uddf.xslt " , NULL } ,
{ " UDDF " , " uddf.xslt " , NULL } ,
{ " profile " , " udcf.xslt " , NULL } ,
{ " Divinglog " , " DivingLog.xslt " , NULL } ,
{ " csv " , " csv2xml.xslt " , NULL } ,
{ " sensuscsv " , " sensuscsv.xslt " , NULL } ,
2014-12-26 14:11:38 +00:00
{ " SubsurfaceCSV " , " subsurfacecsv.xslt " , NULL } ,
2014-02-16 23:42:56 +00:00
{ " manualcsv " , " manualcsv2xml.xslt " , NULL } ,
2015-02-17 17:16:21 +00:00
{ " logbook " , " DiveLog.xslt " , NULL } ,
2016-12-31 07:56:08 +00:00
{ " AV1 " , " av1.xslt " , NULL } ,
2014-02-16 23:42:56 +00:00
{ NULL , }
} ;
2011-11-16 05:12:43 +00:00
2014-03-14 18:26:07 +00:00
static xmlDoc * test_xslt_transforms ( xmlDoc * doc , const char * * params )
2011-11-05 10:39:17 +00:00
{
2011-11-16 05:12:43 +00:00
struct xslt_files * info = xslt_files ;
2011-11-05 10:39:17 +00:00
xmlDoc * transformed ;
xsltStylesheetPtr xslt = NULL ;
xmlNode * root_element = xmlDocGetRootElement ( doc ) ;
2013-02-07 19:20:02 +00:00
char * attribute ;
2011-11-16 05:12:43 +00:00
2014-02-08 12:00:54 +00:00
while ( info - > root ) {
2015-05-28 12:59:08 +00:00
if ( ( strcasecmp ( ( const char * ) root_element - > name , info - > root ) = = 0 ) ) {
2014-02-08 12:00:54 +00:00
if ( info - > attribute = = NULL )
break ;
2015-05-28 12:59:08 +00:00
else if ( xmlGetProp ( root_element , ( const xmlChar * ) info - > attribute ) ! = NULL )
2014-02-08 12:00:54 +00:00
break ;
2014-02-09 21:00:33 +00:00
}
2011-11-16 05:12:43 +00:00
info + + ;
}
if ( info - > root ) {
2015-05-28 12:59:08 +00:00
attribute = ( char * ) xmlGetProp ( xmlFirstElementChild ( root_element ) , ( const xmlChar * ) " name " ) ;
2013-02-21 01:07:22 +00:00
if ( attribute ) {
if ( strcasecmp ( attribute , " subsurface " ) = = 0 ) {
free ( ( void * ) attribute ) ;
return doc ;
}
free ( ( void * ) attribute ) ;
}
2011-11-05 10:39:17 +00:00
xmlSubstituteEntitiesDefault ( 1 ) ;
2011-11-16 05:12:43 +00:00
xslt = get_stylesheet ( info - > file ) ;
2013-02-22 19:20:46 +00:00
if ( xslt = = NULL ) {
2014-03-14 18:26:07 +00:00
report_error ( translate ( " gettextFromC " , " Can't open stylesheet %s " ) , info - > file ) ;
2011-11-05 10:39:17 +00:00
return doc ;
2013-02-22 19:20:46 +00:00
}
2013-10-16 19:05:19 +00:00
transformed = xsltApplyStylesheet ( xslt , doc , params ) ;
2011-11-05 10:39:17 +00:00
xmlFreeDoc ( doc ) ;
xsltFreeStylesheet ( xslt ) ;
2013-10-16 19:05:19 +00:00
2011-11-05 10:39:17 +00:00
return transformed ;
}
return doc ;
}