+static int gpx_waypoint_and_name_compar(const void *x, const void *y)
+{
+ gpx_waypoint_and_name *a = (gpx_waypoint_and_name *)x;
+ gpx_waypoint_and_name *b = (gpx_waypoint_and_name *)y;
+ return strcmp(a->name,b->name);
+}
+
+/* Function to compare two tracks by their first timestamp */
+static int gpx_track_and_timestamp_compar(const void *x, const void *y)
+{
+ gpx_track_and_timestamp *a = (gpx_track_and_timestamp *)x;
+ gpx_track_and_timestamp *b = (gpx_track_and_timestamp *)y;
+ if (a->first_timestamp < b->first_timestamp)
+ {
+ return -1;
+ }
+ if (a->first_timestamp > b->first_timestamp)
+ {
+ return 1;
+ }
+ return 0;
+}
+
+void a_gpx_write_file( VikTrwLayer *vtl, FILE *f )
+{
+ a_gpx_write_file_options(NULL, vtl, f);
+}
+
+void a_gpx_write_file_options ( GpxWritingOptions *options, VikTrwLayer *vtl, FILE *f )
+{
+ GpxWritingContext context = { options, f };
+ int i;
+
+ gpx_write_header ( f );
+
+
+ gpx_gather_waypoints_passalong_t passalong;
+ passalong.n_wps = g_hash_table_size ( vik_trw_layer_get_waypoints ( vtl ) );
+ passalong.i = 0;
+ passalong.wps = g_new(gpx_waypoint_and_name,passalong.n_wps);
+ g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_collect_waypoint, &passalong );
+ /* gather waypoints in a list, then sort */
+ qsort(passalong.wps, passalong.n_wps, sizeof(gpx_waypoint_and_name), gpx_waypoint_and_name_compar);
+ for ( i = 0; i < passalong.n_wps; i++ )
+ gpx_write_waypoint ( passalong.wps[i].name, passalong.wps[i].wp, &context);
+ g_free ( passalong.wps );
+
+ gpx_gather_tracks_passalong_t passalong_tracks;
+ passalong_tracks.n_trks = g_hash_table_size ( vik_trw_layer_get_tracks (vtl) );
+ passalong_tracks.i = 0;
+ passalong_tracks.trks = g_new(gpx_track_and_timestamp,passalong_tracks.n_trks);
+ g_hash_table_foreach (vik_trw_layer_get_tracks(vtl), (GHFunc) gpx_collect_track, &passalong_tracks);
+ /* Sort by timestamp */
+ qsort(passalong_tracks.trks, passalong_tracks.n_trks, sizeof(gpx_track_and_timestamp), gpx_track_and_timestamp_compar);
+ for (i=0;i<passalong_tracks.n_trks; i++)
+ {
+ gpx_write_track(passalong_tracks.trks[i].name, (VikTrack *)g_hash_table_lookup(vik_trw_layer_get_tracks(vtl), passalong_tracks.trks[i].name), &context);
+ }
+ g_free ( passalong_tracks.trks );
+ gpx_write_footer ( f );
+}
+
+void a_gpx_write_track_file ( const gchar *name, VikTrack *t, FILE *f )
+{
+ a_gpx_write_track_file_options ( NULL, name, t, f );
+}
+
+void a_gpx_write_track_file_options ( GpxWritingOptions *options, const gchar *name, VikTrack *t, FILE *f )
+{
+ GpxWritingContext context = {options, f};
+ gpx_write_header ( f );
+ gpx_write_track ( name, t, &context );
+ gpx_write_footer ( f );