X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/ddc47a46f3a6b10f73c527d63beeaa0b18db980f..d84ade777eb0eb7a33c56c39c92b8c07cc66cd3a:/src/vikwaypoint.c diff --git a/src/vikwaypoint.c b/src/vikwaypoint.c index 87073e5a..3649caeb 100644 --- a/src/vikwaypoint.c +++ b/src/vikwaypoint.c @@ -24,17 +24,29 @@ #include "coords.h" #include "vikcoord.h" #include "vikwaypoint.h" - +#include "globals.h" +#include VikWaypoint *vik_waypoint_new() { - VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) ); - wp->comment = NULL; - wp->image = NULL; - wp->symbol = NULL; + VikWaypoint *wp = g_malloc0 ( sizeof ( VikWaypoint ) ); + wp->altitude = VIK_DEFAULT_ALTITUDE; + wp->name = g_strdup(_("Waypoint")); return wp; } +// Hmmm tempted to put in new constructor +void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name) +{ + if ( wp->name ) + g_free ( wp->name ); + + if ( name && name[0] != '\0' ) + wp->name = g_strdup(name); + else + wp->name = NULL; +} + void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment) { if ( wp->comment ) @@ -53,6 +65,17 @@ void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment) wp->comment = NULL; } +void vik_waypoint_set_description(VikWaypoint *wp, const gchar *description) +{ + if ( wp->description ) + g_free ( wp->description ); + + if ( description && description[0] != '\0' ) + wp->description = g_strdup(description); + else + wp->description = NULL; +} + void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image) { if ( wp->image ) @@ -62,6 +85,7 @@ void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image) wp->image = g_strdup(image); else wp->image = NULL; + // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn } void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname) @@ -79,6 +103,8 @@ void vik_waypoint_free(VikWaypoint *wp) { if ( wp->comment ) g_free ( wp->comment ); + if ( wp->description ) + g_free ( wp->description ); if ( wp->image ) g_free ( wp->image ); if ( wp->symbol ) @@ -90,28 +116,36 @@ VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp) { VikWaypoint *new_wp = vik_waypoint_new(); *new_wp = *wp; - new_wp->comment = NULL; /* if the waypoint had a comment, FOR CRYING OUT LOUD DON'T FREE IT! This lousy bug took me TWO HOURS to figure out... sigh... */ + vik_waypoint_set_name(new_wp,wp->name); vik_waypoint_set_comment(new_wp,wp->comment); - new_wp->image = NULL; + vik_waypoint_set_description(new_wp,wp->description); vik_waypoint_set_image(new_wp,wp->image); - new_wp->symbol = NULL; vik_waypoint_set_symbol(new_wp,wp->symbol); return new_wp; } +/* + * Take a Waypoint and convert it into a byte array + */ void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen) { GByteArray *b = g_byte_array_new(); guint len; + // This creates space for fixed sized members like gints and whatnot + // and copies that amount of data from the waypoint to byte array g_byte_array_append(b, (guint8 *)wp, sizeof(*wp)); + // This allocates space for variant sized strings + // and copies that amount of data from the waypoint to byte array #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->name); vwm_append(wp->comment); + vwm_append(wp->description); vwm_append(wp->image); vwm_append(wp->symbol); @@ -121,13 +155,18 @@ void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen) #undef vwm_append } +/* + * Take a byte array and convert it into a Waypoint + */ VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen) { guint len; VikWaypoint *new_wp = vik_waypoint_new(); + // This copies the fixed sized elements (i.e. visibility, altitude, image_width, etc...) memcpy(new_wp, data, sizeof(*new_wp)); data += sizeof(*new_wp); + // Now the variant sized strings... #define vwu_get(s) \ len = *(guint *)data; \ data += sizeof(len); \ @@ -138,7 +177,9 @@ VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen) } \ data += len; + vwu_get(new_wp->name); vwu_get(new_wp->comment); + vwu_get(new_wp->description); vwu_get(new_wp->image); vwu_get(new_wp->symbol);