]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpspoint.c
Import Launchpad updates
[andy/viking.git] / src / gpspoint.c
index c7f3d381951a4415b80f2cb89c9d876edd1efd56..96d279d1910485f5b195576f588d9d62a090138b 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_MATH_H
+#include <math.h>
+#endif
 
 #include "viking.h"
 
 #include <ctype.h>
+#ifdef HAVE_STRING_H
 #include <string.h>
+#endif
 
 #include <stdlib.h>
 /* strtod */
@@ -70,6 +79,12 @@ static gboolean line_has_timestamp = FALSE;
 static time_t line_timestamp = 0;
 static gdouble line_altitude = VIK_DEFAULT_ALTITUDE;
 static gboolean line_visible = TRUE;
+
+static gboolean line_extended = FALSE;
+static gdouble line_speed = NAN;
+static gdouble line_course = NAN;
+static gint line_sat = 0;
+static gint line_fix = 0;
 /* other possible properties go here */
 
 
@@ -132,8 +147,6 @@ static gchar *deslashndup ( const gchar *str, guint16 len )
 void a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
   VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw );
   gchar *tag_start, *tag_end;
-  GHashTable *tracks = vik_trw_layer_get_tracks ( trw );
-  GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw );
   g_assert ( f != NULL && trw != NULL );
   line_type = 0;
   line_timestamp = 0;
@@ -189,15 +202,14 @@ void a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
     if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name)
     {
       VikWaypoint *wp = vik_waypoint_new();
-      gint i = strlen(line_name);
       wp->visible = line_visible;
       wp->altitude = line_altitude;
-      while ( i-- )
-        line_name[i] = toupper(line_name[i]); /* TODO: check for acceptable chars */
 
       vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
 
-      g_hash_table_insert ( waypoints, line_name, wp );
+      vik_trw_layer_filein_add_waypoint ( trw, line_name, wp );
+      g_free ( line_name );
+      line_name = NULL;
 
       if ( line_comment )
       {
@@ -216,22 +228,16 @@ void a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
         vik_waypoint_set_symbol ( wp, line_symbol );
        line_symbol = NULL;
       }
-
-      line_name = NULL; /* will be freed automatically */
     }
     else if (line_type == GPSPOINT_TYPE_TRACK && line_name)
     {
       VikTrack *pl = vik_track_new();
-      gint i = strlen(line_name);
 
       /* Thanks to Peter Jones for this Fix */
       if (!line_name) line_name = g_strdup("UNK");
 
       pl->visible = line_visible;
 
-      while ( i-- )
-        line_name[i] = toupper(line_name[i]);
-
       if ( line_comment )
       {
         vik_track_set_comment ( pl, line_comment );
@@ -239,19 +245,26 @@ void a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       }
 
       pl->trackpoints = NULL;
-      g_hash_table_insert ( tracks, line_name, pl );
-      line_name = NULL; /* will be freed automatically */
+      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 = g_malloc ( sizeof ( VikTrackpoint ) );
+      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->speed = line_speed;
+        tp->course = line_course;
+        tp->nsats = line_sat;
+        tp->fix_mode = line_fix;
+      }
       current_track->trackpoints = g_list_append ( current_track->trackpoints, tp );
     }
 
@@ -274,6 +287,12 @@ void a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
     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;
   }
 }
 
@@ -363,15 +382,15 @@ static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, con
   }
   else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL)
   {
-    line_latlon.lat = g_strtod(value, NULL);
+    line_latlon.lat = g_ascii_strtod(value, NULL);
   }
   else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL)
   {
-    line_latlon.lon = g_strtod(value, NULL);
+    line_latlon.lon = g_ascii_strtod(value, NULL);
   }
   else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL)
   {
-    line_altitude = g_strtod(value, 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')
   {
@@ -383,7 +402,7 @@ static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, con
   }
   else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL)
   {
-    line_timestamp = g_strtod(value, NULL);
+    line_timestamp = g_ascii_strtod(value, NULL);
     if ( line_timestamp != 0x80000000 )
       line_has_timestamp = TRUE;
   }
@@ -391,6 +410,26 @@ static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, con
   {
     line_newsegment = TRUE;
   }
+  else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL)
+  {
+    line_extended = TRUE;
+  }
+  else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL)
+  {
+    line_speed = g_ascii_strtod(value, NULL);
+  }
+  else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL)
+  {
+    line_course = g_ascii_strtod(value, NULL);
+  }
+  else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL)
+  {
+    line_sat = atoi(value);
+  }
+  else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL)
+  {
+    line_fix = atoi(value);
+  }
 }
 
 static void a_gpspoint_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f )
@@ -436,6 +475,8 @@ static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, FILE *f )
   gchar *s_lat, *s_lon;
   vik_coord_to_latlon ( &(tp->coord), &ll );
 
+  /* 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);
   fprintf ( f, "type=\"trackpoint\" latitude=\"%s\" longitude=\"%s\"", s_lat, s_lon );
@@ -451,6 +492,24 @@ static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, FILE *f )
     fprintf ( f, " unixtime=\"%ld\"", tp->timestamp );
   if ( tp->newsegment )
     fprintf ( f, " newsegment=\"yes\"" );
+
+  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);
+      fprintf ( f, " speed=\"%s\"", s_speed );
+      g_free(s_speed);
+    }
+    if (!isnan(tp->course)) {
+      gchar *s_course = a_coords_dtostr(tp->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 );
+  }
   fprintf ( f, "\n" );
 }