*/
/* Thanks to etrex-cache's gpsbabel's gpspoint.c for starting me off! */
-
-static char line_buffer[2048];
+#define VIKING_LINE_SIZE 4096
+static char line_buffer[VIKING_LINE_SIZE];
#define GPSPOINT_TYPE_NONE 0
#define GPSPOINT_TYPE_WAYPOINT 1
static gchar *line_name;
static gchar *line_comment;
static gchar *line_description;
+static gchar *line_source;
+static gchar *line_xtype;
static gchar *line_color;
static gint line_name_label = 0;
static gint line_dist_label = 0;
static gdouble line_course = NAN;
static gint line_sat = 0;
static gint line_fix = 0;
+static gdouble line_hdop = VIK_DEFAULT_DOP;
+static gdouble line_vdop = VIK_DEFAULT_DOP;
+static gdouble line_pdop = VIK_DEFAULT_DOP;
/* other possible properties go here */
-static void gpspoint_process_tag ( const gchar *tag, gint len );
-static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len );
+static void gpspoint_process_tag ( const gchar *tag, guint len );
+static void gpspoint_process_key_and_value ( const gchar *key, guint key_len, const gchar *value, guint value_len );
static gchar *slashdup(const gchar *str)
{
- guint16 len = strlen(str);
- guint16 need_bs_count, i, j;
+ size_t len = strlen(str);
+ size_t need_bs_count, i, j;
gchar *rv;
for ( i = 0, need_bs_count = 0; i < len; i++ )
if ( str[i] == '\\' || str[i] == '"' )
if ( str[i] == '\\' || str[i] == '"' )
rv[j++] = '\\';
rv[j] = str[i];
+ // Basic normalization of strings - replace Linefeed and Carriage returns as blanks.
+ // although allowed in GPX Spec - Viking file format can't handle multi-line strings yet...
+ if ( str[i] == '\n' || str[i] == '\r' )
+ rv[j] = ' ';
}
rv[j] = '\0';
return rv;
current_track = NULL;
gboolean have_read_something = FALSE;
- while (fgets(line_buffer, 2048, f))
+ while (fgets(line_buffer, VIKING_LINE_SIZE, f))
{
gboolean inside_quote = 0;
gboolean backslash = 0;
/* 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')
+ if (*tag_start == '\0')
break;
if (*tag_start == '#')
inside_quote = !inside_quote;
}
- gpspoint_process_tag ( tag_start, tag_end - tag_start );
+ // 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;
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 ) )
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) ) )
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 );
}
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_symbol );
line_comment = NULL;
line_description = NULL;
+ line_source = NULL;
+ line_xtype = NULL;
line_color = NULL;
line_image = NULL;
line_symbol = NULL;
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;
}
So we must determine end of tag name, start of value, end of value.
*/
-static void gpspoint_process_tag ( const gchar *tag, gint len )
+static void gpspoint_process_tag ( const gchar *tag, guint len )
{
const gchar *key_end, *value_start, *value_end;
else
{
if (*(tag+len-1) == '"')
- value_end = tag + len - 1;
+ value_end = tag + len - 1;
else
return; /* bogus */
}
else
value_end = tag + len; /* value start really IS value start. */
+ // Detect broken lines which end without any text or the enclosing ". i.e. like: comment="
+ if ( (value_end - value_start) < 0 )
+ return;
+
gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start);
}
}
/*
value = NULL for none
*/
-static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len )
+static void gpspoint_process_key_and_value ( const gchar *key, guint key_len, const gchar *value, guint value_len )
{
if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 )
{
if (line_description == NULL)
line_description = deslashndup ( value, value_len );
}
+ else if (key_len == 6 && strncasecmp( key, "source", key_len ) == 0 && value != NULL)
+ {
+ if (line_source == NULL)
+ line_source = deslashndup ( value, value_len );
+ }
+ // NB using 'xtype' to differentiate from our own 'type' key
+ else if (key_len == 5 && strncasecmp( key, "xtype", key_len ) == 0 && value != NULL)
+ {
+ if (line_xtype == NULL)
+ line_xtype = deslashndup ( value, value_len );
+ }
else if (key_len == 5 && strncasecmp( key, "color", key_len ) == 0 && value != NULL)
{
if (line_color == NULL)
{
line_altitude = g_ascii_strtod(value, NULL);
}
- else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
+ else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value != NULL && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
{
line_visible = FALSE;
}
{
line_fix = atoi(value);
}
+ else if (key_len == 4 && strncasecmp( key, "hdop", key_len ) == 0 && value != NULL)
+ {
+ line_hdop = g_ascii_strtod(value, NULL);
+ }
+ else if (key_len == 4 && strncasecmp( key, "vdop", key_len ) == 0 && value != NULL)
+ {
+ line_vdop = g_ascii_strtod(value, NULL);
+ }
+ else if (key_len == 4 && strncasecmp( key, "pdop", key_len ) == 0 && value != NULL)
+ {
+ line_pdop = g_ascii_strtod(value, NULL);
+ }
}
static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f )
{
- static struct LatLon ll;
- gchar *s_lat, *s_lon;
- // Sanity clause
- if ( wp && !(wp->name) ) {
+ struct LatLon ll;
+ gchar s_lat[COORDS_STR_BUFFER_SIZE];
+ gchar s_lon[COORDS_STR_BUFFER_SIZE];
+ // Sanity clauses
+ if ( !wp )
return;
- }
+ if ( !(wp->name) )
+ return;
+
vik_coord_to_latlon ( &(wp->coord), &ll );
- s_lat = a_coords_dtostr(ll.lat);
- s_lon = a_coords_dtostr(ll.lon);
+ a_coords_dtostr_buffer ( ll.lat, s_lat );
+ a_coords_dtostr_buffer ( ll.lon, s_lon );
gchar *tmp_name = slashdup(wp->name);
fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name );
g_free ( tmp_name );
- g_free ( s_lat );
- g_free ( s_lon );
if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) {
- gchar *s_alt = a_coords_dtostr(wp->altitude);
+ gchar s_alt[COORDS_STR_BUFFER_SIZE];
+ a_coords_dtostr_buffer ( wp->altitude, s_alt );
fprintf ( f, " altitude=\"%s\"", s_alt );
- g_free(s_alt);
}
if ( wp->has_timestamp )
fprintf ( f, " unixtime=\"%ld\"", wp->timestamp );
fprintf ( f, " description=\"%s\"", tmp_description );
g_free ( tmp_description );
}
+ if ( wp->source )
+ {
+ gchar *tmp_source = slashdup(wp->source);
+ fprintf ( f, " source=\"%s\"", tmp_source );
+ g_free ( tmp_source );
+ }
+ if ( wp->type )
+ {
+ gchar *tmp_type = slashdup(wp->type);
+ fprintf ( f, " xtype=\"%s\"", tmp_type );
+ g_free ( tmp_type );
+ }
if ( wp->image )
{
gchar *tmp_image = NULL;
static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info )
{
- static struct LatLon ll;
- gchar *s_lat, *s_lon;
+ struct LatLon ll;
+ gchar s_lat[COORDS_STR_BUFFER_SIZE];
+ gchar s_lon[COORDS_STR_BUFFER_SIZE];
+ gchar s_alt[COORDS_STR_BUFFER_SIZE];
vik_coord_to_latlon ( &(tp->coord), &ll );
FILE *f = write_info->f;
- /* TODO: modify a_coords_dtostr() to accept (optional) buffer
- * instead of doing malloc/free everytime */
- s_lat = a_coords_dtostr(ll.lat);
- s_lon = a_coords_dtostr(ll.lon);
+ a_coords_dtostr_buffer ( ll.lat, s_lat );
+ a_coords_dtostr_buffer ( ll.lon, s_lon );
fprintf ( f, "type=\"%spoint\" latitude=\"%s\" longitude=\"%s\"", write_info->is_route ? "route" : "track", s_lat, s_lon );
- g_free ( s_lat );
- g_free ( s_lon );
if ( tp->name ) {
gchar *name = slashdup(tp->name);
}
if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
- gchar *s_alt = a_coords_dtostr(tp->altitude);
+ a_coords_dtostr_buffer ( tp->altitude, s_alt );
fprintf ( f, " altitude=\"%s\"", s_alt );
- g_free(s_alt);
}
if ( tp->has_timestamp )
fprintf ( f, " unixtime=\"%ld\"", tp->timestamp );
if (!isnan(tp->speed) || !isnan(tp->course) || tp->nsats > 0) {
fprintf ( f, " extended=\"yes\"" );
if (!isnan(tp->speed)) {
- gchar *s_speed = a_coords_dtostr(tp->speed);
+ gchar s_speed[COORDS_STR_BUFFER_SIZE];
+ a_coords_dtostr_buffer ( tp->speed, s_speed );
fprintf ( f, " speed=\"%s\"", s_speed );
- g_free(s_speed);
}
if (!isnan(tp->course)) {
- gchar *s_course = a_coords_dtostr(tp->course);
+ gchar s_course[COORDS_STR_BUFFER_SIZE];
+ a_coords_dtostr_buffer ( tp->course, s_course );
fprintf ( f, " course=\"%s\"", s_course );
- g_free(s_course);
}
if (tp->nsats > 0)
fprintf ( f, " sat=\"%d\"", tp->nsats );
if (tp->fix_mode > 0)
fprintf ( f, " fix=\"%d\"", tp->fix_mode );
+
+ if ( tp->hdop != VIK_DEFAULT_DOP ) {
+ gchar ss[COORDS_STR_BUFFER_SIZE];
+ a_coords_dtostr_buffer ( tp->hdop, ss );
+ fprintf ( f, " hdop=\"%s\"", ss );
+ }
+ if ( tp->vdop != VIK_DEFAULT_DOP ) {
+ gchar ss[COORDS_STR_BUFFER_SIZE];
+ a_coords_dtostr_buffer ( tp->vdop, ss );
+ fprintf ( f, " vdop=\"%s\"", ss );
+ }
+ if ( tp->pdop != VIK_DEFAULT_DOP ) {
+ gchar ss[COORDS_STR_BUFFER_SIZE];
+ a_coords_dtostr_buffer ( tp->pdop, ss );
+ fprintf ( f, " pdop=\"%s\"", ss );
+ }
}
fprintf ( f, "\n" );
}
g_free ( tmp );
}
+ if ( trk->source ) {
+ gchar *tmp = slashdup(trk->source);
+ fprintf ( f, " source=\"%s\"", tmp );
+ g_free ( tmp );
+ }
+
+ if ( trk->type ) {
+ gchar *tmp = slashdup(trk->type);
+ fprintf ( f, " xtype=\"%s\"", tmp );
+ g_free ( tmp );
+ }
+
if ( trk->has_color ) {
fprintf ( f, " color=#%.2x%.2x%.2x", (int)(trk->color.red/256),(int)(trk->color.green/256),(int)(trk->color.blue/256));
}