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