]> git.street.me.uk Git - andy/viking.git/blob - src/vikwaypoint.c
Add copyright
[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 #include "globals.h"
28
29
30 VikWaypoint *vik_waypoint_new()
31 {
32   VikWaypoint *wp = g_malloc ( sizeof ( VikWaypoint ) );
33   wp->altitude = VIK_DEFAULT_ALTITUDE;
34   wp->comment = NULL;
35   wp->image = NULL;
36   wp->symbol = NULL;
37   return wp;
38 }
39
40 void 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
47 void 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
58 void 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
69 void 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
80 void vik_waypoint_free(VikWaypoint *wp)
81 {
82   if ( wp->comment )
83     g_free ( wp->comment );
84   if ( wp->image )
85     g_free ( wp->image );
86   if ( wp->symbol )
87     g_free ( wp->symbol );
88   g_free ( wp );
89 }
90
91 VikWaypoint *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);
99   new_wp->symbol = NULL;
100   vik_waypoint_set_symbol(new_wp,wp->symbol);
101   return new_wp;
102 }
103
104 void 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
126 VikWaypoint *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