g_free ( tr->comment );
if ( tr->description )
g_free ( tr->description );
- g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
+ g_list_foreach ( tr->trackpoints, (GFunc) vik_trackpoint_free, NULL );
g_list_free( tr->trackpoints );
if (tr->property_dialog)
if ( GTK_IS_WIDGET(tr->property_dialog) )
VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
{
VikTrack *new_tr = vik_track_new();
- VikTrackpoint *new_tp;
- GList *tp_iter = tr->trackpoints;
+ new_tr->name = g_strdup(tr->name);
new_tr->visible = tr->visible;
new_tr->is_route = tr->is_route;
new_tr->draw_name_mode = tr->draw_name_mode;
new_tr->trackpoints = NULL;
if ( copy_points )
{
+ GList *tp_iter = tr->trackpoints;
while ( tp_iter )
{
- new_tp = g_malloc ( sizeof ( VikTrackpoint ) );
- *new_tp = *((VikTrackpoint *)(tp_iter->data));
- new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
+ VikTrackpoint *new_tp = vik_trackpoint_copy ( (VikTrackpoint*)(tp_iter->data) );
+ new_tr->trackpoints = g_list_prepend ( new_tr->trackpoints, new_tp );
tp_iter = tp_iter->next;
}
+ if ( new_tr->trackpoints )
+ new_tr->trackpoints = g_list_reverse ( new_tr->trackpoints );
}
vik_track_set_name(new_tr,tr->name);
vik_track_set_comment(new_tr,tr->comment);
void vik_trackpoint_free(VikTrackpoint *tp)
{
+ g_free(tp->name);
g_free(tp);
}
+void vik_trackpoint_set_name(VikTrackpoint *tp, const gchar *name)
+{
+ if ( tp->name )
+ g_free ( tp->name );
+
+ // If the name is blank then completely remove it
+ if ( name && name[0] == '\0' )
+ tp->name = NULL;
+ else if ( name )
+ tp->name = g_strdup(name);
+ else
+ tp->name = NULL;
+}
+
VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
{
- VikTrackpoint *rv = vik_trackpoint_new();
- *rv = *tp;
- return rv;
+ VikTrackpoint *new_tp = vik_trackpoint_new();
+ memcpy ( new_tp, tp, sizeof(VikTrackpoint) );
+ if ( tp->name )
+ new_tp->name = g_strdup (tp->name);
+ return new_tp;
}
/**
track_recalculate_bounds_last_tp ( tr );
}
+/**
+ * vik_track_get_length_to_trackpoint:
+ *
+ */
+gdouble vik_track_get_length_to_trackpoint (const VikTrack *tr, const VikTrackpoint *tp)
+{
+ gdouble len = 0.0;
+ if ( tr->trackpoints )
+ {
+ GList *iter = tr->trackpoints->next;
+ while (iter)
+ {
+ VikTrackpoint *tp1 = VIK_TRACKPOINT(iter->data);
+ if ( ! tp1->newsegment )
+ len += vik_coord_diff ( &(tp1->coord),
+ &(VIK_TRACKPOINT(iter->prev->data)->coord) );
+
+ // Exit when we reach the desired point
+ if ( tp1 == tp )
+ break;
+
+ iter = iter->next;
+ }
+ }
+ return len;
+}
+
gdouble vik_track_get_length(const VikTrack *tr)
{
gdouble len = 0.0;
return min_alt_tp;
}
+VikTrackpoint *vik_track_get_tp_first( const VikTrack *tr )
+{
+ if ( !tr->trackpoints )
+ return NULL;
+
+ return (VikTrackpoint*)g_list_first(tr->trackpoints)->data;
+}
+
+VikTrackpoint *vik_track_get_tp_last ( const VikTrack *tr )
+{
+ if ( !tr->trackpoints )
+ return NULL;
+
+ return (VikTrackpoint*)g_list_last(tr->trackpoints)->data;
+}
+
gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
{
*min_alt = 25000;
intp = b->len;
g_byte_array_append(b, (guint8 *)&len, sizeof(len));
+ // This allocates space for variant sized strings
+ // and copies that amount of data from the track to byte array
+#define vtm_append(s) \
+ len = (s) ? strlen(s)+1 : 0; \
+ g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
+ if (s) g_byte_array_append(b, (guint8 *)s, len);
+
tps = tr->trackpoints;
ntp = 0;
while (tps) {
g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
+ vtm_append(VIK_TRACKPOINT(tps->data)->name);
tps = tps->next;
ntp++;
}
*(guint *)(b->data + intp) = ntp;
- // This allocates space for variant sized strings
- // and copies that amount of data from the track to byte array
-#define vtm_append(s) \
- len = (s) ? strlen(s)+1 : 0; \
- g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
- if (s) g_byte_array_append(b, (guint8 *)s, len);
-
vtm_append(tr->name);
vtm_append(tr->comment);
vtm_append(tr->description);
ntp = *(guint *)data;
data += sizeof(ntp);
- for (i=0; i<ntp; i++) {
- new_tp = vik_trackpoint_new();
- memcpy(new_tp, data, sizeof(*new_tp));
- data += sizeof(*new_tp);
- new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
- }
-
#define vtu_get(s) \
len = *(guint *)data; \
data += sizeof(len); \
} \
data += len;
+ for (i=0; i<ntp; i++) {
+ new_tp = vik_trackpoint_new();
+ memcpy(new_tp, data, sizeof(*new_tp));
+ data += sizeof(*new_tp);
+ vtu_get(new_tp->name);
+ new_tr->trackpoints = g_list_prepend(new_tr->trackpoints, new_tp);
+ }
+ if ( new_tr->trackpoints )
+ new_tr->trackpoints = g_list_reverse(new_tr->trackpoints);
+
vtu_get(new_tr->name);
vtu_get(new_tr->comment);
vtu_get(new_tr->description);
return rv;
}
+/**
+ * Function to compare two tracks by their first timestamp
+ **/
+int vik_track_compare_timestamp (const void *x, const void *y)
+{
+ VikTrack *a = (VikTrack *)x;
+ VikTrack *b = (VikTrack *)y;
+
+ VikTrackpoint *tpa = NULL;
+ VikTrackpoint *tpb = NULL;
+
+ if ( a->trackpoints )
+ tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
+
+ if ( b->trackpoints )
+ tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
+
+ if ( tpa && tpb ) {
+ if ( tpa->timestamp < tpb->timestamp )
+ return -1;
+ if ( tpa->timestamp > tpb->timestamp )
+ return 1;
+ }
+
+ if ( tpa && !tpb )
+ return 1;
+
+ if ( !tpa && tpb )
+ return -1;
+
+ return 0;
+}