From c7123bcbba7fdc85f680a1e13647b010f01f2b43 Mon Sep 17 00:00:00 2001 From: gnuite Date: Mon, 10 Mar 2008 02:25:55 +0000 Subject: [PATCH] Fixed GPX writing to support output of XML-escaped characters (ampersand, etc.). git-svn-id: svn+ssh://garage/var/lib/gforge/svnroot/maemo-mapper/trunk@163 6c538b50-5814-0410-93ad-8bdf4c0149d1 --- src/gpx.c | 180 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 110 insertions(+), 70 deletions(-) diff --git a/src/gpx.c b/src/gpx.c index 4c668f0..8dc9bf4 100644 --- a/src/gpx.c +++ b/src/gpx.c @@ -99,6 +99,79 @@ gpx_error(SaxData *data, const gchar *msg, ...) data->state = ERROR; } +static gboolean +gpx_write_string(GnomeVFSHandle *handle, const gchar *str) +{ + GnomeVFSResult vfs_result; + GnomeVFSFileSize size; + if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write( + handle, str, strlen(str), &size))) + { + gchar buffer[BUFFER_SIZE]; + 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; + } + return TRUE; +} + +static gboolean +gpx_write_escaped(GnomeVFSHandle *handle, const gchar *str) +{ + const gchar *ptr = str; + const gchar *nullchr = ptr + strlen(ptr); + while(ptr < nullchr) + { + gchar *newptr = strpbrk(ptr, "&<>"); + if(newptr != NULL) + { + /* First, write out what we have so far. */ + const gchar *to_write; + GnomeVFSResult vfs_result; + GnomeVFSFileSize size; + if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write( + handle, ptr, newptr - ptr, &size))) + { + gchar buffer[BUFFER_SIZE]; + 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; + } + + /* Now, write the XML entity. */ + switch(*newptr) + { + case '&': + to_write = "&"; + break; + case '<': + to_write = "<"; + break; + case '>': + to_write = ">"; + break; + } + gpx_write_string(handle, to_write); + + /* Advance the pointer to continue searching for entities. */ + ptr = newptr + 1; + } + else + { + /* No characters need escaping - write the whole thing. */ + gpx_write_string(handle, ptr); + ptr = nullchr; + } + } + return TRUE; +} + /**************************************************************************** * BELOW: OPEN PATH ********************************************************* ****************************************************************************/ @@ -470,22 +543,6 @@ gpx_path_parse(Path *to_replace, gchar *buffer, gint size, gint policy_old) * BELOW: SAVE PATH ********************************************************* ****************************************************************************/ -#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]; \ - 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; \ - } \ -} - gboolean gpx_path_write(Path *path, GnomeVFSHandle *handle) { @@ -504,7 +561,7 @@ gpx_path_write(Path *path, GnomeVFSHandle *handle) } /* Write the header. */ - WRITE_STRING( + gpx_write_string(handle, "\n" "\n" @@ -522,33 +579,33 @@ gpx_path_write(Path *path, GnomeVFSHandle *handle) if(trkseg_break) { /* First trkpt of the segment - write trkseg header. */ - WRITE_STRING(" \n" + gpx_write_string(handle, " \n" " \n"); trkseg_break = FALSE; } unit2latlon(curr->unitx, curr->unity, lat, lon); - WRITE_STRING(" altitude != 0) { if(first_sub) { - WRITE_STRING(">\n"); + gpx_write_string(handle, ">\n"); first_sub = FALSE; } - WRITE_STRING(" "); + gpx_write_string(handle, " "); { g_ascii_formatd(buffer, 80, "%.2f", curr->altitude); - WRITE_STRING(buffer); + gpx_write_string(handle, buffer); } - WRITE_STRING("\n"); + gpx_write_string(handle, "\n"); } /* write the time */ @@ -556,35 +613,35 @@ gpx_path_write(Path *path, GnomeVFSHandle *handle) { if(first_sub) { - WRITE_STRING(">\n"); + gpx_write_string(handle, ">\n"); first_sub = FALSE; } - WRITE_STRING(" \n"); } if(wcurr && curr == wcurr->point) { if(first_sub) { - WRITE_STRING(">\n"); + gpx_write_string(handle, ">\n"); first_sub = FALSE; } - WRITE_STRING(" "); - WRITE_STRING(wcurr->desc); - WRITE_STRING("\n"); + gpx_write_string(handle, " "); + gpx_write_escaped(handle, wcurr->desc); + gpx_write_string(handle, "\n"); wcurr++; } if(first_sub) { - WRITE_STRING("/>\n"); + gpx_write_string(handle, "/>\n"); } else { - WRITE_STRING(" \n"); + gpx_write_string(handle, " \n"); } } else @@ -592,7 +649,7 @@ gpx_path_write(Path *path, GnomeVFSHandle *handle) } /* Write the footer. */ - WRITE_STRING( + gpx_write_string(handle, " \n" " \n" "\n"); @@ -797,22 +854,6 @@ gpx_poi_parse(gchar *buffer, gint size, GList **poi_list) * BELOW: SAVE POI ********************************************************** ****************************************************************************/ -#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]; \ - 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; \ - } \ -} - gint gpx_poi_write(GtkTreeModel *model, GnomeVFSHandle *handle) { @@ -821,7 +862,7 @@ gpx_poi_write(GtkTreeModel *model, GnomeVFSHandle *handle) printf("%s()\n", __PRETTY_FUNCTION__); /* Write the header. */ - WRITE_STRING( + gpx_write_string(handle, "\n" "\n"); @@ -848,35 +889,34 @@ gpx_poi_write(GtkTreeModel *model, GnomeVFSHandle *handle) { gchar buffer[80]; - WRITE_STRING(" \n"); + gpx_write_string(handle, buffer); + gpx_write_string(handle, "\">\n"); if(poi.label && *poi.label) { - WRITE_STRING(" "); - WRITE_STRING(poi.label); - WRITE_STRING("\n"); + gpx_write_string(handle, " "); + gpx_write_escaped(handle, poi.label); + gpx_write_string(handle, "\n"); } if(poi.desc && *poi.desc) { - WRITE_STRING(" "); - WRITE_STRING(poi.desc); - WRITE_STRING("\n"); + gpx_write_string(handle, " "); + gpx_write_escaped(handle, poi.desc); + gpx_write_string(handle, "\n"); } - WRITE_STRING(" \n"); + gpx_write_string(handle, " \n"); ++ num_written; } } while(gtk_tree_model_iter_next(model, &iter)); /* Write the footer. */ - WRITE_STRING( - "\n"); + gpx_write_string(handle, "\n"); vprintf("%s(): return %d\n", __PRETTY_FUNCTION__, num_written); return num_written; -- 2.44.0