]> git.street.me.uk Git - andy/viking.git/blame - src/vikwaypoint.c
Add function to return an average moving speed for 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"
50a14534 28
acaf7113 29
50a14534
EB
30VikWaypoint *vik_waypoint_new()
31{
32 VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) );
8541f2cf 33 wp->altitude = VIK_DEFAULT_ALTITUDE;
50a14534
EB
34 wp->comment = NULL;
35 wp->image = NULL;
ba45ceea
RN
36 wp->image_width = 0;
37 wp->image_height = 0;
acaf7113 38 wp->symbol = NULL;
50a14534
EB
39 return wp;
40}
41
42void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
43{
44 if ( wp->comment )
45 g_free ( wp->comment );
46 wp->comment = comment;
47}
48
49void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
50{
51 if ( wp->comment )
52 g_free ( wp->comment );
53
54 if ( comment && comment[0] != '\0' )
55 wp->comment = g_strdup(comment);
56 else
57 wp->comment = NULL;
58}
59
60void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
61{
62 if ( wp->image )
63 g_free ( wp->image );
64
65 if ( image && image[0] != '\0' )
66 wp->image = g_strdup(image);
67 else
68 wp->image = NULL;
ba45ceea 69 // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn
50a14534
EB
70}
71
acaf7113
AF
72void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
73{
74 if ( wp->symbol )
75 g_free ( wp->symbol );
76
77 if ( symname && symname[0] != '\0' )
78 wp->symbol = g_strdup(symname);
79 else
80 wp->symbol = NULL;
81}
82
50a14534
EB
83void vik_waypoint_free(VikWaypoint *wp)
84{
85 if ( wp->comment )
86 g_free ( wp->comment );
87 if ( wp->image )
88 g_free ( wp->image );
acaf7113
AF
89 if ( wp->symbol )
90 g_free ( wp->symbol );
50a14534
EB
91 g_free ( wp );
92}
93
94VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
95{
96 VikWaypoint *new_wp = vik_waypoint_new();
97 *new_wp = *wp;
98 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... */
99 vik_waypoint_set_comment(new_wp,wp->comment);
100 new_wp->image = NULL;
101 vik_waypoint_set_image(new_wp,wp->image);
acaf7113
AF
102 new_wp->symbol = NULL;
103 vik_waypoint_set_symbol(new_wp,wp->symbol);
50a14534
EB
104 return new_wp;
105}
acaf7113 106
ddc47a46
AF
107void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
108{
109 GByteArray *b = g_byte_array_new();
110 guint len;
111
112 g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
113
114#define vwm_append(s) \
115 len = (s) ? strlen(s)+1 : 0; \
116 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
117 if (s) g_byte_array_append(b, (guint8 *)s, len);
118
119 vwm_append(wp->comment);
120 vwm_append(wp->image);
121 vwm_append(wp->symbol);
122
123 *data = b->data;
124 *datalen = b->len;
125 g_byte_array_free(b, FALSE);
126#undef vwm_append
127}
128
129VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
130{
131 guint len;
132 VikWaypoint *new_wp = vik_waypoint_new();
133 memcpy(new_wp, data, sizeof(*new_wp));
134 data += sizeof(*new_wp);
135
136#define vwu_get(s) \
137 len = *(guint *)data; \
138 data += sizeof(len); \
139 if (len) { \
140 (s) = g_strdup((gchar *)data); \
141 } else { \
142 (s) = NULL; \
143 } \
144 data += len;
145
146 vwu_get(new_wp->comment);
147 vwu_get(new_wp->image);
148 vwu_get(new_wp->symbol);
149
150 return new_wp;
151#undef vwu_get
152}
153