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"
30 VikWaypoint *vik_waypoint_new()
32 VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) );
33 wp->altitude = VIK_DEFAULT_ALTITUDE;
40 void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
43 g_free ( wp->comment );
44 wp->comment = comment;
47 void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
50 g_free ( wp->comment );
52 if ( comment && comment[0] != '\0' )
53 wp->comment = g_strdup(comment);
58 void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
63 if ( image && image[0] != '\0' )
64 wp->image = g_strdup(image);
69 void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
72 g_free ( wp->symbol );
74 if ( symname && symname[0] != '\0' )
75 wp->symbol = g_strdup(symname);
80 void vik_waypoint_free(VikWaypoint *wp)
83 g_free ( wp->comment );
87 g_free ( wp->symbol );
91 VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
93 VikWaypoint *new_wp = vik_waypoint_new();
95 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... */
96 vik_waypoint_set_comment(new_wp,wp->comment);
98 vik_waypoint_set_image(new_wp,wp->image);
99 new_wp->symbol = NULL;
100 vik_waypoint_set_symbol(new_wp,wp->symbol);
104 void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
106 GByteArray *b = g_byte_array_new();
109 g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
111 #define vwm_append(s) \
112 len = (s) ? strlen(s)+1 : 0; \
113 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
114 if (s) g_byte_array_append(b, (guint8 *)s, len);
116 vwm_append(wp->comment);
117 vwm_append(wp->image);
118 vwm_append(wp->symbol);
122 g_byte_array_free(b, FALSE);
126 VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
129 VikWaypoint *new_wp = vik_waypoint_new();
130 memcpy(new_wp, data, sizeof(*new_wp));
131 data += sizeof(*new_wp);
134 len = *(guint *)data; \
135 data += sizeof(len); \
137 (s) = g_strdup((gchar *)data); \
143 vwu_get(new_wp->comment);
144 vwu_get(new_wp->image);
145 vwu_get(new_wp->symbol);