]> err.no Git - mapper/blobdiff - src/gpx.c
Move some variables around
[mapper] / src / gpx.c
index eb8f70e4c5d5a87ad759bb5e39143a505e0d6b67..405ae26e9a05c89e4611047909659d8c7c7c46d0 100644 (file)
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -1,5 +1,5 @@
 /*
- * This file is part of maemo-mapper
+ * This file is part of mapper
  *
  * Copyright (C) 2006-2007 John Costigan.
  *
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
-
-#define _(String) gettext(String)
-
 #include <config.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -37,6 +33,7 @@
 #include <errno.h>
 #include <sys/wait.h>
 #include <glib/gstdio.h>
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <fcntl.h>
 #include <libgnomevfs/gnome-vfs.h>
 #include <sqlite3.h>
 
 #include "ui-common.h"
+#include "path.h"
 #include "utils.h"
 #include "gps.h"
 #include "mapper-types.h"
+#include "latlon.h"
+#include "map.h"
 #include "gpx.h"
-#include "path.h"
+#include "dialogs.h"
 
 #define XML_DATE_FORMAT "%FT%T"
 
+#define WRITE_STRING(string) { \
+    GnomeVFSResult vfs_result; \
+    GnomeVFSFileSize size; \
+    if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write(handle, (string), strlen((string)), &size))) \
+    { \
+        gchar buffer[BUFFER_SIZE]; \
+        g_snprintf(buffer, sizeof(buffer), \
+                "%s:\n%s\n%s", _("Error while writing to file"), _("File is incomplete."), \
+                gnome_vfs_result_to_string(vfs_result)); \
+        popup_error(_window, buffer); \
+        return FALSE; \
+    } \
+}
+
+/** This enum defines the states of the SAX parsing state machine. */
+typedef enum {
+       START=1,
+       INSIDE_GPX=100,
+       INSIDE_METADATA,
+       INSIDE_PATH,
+       INSIDE_PATH_SEGMENT,
+       INSIDE_PATH_POINT,
+       INSIDE_PATH_POINT_ELE,
+       INSIDE_PATH_POINT_TIME,
+       INSIDE_PATH_POINT_DESC,
+       INSIDE_PATH_POINT_NAME,
+       FINISH=5000,
+       UNKNOWN=6666,
+       ERROR=9999,
+} SaxState;
+
+/** Data used during the SAX parsing operation. */
+typedef struct _SaxData SaxData;
+struct _SaxData {
+       Path path;
+       SaxState state;
+       SaxState prev_state;
+       guint unknown_depth;
+       gboolean at_least_one_trkpt;
+       GString *chars;
+};
+
 gchar XML_TZONE[7];
 
 void 
@@ -67,7 +109,7 @@ time_t time1;
 struct tm time2;
 time1 = time(NULL);
 localtime_r(&time1, &time2);
-snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld",
+g_snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld",
         (time2.tm_gmtoff / 60 / 60), (time2.tm_gmtoff / 60) % 60);
 }
 
@@ -77,7 +119,6 @@ write_gpx(Path * path, GnomeVFSHandle * handle)
        Point *curr = NULL;
        WayPoint *wcurr = NULL;
        gboolean trkseg_break = FALSE;
-       printf("%s()\n", __PRETTY_FUNCTION__);
 
        /* Find first non-zero point. */
        for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
@@ -165,7 +206,6 @@ write_gpx(Path * path, GnomeVFSHandle * handle)
        /* Write the footer. */
        WRITE_STRING("    </trkseg>\n" "  </trk>\n" "</gpx>\n");
 
-       vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
        return TRUE;
 }
 
@@ -176,35 +216,39 @@ write_gpx(Path * path, GnomeVFSHandle * handle)
     data->prev_state = data->state; \
     data->state = UNKNOWN; \
     data->unknown_depth = 1; \
