2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "vikwaypoint.h"
29 VikWaypoint *vik_waypoint_new()
31 VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) );
32 wp->altitude = VIK_DEFAULT_ALTITUDE;
39 void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
42 g_free ( wp->comment );
43 wp->comment = comment;
46 void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
49 g_free ( wp->comment );
51 if ( comment && comment[0] != '\0' )
52 wp->comment = g_strdup(comment);
57 void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
62 if ( image && image[0] != '\0' )
63 wp->image = g_strdup(image);
68 void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
71 g_free ( wp->symbol );
73 if ( symname && symname[0] != '\0' )
74 wp->symbol = g_strdup(symname);
79 void vik_waypoint_free(VikWaypoint *wp)
82 g_free ( wp->comment );
86 g_free ( wp->symbol );
90 VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
92 VikWaypoint *new_wp = vik_waypoint_new();
94 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... */
95 vik_waypoint_set_comment(new_wp,wp->comment);
97 vik_waypoint_set_image(new_wp,wp->image);
98 new_wp->symbol = NULL;
99 vik_waypoint_set_symbol(new_wp,wp->symbol);
103 void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
105 GByteArray *b = g_byte_array_new();
108 g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
110 #define vwm_append(s) \
111 len = (s) ? strlen(s)+1 : 0; \
112 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
113 if (s) g_byte_array_append(b, (guint8 *)s, len);
115 vwm_append(wp->comment);
116 vwm_append(wp->image);
117 vwm_append(wp->symbol);
121 g_byte_array_free(b, FALSE);
125 VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
128 VikWaypoint *new_wp = vik_waypoint_new();
129 memcpy(new_wp, data, sizeof(*new_wp));
130 data += sizeof(*new_wp);
133 len = *(guint *)data; \
134 data += sizeof(len); \
136 (s) = g_strdup((gchar *)data); \
142 vwu_get(new_wp->comment);
143 vwu_get(new_wp->image);
144 vwu_get(new_wp->symbol);