From: Kaj-Michael Lang Date: Fri, 25 Apr 2008 13:15:46 +0000 (+0300) Subject: Path bug fixes and checks for valid Path X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84189de4927e5ad90fbd1c07e3216fcfe86be82d;p=mapper Path bug fixes and checks for valid Path --- diff --git a/src/path.c b/src/path.c index 2a6268c..a26d874 100644 --- a/src/path.c +++ b/src/path.c @@ -33,15 +33,36 @@ #include "gpx.h" struct sql_select_stmt { - sqlite3_stmt *select_path; - sqlite3_stmt *select_path_nodes; - sqlite3_stmt *insert_path; - sqlite3_stmt *insert_path_node; - sqlite3_stmt *delete_path_nodes; - sqlite3_stmt *delete_path; + sqlite3_stmt *select_paths; + sqlite3_stmt *select_path_nodes; + + sqlite3_stmt *insert_path; + sqlite3_stmt *insert_path_node; + + sqlite3_stmt *delete_path; + sqlite3_stmt *delete_path_nodes; }; static struct sql_select_stmt sql; +#define PATH_TABLE_PATHS "create table IF NOT EXISTS paths ( \ + nid int primary key, \ + name text not null, \ + desc text, \ + t int not null);" + +#define PATH_TABLE_NODES "create table IF NOT EXISTS path_nodes ( \ + nid int not null, \ + lat real not null, \ + lon real not null, \ + sats int, \ + speed real, \ + course real, \ + pdop real, \ + hdop real, \ + vdop real, \ + name text default null, \ + t int not null);" + /* Path point add sensitivity */ static gint sensitivity=3; @@ -75,10 +96,14 @@ p->points=0; gboolean path_resize(Path *path, guint size) { +g_return_val_if_fail(path, FALSE); + if (path->head + size != path->cap) { - Point *old_head = path->head; WayPoint *curr; + Point *old_head = path->head; + path->head = g_renew(Point, old_head, size); + g_assert(path->head); path->cap = path->head + size; if (path->head != old_head) { path->tail = path->head + (path->tail - old_head); @@ -95,11 +120,15 @@ return FALSE; gboolean path_wresize(Path *path, guint wsize) { +g_return_val_if_fail(path, FALSE); + if (path->whead + wsize != path->wcap) { WayPoint *old_whead = path->whead; + path->whead = g_renew(WayPoint, old_whead, wsize); path->wtail = path->whead + (path->wtail - old_whead); path->wcap = path->whead + wsize; + return TRUE; } return FALSE; @@ -108,6 +137,8 @@ return FALSE; gboolean path_add_point(Path *path, GpsData *gps) { +g_return_val_if_fail(path, FALSE); + if (!gps) { MACRO_PATH_INCREMENT_TAIL(*path); *path->tail=_point_null; @@ -139,11 +170,8 @@ return TRUE; gboolean path_insert_break(Path *path) { -if (!path) - return FALSE; - -if (!path->tail) - return FALSE; +g_return_val_if_fail(path, FALSE); +g_return_val_if_fail(path->tail, FALSE); if (path->tail->unity) { guint x1, y1; @@ -171,6 +199,8 @@ path_find_last_point(Path *path) { Point *p=NULL; +g_return_val_if_fail(path, NULL); + if (path->head == path->tail) return p; @@ -185,6 +215,8 @@ path_get_distance_to(Path *path, Point *point, gdouble lat, gdouble lon) gdouble lat1, lon1, lat2, lon2; gdouble sum=0.0; +g_return_val_if_fail(path, 0.0); + /* If point is NULL, use the next waypoint. */ if (point == NULL && path->next_way) point = path->next_way->point; @@ -233,6 +265,10 @@ gchar *pfile; gchar *bytes; gint size; +g_return_val_if_fail(path, FALSE); +g_return_val_if_fail(config_dir, FALSE); +g_return_val_if_fail(file, FALSE); + pfile = gnome_vfs_uri_make_full_from_relative(config_dir, file); if (gnome_vfs_read_entire_file(pfile, &size, &bytes)==GNOME_VFS_OK) gpx_parse(path, bytes, size, GPX_PATH_NEW); @@ -245,6 +281,11 @@ path_save(Path *path, const gchar *config_dir, const gchar *file) { GnomeVFSHandle *handle; gchar *tfile; + +g_return_val_if_fail(path, FALSE); +g_return_val_if_fail(config_dir, FALSE); +g_return_val_if_fail(file, FALSE); + tfile=gnome_vfs_uri_make_full_from_relative(config_dir, file); if (gnome_vfs_create(&handle, tfile, GNOME_VFS_OPEN_WRITE, FALSE, 0600)==GNOME_VFS_OK) { gpx_write(path, handle); @@ -261,7 +302,7 @@ return TRUE; void path_insert_mark_text(Path *path, gchar *text) { -g_assert(path); +g_return_if_fail(path); MACRO_PATH_INCREMENT_WTAIL(*path); path->wtail->point=path->tail; if (text) { @@ -284,28 +325,26 @@ gchar tmp1[16], tmp2[16]; gdouble lat1, lon1, lat2, lon2; gdouble sum=0.0; -if (path->whead==path->wtail) - return NULL; +g_return_val_if_fail(path, NULL); wcurr=path->whead; -if (!wcurr) - return NULL; +g_return_val_if_fail(wcurr, NULL); +g_return_val_if_fail(wcurr->point, NULL); -if (!wcurr->point) - return NULL; +store=gtk_list_store_new(ROUTE_NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_DOUBLE); -unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat1, lon1); +if (path->whead==path->wtail) + return store; -store=gtk_list_store_new(ROUTE_NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_DOUBLE); +unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat1, lon1); while (wcurr!=path->wtail) { - if (!wcurr) { + if (!wcurr) break; - } if (!wcurr->point) { - g_debug("No point for waypoint (%s) skipping", wcurr->desc); + g_debug("PSTORE: No point for waypoint (%s) skipping", wcurr->desc); wcurr++; continue; } @@ -339,14 +378,8 @@ return store; void position_set(Position *pos, gboolean valid, gdouble lat, gdouble lon) { +g_assert(pos); pos->valid=valid; pos->lat=valid ? lat : NAN; pos->lon=valid ? lon : NAN; } - -void -position_update(Position *pos, GpsData *data) -{ -if (!pos->valid) - return; -} diff --git a/src/path.h b/src/path.h index a949178..95f977c 100644 --- a/src/path.h +++ b/src/path.h @@ -119,6 +119,8 @@ void path_insert_mark_text(Path *path, gchar *text); void path_insert_mark_autonumber(Path *path); void path_insert_mark_audio(Path *path, gchar *audio); +void position_set(Position *pos, gboolean valid, gdouble lat, gdouble lon); + GtkListStore *path_generate_store(Path *path); #define MACRO_PATH_INIT(path) { \ diff --git a/src/route.c b/src/route.c index e972975..88fd3fe 100644 --- a/src/route.c +++ b/src/route.c @@ -56,6 +56,8 @@ route_clear(Path *route) GtkWidget *confirm; gboolean r=FALSE; +g_return_val_if_fail(route, FALSE); + confirm=hildon_note_new_confirmation(GTK_WINDOW(_window), _("Really clear the route?")); if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) { @@ -78,8 +80,8 @@ route_check_waypoint_announce(Path *route, GpsData *gps) { guint announce_thres_unsquared; -if (!gps) - return; +g_return_if_fail(route); +g_return_if_fail(gps); if (!_announce_waypoints) return; @@ -102,6 +104,8 @@ if (route->next_way_dist_squared<(announce_thres_unsquared * announce_thres_unsq void route_autoroute_check(Path *route) { +g_return_if_fail(route); + if (_autoroute_data.enabled && !_autoroute_data.in_progress && route->near_point_dist_squared > 400) { MACRO_BANNER_SHOW_INFO(_window, _("Recalculating directions...")); _autoroute_data.in_progress = TRUE; @@ -116,6 +120,8 @@ route_open_file(Path *route) gchar *buffer; gint size; +g_return_val_if_fail(route, FALSE); + 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 ? GPX_PATH_APPEND : GPX_PATH_NEW)) { @@ -124,7 +130,7 @@ if (file_open_get_contents(&_route_dir_uri, &buffer, &size)) { MACRO_BANNER_SHOW_INFO(_window, _("Route Opened")); /* Find the nearest route point, if we're connected. */ route_find_nearest_point(route); - route_set_destination_from_last(route); + route_set_destination_from_last(route, &_dest); return TRUE; } else { popup_error(_window, _("Error parsing GPX file.")); @@ -143,6 +149,8 @@ route_save(Path *route) { GnomeVFSHandle *handle; +g_return_val_if_fail(route, FALSE); + if (route->head==route->tail) { MACRO_BANNER_SHOW_INFO(_window, _("No route exist.")); return FALSE; @@ -464,7 +472,7 @@ while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) { MACRO_BANNER_SHOW_INFO(_window, _("Route Downloaded")); g_free(rdl_data.bytes); - route_set_destination_from_last(route); + route_set_destination_from_last(route, &_dest); /* Success! Get out of the while loop. */ r=TRUE; @@ -489,6 +497,8 @@ WayPoint *wnear; guint64 nearest_squared; Point pos = { unitx, unity, 0, NAN }; +g_return_val_if_fail(route, NULL); + wcurr = wnear = route->whead; if (wcurr && wcurr->point && wcurr != route->wtail) { nearest_squared = DISTANCE_SQUARED(pos, *(wcurr->point)); @@ -528,6 +538,8 @@ Point *curr, *near; WayPoint *wcurr, *wnext; guint64 near_dist_squared; +g_return_val_if_fail(route, FALSE); + /* If we have waypoints (_next_way != NULL), then determine the "next * waypoint", which is defined as the waypoint after the nearest point, * UNLESS we've passed that waypoint, in which case the waypoint after @@ -616,6 +628,8 @@ return ret; void route_find_nearest_point(Path *route) { +g_return_if_fail(route); + /* Initialize near_point to first non-zero point. */ route->near_point = route->head; while (!route->near_point->unity && route->near_point != route->tail) @@ -649,6 +663,9 @@ gchar buffer[80]; gdouble lat, lon; gdouble sum; +g_return_val_if_fail(route, FALSE); +g_return_val_if_fail(point, FALSE); + unit2latlon(_gps->data.unitx, _gps->data.unity, lat, lon); sum=path_get_distance_to(route, point, lat, lon); g_snprintf(buffer, sizeof(buffer), "%s: %.02f %s", _("Distance"), sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]); @@ -660,6 +677,8 @@ return TRUE; void route_show_distance_to_next(Path *route) { +g_return_if_fail(route); + if (!route_show_distance_to(route, NULL)) MACRO_BANNER_SHOW_INFO(_window, _("There is no next waypoint.")); } @@ -670,6 +689,9 @@ route_set_destination_from_last(Path *route, Position *pos) Point *p; gdouble lat,lon; +g_return_val_if_fail(route, FALSE); +g_return_val_if_fail(pos, FALSE); + if (route->head==route->tail) { position_set(pos, FALSE, NAN, NAN); return FALSE; @@ -686,6 +708,8 @@ return TRUE; void route_show_distance_to_last(Path *route) { +g_return_if_fail(route); + if (route->head != route->tail) { route_show_distance_to(route, path_find_last_point(route)); } else { diff --git a/src/route.h b/src/route.h index 39450c6..b39dca1 100644 --- a/src/route.h +++ b/src/route.h @@ -41,6 +41,8 @@ WayPoint *route_find_nearest_waypoint(Path *route, guint unitx, guint unity); gboolean route_update_nears(Path *route, gboolean quick); +gboolean route_set_destination_from_last(Path *route, Position *pos); + gboolean route_show_distance_to(Path *route, Point *point); void route_show_distance_to_last(Path *route); void route_show_distance_to_next(Path *route);