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