From 41e65eee193474e40b3b5e403db774f0d6c9f8ef Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Wed, 19 Mar 2008 16:12:36 +0200 Subject: [PATCH] Add a enum for the gpx path prepend/append/new modes instead of arbitrary numbers. --- src/config-gconf.c | 2 +- src/gpx.c | 48 ++++++++++++++++++++++++++++++---------------- src/gpx.h | 8 +++++++- src/route.c | 12 ++++-------- src/track.c | 2 +- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/config-gconf.c b/src/config-gconf.c index 8d7cfd4..87864d4 100644 --- a/src/config-gconf.c +++ b/src/config-gconf.c @@ -426,7 +426,7 @@ gint size; pfile = gnome_vfs_uri_make_full_from_relative(config_dir, file); if (GNOME_VFS_OK == gnome_vfs_read_entire_file(pfile, &size, &bytes)) - gpx_parse(path, bytes, size, 0); /* 0 to replace track. */ + gpx_parse(path, bytes, size, GPX_PATH_NEW); g_free(pfile); } diff --git a/src/gpx.c b/src/gpx.c index 2b8ef15..6a60b25 100644 --- a/src/gpx.c +++ b/src/gpx.c @@ -497,8 +497,14 @@ va_end(args); data->state = ERROR; } +/** + * Parse a buffer of GPX data. + * + * XXX: Add support for parsing directly from file if the file is local. + * + */ gboolean -gpx_parse(Path *to_replace, gchar *buffer, gint size, gint policy_old) +gpx_parse(Path *path, gchar *buffer, gint size, gpx_path_policy policy) { SaxData data; xmlSAXHandler sax_handler; @@ -517,28 +523,32 @@ sax_handler.error = (errorSAXFunc) gpx_error; sax_handler.fatalError = (fatalErrorSAXFunc) gpx_error; xmlSAXUserParseMemory(&sax_handler, &data, buffer, size); + g_string_free(data.chars, TRUE); if (data.state != FINISH) { - g_debug("GPX: Parser stopped in error state %d", data.state); + g_warning("GPX: Parser stopped in error state %d", data.state); return FALSE; } -if (policy_old && to_replace->head != to_replace->tail) { +switch (policy) { +case GPX_PATH_APPEND: +case GPX_PATH_PREPEND: + { Point *src_first; Path *src, *dest; - if (policy_old > 0) { + if (policy==GPX_PATH_APPEND) { /* Append to current path. Make sure last path point is zero. */ - if (to_replace->tail->unity != 0) { - MACRO_PATH_INCREMENT_TAIL((*to_replace)); - *to_replace->tail = _point_null; + if (path->tail->unity != 0) { + MACRO_PATH_INCREMENT_TAIL((*path)); + *path->tail = _point_null; } src = &data.path; - dest = to_replace; + dest = path; } else { /* Prepend to current route. */ - src = to_replace; + src = path; dest = &data.path; } @@ -573,14 +583,20 @@ if (policy_old && to_replace->head != to_replace->tail) { * would free the string desc's that we just moved to data.route. */ g_free(src->head); g_free(src->whead); - if (policy_old < 0) - (*to_replace) = *dest; -} else { - MACRO_PATH_FREE((*to_replace)); + if (policy==GPX_PATH_PREPEND) + (*path) = *dest; + } +break; +case GPX_PATH_NEW: /* 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); + MACRO_PATH_FREE((*path)); + (*path) = data.path; + path_resize(path, path->tail - path->head + 1); + path_wresize(path, path->wtail - path->whead + 1); +break; +default: + g_assert_not_reached(); +break; } return TRUE; diff --git a/src/gpx.h b/src/gpx.h index d6c5e5d..d117b9d 100644 --- a/src/gpx.h +++ b/src/gpx.h @@ -5,8 +5,14 @@ #include #include "path.h" +typedef enum { + GPX_PATH_PREPEND=-1, + GPX_PATH_NEW=0, + GPX_PATH_APPEND=1, +} gpx_path_policy; + void gpx_init(void); gboolean gpx_write(Path *path, GnomeVFSHandle *handle); -gboolean gpx_parse(Path *to_replace, gchar *buffer, gint size, gint policy_old); +gboolean gpx_parse(Path *to_replace, gchar *buffer, gint size, gpx_path_policy policy); #endif diff --git a/src/route.c b/src/route.c index 3571922..7c54e87 100644 --- a/src/route.c +++ b/src/route.c @@ -109,7 +109,7 @@ gint size; if (file_open_get_contents(_route_dir_uri, &buffer, &size)) { /* If auto is enabled, append the route, otherwise replace it. */ - if (gpx_parse(_route, buffer, size, _autoroute_data.enabled ? 0 : 1)) { + if (gpx_parse(_route, buffer, size, _autoroute_data.enabled ? GPX_PATH_APPEND : GPX_PATH_NEW)) { route_cancel_autoroute(FALSE); MACRO_BANNER_SHOW_INFO(_window, _("Route Opened")); @@ -446,16 +446,13 @@ while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) { if (strncmp(rdl_data.bytes, "