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 "garminsymbols.h"
29 #include <glib/gi18n.h>
31 VikWaypoint *vik_waypoint_new()
33 VikWaypoint *wp = g_malloc0 ( sizeof ( VikWaypoint ) );
34 wp->altitude = VIK_DEFAULT_ALTITUDE;
35 wp->name = g_strdup(_("Waypoint"));
39 // Hmmm tempted to put in new constructor
40 void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name)
45 wp->name = g_strdup(name);
48 void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
51 g_free ( wp->comment );
52 wp->comment = comment;
55 void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
58 g_free ( wp->comment );
60 if ( comment && comment[0] != '\0' )
61 wp->comment = g_strdup(comment);
66 void vik_waypoint_set_description(VikWaypoint *wp, const gchar *description)
68 if ( wp->description )
69 g_free ( wp->description );
71 if ( description && description[0] != '\0' )
72 wp->description = g_strdup(description);
74 wp->description = NULL;
77 void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
82 if ( image && image[0] != '\0' )
83 wp->image = g_strdup(image);
86 // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn
89 void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
91 const gchar *hashed_symname;
94 g_free ( wp->symbol );
96 // NB symbol_pixbuf is just a reference, so no need to free it
98 if ( symname && symname[0] != '\0' ) {
99 hashed_symname = a_get_hashed_sym ( symname );
100 if ( hashed_symname )
101 symname = hashed_symname;
102 wp->symbol = g_strdup ( symname );
103 wp->symbol_pixbuf = a_get_wp_sym ( wp->symbol );
107 wp->symbol_pixbuf = NULL;
111 void vik_waypoint_free(VikWaypoint *wp)
114 g_free ( wp->comment );
115 if ( wp->description )
116 g_free ( wp->description );
118 g_free ( wp->image );
120 g_free ( wp->symbol );
124 VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
126 VikWaypoint *new_wp = vik_waypoint_new();
127 new_wp->coord = wp->coord;
128 new_wp->visible = wp->visible;
129 new_wp->altitude = wp->altitude;
130 new_wp->has_timestamp = wp->has_timestamp;
131 new_wp->timestamp = wp->timestamp;
132 vik_waypoint_set_name(new_wp,wp->name);
133 vik_waypoint_set_comment(new_wp,wp->comment);
134 vik_waypoint_set_description(new_wp,wp->description);
135 vik_waypoint_set_image(new_wp,wp->image);
136 vik_waypoint_set_symbol(new_wp,wp->symbol);
141 * Take a Waypoint and convert it into a byte array
143 void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
145 GByteArray *b = g_byte_array_new();
148 // This creates space for fixed sized members like gints and whatnot
149 // and copies that amount of data from the waypoint to byte array
150 g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
152 // This allocates space for variant sized strings
153 // and copies that amount of data from the waypoint to byte array
154 #define vwm_append(s) \
155 len = (s) ? strlen(s)+1 : 0; \
156 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
157 if (s) g_byte_array_append(b, (guint8 *)s, len);
159 vwm_append(wp->name);
160 vwm_append(wp->comment);
161 vwm_append(wp->description);
162 vwm_append(wp->image);
163 vwm_append(wp->symbol);
167 g_byte_array_free(b, FALSE);
172 * Take a byte array and convert it into a Waypoint
174 VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
177 VikWaypoint *new_wp = vik_waypoint_new();
178 // This copies the fixed sized elements (i.e. visibility, altitude, image_width, etc...)
179 memcpy(new_wp, data, sizeof(*new_wp));
180 data += sizeof(*new_wp);
182 // Now the variant sized strings...
184 len = *(guint *)data; \
185 data += sizeof(len); \
187 (s) = g_strdup((gchar *)data); \
193 vwu_get(new_wp->name);
194 vwu_get(new_wp->comment);
195 vwu_get(new_wp->description);
196 vwu_get(new_wp->image);
197 vwu_get(new_wp->symbol);