altoslib: Expose locale and non-locale floating point parsing functions
authorKeith Packard <keithp@keithp.com>
Tue, 19 May 2015 17:09:22 +0000 (10:09 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 19 May 2015 17:09:22 +0000 (10:09 -0700)
UI bits use locale-specific floating point formats, so parsing those
needs to use the locale. Network-based data, like .kml bits need to
use non-locale-specific parsing code, so now we've got both APIs
available, and each used as appropriate.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java
altoslib/AltosGPS.java
altoslib/AltosParse.java
altoslib/AltosUnits.java
altosui/AltosConfigPyroUI.java
altosui/AltosConfigUI.java
altosuilib/AltosConfigFreqUI.java
altosuilib/AltosUIMapPreload.java
telegps/TeleGPS.java
telegps/TeleGPSConfigUI.java

index b4fc3bc3a3ad105d411b66c3e8a84b88d01b3bc8..db065b3fcafbd8248cc16b324cf29a60904994e4 100644 (file)
@@ -21,6 +21,7 @@ import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.text.*;
 
 import android.app.Activity;
 import android.app.PendingIntent;
@@ -687,8 +688,8 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
 
        void setFrequency(String freq) {
                try {
-                       setFrequency (Double.parseDouble(freq.substring(11, 17)));
-               } catch (NumberFormatException e) {
+                       setFrequency (AltosParse.parse_double_net(freq.substring(11, 17)));
+               } catch (ParseException e) {
                }
        }
 
index 2139efb22fe3e6c106c963a43dab2a1b308dba5f..e3aee4eafe6e9af9851ce183d2dd6e6f038671e7 100644 (file)
@@ -195,10 +195,10 @@ public class AltosGPS implements Cloneable, Serializable {
                        lon = AltosParse.parse_coord(words[i++]);
                        alt = AltosParse.parse_int(words[i++]);
                        if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
-                               ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
+                               ground_speed = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "m/s(H)"));
                                course = AltosParse.parse_int(words[i++]);
-                               climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
-                               hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
+                               climb_rate = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "m/s(V)"));
+                               hdop = AltosParse.parse_double_net(AltosParse.strip_suffix(words[i++], "(hdop)"));
                                h_error = AltosParse.parse_int(words[i++]);
                                v_error = AltosParse.parse_int(words[i++]);
                        }
index 2fb69c15ce40de247537c22377f422d15afcbf18..a8b02b5652611433420350294e35c2e5725fc825 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.altusmetrum.altoslib_6;
 
+import java.util.*;
 import java.text.*;
 
 public class AltosParse {
@@ -40,11 +41,23 @@ public class AltosParse {
                }
        }
 
