X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/18ec873dc8439733b81cb96bc958b35cabdacc8c..961e8114b32c207d5645c097c13ab0c65000938d:/src/babel.c?ds=sidebyside diff --git a/src/babel.c b/src/babel.c index 7877f7ff..27bc918d 100644 --- a/src/babel.c +++ b/src/babel.c @@ -43,6 +43,7 @@ #ifdef HAVE_UNISTD_H #include #endif +#include #include #include @@ -59,6 +60,16 @@ static gchar *gpsbabel_loc = NULL; */ static gchar *unbuffer_loc = NULL; +/** + * List of file formats supported by gpsbabel. + */ +GList *a_babel_file_list; + +/** + * List of device supported by gpsbabel. + */ +GList *a_babel_device_list; + /** * a_babel_convert: * @vt: The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs. @@ -194,6 +205,10 @@ static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointe /** * babel_general_convert_from: + * @vtl: The TrackWaypoint Layer to save the data into + * If it is null it signifies that no data is to be processed, + * however the gpsbabel command is still ran as it can be for non-data related options eg: + * for use with the power off command - 'command_off' * @cb: callback that is run upon new data from STDOUT (?) * (TODO: STDERR would be nice since we usually redirect STDOUT) * @user_data: passed along to cb @@ -210,12 +225,13 @@ static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gboolean ret = FALSE; FILE *f = NULL; - /* No data required */ - if ( vt == NULL ) - return TRUE; - if (babel_general_convert(cb, args, user_data)) { + /* No data actually required but still need to have run gpsbabel anyway + - eg using the device power command_off */ + if ( vt == NULL ) + return TRUE; + f = g_fopen(name_dst, "r"); if (f) { a_gpx_read_file ( vt, f ); @@ -408,6 +424,88 @@ gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, BabelStatus return ret; } +static void set_mode(BabelMode mode, gchar *smode) +{ + mode.waypointsRead = smode[0] == 'r'; + mode.waypointsWrite = smode[1] == 'w'; + mode.tracksRead = smode[2] == 'r'; + mode.tracksWrite = smode[3] == 'w'; + mode.routesRead = smode[4] == 'r'; + mode.routesWrite = smode[5] == 'w'; +} + +/** + * load_feature: + * + * Load a single feature stored in the given line. + */ +static void load_feature_parse_line (gchar *line) +{ + gchar **tokens = g_strsplit ( line, "\t", 0 ); + if ( tokens != NULL + && tokens[0] != NULL ) { + if ( strcmp("serial", tokens[0]) == 0 ) { + if ( tokens[1] != NULL + && tokens[2] != NULL + && tokens[3] != NULL + && tokens[4] != NULL ) { + BabelDevice *device = g_malloc ( sizeof (BabelDevice) ); + set_mode (device->mode, tokens[1]); + device->name = g_strdup (tokens[2]); + device->label = g_strdup (tokens[4]); + a_babel_device_list = g_list_append (a_babel_device_list, device); + g_debug ("New gpsbabel device: %s", device->name); + } else { + g_warning ( "Unexpected gpsbabel format string: %s", line); + } + } else if ( strcmp("file", tokens[0]) == 0 ) { + if ( tokens[1] != NULL + && tokens[2] != NULL + && tokens[3] != NULL + && tokens[4] != NULL ) { + BabelFile *file = g_malloc ( sizeof (BabelFile) ); + set_mode (file->mode, tokens[1]); + file->name = g_strdup (tokens[2]); + file->ext = g_strdup (tokens[3]); + file->label = g_strdup (tokens[4]); + a_babel_file_list = g_list_append (a_babel_file_list, file); + g_debug ("New gpsbabel file: %s", file->name); + } else { + g_warning ( "Unexpected gpsbabel format string: %s", line); + } + } /* else: ignore */ + } else { + g_warning ( "Unexpected gpsbabel format string: %s", line); + } + g_strfreev ( tokens ); +} + +static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data) +{ + if (line != NULL) + load_feature_parse_line (line); +} + +static gboolean load_feature () +{ + int i; + gboolean ret = FALSE; + gchar *args[4]; + + if ( gpsbabel_loc ) { + i = 0; + if ( unbuffer_loc ) + args[i++] = unbuffer_loc; + args[i++] = gpsbabel_loc; + args[i++] = "-^3"; + args[i] = NULL; + + ret = babel_general_convert (load_feature_cb, args, NULL); + } else + g_error("gpsbabel not found in PATH"); + + return ret; +} void a_babel_init () { @@ -419,6 +517,7 @@ void a_babel_init () if ( !unbuffer_loc ) g_warning( "unbuffer not found in PATH" ); + load_feature (); } void a_babel_uninit ()