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>
This commit is contained in:
Anton Lundin 2014-05-07 00:30:13 +02:00 committed by Dirk Hohndel
parent eba0790a04
commit 84414baece

View file

@ -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)