summaryrefslogtreecommitdiffstats
path: root/save-xml.c
diff options
context:
space:
mode:
authorGravatar Anton Lundin <glance@acc.umu.se>2014-05-07 00:30:13 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-05-08 12:25:32 -0700
commit84414baece62b401ce3a3f7fa5366bfff278cc65 (patch)
tree4a95f8d5cf7a4d55475cc23b4ec9c76bbbb76c16 /save-xml.c
parenteba0790a040981858a4652d802721f0f7ab9801c (diff)
downloadsubsurface-84414baece62b401ce3a3f7fa5366bfff278cc65.tar.gz
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 <glance@acc.umu.se> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'save-xml.c')
-rw-r--r--save-xml.c58
1 files changed, 14 insertions, 44 deletions
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 = " <location>";
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");
+
/*
* Ok, theoretically I guess you could dive at
* exactly 0,0. But we don't support that. So
@@ -192,19 +166,15 @@ static void show_location(struct membuffer *b, struct dive *dive)
* you dove a few meters away.
*/
if (latitude.udeg || longitude.udeg) {
- int len = sprintf(buffer, " <location ");
-
- len += format_location(buffer + len, latitude, longitude);
- if (!dive->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, "</location>\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, ">", "</location>\n", 0);
+ else
+ put_string(b, "/>\n");
}
static void save_overview(struct membuffer *b, struct dive *dive)