+static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord )
+{
+ gint latdeg, londeg;
+ gdouble latm, lonm;
+ gdouble lat, lon;
+ gchar *cand;
+ gint len, i;
+ gchar *s = g_strdup(text);
+
+ // g_print("parsing %s\n", s);
+
+ len = strlen(s);
+
+ /* remove non-digits following digits; gets rid of degree symbols or whatever people use, and
+ * punctuation
+ */
+ for (i=0; i<len-2; i++) {
+ if (g_ascii_isdigit (s[i]) && s[i+1] != '.' && !g_ascii_isdigit (s[i+1])) {
+ s[i+1] = ' ';
+ if (!g_ascii_isalnum (s[i+2]) && s[i+2] != '-') {
+ s[i+2] = ' ';
+ }
+ }
+ }
+
+ /* now try reading coordinates */
+ for (i=0; i<len; i++) {
+ if (s[i] == 'N' || s[i] == 'S' || g_ascii_isdigit (s[i])) {
+ gchar latc[2] = "SN";
+ gchar lonc[2] = "WE";
+ gint j, k;
+ cand = s+i;
+
+ for (j=0; j<2; j++) {
+ for (k=0; k<2; k++) {
+ gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf";
+ gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W";
+ gchar fmt3[] = "N %lf W %lf";
+ gchar fmt4[] = "%lf N %lf W";
+
+ fmt1[0] = latc[j]; fmt1[13] = lonc[k];
+ fmt2[11] = latc[j]; fmt2[24] = lonc[k];
+ fmt3[0] = latc[j]; fmt3[6] = lonc[k];
+ fmt4[4] = latc[j]; fmt4[10] = lonc[k];
+
+ if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 ||
+ sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) {
+ lat = (j*2-1) * (latdeg + latm / 60);
+ lon = (k*2-1) * (londeg + lonm / 60);
+ break;
+ }
+ if (sscanf(cand, fmt3, &lat, &lon) == 2 ||
+ sscanf(cand, fmt4, &lat, &lon) == 2) {
+ lat *= (j*2-1);
+ lon *= (k*2-1);
+ break;
+ }
+ }
+ if (k!=2) break;
+ }
+ if (j!=2) break;
+
+ if (sscanf(cand, "%d%*[ ]%lf %d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) {
+ lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60);
+ lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60);
+ break;
+ }
+ if (sscanf(cand, "%lf %lf", &lat, &lon) == 2) {
+ break;
+ }
+ }
+ }
+ g_free(s);
+
+ /* did we get to the end without actually finding a coordinate? */
+ if (i<len && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) {
+ coord->lat = lat;
+ coord->lon = lon;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord)
+{
+ VikCoord vc;
+ VikLayer *sel = vik_layers_panel_get_selected ( vlp );
+
+
+ vik_coord_load_from_latlon ( &vc, VIK_COORD_LATLON, coord );
+
+ if (sel && sel->type == VIK_LAYER_TRW) {
+ vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc );
+ } else {
+ a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), _("In order to paste a waypoint, please select an appropriate layer to paste into."), NULL);
+ }
+}
+