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