X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/50a14534a51f892500ee82f867e8ab2f85b936ae..4f14a010d0a7d09721c32f0d35ffbeae05ea994c:/src/vikwaypoint.c diff --git a/src/vikwaypoint.c b/src/vikwaypoint.c index d5598f5e..57626671 100644 --- a/src/vikwaypoint.c +++ b/src/vikwaypoint.c @@ -20,15 +20,20 @@ */ #include +#include #include "coords.h" #include "vikcoord.h" #include "vikwaypoint.h" +#include "globals.h" + VikWaypoint *vik_waypoint_new() { VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) ); + wp->altitude = VIK_DEFAULT_ALTITUDE; wp->comment = NULL; wp->image = NULL; + wp->symbol = NULL; return wp; } @@ -61,12 +66,25 @@ void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image) wp->image = NULL; } +void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname) +{ + if ( wp->symbol ) + g_free ( wp->symbol ); + + if ( symname && symname[0] != '\0' ) + wp->symbol = g_strdup(symname); + else + wp->symbol = NULL; +} + void vik_waypoint_free(VikWaypoint *wp) { if ( wp->comment ) g_free ( wp->comment ); if ( wp->image ) g_free ( wp->image ); + if ( wp->symbol ) + g_free ( wp->symbol ); g_free ( wp ); } @@ -78,5 +96,55 @@ VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp) vik_waypoint_set_comment(new_wp,wp->comment); new_wp->image = NULL; vik_waypoint_set_image(new_wp,wp->image); + new_wp->symbol = NULL; + vik_waypoint_set_symbol(new_wp,wp->symbol); return new_wp; } + +void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen) +{ + GByteArray *b = g_byte_array_new(); + guint len; + + g_byte_array_append(b, (guint8 *)wp, sizeof(*wp)); + +#define vwm_append(s) \ + len = (s) ? strlen(s)+1 : 0; \ + g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \ + if (s) g_byte_array_append(b, (guint8 *)s, len); + + vwm_append(wp->comment); + vwm_append(wp->image); + vwm_append(wp->symbol); + + *data = b->data; + *datalen = b->len; + g_byte_array_free(b, FALSE); +#undef vwm_append +} + +VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen) +{ + guint len; + VikWaypoint *new_wp = vik_waypoint_new(); + memcpy(new_wp, data, sizeof(*new_wp)); + data += sizeof(*new_wp); + +#define vwu_get(s) \ + len = *(guint *)data; \ + data += sizeof(len); \ + if (len) { \ + (s) = g_strdup((gchar *)data); \ + } else { \ + (s) = NULL; \ + } \ + data += len; + + vwu_get(new_wp->comment); + vwu_get(new_wp->image); + vwu_get(new_wp->symbol); + + return new_wp; +#undef vwu_get +} +