-       public static double parse_double(String v) throws ParseException {
+       static NumberFormat nf_locale = NumberFormat.getInstance();
+
+       static NumberFormat nf_net = NumberFormat.getInstance(Locale.ROOT);
+
+       public static double parse_double_locale(String str) throws ParseException {
                try {
-                       return Double.parseDouble(v);
-               } catch (NumberFormatException e) {
-                       throw new ParseException("error parsing double " + v, 0);
+                       return nf_locale.parse(str.trim()).doubleValue();
+               } catch (ParseException pe) {
+                       throw new ParseException("error parsing double " + str, 0);
+               }
+       }
+
+       public static double parse_double_net(String str) throws ParseException {
+               try {
+                       return nf_net.parse(str.trim()).doubleValue();
+               } catch (ParseException pe) {
+                       throw new ParseException("error parsing double " + str, 0);
                }
        }
 
index f6e34e773ed36e153d46dd8e6d8be2a0972d4624..e266b2437b606d7e1c45bf8f932246bcec359b39 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.altusmetrum.altoslib_6;
 
+import java.text.*;
+
 public abstract class AltosUnits {
 
        public abstract double value(double v, boolean imperial_units);
@@ -29,13 +31,22 @@ public abstract class AltosUnits {
 
        public abstract int show_fraction(int width, boolean imperial_units);
 
-       public double parse(String s, boolean imperial_units) throws NumberFormatException {
-               double v = Double.parseDouble(s);
+       public double parse_locale(String s, boolean imperial_units) throws ParseException {
+               double v = AltosParse.parse_double_locale(s);
+               return inverse(v, imperial_units);
+       }
+
+       public double parse_net(String s, boolean imperial_units) throws ParseException {
+               double v = AltosParse.parse_double_net(s);
                return inverse(v, imperial_units);
        }
 
-       public double parse(String s) throws NumberFormatException {
-               return parse(s, AltosConvert.imperial_units);
+       public double parse_locale(String s) throws ParseException {
+               return parse_locale(s, AltosConvert.imperial_units);
+       }
+
+       public double parse_net(String s) throws ParseException {
+               return parse_net(s, AltosConvert.imperial_units);
        }
 
        public double value(double v) {
@@ -105,4 +116,4 @@ public abstract class AltosUnits {
        public String say_units(double v) {
                return say_units(v, AltosConvert.imperial_units);
        }
-}
\ No newline at end of file
+}
index 61208dfe91676a81acfdc1b0f317d9e48d53a6a6..69b31ff52529e1831b59ba54ab57281060b7eb34 100644 (file)
@@ -17,6 +17,7 @@
 
 package altosui;
 
+import java.text.*;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
@@ -87,9 +88,9 @@ public class AltosConfigPyroUI
 
                        if (units != null) {
                                try {
-                                       double v = units.parse(value.getText(), !imperial_units);
+                                       double v = units.parse_locale(value.getText(), !imperial_units);
                                        set(enabled(), v);
-                               } catch (NumberFormatException ne) {
+                               } catch (ParseException pe) {
                                        set(enabled(), 0.0);
                                }
                        }
@@ -129,9 +130,9 @@ public class AltosConfigPyroUI
                                AltosUnits units = AltosPyro.pyro_to_units(flag);
                                try {
                                        if (units != null)
-                                               return units.parse(value.getText());
-                                       return Double.parseDouble(value.getText());
-                               } catch (NumberFormatException e) {
+                                               return units.parse_locale(value.getText());
+                                       return AltosParse.parse_double_locale(value.getText());
+                               } catch (ParseException e) {
                                        throw new AltosConfigDataException("\"%s\": %s\n", value.getText(), e.getMessage());
                                }
                        }
@@ -298,8 +299,8 @@ public class AltosConfigPyroUI
                String  v = pyro_firing_time_value.getSelectedItem().toString();
 
                try {
-                       return Double.parseDouble(v);
-               } catch (NumberFormatException e) {
+                       return AltosParse.parse_double_locale(v);
+               } catch (ParseException e) {
                        throw new AltosConfigDataException("Invalid pyro firing time \"%s\"", v);
                }
        }
index 67decaa4f589e4e07cca3845763ae0392e1424c2..ef477ca99596181e043e11af6e4171f2e6f3f030 100644 (file)
@@ -21,6 +21,7 @@ import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.event.*;
+import java.text.*;
 import org.altusmetrum.altoslib_6.*;
 import org.altusmetrum.altosuilib_6.*;
 
@@ -976,8 +977,13 @@ public class AltosConfigUI
 
        }
 
-       public int main_deploy() {
-               return (int) (AltosConvert.height.parse(main_deploy_value.getSelectedItem().toString()) + 0.5);
+       public int main_deploy() throws AltosConfigDataException {
+               String  str = main_deploy_value.getSelectedItem().toString();
+               try {
+                       return (int) (AltosConvert.height.parse_locale(str) + 0.5);
+               } catch (ParseException pe) {
+                       throw new AltosConfigDataException("invalid main deploy height %s", str);
+               }
        }
 
        String get_main_deploy_label() {
@@ -1006,14 +1012,21 @@ public class AltosConfigUI
                String v = main_deploy_value.getSelectedItem().toString();
                main_deploy_label.setText(get_main_deploy_label());
                set_main_deploy_values();
-               int m = (int) (AltosConvert.height.parse(v, !imperial_units) + 0.5);
-               set_main_deploy(m);
+               try {
+                       int m = (int) (AltosConvert.height.parse_locale(v, !imperial_units) + 0.5);
+                       set_main_deploy(m);
+               } catch (ParseException pe) {
+               }
 
                if (tracker_motion_value.isEnabled()) {
                        String motion = tracker_motion_value.getSelectedItem().toString();
                        tracker_motion_label.setText(get_tracker_motion_label());
                        set_tracker_motion_values();
-                       set_tracker_motion((int) (AltosConvert.height.parse(motion, !imperial_units) + 0.5));
+                       try {
+                               int m = (int) (AltosConvert.height.parse_locale(motion, !imperial_units) + 0.5);
+                               set_tracker_motion(m);
+                       } catch (ParseException pe) {
+                       }
                }
 
                if (!was_dirty)
@@ -1272,7 +1285,12 @@ public class AltosConfigUI
        }
 
        public int tracker_motion() throws AltosConfigDataException {
-               return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
+               String str = tracker_motion_value.getSelectedItem().toString();
+               try {
+                       return (int) (AltosConvert.height.parse_locale(str) + 0.5);
+               } catch (ParseException pe) {
+                       throw new AltosConfigDataException("invalid tracker motion %s", str);
+               }
        }
 
        public void set_tracker_interval(int tracker_interval) {
index 6253e3e4c254f1e51e73a0a8d437c7b281642576..21514a816bfa5ed5caa8fe89ac977f389935c882 100644 (file)
@@ -18,6 +18,7 @@
 package org.altusmetrum.altosuilib_6;
 
 import java.awt.*;
+import java.text.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.util.*;
@@ -51,10 +52,10 @@ class AltosEditFreqUI extends AltosUIDialog implements ActionListener {
                String  d_s = description.getText();
 
                try {
-                       double  f_d = Double.parseDouble(f_s);
+                       double  f_d = AltosParse.parse_double_locale(f_s);
 
                        return new AltosFrequency(f_d, d_s);
-               } catch (NumberFormatException ne) {
+               } catch (ParseException ne) {
                }
                return null;
        }
index e82b6c60880c1d0b1dcde0d4f4a7cc749fb2d977..1973ae6ecd04c5c7ed22006c4099cf4bc9456ea3 100644 (file)
@@ -53,33 +53,33 @@ class AltosUIMapPos extends Box {
                hemi.setSelectedIndex(h);
        }
 
-       public double get_value() throws NumberFormatException {
+       public double get_value() throws ParseException {
                int     h = hemi.getSelectedIndex();
                String  d_t = deg.getText();
                String  m_t = min.getText();
                double  d, m, v;
                try {
-                       d = Double.parseDouble(d_t);
-               } catch (NumberFormatException ne) {
+                       d = AltosParse.parse_double_locale(d_t);
+               } catch (ParseException pe) {
                        JOptionPane.showMessageDialog(owner,
                                                      String.format("Invalid degrees \"%s\"",
                                                                    d_t),
                                                      "Invalid number",
                                                      JOptionPane.ERROR_MESSAGE);
-                       throw ne;
+                       throw pe;
                }
                try {
                        if (m_t.equals(""))
                                m = 0;
                        else
-                               m = Double.parseDouble(m_t);
-               } catch (NumberFormatException ne) {
+                               m = AltosParse.parse_double_locale(m_t);
+               } catch (ParseException pe) {
                        JOptionPane.showMessageDialog(owner,
                                                      String.format("Invalid minutes \"%s\"",
                                                                    m_t),
                                                      "Invalid number",
                                                      JOptionPane.ERROR_MESSAGE);
-                       throw ne;
+                       throw pe;
                }
                v = d + m/60.0;
                if (h == 1)
@@ -142,9 +142,9 @@ class AltosUISite {
                name = elements[0];
 
                try {
-                       latitude = Double.parseDouble(elements[1]);
-                       longitude = Double.parseDouble(elements[2]);
-               } catch (NumberFormatException ne) {
+                       latitude = AltosParse.parse_double_net(elements[1]);
+                       longitude = AltosParse.parse_double_net(elements[2]);
+               } catch (ParseException pe) {
                        throw new ParseException(String.format("Invalid site line %s", line), 0);
                }
        }
@@ -171,6 +171,7 @@ class AltosUISites extends Thread {
                try {
                        add(new AltosUISite(line));
                } catch (ParseException pe) {
+                       System.out.printf("parse exception %s\n", pe.toString());
                }
        }
 
@@ -394,7 +395,7 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I
                                                max_z = min_z;
                                        r = (Integer) radius.getSelectedItem();
                                        loading = true;
-                               } catch (NumberFormatException ne) {
+                               } catch (ParseException pe) {
                                        load_button.setSelected(false);
                                }
                                start_load();
index fe3351762a926b8086e72d0b17ad619a6cafa057..7570d380f18b9889c88db0e5d221eec4ca4586d3 100644 (file)
@@ -23,6 +23,7 @@ import javax.swing.*;
 import java.io.*;
 import java.util.concurrent.*;
 import java.util.*;
+import java.text.*;
 import org.altusmetrum.altoslib_6.*;
 import org.altusmetrum.altosuilib_6.*;
 
@@ -679,9 +680,13 @@ public class TeleGPS
                                if (args.length < i + 3) {
                                        help(1);
                                } else {
-                                       double lat = Double.parseDouble(args[i+1]);
-                                       double lon = Double.parseDouble(args[i+2]);
+                                       try {
+                                       double lat = AltosParse.parse_double_locale(args[i+1]);
+                                       double lon = AltosParse.parse_double_locale(args[i+2]);
                                        AltosUIMap.prefetch_maps(lat, lon);
+                                       } catch (ParseException e) {
+                                               System.out.printf("Can't parse number %s\n", e.toString());
+                                       }
                                        i += 2;
                                }
                        } else if (args[i].equals("--replay"))
index 97ab34b40b0b1846d1a93f8ef4b9e958869199e1..4e6575868ab33a609cc897b5f8c9c8402f1165f4 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.altusmetrum.telegps;
 
+import java.text.*;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
@@ -686,7 +687,11 @@ public class TeleGPSConfigUI
                        String motion = tracker_motion_value.getSelectedItem().toString();
                        tracker_motion_label.setText(get_tracker_motion_label());
                        set_tracker_motion_values();
-                       set_tracker_motion((int) (AltosConvert.height.parse(motion, !imperial_units) + 0.5));
+                       try {
+                               int m = (int) (AltosConvert.height.parse_locale(motion, !imperial_units) + 0.5);
+                               set_tracker_motion(m);
+                       } catch (ParseException pe) {
+                       }
                }
                if (!was_dirty)
                        set_clean();
@@ -886,7 +891,12 @@ public class TeleGPSConfigUI
        }
 
        public int tracker_motion() throws AltosConfigDataException {
-               return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
+               String str = tracker_motion_value.getSelectedItem().toString();
+               try {
+                       return (int) (AltosConvert.height.parse_locale(str) + 0.5);
+               } catch (ParseException pe) {
+                       throw new AltosConfigDataException("invalid tracker motion %s", str);
+               }
        }
 
        public void set_tracker_interval(int tracker_interval) {