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; }
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; }
/** * a_babel_convert_from_url_filter: * @vt: The #VikTrwLayer where to insert the collected data * @url: the URL to fetch * @input_type: If input_type is %NULL, input must be GPX. * @babelfilters: The filter arguments to pass to gpsbabel * @cb: Optional callback function. Same usage as in a_babel_convert(). * @user_data: Passed along to cb * @options: Download options. If %NULL then default download options will be used. * * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_type. * If input_type and babelfilters are %NULL, gpsbabel is not used. * * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX * */ gboolean a_babel_convert_from_url_filter ( VikTrwLayer *vt, const char *url, const char *input_type, const char *babelfilters, BabelStatusFunc cb, gpointer user_data, DownloadFileOptions *options ) { // If no download options specified, use defaults: DownloadFileOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL }; if ( options ) myoptions = *options; gint fd_src; int fetch_ret; gboolean ret = FALSE; gchar *name_src = NULL; gchar *babelargs = NULL; g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url); if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); close(fd_src); (void)g_remove(name_src); fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL); if (fetch_ret == DOWNLOAD_SUCCESS) { if (input_type != NULL || babelfilters != NULL) { babelargs = (input_type) ? g_strdup_printf(" -i %s", input_type) : g_strdup(""); ret = a_babel_convert_from_filter( vt, babelargs, name_src, babelfilters, NULL, NULL, NULL ); } else { /* Process directly the retrieved file */ g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src); FILE *f = g_fopen(name_src, "r"); if (f) { gchar *dirpath = g_path_get_dirname ( name_src ); ret = a_gpx_read_file ( vt, f, dirpath ); g_free ( dirpath ); fclose(f); } // Try to avoid adding the description if URL is OAuth signed if ( !g_ascii_strncasecmp(url, "?oauth_consumer_key=", 20) ) { VikTRWMetadata *meta = vik_trw_layer_get_metadata(vt); if ( meta && !meta->description ) { meta->description = g_strdup ( url ); } } } } (void)util_remove(name_src); g_free(babelargs); g_free(name_src); } return ret; }
/** * a_babel_convert_from_url: * @vt: The #VikTrwLayer where to insert the collected data * @url: the URL to fetch * @cb: Optional callback function. Same usage as in a_babel_convert(). * @user_data: passed along to cb * @options: download options. Maybe NULL. * * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type. * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX. * * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX * */ gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options ) { // If no download options specified, use defaults: DownloadMapOptions myoptions = { FALSE, FALSE, NULL, 2, NULL, NULL, NULL }; if ( options ) myoptions = *options; gint fd_src; int fetch_ret; gboolean ret = FALSE; gchar *name_src = NULL; gchar *babelargs = NULL; g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url); if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); close(fd_src); g_remove(name_src); fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL); if (fetch_ret == 0) { if (input_type != NULL) { babelargs = g_strdup_printf(" -i %s", input_type); ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL, NULL ); } else { /* Process directly the retrieved file */ g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src); FILE *f = g_fopen(name_src, "r"); if (f) { ret = a_gpx_read_file ( vt, f ); fclose(f); f = NULL; } } } g_remove(name_src); g_free(babelargs); g_free(name_src); } return ret; }
/** * babel_general_convert_from: * @vtl: The TrackWaypoint Layer to save the data into * If it is null it signifies that no data is to be processed, * however the gpsbabel command is still ran as it can be for non-data related options eg: * for use with the power off command - 'command_off' * @cb: callback that is run upon new data from STDOUT (?) * (TODO: STDERR would be nice since we usually redirect STDOUT) * @user_data: passed along to cb * * Runs args[0] with the arguments and uses the GPX module * to import the GPX data into layer vt. Assumes that upon * running the command, the data will appear in the (usually * temporary) file name_dst. * * Returns: %TRUE on success */ static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data ) { gboolean ret = FALSE; FILE *f = NULL; if (babel_general_convert(cb, args, user_data)) { /* No data actually required but still need to have run gpsbabel anyway - eg using the device power command_off */ if ( vt == NULL ) return TRUE; f = g_fopen(name_dst, "r"); if (f) { ret = a_gpx_read_file ( vt, f ); fclose(f); f = NULL; } } return ret; }
VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename_or_uri ) { g_return_val_if_fail ( vp != NULL, LOAD_TYPE_READ_FAILURE ); char *filename = (char *)filename_or_uri; if (strncmp(filename, "file://", 7) == 0) { // Consider replacing this with: // filename = g_filename_from_uri ( entry, NULL, NULL ); // Since this doesn't support URIs properly (i.e. will failure if is it has %20 characters in it) filename = filename + 7; g_debug ( "Loading file %s from URI %s", filename, filename_or_uri ); } FILE *f = xfopen ( filename ); if ( ! f ) return LOAD_TYPE_READ_FAILURE; VikLoadType_t load_answer = LOAD_TYPE_OTHER_SUCCESS; gchar *dirpath = g_path_get_dirname ( filename ); // Attempt loading the primary file type first - our internal .vik file: if ( check_magic ( f, VIK_MAGIC ) ) { if ( file_read ( top, f, dirpath, vp ) ) load_answer = LOAD_TYPE_VIK_SUCCESS; else load_answer = LOAD_TYPE_VIK_FAILURE_NON_FATAL; } else if ( a_jpg_magic_check ( filename ) ) { if ( ! a_jpg_load_file ( top, filename, vp ) ) load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE; } else { // For all other file types which consist of tracks, routes and/or waypoints, // must be loaded into a new TrackWaypoint layer (hence it be created) gboolean success = TRUE; // Detect load failures - mainly to remove the layer created as it's not required VikLayer *vtl = vik_layer_create ( VIK_LAYER_TRW, vp, FALSE ); vik_layer_rename ( vtl, a_file_basename ( filename ) ); // In fact both kml & gpx files start the same as they are in xml if ( a_file_check_ext ( filename, ".kml" ) && check_magic ( f, GPX_MAGIC ) ) { // Implicit Conversion if ( ! ( success = a_babel_convert_from ( VIK_TRW_LAYER(vtl), "-i kml", filename, NULL, NULL, NULL ) ) ) { load_answer = LOAD_TYPE_GPSBABEL_FAILURE; } } // NB use a extension check first, as a GPX file header may have a Byte Order Mark (BOM) in it // - which currently confuses our check_magic function else if ( a_file_check_ext ( filename, ".gpx" ) || check_magic ( f, GPX_MAGIC ) ) { if ( ! ( success = a_gpx_read_file ( VIK_TRW_LAYER(vtl), f ) ) ) { load_answer = LOAD_TYPE_GPX_FAILURE; } } else { // Try final supported file type if ( ! ( success = a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f, dirpath ) ) ) { // Failure here means we don't know how to handle the file load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE; } } g_free ( dirpath ); // Clean up when we can't handle the file if ( ! success ) { // free up layer g_object_unref ( vtl ); } else { // Complete the setup from the successful load vik_layer_post_read ( vtl, vp, TRUE ); vik_aggregate_layer_add_layer ( top, vtl, FALSE ); vik_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vp ); } } xfclose(f); return load_answer; }