-}
+       g_debug("GPX: unknown tag"); }
 
 static void
 gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
 {
-       vprintf("%s(%s)\n", __PRETTY_FUNCTION__, name);
-
+       g_debug("GPX-START: %s (%d)", name, data->state);
        switch (data->state) {
        case ERROR:
-               break;
+       break;
        case START:
                if (!strcmp((gchar *) name, "gpx"))
                        data->state = INSIDE_GPX;
                else
                        MACRO_SET_UNKNOWN();
-               break;
+       break;
        case INSIDE_GPX:
                if (!strcmp((gchar *) name, "trk"))
                        data->state = INSIDE_PATH;
+               else if (!strcmp((gchar *) name, "metadata"))
+                       data->state = INSIDE_METADATA;
                else
                        MACRO_SET_UNKNOWN();
-               break;
+       break;
+       case INSIDE_METADATA:
+
+       break;
        case INSIDE_PATH:
                if (!strcmp((gchar *) name, "trkseg")) {
                        data->state = INSIDE_PATH_SEGMENT;
                        data->at_least_one_trkpt = FALSE;
                } else
                        MACRO_SET_UNKNOWN();
-               break;
+       break;
        case INSIDE_PATH_SEGMENT:
                if (!strcmp((gchar *) name, "trkpt")) {
                        const xmlChar **curr_attr;
@@ -236,7 +280,7 @@ gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
                                data->state = ERROR;
                } else
                        MACRO_SET_UNKNOWN();
-               break;
+       break;
        case INSIDE_PATH_POINT:
                if (!strcmp((gchar *) name, "time"))
                        data->state = INSIDE_PATH_POINT_TIME;
@@ -244,18 +288,18 @@ gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
                        data->state = INSIDE_PATH_POINT_ELE;
                else if (!strcmp((gchar *) name, "desc"))
                        data->state = INSIDE_PATH_POINT_DESC;
-
+               else if (!strcmp((gchar *) name, "name"))
+                       data->state = INSIDE_PATH_POINT_NAME;
                else
                        MACRO_SET_UNKNOWN();
-               break;
+       break;
        case UNKNOWN:
                data->unknown_depth++;
-               break;
+       break;
        default:
                ;
        }
 
-       vprintf("%s(): return\n", __PRETTY_FUNCTION__);
 }
 
 /**
@@ -264,26 +308,31 @@ gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
 static void 
 gpx_end_element(SaxData * data, const xmlChar * name)
 {
-       vprintf("%s(%s)\n", __PRETTY_FUNCTION__, name);
+       g_debug("GPX-END: %s", name);
 
        switch (data->state) {
        case ERROR:
-               break;
+
+       break;
        case START:
                data->state = ERROR;
-               break;
+       break;
        case INSIDE_GPX:
                if (!strcmp((gchar *) name, "gpx"))
                        data->state = FINISH;
                else
                        data->state = ERROR;
-               break;
+       break;
+       case INSIDE_METADATA:
+               if (!strcmp((gchar *) name, "metadata"))
+                       data->state = INSIDE_GPX;
+       break;
        case INSIDE_PATH:
                if (!strcmp((gchar *) name, "trk"))
                        data->state = INSIDE_GPX;
                else
                        data->state = ERROR;
-               break;
+       break;
        case INSIDE_PATH_SEGMENT:
                if (!strcmp((gchar *) name, "trkseg")) {
                        if (data->at_least_one_trkpt) {
@@ -293,14 +342,14 @@ gpx_end_element(SaxData * data, const xmlChar * name)
                        data->state = INSIDE_PATH;
                } else
                        data->state = ERROR;
-               break;
+       break;
        case INSIDE_PATH_POINT:
                if (!strcmp((gchar *) name, "trkpt")) {
                        data->state = INSIDE_PATH_SEGMENT;
                        data->at_least_one_trkpt = TRUE;
                } else
                        data->state = ERROR;
-               break;
+       break;
        case INSIDE_PATH_POINT_ELE:
                if (!strcmp((gchar *) name, "ele")) {
                        gchar *error_check;
@@ -312,14 +361,13 @@ gpx_end_element(SaxData * data, const xmlChar * name)
                        data->chars = g_string_new("");
                } else
                        data->state = ERROR;
-               break;
+       break;
        case INSIDE_PATH_POINT_TIME:
                if (!strcmp((gchar *) name, "time")) {
                        struct tm time;
                        gchar *ptr;
 
-                       if (NULL == (ptr = strptime(data->chars->str,
-                                                   XML_DATE_FORMAT, &time)))
+                       if (NULL == (ptr = strptime(data->chars->str, XML_DATE_FORMAT, &time)))
                                /* Failed to parse dateTime format. */
                                data->state = ERROR;
                        else {
@@ -333,42 +381,24 @@ gpx_end_element(SaxData * data, const xmlChar * name)
                                data->path.tail->time = (mktime(&time));
 
                                /* Now, skip inconsequential characters */
-                               while (*ptr && *ptr != 'Z' && *ptr != '-'
-                                      && *ptr != '+')
+                               while (*ptr && *ptr != 'Z' && *ptr != '-' && *ptr != '+')
                                        ptr++;
 
                                /* Check if we ran to the end of the string. */
                                if (*ptr) {
                                        /* Next character is either 'Z', '-', or '+' */
                                        if (*ptr == 'Z')
-                                               /* Zulu (UTC) time. Undo the local time zone's
-                                                * offset. */
+                                               /* Zulu (UTC) time. Undo the local time zone's offset. */
                                                data->path.tail->time += time.tm_gmtoff;
                                        else {
                                                /* Not Zulu (UTC). Must parse hours and minutes. */
-                                               gint offhours =
-                                                   strtol(ptr, &error_check,
-                                                          10);
-                                               if (error_check != ptr
-                                                   && *(ptr =
-                                                        error_check) == ':') {
+                                               gint offhours = strtol(ptr, &error_check, 10);
+                                               if (error_check != ptr && *(ptr = error_check) == ':') {
                                                        /* Parse of hours worked. Check minutes. */
-                                                       gint offmins =
-                                                           strtol(ptr + 1,
-                                                                  &error_check,
-                                                                  10);
-                                                       if (error_check !=
-                                                           (ptr + 1)) {
+                                                       gint offmins = strtol(ptr + 1, &error_check, 10);
+                                                       if (error_check != (ptr + 1)) {
                                                                /* Parse of minutes worked. Calculate. */
-                                                               data->path.
-                                                                   tail->
-                                                                   time +=
-                                                                   (time.
-                                                                    tm_gmtoff -
-                                                                    (offhours *
-                                                                     60 * 60 +
-                                                                     offmins *
-                                                                     60));
+                                                               data->path.tail->time += (time.tm_gmtoff - (offhours * 60 * 60 + offmins * 60));
                                                        }
                                                }
                                        }
@@ -381,30 +411,34 @@ gpx_end_element(SaxData * data, const xmlChar * name)
                        data->chars = g_string_new("");
                } else
                        data->state = ERROR;
-               break;
+       break;
        case INSIDE_PATH_POINT_DESC:
                /* only parse description for routes */
                if (!strcmp((gchar *) name, "desc")) {
                        MACRO_PATH_INCREMENT_WTAIL(data->path);
                        data->path.wtail->point = data->path.tail;
-                       data->path.wtail->desc
-                           = g_string_free(data->chars, FALSE);
+                       data->path.wtail->desc = g_string_free(data->chars, FALSE);
                        data->chars = g_string_new("");
                        data->state = INSIDE_PATH_POINT;
                } else
                        data->state = ERROR;
-               break;
+       break;
+       case INSIDE_PATH_POINT_NAME:
+                       /* Just ignore these for now */
+                       g_string_free(data->chars, FALSE);
+                       data->chars = g_string_new("");
+                       data->state = INSIDE_PATH_POINT;
+       break;
        case UNKNOWN:
                if (!--data->unknown_depth)
                        data->state = data->prev_state;
                else
                        data->state = ERROR;
-               break;
+       break;
        default:
                ;
        }
 
-       vprintf("%s(): return\n", __PRETTY_FUNCTION__);
 }
 
 /**
@@ -414,7 +448,6 @@ static void
 gpx_chars(SaxData * data, const xmlChar * ch, int len)
 {
        guint i;
-       vprintf("%s()\n", __PRETTY_FUNCTION__);
 
        switch (data->state) {
        case ERROR:
@@ -423,15 +456,15 @@ gpx_chars(SaxData * data, const xmlChar * ch, int len)
        case INSIDE_PATH_POINT_ELE:
        case INSIDE_PATH_POINT_TIME:
        case INSIDE_PATH_POINT_DESC:
+       case INSIDE_PATH_POINT_NAME:
                for (i = 0; i < len; i++)
                        data->chars = g_string_append_c(data->chars, ch[i]);
-               vprintf("%s\n", data->chars->str);
+               g_debug("GPXC: %s", data->chars->str);
                break;
        default:
                break;
        }
 
-       vprintf("%s(): return\n", __PRETTY_FUNCTION__);
 }
 
 /**
@@ -441,8 +474,6 @@ gpx_chars(SaxData * data, const xmlChar * ch, int len)
 static xmlEntityPtr 
 gpx_get_entity(SaxData * data, const xmlChar * name)
 {
-       vprintf("%s()\n", __PRETTY_FUNCTION__);
-       vprintf("%s(): return\n", __PRETTY_FUNCTION__);
        return xmlGetPredefinedEntity(name);
 }
 
@@ -452,8 +483,6 @@ gpx_get_entity(SaxData * data, const xmlChar * name)
 static void 
 gpx_error(SaxData * data, const gchar * msg, ...)
 {
-       vprintf("%s()\n", __PRETTY_FUNCTION__);
-       vprintf("%s(): return\n", __PRETTY_FUNCTION__);
        data->state = ERROR;
 }
 
@@ -462,7 +491,6 @@ parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
 {
        SaxData data;
        xmlSAXHandler sax_handler;
-       printf("%s()\n", __PRETTY_FUNCTION__);
 
        MACRO_PATH_INIT(data.path);
        data.state = START;
@@ -481,7 +509,7 @@ parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
        g_string_free(data.chars, TRUE);
 
        if (data.state != FINISH) {
-               vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
+               g_debug("GPX: Parser stopped in error state %d", data.state);
                return FALSE;
        }
 
@@ -518,18 +546,14 @@ parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
                         * plus room for more route data. */
                        path_resize(dest, num_dest_points + num_src_points);
 
-                       memcpy(dest->tail + 1, src_first,
-                              num_src_points * sizeof(Point));
+                       memcpy(dest->tail + 1, src_first, num_src_points * sizeof(Point));
 
                        dest->tail += num_src_points;
 
                        /* Append waypoints from src to dest->. */
-                       path_wresize(dest, (dest->wtail - dest->whead)
-                                    + (src->wtail - src->whead) + 2);
+                       path_wresize(dest, (dest->wtail - dest->whead) + (src->wtail - src->whead) + 2);
                        for (curr = src->whead - 1; curr++ != src->wtail;) {
-                               (++(dest->wtail))->point =
-                                   dest->head + num_dest_points +
-                                   (curr->point - src_first);
+                               (++(dest->wtail))->point = dest->head + num_dest_points + (curr->point - src_first);
                                dest->wtail->desc = curr->desc;
                        }
 
@@ -545,12 +569,9 @@ parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
                MACRO_PATH_FREE((*to_replace));
                /* Overwrite with data.route. */
                (*to_replace) = data.path;
-               path_resize(to_replace,
-                           to_replace->tail - to_replace->head + 1);
-               path_wresize(to_replace,
-                            to_replace->wtail - to_replace->whead + 1);
+               path_resize(to_replace, to_replace->tail - to_replace->head + 1);
+               path_wresize(to_replace, to_replace->wtail - to_replace->whead + 1);
        }
 
-       vprintf("%s(): return TRUE\n", __PRETTY_FUNCTION__);
        return TRUE;
 }