]> git.street.me.uk Git - andy/viking.git/blame - src/vikwaypoint.c
Add function to get a trackpoint by distance along a track.
[andy/viking.git] / src / vikwaypoint.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#include <glib.h>
ddc47a46 23#include <string.h>
50a14534
EB
24#include "coords.h"
25#include "vikcoord.h"
26#include "vikwaypoint.h"
67dc0a98 27#include "globals.h"
98acb9a1 28#include "garminsymbols.h"
c9570f86 29#include <glib/gi18n.h>
acaf7113 30
50a14534
EB
31VikWaypoint *vik_waypoint_new()
32{
6b2f262e 33 VikWaypoint *wp = g_malloc0 ( sizeof ( VikWaypoint ) );
8541f2cf 34 wp->altitude = VIK_DEFAULT_ALTITUDE;
c9570f86 35 wp->name = g_strdup(_("Waypoint"));
50a14534
EB
36 return wp;
37}
38
c9570f86
RN
39// Hmmm tempted to put in new constructor
40void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name)
41{
42 if ( wp->name )
43 g_free ( wp->name );
44
27d267f4 45 wp->name = g_strdup(name);
c9570f86
RN
46}
47
50a14534
EB
48void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
49{
50 if ( wp->comment )
51 g_free ( wp->comment );
52 wp->comment = comment;
53}
54
55void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
56{
57 if ( wp->comment )
58 g_free ( wp->comment );
59
60 if ( comment && comment[0] != '\0' )
61 wp->comment = g_strdup(comment);
62 else
63 wp->comment = NULL;
64}
65
6b2f262e
RN
66void vik_waypoint_set_description(VikWaypoint *wp, const gchar *description)
67{
68 if ( wp->description )
69 g_free ( wp->description );
70
71 if ( description && description[0] != '\0' )
72 wp->description = g_strdup(description);
73 else
74 wp->description = NULL;
75}
76
50a14534
EB
77void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
78{
79 if ( wp->image )
80 g_free ( wp->image );
81
82 if ( image && image[0] != '\0' )
83 wp->image = g_strdup(image);
84 else
85 wp->image = NULL;
ba45ceea 86 // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn
50a14534
EB
87}
88
acaf7113
AF
89void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
90{
fffcc269
SE
91 const gchar *hashed_symname;
92
acaf7113
AF
93 if ( wp->symbol )
94 g_free ( wp->symbol );
95
98acb9a1
RN
96 // NB symbol_pixbuf is just a reference, so no need to free it
97
98 if ( symname && symname[0] != '\0' ) {
fffcc269
SE
99 hashed_symname = a_get_hashed_sym ( symname );
100 if ( hashed_symname )
101 symname = hashed_symname;
102 wp->symbol = g_strdup ( symname );
98acb9a1
RN
103 wp->symbol_pixbuf = a_get_wp_sym ( wp->symbol );
104 }
105 else {
acaf7113 106 wp->symbol = NULL;
98acb9a1
RN
107 wp->symbol_pixbuf = NULL;
108 }
acaf7113
AF
109}
110
50a14534
EB
111void vik_waypoint_free(VikWaypoint *wp)
112{
113 if ( wp->comment )
114 g_free ( wp->comment );
6b2f262e
RN
115 if ( wp->description )
116 g_free ( wp->description );
50a14534
EB
117 if ( wp->image )
118 g_free ( wp->image );
acaf7113
AF
119 if ( wp->symbol )
120 g_free ( wp->symbol );
50a14534
EB
121 g_free ( wp );
122}
123
124VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
125{
126 VikWaypoint *new_wp = vik_waypoint_new();
a601900a
RN
127 new_wp->coord = wp->coord;
128 new_wp->visible = wp->visible;
129 new_wp->altitude = wp->altitude;
f2ef466d
RN
130 new_wp->has_timestamp = wp->has_timestamp;
131 new_wp->timestamp = wp->timestamp;
c9570f86 132 vik_waypoint_set_name(new_wp,wp->name);
50a14534 133 vik_waypoint_set_comment(new_wp,wp->comment);
6b2f262e 134 vik_waypoint_set_description(new_wp,wp->description);
50a14534 135 vik_waypoint_set_image(new_wp,wp->image);
acaf7113 136 vik_waypoint_set_symbol(new_wp,wp->symbol);
50a14534
EB
137 return new_wp;
138}
acaf7113 139
c9570f86
RN
140/*
141 * Take a Waypoint and convert it into a byte array
142 */
ddc47a46
AF
143void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
144{
145 GByteArray *b = g_byte_array_new();
146 guint len;
147
c9570f86
RN
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
ddc47a46
AF
150 g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
151
c9570f86
RN
152 // This allocates space for variant sized strings
153 // and copies that amount of data from the waypoint to byte array
ddc47a46
AF
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);
158
c9570f86 159 vwm_append(wp->name);
ddc47a46 160 vwm_append(wp->comment);
6b2f262e 161 vwm_append(wp->description);
ddc47a46
AF
162 vwm_append(wp->image);
163 vwm_append(wp->symbol);
164
165 *data = b->data;
166 *datalen = b->len;
167 g_byte_array_free(b, FALSE);
168#undef vwm_append
169}
170
c9570f86
RN
171/*
172 * Take a byte array and convert it into a Waypoint
173 */
ddc47a46
AF
174VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
175{
176 guint len;
177 VikWaypoint *new_wp = vik_waypoint_new();
6b2f262e 178 // This copies the fixed sized elements (i.e. visibility, altitude, image_width, etc...)
ddc47a46
AF
179 memcpy(new_wp, data, sizeof(*new_wp));
180 data += sizeof(*new_wp);
181
6b2f262e 182 // Now the variant sized strings...
ddc47a46
AF
183#define vwu_get(s) \
184 len = *(guint *)data; \
185 data += sizeof(len); \
186 if (len) { \
187 (s) = g_strdup((gchar *)data); \
188 } else { \
189 (s) = NULL; \
190 } \
191 data += len;
192
c9570f86 193 vwu_get(new_wp->name);
ddc47a46 194 vwu_get(new_wp->comment);
6b2f262e 195 vwu_get(new_wp->description);
ddc47a46
AF
196 vwu_get(new_wp->image);
197 vwu_get(new_wp->symbol);
198
199 return new_wp;
200#undef vwu_get
201}
202