2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_11;
23 public class AltosParse {
24 public static boolean isdigit(char c) {
25 return '0' <= c && c <= '9';
28 public static int parse_int(String v) throws ParseException {
30 return (int) AltosLib.fromdec(v);
31 } catch (NumberFormatException e) {
32 throw new ParseException("error parsing int " + v, 0);
36 public static long parse_long(String v) throws ParseException {
38 return AltosLib.fromdec(v);
39 } catch (NumberFormatException e) {
40 throw new ParseException("error parsing int " + v, 0);
44 public static int parse_hex(String v) throws ParseException {
46 return AltosLib.fromhex(v);
47 } catch (NumberFormatException e) {
48 throw new ParseException("error parsing hex " + v, 0);
52 static NumberFormat nf_locale = NumberFormat.getInstance();
54 static NumberFormat nf_net = NumberFormat.getInstance(Locale.ROOT);
56 public static double parse_double_locale(String str) throws ParseException {
58 return nf_locale.parse(str.trim()).doubleValue();
59 } catch (ParseException pe) {
60 throw new ParseException("error parsing double " + str, 0);
64 public static String format_double_locale(double number) {
65 return nf_locale.format(number);
68 public static double parse_double_net(String str) throws ParseException {
70 return nf_net.parse(str.trim()).doubleValue();
71 } catch (ParseException pe) {
72 throw new ParseException("error parsing double " + str, 0);
76 public static String format_double_net(double number) {
77 return nf_net.format(number);
80 public static double parse_coord(String coord) throws ParseException {
81 String[] dsf = coord.split("\\D+");
83 if (dsf.length != 3) {
84 throw new ParseException("error parsing coord " + coord, 0);
86 int deg = parse_int(dsf[0]);
87 int min = parse_int(dsf[1]);
88 int frac = parse_int(dsf[2]);
90 double r = deg + (min + frac / 10000.0) / 60.0;
91 if (coord.endsWith("S") || coord.endsWith("W"))
96 public static String strip_suffix(String v, String suffix) {
97 if (v.endsWith(suffix))
98 return v.substring(0, v.length() - suffix.length());
102 public static void word(String v, String m) throws ParseException {
104 throw new ParseException("error matching '" + v + "' '" + m + "'", 0);