From 84414baece62b401ce3a3f7fa5366bfff278cc65 Mon Sep 17 00:00:00 2001 From: Anton Lundin Date: Wed, 7 May 2014 00:30:13 +0200 Subject: Rewrite show_location to use membuffer primitives This simplifies the logic and doesn't rely on show_utf8 to print empty blocks. Signed-off-by: Anton Lundin Signed-off-by: Dirk Hohndel --- save-xml.c | 58 ++++++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) (limited to 'save-xml.c') diff --git a/save-xml.c b/save-xml.c index 196143286..ddaa8cd2a 100644 --- a/save-xml.c +++ b/save-xml.c @@ -148,43 +148,17 @@ static void save_salinity(struct membuffer *b, struct divecomputer *dc) put_string(b, " />\n"); } -/* - * Format degrees to within 6 decimal places. That's about 0.1m - * on a great circle (ie longitude at equator). And micro-degrees - * is also enough to fit in a fixed-point 32-bit integer. - */ -static int format_degrees(char *buffer, degrees_t value) -{ - int udeg = value.udeg; - const char *sign = ""; - - if (udeg < 0) { - sign = "-"; - udeg = -udeg; - } - return sprintf(buffer, "%s%u.%06u", - sign, udeg / 1000000, udeg % 1000000); -} - -static int format_location(char *buffer, degrees_t latitude, degrees_t longitude) -{ - int len = sprintf(buffer, "gps='"); - - len += format_degrees(buffer + len, latitude); - buffer[len++] = ' '; - len += format_degrees(buffer + len, longitude); - buffer[len++] = '\''; - - return len; -} - static void show_location(struct membuffer *b, struct dive *dive) { - char buffer[80]; - const char *prefix = " "; degrees_t latitude = dive->latitude; degrees_t longitude = dive->longitude; + /* Should we write a location tag at all? */ + if (!(latitude.udeg || longitude.udeg) && !dive->location) + return; + + put_string(b, " location || dive->location[0] == '\0') { - memcpy(buffer + len, "/>\n\0", 5); - put_string(b, buffer); - return; - } - buffer[len++] = '>'; - buffer[len] = 0; - prefix = buffer; + put_degrees(b, latitude, " gps='", " "); + put_degrees(b, longitude, "", "'"); } - show_utf8(b, dive->location, prefix, "\n", 0); + + /* Do we have a location name or should we write a empty tag? */ + if (dive->location && dive->location[0] != '\0') + show_utf8(b, dive->location, ">", "\n", 0); + else + put_string(b, "/>\n"); } static void save_overview(struct membuffer *b, struct dive *dive) -- cgit v1.2.3-70-g09d2