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_malloc0 ( sizeof ( VikWaypoint ) );
33 wp->altitude = VIK_DEFAULT_ALTITUDE;
34 wp->name = g_strdup(_("Waypoint"));
38 // Hmmm tempted to put in new constructor
39 void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name)
44 if ( name && name[0] != '\0' )
45 wp->name = g_strdup(name);
50 void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
53 g_free ( wp->comment );
54 wp->comment = comment;
57 void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
60 g_free ( wp->comment );
62 if ( comment && comment[0] != '\0' )
63 wp->comment = g_strdup(comment);
68 void vik_waypoint_set_description(VikWaypoint *wp, const gchar *description)
70 if ( wp->description )
71 g_free ( wp->description );
73 if ( description && description[0] != '\0' )
74 wp->description = g_strdup(description);
76 wp->description = NULL;
79 void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
84 if ( image && image[0] != '\0' )
85 wp->image = g_strdup(image);
88 // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn
91 void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
94 g_free ( wp->symbol );
96 if ( symname && symname[0] != '\0' )
97 wp->symbol = g_strdup(symname);
102 void vik_waypoint_free(VikWaypoint *wp)
105 g_free ( wp->comment );
106 if ( wp->description )
107 g_free ( wp->description );
109 g_free ( wp->image );
111 g_free ( wp->symbol );
115 VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
117 VikWaypoint *new_wp = vik_waypoint_new();
118 new_wp->coord = wp->coord;
119 new_wp->visible = wp->visible;
120 new_wp->altitude = wp->altitude;
121 vik_waypoint_set_name(new_wp,wp->name);
122 vik_waypoint_set_comment(new_wp,wp->comment);
123 vik_waypoint_set_description(new_wp,wp->description);
124 vik_waypoint_set_image(new_wp,wp->image);
125 vik_waypoint_set_symbol(new_wp,wp->symbol);
130 * Take a Waypoint and convert it into a byte array
132 void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
134 GByteArray *b = g_byte_array_new();
137 // This creates space for fixed sized members like gints and whatnot
138 // and copies that amount of data from the waypoint to byte array
139 g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
141 // This allocates space for variant sized strings
142 // and copies that amount of data from the waypoint to byte array
143 #define vwm_append(s) \
144 len = (s) ? strlen(s)+1 : 0; \
145 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
146 if (s) g_byte_array_append(b, (guint8 *)s, len);
148 vwm_append(wp->name);
149 vwm_append(wp->comment);
150 vwm_append(wp->description);
151 vwm_append(wp->image);
152 vwm_append(wp->symbol);
156 g_byte_array_free(b, FALSE);
161 * Take a byte array and convert it into a Waypoint
163 VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
166 VikWaypoint *new_wp = vik_waypoint_new();
167 // This copies the fixed sized elements (i.e. visibility, altitude, image_width, etc...)
168 memcpy(new_wp, data, sizeof(*new_wp));
169 data += sizeof(*new_wp);
171 // Now the variant sized strings...
173 len = *(guint *)data; \
174 data += sizeof(len); \
176 (s) = g_strdup((gchar *)data); \
182 vwu_get(new_wp->name);
183 vwu_get(new_wp->comment);
184 vwu_get(new_wp->description);
185 vwu_get(new_wp->image);
186 vwu_get(new_wp->symbol);