VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) { guint len; VikTrack *new_tr = vik_track_new(); VikTrackpoint *new_tp; guint ntp; gint i; /* only the visibility is needed */ new_tr->visible = ((VikTrack *)data)->visible; data += sizeof(*new_tr); 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); } len = *(guint *)data; data += sizeof(len); if (len) { new_tr->comment = g_strdup((gchar *)data); } return new_tr; }
VikTrack *vik_track_copy ( const VikTrack *tr ) { VikTrack *new_tr = vik_track_new(); VikTrackpoint *new_tp; GList *tp_iter = tr->trackpoints; new_tr->visible = tr->visible; new_tr->trackpoints = NULL; 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 ); tp_iter = tp_iter->next; } vik_track_set_comment(new_tr,tr->comment); return new_tr; }
static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) { static const gchar *tmp; g_string_append_c ( xpath, '/' ); g_string_append ( xpath, el ); current_tag = get_tag ( xpath->str ); switch ( current_tag ) { case tt_gpx: c_md = vik_trw_metadata_new(); break; case tt_wpt: if ( set_c_ll( attr ) ) { c_wp = vik_waypoint_new (); c_wp->visible = TRUE; if ( get_attr ( attr, "hidden" ) ) c_wp->visible = FALSE; vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); } break; case tt_trk: case tt_rte: c_tr = vik_track_new (); vik_track_set_defaults ( c_tr ); c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE; c_tr->visible = TRUE; if ( get_attr ( attr, "hidden" ) ) c_tr->visible = FALSE; break; case tt_trk_trkseg: f_tr_newseg = TRUE; break; case tt_trk_trkseg_trkpt: if ( set_c_ll( attr ) ) { c_tp = vik_trackpoint_new (); vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); if ( f_tr_newseg ) { c_tp->newsegment = TRUE; f_tr_newseg = FALSE; } c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp ); } break; case tt_gpx_name: case tt_gpx_author: case tt_gpx_desc: case tt_gpx_keywords: case tt_gpx_time: case tt_trk_trkseg_trkpt_name: case tt_trk_trkseg_trkpt_ele: case tt_trk_trkseg_trkpt_time: case tt_wpt_cmt: case tt_wpt_desc: case tt_wpt_name: case tt_wpt_ele: case tt_wpt_time: case tt_wpt_url: case tt_wpt_link: case tt_trk_cmt: case tt_trk_desc: case tt_trk_name: g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */ break; case tt_waypoint: c_wp = vik_waypoint_new (); c_wp->visible = TRUE; break; case tt_waypoint_coord: if ( set_c_ll( attr ) ) vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); break; case tt_waypoint_name: if ( ( tmp = get_attr(attr, "id") ) ) { if ( c_wp_name ) g_free ( c_wp_name ); c_wp_name = g_strdup ( tmp ); } g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */ break; default: break; } }
void a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) { 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; while (fgets(line_buffer, 2048, 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 ) 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; } gpspoint_process_tag ( tag_start, tag_end - tag_start ); if (*tag_end == '\0' ) break; else tag_start = tag_end+1; } if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name) { VikWaypoint *wp = vik_waypoint_new(); wp->visible = line_visible; wp->altitude = line_altitude; 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 ); line_comment = NULL; } if ( line_image ) { vik_waypoint_set_image ( wp, line_image ); line_image = NULL; } if ( line_symbol ) { vik_waypoint_set_symbol ( wp, line_symbol ); line_symbol = NULL; } } else if (line_type == GPSPOINT_TYPE_TRACK && line_name) { VikTrack *pl = vik_track_new(); /* Thanks to Peter Jones for this Fix */ if (!line_name) line_name = g_strdup("UNK"); pl->visible = line_visible; if ( line_comment ) { vik_track_set_comment ( pl, line_comment ); line_comment = NULL; } 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 && current_track) { 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; if (line_extended) { tp->extended = TRUE; tp->speed = line_speed; tp->course = line_course; tp->nsats = line_sat; tp->fix_mode = line_fix; } else { tp->extended = FALSE; } 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_image) g_free ( line_image ); if (line_symbol) g_free ( line_symbol ); line_comment = 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; } }
/* * 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; }