Load and save the dc type for CCR dives

Oddly we already had code to load this from XML, but nothing else.
This makes the load from XML work like the rest of our code and adds the
save to XML plus the load and save for the git format.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-11-16 23:11:18 +00:00
parent 202c5cbfeb
commit 23304f69c0
6 changed files with 56 additions and 30 deletions

1
dive.c
View file

@ -27,6 +27,7 @@ static const char *default_tags[] = {
};
const char *cylinderuse_text[] = { "OC-gas", "diluent", "oxygen" };
const char *dctype_text[] = { "OC", "CCR", "PSCR" };
int event_is_gaschange(struct event *ev)

3
dive.h
View file

@ -47,10 +47,11 @@ extern "C" {
#include <stdbool.h>
#endif
enum dive_comp_type {OC, CCR, PSCR}; // Flags (Open-circuit and Closed-circuit-rebreather) for setting dive computer type
enum dive_comp_type {OC, CCR, PSCR, NUM_DC_TYPE}; // Flags (Open-circuit and Closed-circuit-rebreather) for setting dive computer type
enum cylinderuse {OC_GAS, DILUENT, OXYGEN, NUM_GAS_USE}; // The different uses for cylinders
extern const char *cylinderuse_text[];
extern const char *dctype_text[];
struct gasmix {
fraction_t o2;

View file

@ -130,6 +130,15 @@ static duration_t get_duration(const char *line)
return d;
}
static enum dive_comp_type get_dctype(const char *line)
{
for (enum dive_comp_type i = 0; i < NUM_DC_TYPE; i++) {
if (strcmp(line, dctype_text[i]) == 0)
return i;
}
return 0;
}
static int get_index(const char *line)
{ return atoi(line); }
static int get_hex(const char *line)
@ -500,6 +509,9 @@ static void parse_dc_diveid(char *line, struct membuffer *str, void *_dc)
static void parse_dc_duration(char *line, struct membuffer *str, void *_dc)
{ struct divecomputer *dc = _dc; dc->duration = get_duration(line); }
static void parse_dc_dctype(char *line, struct membuffer *str, void *_dc)
{ struct divecomputer *dc = _dc; dc->dctype = get_dctype(line); }
static void parse_dc_maxdepth(char *line, struct membuffer *str, void *_dc)
{ struct divecomputer *dc = _dc; dc->maxdepth = get_depth(line); }
@ -732,7 +744,7 @@ static void parse_picture_gps(char *line, struct membuffer *str, void *_pic)
struct keyword_action dc_action[] = {
#undef D
#define D(x) { #x, parse_dc_ ## x }
D(airtemp), D(date), D(deviceid), D(diveid), D(duration),
D(airtemp), D(date), D(dctype), D(deviceid), D(diveid), D(duration),
D(event), D(keyvalue), D(maxdepth), D(meandepth), D(model), D(salinity),
D(surfacepressure), D(surfacetime), D(time), D(watertemp),
};

View file

@ -29,6 +29,25 @@ static xmlDoc *test_xslt_transforms(xmlDoc *doc, const char **params);
struct dive_table dive_table;
struct dive_table *target_table = NULL;
/* 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
}
/*
* Add a dive into the dive_table array
*/
@ -319,10 +338,12 @@ static void pressure(char *buffer, pressure_t *pressure)
static void cylinder_use(char *buffer, enum cylinderuse *cyl_use)
{
for (enum cylinderuse i = 0; i < NUM_GAS_USE; i++) {
if (same_string(buffer, cylinderuse_text[i])) {
*cyl_use = i;
return;
if (trimspace(buffer)) {
for (enum cylinderuse i = 0; i < NUM_GAS_USE; i++) {
if (same_string(buffer, cylinderuse_text[i])) {
*cyl_use = i;
return;
}
}
}
}
@ -522,26 +543,6 @@ static void cylindersize(char *buffer, volume_t *volume)
}
}
/* 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
}
static void utf8_string(char *buffer, void *_res)
{
char **res = _res;
@ -561,11 +562,15 @@ static void event_name(char *buffer, char *name)
}
/* Extract the dive computer type from the xml text buffer */
static void get_dc_type(char *buffer, enum dive_comp_type *i)
static void get_dc_type(char *buffer, enum dive_comp_type *dct)
{
if((trimspace(buffer)) && (strcmp(buffer,"CCR") == 0))
*i = CCR; // if the xml string = "CCR", set dc-type to CCR
} // otherwise the default dc-type is used (OC)
if (trimspace(buffer)) {
for (enum dive_comp_type i = 0; i < NUM_DC_TYPE; i++) {
if (strcmp(buffer, dctype_text[i]) == 0)
*dct = i;
}
}
}
#define MATCH(pattern, fn, dest) ({ \
/* Silly type compatibility test */ \

View file

@ -339,6 +339,8 @@ static void save_dc(struct membuffer *b, struct dive *dive, struct divecomputer
show_date(b, dc->when);
if (dc->duration.seconds && dc->duration.seconds != dive->dc.duration.seconds)
put_duration(b, dc->duration, "duration ", "min\n");
if (dc->dctype != OC)
put_format(b, "dctype %s\n", dctype_text[dc->dctype]);
save_depths(b, dc);
save_temperatures(b, dc);

View file

@ -351,6 +351,11 @@ static void save_dc(struct membuffer *b, struct dive *dive, struct divecomputer
show_date(b, dc->when);
if (dc->duration.seconds && dc->duration.seconds != dive->dc.duration.seconds)
put_duration(b, dc->duration, " duration='", " min'");
if (dc->dctype != OC) {
for (enum dive_comp_type i = 0; i < NUM_DC_TYPE; i++)
if (dc->dctype == i)
show_utf8(b, dctype_text[i], " dctype='", "'", 1);
}
put_format(b, ">\n");
save_depths(b, dc);
save_temperatures(b, dc);