/** * vu_get_canonical_filename: * * Returns: Canonical absolute filename * * Any time a path may contain a relative component, so need to prepend that directory it is relative to * Then resolve the full path to get the normal canonical filename */ gchar *vu_get_canonical_filename ( VikLayer *vl, const gchar *filename ) { gchar *canonical = NULL; if ( !filename ) return NULL; if ( g_path_is_absolute ( filename ) ) canonical = g_strdup ( filename ); else { const gchar *vw_filename = vik_window_get_filename ( VIK_WINDOW_FROM_WIDGET (vl->vvp) ); gchar *dirpath = NULL; if ( vw_filename ) dirpath = g_path_get_dirname ( vw_filename ); else dirpath = g_get_current_dir(); // Fallback - if here then probably can't create the correct path gchar *full = NULL; if ( g_path_is_absolute ( dirpath ) ) full = g_strconcat ( dirpath, G_DIR_SEPARATOR_S, filename, NULL ); else full = g_strconcat ( g_get_current_dir(), G_DIR_SEPARATOR_S, dirpath, G_DIR_SEPARATOR_S, filename, NULL ); canonical = file_realpath_dup ( full ); // resolved g_free ( full ); g_free ( dirpath ); } return canonical; }
/** * util_make_absolute_filename: * * Returns a newly allocated string of the absolute filename or * NULL if name is already absolute (or dirpath is unusable) */ gchar* util_make_absolute_filename ( const gchar *filename, const gchar *dirpath ) { if ( !dirpath ) return NULL; // Is it ready absolute? if ( g_path_is_absolute ( filename ) ) { return NULL; } else { // Otherwise create the absolute filename from the given directory and filename gchar *full = g_strconcat ( dirpath, G_DIR_SEPARATOR_S, filename, NULL ); gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name g_free ( full ); return absolute; } }
/* * Returns whether file read was a success * No obvious way to test for a 'gpspoint' file, * thus set a flag if any actual tag found during processing of the file */ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath ) { VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw ); gchar *tag_start, *tag_end; g_assert ( f != NULL && trw != NULL ); line_type = 0; line_timestamp = 0; line_newsegment = FALSE; line_image = NULL; line_symbol = NULL; current_track = NULL; gboolean have_read_something = FALSE; while (fgets(line_buffer, VIKING_LINE_SIZE, f)) { gboolean inside_quote = 0; gboolean backslash = 0; line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */ /* for gpspoint files wrapped inside */ if ( strlen(line_buffer) >= 13 && strncmp ( line_buffer, "~EndLayerData", 13 ) == 0 ) { // Even just a blank TRW is ok when in a .vik file have_read_something = TRUE; break; } /* each line: nullify stuff, make thing if nes, free name if ness */ tag_start = line_buffer; for (;;) { /* my addition: find first non-whitespace character. if the null, skip line. */ while (*tag_start != '\0' && isspace(*tag_start)) tag_start++; if (*tag_start == '\0') break; if (*tag_start == '#') break; tag_end = tag_start; if (*tag_end == '"') inside_quote = !inside_quote; while (*tag_end != '\0' && (!isspace(*tag_end) || inside_quote)) { tag_end++; if (*tag_end == '\\' && !backslash) backslash = TRUE; else if (backslash) backslash = FALSE; else if (*tag_end == '"') inside_quote = !inside_quote; } // Won't have super massively long strings, so potential truncation in cast is acceptable. guint len = (guint)(tag_end - tag_start); gpspoint_process_tag ( tag_start, len ); if (*tag_end == '\0' ) break; else tag_start = tag_end+1; } if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name) { have_read_something = TRUE; VikWaypoint *wp = vik_waypoint_new(); wp->visible = line_visible; wp->altitude = line_altitude; wp->has_timestamp = line_has_timestamp; wp->timestamp = line_timestamp; vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon ); vik_trw_layer_filein_add_waypoint ( trw, line_name, wp ); g_free ( line_name ); line_name = NULL; if ( line_comment ) vik_waypoint_set_comment ( wp, line_comment ); if ( line_description ) vik_waypoint_set_description ( wp, line_description ); if ( line_source ) vik_waypoint_set_source ( wp, line_source ); if ( line_xtype ) vik_waypoint_set_type ( wp, line_xtype ); if ( line_image ) { // Ensure the filename is absolute if ( g_path_is_absolute ( line_image ) ) vik_waypoint_set_image ( wp, line_image ); else { // Otherwise create the absolute filename from the directory of the .vik file & and the relative filename gchar *full = g_strconcat(dirpath, G_DIR_SEPARATOR_S, line_image, NULL); gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name vik_waypoint_set_image ( wp, absolute ); g_free ( absolute ); g_free ( full ); } } if ( line_symbol ) vik_waypoint_set_symbol ( wp, line_symbol ); } else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name) { have_read_something = TRUE; VikTrack *pl = vik_track_new(); // NB don't set defaults here as all properties are stored in the GPS_POINT format //vik_track_set_defaults ( pl ); /* Thanks to Peter Jones for this Fix */ if (!line_name) line_name = g_strdup("UNK"); pl->visible = line_visible; pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE); if ( line_comment ) vik_track_set_comment ( pl, line_comment ); if ( line_description ) vik_track_set_description ( pl, line_description ); if ( line_source ) vik_track_set_source ( pl, line_source ); if ( line_xtype ) vik_track_set_type ( pl, line_xtype ); if ( line_color ) { if ( gdk_color_parse ( line_color, &(pl->color) ) ) pl->has_color = TRUE; } pl->draw_name_mode = line_name_label; pl->max_number_dist_labels = line_dist_label; pl->trackpoints = NULL; vik_trw_layer_filein_add_track ( trw, line_name, pl ); g_free ( line_name ); line_name = NULL; current_track = pl; } else if ((line_type == GPSPOINT_TYPE_TRACKPOINT || line_type == GPSPOINT_TYPE_ROUTEPOINT) && current_track) { have_read_something = TRUE; VikTrackpoint *tp = vik_trackpoint_new(); vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon ); tp->newsegment = line_newsegment; tp->has_timestamp = line_has_timestamp; tp->timestamp = line_timestamp; tp->altitude = line_altitude; vik_trackpoint_set_name ( tp, line_name ); if (line_extended) { tp->speed = line_speed; tp->course = line_course; tp->nsats = line_sat; tp->fix_mode = line_fix; tp->hdop = line_hdop; tp->vdop = line_vdop; tp->pdop = line_pdop; } current_track->trackpoints = g_list_append ( current_track->trackpoints, tp ); } if (line_name) g_free ( line_name ); line_name = NULL; if (line_comment) g_free ( line_comment ); if (line_description) g_free ( line_description ); if (line_source) g_free ( line_source ); if (line_xtype) g_free ( line_xtype ); if (line_color) g_free ( line_color ); if (line_image) g_free ( line_image ); if (line_symbol) g_free ( line_symbol ); line_comment = NULL; line_description = NULL; line_source = NULL; line_xtype = NULL; line_color = NULL; line_image = NULL; line_symbol = NULL; line_type = GPSPOINT_TYPE_NONE; line_newsegment = FALSE; line_has_timestamp = FALSE; line_timestamp = 0; line_altitude = VIK_DEFAULT_ALTITUDE; line_visible = TRUE; line_symbol = NULL; line_extended = FALSE; line_speed = NAN; line_course = NAN; line_sat = 0; line_fix = 0; line_hdop = VIK_DEFAULT_DOP; line_vdop = VIK_DEFAULT_DOP; line_pdop = VIK_DEFAULT_DOP; line_name_label = 0; line_dist_label = 0; } return have_read_something; }