]> err.no Git - mapper/commitdiff
GPX output fixes. Add metadata section, only time for now. Don't waste space with...
authorKaj-Michael Lang <milang@tal.org>
Sat, 15 Mar 2008 12:43:33 +0000 (14:43 +0200)
committerKaj-Michael Lang <milang@tal.org>
Sat, 15 Mar 2008 12:43:33 +0000 (14:43 +0200)
src/gpx.c

index 7b782a2f2410a80a3ba6298027a0de317afe4c4b..4b17050de8dee9596d8e56ab65985c94451b35c5 100644 (file)
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -100,7 +100,7 @@ struct _SaxData {
        GString *chars;
 };
 
-gchar XML_TZONE[7];
+static gchar XML_TZONE[7];
 
 void 
 gpx_init(void)
@@ -113,11 +113,12 @@ g_snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld", (time2.tm_gmtoff / 60 /
 }
 
 gboolean 
-write_gpx(Path * path, GnomeVFSHandle * handle)
+write_gpx(Path *path, GnomeVFSHandle *handle)
 {
 Point *curr = NULL;
 WayPoint *wcurr = NULL;
 gboolean trkseg_break = FALSE;
+gchar buffer[80];
 
 /* Find first non-zero point. */
 for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
@@ -129,24 +130,34 @@ for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
 
 /* Write the header. */
 WRITE_STRING("<?xml version=\"1.0\"?>\n"
-            "<gpx version=\"1.0\" creator=\"mapper\" "
-            "xmlns=\"http://www.topografix.com/GPX/1/0\">\n"
-            "  <trk>\n" "    <trkseg>\n");
+       "<gpx version=\"1.0\" creator=\"mapper\" xmlns=\"http://www.topografix.com/GPX/1/0\">\n");
+
+/* Write any metadata */
+WRITE_STRING("<metadata>\n");
+
+WRITE_STRING("<time>");
+strftime(buffer, sizeof(buffer), XML_DATE_FORMAT, localtime(time(NULL)));
+WRITE_STRING(buffer);
+WRITE_STRING(XML_TZONE);
+WRITE_STRING("</time>\n");
+
+WRITE_STRING("</metadata>\n");
+
+/* Write track(s) and waypoint(s) */
+WRITE_STRING("<trk>\n<trkseg>\n");
 
 /* Curr points to first non-zero point. */
 for (curr--; curr++ != path->tail;) {
        gdouble lat, lon;
        if (curr->unity) {
-               gchar buffer[80];
                gboolean first_sub = TRUE;
                if (trkseg_break) {
                        /* First trkpt of the segment - write trkseg header. */
-                       WRITE_STRING("    </trkseg>\n"
-                                    "    <trkseg>\n");
+                       WRITE_STRING("</trkseg>\n<trkseg>\n");
                        trkseg_break = FALSE;
                }
                unit2latlon(curr->unitx, curr->unity, lat, lon);
-               WRITE_STRING("      <trkpt lat=\"");
+               WRITE_STRING("<trkpt lat=\"");
                g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lat);
                WRITE_STRING(buffer);
                WRITE_STRING("\" lon=\"");
@@ -160,11 +171,9 @@ for (curr--; curr++ != path->tail;) {
                                WRITE_STRING(">\n");
                                first_sub = FALSE;
                        }
-                       WRITE_STRING("        <ele>");
-                       {
-                               g_ascii_formatd(buffer, 80, "%.2f", curr->altitude);
-                               WRITE_STRING(buffer);
-                       }
+                       WRITE_STRING("<ele>");
+                       g_ascii_formatd(buffer, sizeof(buffer), "%.2f", curr->altitude);
+                       WRITE_STRING(buffer);
                        WRITE_STRING("</ele>\n");
                }
 
@@ -174,35 +183,37 @@ for (curr--; curr++ != path->tail;) {
                                WRITE_STRING(">\n");
                                first_sub = FALSE;
                        }
-                       WRITE_STRING("        <time>");
-                       strftime(buffer, 80, XML_DATE_FORMAT,
-                                localtime(&curr->time));
+                       WRITE_STRING("<time>");
+                       strftime(buffer, sizeof(buffer), XML_DATE_FORMAT, localtime(&curr->time));
                        WRITE_STRING(buffer);
                        WRITE_STRING(XML_TZONE);
                        WRITE_STRING("</time>\n");
                }
 
                if (wcurr && curr == wcurr->point) {
+                       gchar *desc;
                        if (first_sub) {
                                WRITE_STRING(">\n");
                                first_sub = FALSE;
                        }
-                       WRITE_STRING("        <desc>");
-                       WRITE_STRING(wcurr->desc);
-                       WRITE_STRING("</desc>\n");
+                       if (wcurr->desc) {
+                               desc=g_markup_printf_escaped("<desc>%s</desc>\n", wcurr->desc);
+                               WRITE_STRING(desc);
+                               g_free(desc);
+                       }
                        wcurr++;
                }
                if (first_sub) {
                        WRITE_STRING("/>\n");
                } else {
-                       WRITE_STRING("      </trkpt>\n");
+                       WRITE_STRING("</trkpt>\n");
                }
        } else
                trkseg_break = TRUE;
 }
 
 /* Write the footer. */
-WRITE_STRING("    </trkseg>\n" "  </trk>\n" "</gpx>\n");
+WRITE_STRING("</trkseg>\n</trk>\n</gpx>\n");
 
 return TRUE;
 }
@@ -217,7 +228,7 @@ return TRUE;
        g_debug("GPX: unknown tag"); }
 
 static void
-gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
+gpx_start_element(SaxData *data, const xmlChar *name, const xmlChar **attrs)
 {
 g_debug("GPX-START: %s (%d)", name, data->state);
 switch (data->state) {
@@ -440,7 +451,7 @@ default: ;
  * Handle char data in the parsing of a GPX file.
  */
 static void 
-gpx_chars(SaxData * data, const xmlChar * ch, int len)
+gpx_chars(SaxData *data, const xmlChar *ch, int len)
 {
 guint i;
 
@@ -467,7 +478,7 @@ break;
  * special here.
  */
 static xmlEntityPtr 
-gpx_get_entity(SaxData * data, const xmlChar * name)
+gpx_get_entity(SaxData *data, const xmlChar *name)
 {
 return xmlGetPredefinedEntity(name);
 }
@@ -476,13 +487,19 @@ return xmlGetPredefinedEntity(name);
  * Handle an error in the parsing of a GPX file.
  */
 static void 
-gpx_error(SaxData * data, const gchar * msg, ...)
+gpx_error(SaxData *data, const gchar *msg, ...)
 {
+va_list args;
+
+va_start(args, msg);
+g_logv("GPX", G_LOG_LEVEL_WARNING, msg, args);
+va_end(args);
+
 data->state = ERROR;
 }
 
 gboolean
-parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
+parse_gpx(Path *to_replace, gchar *buffer, gint size, gint policy_old)
 {
 SaxData data;
 xmlSAXHandler sax_handler;