]> git.street.me.uk Git - andy/viking.git/blame_incremental - src/vikwaypoint.c
Fix CPU usage going to 100% when statusbar items update is called.
[andy/viking.git] / src / vikwaypoint.c
... / ...
CommitLineData
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>
23#include <string.h>
24#include "coords.h"
25#include "vikcoord.h"
26#include "vikwaypoint.h"
27#include "globals.h"
28#include <glib/gi18n.h>
29
30VikWaypoint *vik_waypoint_new()
31{
32 VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) );
33 wp->altitude = VIK_DEFAULT_ALTITUDE;
34 wp->name = g_strdup(_("Waypoint"));
35 wp->comment = NULL;
36 wp->image = NULL;
37 wp->image_width = 0;
38 wp->image_height = 0;
39 wp->symbol = NULL;
40 return wp;
41}
42
43// Hmmm tempted to put in new constructor
44void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name)
45{
46 if ( wp->name )
47 g_free ( wp->name );
48
49 if ( name && name[0] != '\0' )
50 wp->name = g_strdup(name);
51 else
52 wp->name = NULL;
53}
54
55void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
56{
57 if ( wp->comment )
58 g_free ( wp->comment );
59 wp->comment = comment;
60}
61
62void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
63{
64 if ( wp->comment )
65 g_free ( wp->comment );
66
67 if ( comment && comment[0] != '\0' )
68 wp->comment = g_strdup(comment);
69 else
70 wp->comment = NULL;
71}
72
73void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
74{
75 if ( wp->image )
76 g_free ( wp->image );
77
78 if ( image && image[0] != '\0' )
79 wp->image = g_strdup(image);
80 else
81 wp->image = NULL;
82 // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn
83}
84
85void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
86{
87 if ( wp->symbol )
88 g_free ( wp->symbol );
89
90 if ( symname && symname[0] != '\0' )
91 wp->symbol = g_strdup(symname);
92 else
93 wp->symbol = NULL;
94}
95
96void vik_waypoint_free(VikWaypoint *wp)
97{
98 if ( wp->comment )
99 g_free ( wp->comment );
100 if ( wp->image )
101 g_free ( wp->image );
102 if ( wp->symbol )
103 g_free ( wp->symbol );
104 g_free ( wp );
105}
106
107VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
108{
109 VikWaypoint *new_wp = vik_waypoint_new();
110 *new_wp = *wp;
111 new_wp->name = NULL;
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);
119 return new_wp;
120}
121
122/*
123 * Take a Waypoint and convert it into a byte array
124 */
125void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
126{
127 GByteArray *b = g_byte_array_new();
128 guint len;
129
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));
133
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);
140
141 vwm_append(wp->name);
142 vwm_append(wp->comment);
143 vwm_append(wp->image);
144 vwm_append(wp->symbol);
145
146 *data = b->data;
147 *datalen = b->len;
148 g_byte_array_free(b, FALSE);
149#undef vwm_append
150}
151
152/*
153 * Take a byte array and convert it into a Waypoint
154 */
155VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
156{
157 guint len;
158 VikWaypoint *new_wp = vik_waypoint_new();
159 memcpy(new_wp, data, sizeof(*new_wp));
160 data += sizeof(*new_wp);
161
162#define vwu_get(s) \
163 len = *(guint *)data; \
164 data += sizeof(len); \
165 if (len) { \
166 (s) = g_strdup((gchar *)data); \
167 } else { \
168 (s) = NULL; \
169 } \
170 data += len;
171
172 vwu_get(new_wp->name);
173 vwu_get(new_wp->comment);
174 vwu_get(new_wp->image);
175 vwu_get(new_wp->symbol);
176
177 return new_wp;
178#undef vwu_get
179}
180