int main(int argc, char *argv[]) { VikTrwLayer *trw = NULL; g_type_init (); trw = vik_trw_layer_new(0); a_gpx_read_file(trw, stdin); a_gpx_write_file(trw, stdout); vik_trw_layer_free (trw); return 0; }
/** * a_file_export: * @vtl: The TrackWaypoint to export data from * @filename: The name of the file to be written * @file_type: Choose one of the supported file types for the export * @trk: If specified then only export this track rather than the whole layer * @write_hidden: Whether to write invisible items * * A general export command to convert from Viking TRW layer data to an external supported format. * The write_hidden option is provided mainly to be able to transfer selected items when uploading to a GPS */ gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t file_type, VikTrack *trk, gboolean write_hidden ) { GpxWritingOptions options = { FALSE, FALSE, write_hidden, FALSE }; FILE *f = g_fopen ( filename, "w" ); if ( f ) { if ( trk ) { switch ( file_type ) { case FILE_TYPE_GPX: // trk defined so can set the option options.is_route = trk->is_route; a_gpx_write_track_file ( trk, f, &options ); break; default: g_critical("Houston, we've had a problem. file_type=%d", file_type); } } else { switch ( file_type ) { case FILE_TYPE_GPSMAPPER: a_gpsmapper_write_file ( vtl, f ); break; case FILE_TYPE_GPX: a_gpx_write_file ( vtl, f, &options ); break; case FILE_TYPE_GPSPOINT: a_gpspoint_write_file ( vtl, f ); break; case FILE_TYPE_KML: fclose ( f ); f = NULL; switch ( a_vik_get_kml_export_units () ) { case VIK_KML_EXPORT_UNITS_STATUTE: return a_babel_convert_to ( vtl, NULL, "-o kml", filename, NULL, NULL ); break; case VIK_KML_EXPORT_UNITS_NAUTICAL: return a_babel_convert_to ( vtl, NULL, "-o kml,units=n", filename, NULL, NULL ); break; default: // VIK_KML_EXPORT_UNITS_METRIC: return a_babel_convert_to ( vtl, NULL, "-o kml,units=m", filename, NULL, NULL ); break; } break; default: g_critical("Houston, we've had a problem. file_type=%d", file_type); } } fclose ( f ); f = NULL; return TRUE; } return FALSE; }
static gchar *write_tmp_trwlayer ( VikTrwLayer *vtl ) { int fd_src; gchar *name_src; FILE *f; g_assert ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0); f = fdopen(fd_src, "w"); a_gpx_write_file(vtl, f); fclose(f); f = NULL; return name_src; }
int main(int argc, char *argv[]) { #if !GLIB_CHECK_VERSION (2, 36, 0) g_type_init(); #endif VikLayer *vl = vik_layer_create (VIK_LAYER_TRW, NULL, FALSE); VikTrwLayer *trw = VIK_TRW_LAYER (vl); a_gpx_read_file(trw, stdin); a_gpx_write_file(trw, stdout, NULL); // NB no layer_free functions directly visible anymore // automatically called by layers_panel_finalize cleanup in full Viking program return 0; }
static gchar *write_tmp_trwlayer ( VikTrwLayer *vtl ) { int fd_src; gchar *name_src; FILE *f; g_assert ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0); g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); f = fdopen(fd_src, "w"); a_gpx_write_file(vtl, f, NULL); fclose(f); f = NULL; return name_src; }
/** * a_babel_convert: * @vt: The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs. * @babelargs: A string containing gpsbabel command line filter options. No file types or names should * be specified. * @cb: A callback function. * @user_data: passed along to cb * @not_used: Must use NULL * * This function modifies data in a trw layer using gpsbabel filters. This routine is synchronous; * that is, it will block the calling program until the conversion is done. To avoid blocking, call * this routine from a worker thread. * * Returns: %TRUE on success */ gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) { int fd_src; FILE *f; gchar *name_src = NULL; gboolean ret = FALSE; gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL); if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); f = fdopen(fd_src, "w"); a_gpx_write_file(vt, f, NULL); fclose(f); f = NULL; ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data, not_used ); g_remove(name_src); g_free(name_src); } g_free(bargs); return ret; }
/** * Common write of a temporary GPX file */ static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options ) { gchar *tmp_filename = NULL; GError *error = NULL; // Opening temporary file int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error); if (fd < 0) { g_warning ( _("failed to open temporary file: %s"), error->message ); g_clear_error ( &error ); return NULL; } g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename); FILE *ff = fdopen (fd, "w"); if ( trk ) a_gpx_write_track_file ( trk, ff, options ); else a_gpx_write_file ( vtl, ff, options ); fclose (ff); return tmp_filename; }
/** * uploading function executed by the background" thread */ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata ) { /* Due to OSM limits, we have to enforce ele and time fields also don't upload invisible tracks */ static GpxWritingOptions options = { TRUE, TRUE, FALSE, FALSE }; FILE *file = NULL; gchar *filename = NULL; int fd; GError *error = NULL; int ret; g_assert(oti != NULL); /* Opening temporary file */ fd = g_file_open_tmp("viking_osm_upload_XXXXXX.gpx", &filename, &error); if (fd < 0) { g_error(_("failed to open temporary file: %s"), strerror(errno)); return; } g_clear_error(&error); g_debug("%s: temporary file = %s", __FUNCTION__, filename); /* Creating FILE* */ file = fdopen(fd, "w"); /* writing gpx file */ if (oti->trk != NULL) { /* Upload only the selected track */ if ( oti->anonymize_times ) { VikTrack *trk = vik_track_copy(oti->trk, TRUE); vik_track_anonymize_times(trk); a_gpx_write_track_file(trk, file, &options); vik_track_free(trk); } else a_gpx_write_track_file(oti->trk, file, &options); } else { /* Upload the whole VikTrwLayer */ a_gpx_write_file(oti->vtl, file, &options); } /* We can close the file */ /* This also close the associated fd */ fclose(file); file = NULL; /* finally, upload it */ gint ans = osm_traces_upload_file(osm_user, osm_password, filename, oti->name, oti->description, oti->tags, oti->vistype); // // Show result in statusbar or failure in dialog for user feedback // // Get current time to put into message to show when result was generated // since need to show difference between operations (when displayed on statusbar) // NB If on dialog then don't need time. time_t timenow; struct tm* timeinfo; time ( &timenow ); timeinfo = localtime ( &timenow ); gchar timestr[80]; // Compact time only - as days/date isn't very useful here strftime ( timestr, sizeof(timestr), "%X)", timeinfo ); // // Test to see if window it was invoked on is still valid // Not sure if this test really works! (i.e. if the window was closed in the mean time) // if ( IS_VIK_WINDOW ((VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl)) ) { gchar* msg; if ( ans == 0 ) { // Success msg = g_strdup_printf ( "%s (@%s)", _("Uploaded to OSM"), timestr ); } // Use UPPER CASE for bad news :( else if ( ans < 0 ) { msg = g_strdup_printf ( "%s (@%s)", _("FAILED TO UPLOAD DATA TO OSM - CURL PROBLEM"), timestr ); } else { msg = g_strdup_printf ( "%s : %s %d (@%s)", _("FAILED TO UPLOAD DATA TO OSM"), _("HTTP response code"), ans, timestr ); } vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl), msg, VIK_STATUSBAR_INFO ); g_free (msg); } /* Removing temporary file */ ret = g_unlink(filename); if (ret != 0) { g_critical(_("failed to unlink temporary file: %s"), strerror(errno)); } }