Read and write divemode changes (xml and git)

This provides for reading of divemode change events from dive logs
and for writing them to dive logs. This applies to xml and git
divelogs. Divemode change events have the following structure:
event->name = "modechange"
event->value = integer corresponding to enum dive_comp_type (dive.c),
reflecting the type of divemode change (OC, CCR, PSCR, etc).

In the dive log file, the event value is written as a string that
corresponds to each of the enum values, e.g.
<event name='modechange' divemode='OC' />
This xml is also read from the dive log file and translated to an
appropriate value of event->value.

The file diveeventitem.cpp was udated to reflect this new way of
dealing with divemode change events.

Signed-off-by: Willem Ferguson <willemferguson@zoology.up.ac.za>
This commit is contained in:
Willem Ferguson 2018-04-07 14:56:37 +02:00 committed by Lubomir I. Ivanov
parent 5f95c226ef
commit b9174332d5
6 changed files with 48 additions and 7 deletions

View file

@ -27,7 +27,7 @@ extern "C" {
extern int last_xml_version;
enum dive_comp_type {OC, CCR, PSCR, FREEDIVE, NUM_DC_TYPE}; // Flags (Open-circuit and Closed-circuit-rebreather) for setting dive computer type
enum dive_comp_type {OC, CCR, PSCR, FREEDIVE, NUM_DC_TYPE, UNDEF_COMP_TYPE}; // Flags (Open-circuit and Closed-circuit-rebreather) for setting dive computer type
enum cylinderuse {OC_GAS, DILUENT, OXYGEN, NOT_USED, NUM_GAS_USE}; // The different uses for cylinders
extern const char *cylinderuse_text[];

View file

@ -723,6 +723,15 @@ static void parse_dc_time(char *line, struct membuffer *str, void *_dc)
static void parse_dc_watertemp(char *line, struct membuffer *str, void *_dc)
{ (void) str; struct divecomputer *dc = _dc; dc->watertemp = get_temperature(line); }
int get_divemode(const char *divemodestring) {
for (int i = 0; i < NUM_DC_TYPE; i++) {
if (!strcmp(divemodestring, divemode_text[i]))
return i;
}
return 0;
}
static void parse_event_keyvalue(void *_event, const char *key, const char *value)
{
struct event *event = _event;
@ -736,6 +745,8 @@ static void parse_event_keyvalue(void *_event, const char *key, const char *valu
event->value = val;
} else if (!strcmp(key, "name")) {
/* We get the name from the string handling */
} else if (!strcmp(key,"divemode")) {
event->value = get_divemode(value);
} else if (!strcmp(key, "cylinder")) {
/* NOTE! We add one here as a marker that "yes, we got a cylinder index" */
event->gas.index = 1+get_index(value);

View file

@ -428,7 +428,7 @@ static void event_name(char *buffer, char *name)
{
int size = trimspace(buffer);
if (size >= MAX_EVENT_NAME)
size = MAX_EVENT_NAME-1;
size = MAX_EVENT_NAME - 1;
memcpy(name, buffer, size);
name[size] = 0;
}
@ -449,6 +449,24 @@ static void get_dc_type(char *buffer, enum dive_comp_type *dct)
}
}
/* For divemode_text[] (defined in dive.h) determine the index of
* the string contained in the xml divemode attribute and passed
* in buffer, below. Typical xml input would be:
* <event name='modechange' divemode='OC' /> */
static void event_divemode(char *buffer, int *value)
{
int size = trimspace(buffer);
if (size >= MAX_EVENT_NAME)
size = MAX_EVENT_NAME - 1;
buffer[size] = 0x0;
for (int i = 0; i < NUM_DC_TYPE; i++) {
if (!strcmp(buffer,divemode_text[i])) {
*value = i;
break;
}
}
}
#define MATCH(pattern, fn, dest) ({ \
/* Silly type compatibility test */ \
if (0) (fn)("test", dest); \
@ -708,6 +726,8 @@ static void try_to_fill_event(const char *name, char *buf)
return;
if (MATCH("value", get_index, &cur_event.value))
return;
if (MATCH("divemode", event_divemode, &cur_event.value))
return;
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++;

View file

@ -374,7 +374,11 @@ static void save_one_event(struct membuffer *b, struct dive *dive, struct event
put_format(b, "event %d:%02d", FRACTION(ev->time.seconds, 60));
show_index(b, ev->type, "type=", "");
show_index(b, ev->flags, "flags=", "");
show_index(b, ev->value, "value=", "");
if (!strcmp(ev->name,"modechange"))
show_utf8(b, "divemode=", divemode_text[ev->value], "");
else
show_index(b, ev->value, "value=", "");
show_utf8(b, " name=", ev->name, "");
if (event_is_gaschange(ev)) {
struct gasmix *mix = get_gasmix_from_event(dive, ev);

View file

@ -301,7 +301,10 @@ static void save_one_event(struct membuffer *b, struct dive *dive, struct event
put_format(b, " <event time='%d:%02d min'", FRACTION(ev->time.seconds, 60));
show_index(b, ev->type, "type='", "'");
show_index(b, ev->flags, "flags='", "'");
show_index(b, ev->value, "value='", "'");
if (!strcmp(ev->name,"modechange"))
show_utf8(b, divemode_text[ev->value], "divemode='", "'",1);
else
show_index(b, ev->value, "value='", "'");
show_utf8(b, ev->name, " name='", "'", 1);
if (event_is_gaschange(ev)) {
struct gasmix *mix = get_gasmix_from_event(dive, ev);

View file

@ -84,9 +84,10 @@ void DiveEventItem::setupPixmap(struct gasmix *lastgasmix)
#define EVENT_PIXMAP_BIGGER(PIX) QPixmap(QString(PIX)).scaled(sz_bigger, sz_bigger, Qt::KeepAspectRatio, Qt::SmoothTransformation)
if (empty_string(internalEvent->name)) {
setPixmap(EVENT_PIXMAP(":status-warning-icon"));
} else if (same_string_caseinsensitive(internalEvent->name, "OC")) {
setPixmap(EVENT_PIXMAP(":bailout-icon"));
} else if (same_string_caseinsensitive(internalEvent->name, "CCR") || same_string_caseinsensitive(internalEvent->name, "PSCR")) {
} else if (same_string_caseinsensitive(internalEvent->name, "modechange")) {
if (internalEvent->value == 0)
setPixmap(EVENT_PIXMAP(":bailout-icon"));
else
setPixmap(EVENT_PIXMAP(":onCCRLoop-icon"));
} else if (internalEvent->type == SAMPLE_EVENT_BOOKMARK) {
setPixmap(EVENT_PIXMAP(":dive-bookmark-icon"));
@ -192,6 +193,8 @@ void DiveEventItem::setupToolTipString(struct gasmix *lastgasmix)
free_buffer(&mb);
}
*lastgasmix = *mix;
} else if (same_string(internalEvent->name, "modechange")) {
name += tr(": %1").arg(divemode_text[internalEvent->value]);
} else if (value) {
if (type == SAMPLE_EVENT_PO2 && same_string(internalEvent->name, "SP change")) {
name += QString(": %1bar").arg((double)value / 1000, 0, 'f', 1);