Merge branch 'master' of git://git.gag.com/fw/altos
[fw/altos] / ao-tools / altosui / AltosParse.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.text.*;
21 import java.lang.*;
22
23 public class AltosParse {
24         static int parse_int(String v) throws ParseException {
25                 try {
26                         return Integer.parseInt(v);
27                 } catch (NumberFormatException e) {
28                         throw new ParseException("error parsing int " + v, 0);
29                 }
30         }
31
32         static int parse_hex(String v) throws ParseException {
33                 try {
34                         return Integer.parseInt(v, 16);
35                 } catch (NumberFormatException e) {
36                         throw new ParseException("error parsing hex " + v, 0);
37                 }
38         }
39
40         static double parse_double(String v) throws ParseException {
41                 try {
42                         return Double.parseDouble(v);
43                 } catch (NumberFormatException e) {
44                         throw new ParseException("error parsing double " + v, 0);
45                 }
46         }
47
48         static double parse_coord(String coord) throws ParseException {
49                 String[]        dsf = coord.split("\\D+");
50
51                 if (dsf.length != 3) {
52                         throw new ParseException("error parsing coord " + coord, 0);
53                 }
54                 int deg = parse_int(dsf[0]);
55                 int min = parse_int(dsf[1]);
56                 int frac = parse_int(dsf[2]);
57
58                 double r = deg + (min + frac / 10000.0) / 60.0;
59                 if (coord.endsWith("S") || coord.endsWith("W"))
60                         r = -r;
61                 return r;
62         }
63
64         static String strip_suffix(String v, String suffix) {
65                 if (v.endsWith(suffix))
66                         return v.substring(0, v.length() - suffix.length());
67                 return v;
68         }
69
70         static void word(String v, String m) throws ParseException {
71                 if (!v.equals(m)) {
72                         throw new ParseException("error matching '" + v + "' '" + m + "'", 0);
73                 }
74         }
75 }