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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_12;
24 public class AltosParse {
25 public static boolean isdigit(char c) {
26 return '0' <= c && c <= '9';
29 public static int parse_int(String v) throws ParseException {
31 return (int) AltosLib.fromdec(v);
32 } catch (NumberFormatException e) {
33 throw new ParseException("error parsing int " + v, 0);
37 public static long parse_long(String v) throws ParseException {
39 return AltosLib.fromdec(v);
40 } catch (NumberFormatException e) {
41 throw new ParseException("error parsing int " + v, 0);
45 public static int parse_hex(String v) throws ParseException {
47 return AltosLib.fromhex(v);
48 } catch (NumberFormatException e) {
49 throw new ParseException("error parsing hex " + v, 0);
53 static NumberFormat get_nf_locale() {
54 NumberFormat nf = NumberFormat.getInstance();
55 nf.setParseIntegerOnly(false);
56 nf.setGroupingUsed(false);
60 static NumberFormat nf_locale = get_nf_locale();
62 static NumberFormat get_nf_net() {
63 NumberFormat nf = NumberFormat.getInstance(Locale.ROOT);
64 nf.setParseIntegerOnly(false);
65 nf.setGroupingUsed(false);
69 static NumberFormat nf_net = get_nf_net();
71 public static double parse_double_locale(String str) throws ParseException {
73 return nf_locale.parse(str.trim()).doubleValue();
74 } catch (ParseException pe) {
75 throw new ParseException("error parsing double " + str, 0);
79 public static String format_double_locale(double number) {
80 return nf_locale.format(number);
83 public static double parse_double_net(String str) throws ParseException {
85 String t = str.trim();
86 // System.out.printf("Parse string \"%s\" trim \"%s\"\n", str, t);
87 return nf_net.parse(t).doubleValue();
88 } catch (ParseException pe) {
89 throw new ParseException("error parsing double " + str, 0);
93 public static String format_double_net(double number) {
94 String ret = nf_net.format(number);
95 // System.out.printf("format double %f \"%s\"\n", number, ret);
99 public static double parse_coord(String coord) throws ParseException {
100 String[] dsf = coord.split("\\D+");
102 if (dsf.length != 3) {
103 throw new ParseException("error parsing coord " + coord, 0);
105 int deg = parse_int(dsf[0]);
106 int min = parse_int(dsf[1]);
107 int frac = parse_int(dsf[2]);
109 double r = deg + (min + frac / 10000.0) / 60.0;
110 if (coord.endsWith("S") || coord.endsWith("W"))
115 public static String strip_suffix(String v, String suffix) {
116 if (v.endsWith(suffix))
117 return v.substring(0, v.length() - suffix.length());
121 public static void word(String v, String m) throws ParseException {
123 throw new ParseException("error matching '" + v + "' '" + m + "'", 0);