From f297d9f91e5919c180d15be9562b8e15e6dd368e Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 8 Jul 2014 12:29:06 -0700 Subject: Fix picture loading Signed vs unsigned comparisons are such a pain. Since we want offsets to be +/- 30 minutes around the dive we need to allow negative offsets - but duration_t was defined as uint32_t. Signed-off-by: Dirk Hohndel --- dive.c | 2 +- dive.h | 2 +- parse-xml.c | 19 +++++++++++++++++-- save-xml.c | 11 +++++++++-- units.h | 5 +++++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/dive.c b/dive.c index e4ad78cb9..ce04bd54e 100644 --- a/dive.c +++ b/dive.c @@ -2389,7 +2389,7 @@ void dive_create_picture(struct dive *d, char *filename, int shift_time) picture_load_exif_data(p, ×tamp); if (timestamp) { p->offset.seconds = timestamp - d->when + shift_time; - if (p->offset.seconds < -D30MIN || p->offset.seconds > d->duration.seconds + D30MIN) { + if (p->offset.seconds < -D30MIN || p->offset.seconds > (int)d->duration.seconds + D30MIN) { // this picture doesn't belong to this dive free(p); return; diff --git a/dive.h b/dive.h index 09a4dda17..b682265fc 100644 --- a/dive.h +++ b/dive.h @@ -292,7 +292,7 @@ struct dive { /* picture list and methods related to dive picture handling */ struct picture { char *filename; - duration_t offset; + offset_t offset; degrees_t latitude; degrees_t longitude; struct picture *next; diff --git a/parse-xml.c b/parse-xml.c index 3fb013d23..01599d61a 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -412,6 +412,20 @@ static void sampletime(char *buffer, duration_t *time) } } +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; +} + static void duration(char *buffer, duration_t *time) { /* DivingLog 5.08 (and maybe other versions) appear to sometimes @@ -1106,7 +1120,7 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf) if (MATCH("filename.picture", utf8_string, &cur_picture->filename)) return; - if (MATCH("offset.picture", sampletime, &cur_picture->offset)) + if (MATCH("offset.picture", offsettime, &cur_picture->offset)) return; if (MATCH("gps.picture", gps_picture_location, cur_picture)) return; @@ -1317,7 +1331,8 @@ static void event_end(void) if (cur_event.type == 123) { struct picture *pic = alloc_picture(); pic->filename = strdup(cur_event.name); - pic->offset = cur_event.time; + /* 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 { add_event(dc, cur_event.time.seconds, diff --git a/save-xml.c b/save-xml.c index 4cebc3d61..514600dd4 100644 --- a/save-xml.c +++ b/save-xml.c @@ -333,8 +333,15 @@ static void save_picture(struct membuffer *b, struct picture *pic) put_string(b, " offset.seconds) - put_format(b, " offset='%u:%02u min'", FRACTION(pic->offset.seconds, 60)); + if (pic->offset.seconds) { + int offset = pic->offset.seconds; + char sign = '+'; + if (offset < 0) { + sign = '-'; + offset = -offset; + } + put_format(b, " offset='%c%u:%02u min'", sign, FRACTION(offset, 60)); + } if (pic->latitude.udeg || pic->longitude.udeg) { put_degrees(b, pic->latitude, " gps='", " "); put_degrees(b, pic->longitude, "", "'"); diff --git a/units.h b/units.h index f3c02121a..90d7949b0 100644 --- a/units.h +++ b/units.h @@ -69,6 +69,11 @@ typedef struct uint32_t seconds; // durations up to 68 yrs } duration_t; +typedef struct +{ + int32_t seconds; // offsets up to +/- 34 yrs +} offset_t; + typedef struct { int32_t mm; -- cgit v1.2.3-70-g09d2