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.
25 public class AltosParse {
26 static boolean isdigit(char c) {
27 return '0' <= c && c <= '9';
30 static int parse_int(String v) throws ParseException {
32 return Altos.fromdec(v);
33 } catch (NumberFormatException e) {
34 throw new ParseException("error parsing int " + v, 0);
38 static int parse_hex(String v) throws ParseException {
40 return Altos.fromhex(v);
41 } catch (NumberFormatException e) {
42 throw new ParseException("error parsing hex " + v, 0);
46 static double parse_double(String v) throws ParseException {
48 return Double.parseDouble(v);
49 } catch (NumberFormatException e) {
50 throw new ParseException("error parsing double " + v, 0);
54 static double parse_coord(String coord) throws ParseException {
55 String[] dsf = coord.split("\\D+");
57 if (dsf.length != 3) {
58 throw new ParseException("error parsing coord " + coord, 0);
60 int deg = parse_int(dsf[0]);
61 int min = parse_int(dsf[1]);
62 int frac = parse_int(dsf[2]);
64 double r = deg + (min + frac / 10000.0) / 60.0;
65 if (coord.endsWith("S") || coord.endsWith("W"))
70 static String strip_suffix(String v, String suffix) {
71 if (v.endsWith(suffix))
72 return v.substring(0, v.length() - suffix.length());
76 static void word(String v, String m) throws ParseException {
78 throw new ParseException("error matching '" + v + "' '" + m + "'", 0);