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"
28 #include <glib/gi18n.h>
30 VikWaypoint *vik_waypoint_new()
32 VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) );
33 wp->altitude = VIK_DEFAULT_ALTITUDE;
34 wp->name = g_strdup(_("Waypoint"));
43 // Hmmm tempted to put in new constructor
44 void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name)
49 if ( name && name[0] != '\0' )
50 wp->name = g_strdup(name);
55 void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
58 g_free ( wp->comment );
59 wp->comment = comment;
62 void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
65 g_free ( wp->comment );
67 if ( comment && comment[0] != '\0' )
68 wp->comment = g_strdup(comment);
73 void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
78 if ( image && image[0] != '\0' )
79 wp->image = g_strdup(image);
82 // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn
85 void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
88 g_free ( wp->symbol );
90 if ( symname && symname[0] != '\0' )
91 wp->symbol = g_strdup(symname);
96 void vik_waypoint_free(VikWaypoint *wp)
99 g_free ( wp->comment );
101 g_free ( wp->image );
103 g_free ( wp->symbol );
107 VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
109 VikWaypoint *new_wp = vik_waypoint_new();
112 vik_waypoint_set_name(new_wp,wp->name);
113 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... */
114 vik_waypoint_set_comment(new_wp,wp->comment);
115 new_wp->image = NULL;
116 vik_waypoint_set_image(new_wp,wp->image);
117 new_wp->symbol = NULL;
118 vik_waypoint_set_symbol(new_wp,wp->symbol);
123 * Take a Waypoint and convert it into a byte array
125 void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
127 GByteArray *b = g_byte_array_new();
130 // This creates space for fixed sized members like gints and whatnot
131 // and copies that amount of data from the waypoint to byte array
132 g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
134 // This allocates space for variant sized strings
135 // and copies that amount of data from the waypoint to byte array
136 #define vwm_append(s) \
137 len = (s) ? strlen(s)+1 : 0; \
138 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
139 if (s) g_byte_array_append(b, (guint8 *)s, len);
141 vwm_append(wp->name);
142 vwm_append(wp->comment);
143 vwm_append(wp->image);
144 vwm_append(wp->symbol);
148 g_byte_array_free(b, FALSE);
153 * Take a byte array and convert it into a Waypoint
155 VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
158 VikWaypoint *new_wp = vik_waypoint_new();
159 memcpy(new_wp, data, sizeof(*new_wp));
160 data += sizeof(*new_wp);
163 len = *(guint *)data; \
164 data += sizeof(len); \
166 (s) = g_strdup((gchar *)data); \
172 vwu_get(new_wp->name);
173 vwu_get(new_wp->comment);
174 vwu_get(new_wp->image);
175 vwu_get(new_wp->symbol);