]> git.street.me.uk Git - andy/viking.git/blob - src/vikwaypoint.c
[QA] Move file related function into file.c and rename for general reuse
[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 #include "garminsymbols.h"
29 #include <glib/gi18n.h>
30
31 VikWaypoint *vik_waypoint_new()
32 {
33   VikWaypoint *wp = g_malloc0 ( sizeof ( VikWaypoint ) );
34   wp->altitude = VIK_DEFAULT_ALTITUDE;
35   wp->name = g_strdup(_("Waypoint"));
36   return wp;
37 }
38
39 // Hmmm tempted to put in new constructor
40 void vik_waypoint_set_name(VikWaypoint *wp, const gchar *name)
41 {
42   if ( wp->name )
43     g_free ( wp->name );
44
45   if ( name && name[0] != '\0' )
46     wp->name = g_strdup(name);
47   else
48     wp->name = NULL;
49 }
50
51 void vik_waypoint_set_comment_no_copy(VikWaypoint *wp, gchar *comment)
52 {
53   if ( wp->comment )
54     g_free ( wp->comment );
55   wp->comment = comment;
56 }
57
58 void vik_waypoint_set_comment(VikWaypoint *wp, const gchar *comment)
59 {
60   if ( wp->comment )
61     g_free ( wp->comment );
62
63   if ( comment && comment[0] != '\0' )
64     wp->comment = g_strdup(comment);
65   else
66     wp->comment = NULL;
67 }
68
69 void vik_waypoint_set_description(VikWaypoint *wp, const gchar *description)
70 {
71   if ( wp->description )
72     g_free ( wp->description );
73
74   if ( description && description[0] != '\0' )
75     wp->description = g_strdup(description);
76   else
77     wp->description = NULL;
78 }
79
80 void vik_waypoint_set_image(VikWaypoint *wp, const gchar *image)
81 {
82   if ( wp->image )
83     g_free ( wp->image );
84
85   if ( image && image[0] != '\0' )
86     wp->image = g_strdup(image);
87   else
88     wp->image = NULL;
89   // NOTE - ATM the image (thumbnail) size is calculated on demand when needed to be first drawn
90 }
91
92 void vik_waypoint_set_symbol(VikWaypoint *wp, const gchar *symname)
93 {
94   if ( wp->symbol )
95     g_free ( wp->symbol );
96
97   // NB symbol_pixbuf is just a reference, so no need to free it
98
99   if ( symname && symname[0] != '\0' ) {
100     wp->symbol = g_strdup(symname);
101     wp->symbol_pixbuf = a_get_wp_sym ( wp->symbol );
102   }
103   else {
104     wp->symbol = NULL;
105     wp->symbol_pixbuf = NULL;
106   }
107 }
108
109 void vik_waypoint_free(VikWaypoint *wp)
110 {
111   if ( wp->comment )
112     g_free ( wp->comment );
113   if ( wp->description )
114     g_free ( wp->description );
115   if ( wp->image )
116     g_free ( wp->image );
117   if ( wp->symbol )
118     g_free ( wp->symbol );
119   g_free ( wp );
120 }
121
122 VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp)
123 {
124   VikWaypoint *new_wp = vik_waypoint_new();
125   new_wp->coord = wp->coord;
126   new_wp->visible = wp->visible;
127   new_wp->altitude = wp->altitude;
128   vik_waypoint_set_name(new_wp,wp->name);
129   vik_waypoint_set_comment(new_wp,wp->comment);
130   vik_waypoint_set_description(new_wp,wp->description);
131   vik_waypoint_set_image(new_wp,wp->image);
132   vik_waypoint_set_symbol(new_wp,wp->symbol);
133   return new_wp;
134 }
135
136 /*
137  * Take a Waypoint and convert it into a byte array
138  */
139 void vik_waypoint_marshall ( VikWaypoint *wp, guint8 **data, guint *datalen)
140 {
141   GByteArray *b = g_byte_array_new();
142   guint len;
143
144   // This creates space for fixed sized members like gints and whatnot
145   //  and copies that amount of data from the waypoint to byte array
146   g_byte_array_append(b, (guint8 *)wp, sizeof(*wp));
147
148   // This allocates space for variant sized strings
149   //  and copies that amount of data from the waypoint to byte array
150 #define vwm_append(s) \
151   len = (s) ? strlen(s)+1 : 0; \
152   g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
153   if (s) g_byte_array_append(b, (guint8 *)s, len);
154
155   vwm_append(wp->name);
156   vwm_append(wp->comment);
157   vwm_append(wp->description);
158   vwm_append(wp->image);
159   vwm_append(wp->symbol);
160
161   *data = b->data;
162   *datalen = b->len;
163   g_byte_array_free(b, FALSE);
164 #undef vwm_append
165 }
166
167 /*
168  * Take a byte array and convert it into a Waypoint
169  */
170 VikWaypoint *vik_waypoint_unmarshall (guint8 *data, guint datalen)
171 {
172   guint len;
173   VikWaypoint *new_wp = vik_waypoint_new();
174   // This copies the fixed sized elements (i.e. visibility, altitude, image_width, etc...)
175   memcpy(new_wp, data, sizeof(*new_wp));
176   data += sizeof(*new_wp);
177
178   // Now the variant sized strings...
179 #define vwu_get(s) \
180   len = *(guint *)data; \
181   data += sizeof(len); \
182   if (len) { \
183     (s) = g_strdup((gchar *)data); \
184   } else { \
185     (s) = NULL; \
186   } \
187   data += len;
188
189   vwu_get(new_wp->name);
190   vwu_get(new_wp->comment);
191   vwu_get(new_wp->description);
192   vwu_get(new_wp->image); 
193   vwu_get(new_wp->symbol);
194   
195   return new_wp;
196 #undef vwu_get
197 }
198