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