Change AltosLib to altoslib
[fw/altos] / altoslib / 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 org.altusmetrum.altoslib;
19
20 import java.text.*;
21
22 public class AltosParse {
23         public static boolean isdigit(char c) {
24                 return '0' <= c && c <= '9';
25         }
26
27         public static int parse_int(String v) throws ParseException {
28                 try {
29                         return AltosLib.fromdec(v);
30                 } catch (NumberFormatException e) {
31                         throw new ParseException("error parsing int " + v, 0);
32                 }
33         }
34
35         public static int parse_hex(String v) throws ParseException {
36                 try {
37                         return AltosLib.fromhex(v);
38                 } catch (NumberFormatException e) {
39                         throw new ParseException("error parsing hex " + v, 0);
40                 }
41         }
42
43         public static double parse_double(String v) throws ParseException {
44                 try {
45                         return Double.parseDouble(v);
46                 } catch (NumberFormatException e) {
47                         throw new ParseException("error parsing double " + v, 0);
48                 }
49         }
50
51         public static double parse_coord(String coord) throws ParseException {
52                 String[]        dsf = coord.split("\\D+");
53
54                 if (dsf.length != 3) {
55                         throw new ParseException("error parsing coord " + coord, 0);
56                 }
57                 int deg = parse_int(dsf[0]);
58                 int min = parse_int(dsf[1]);
59                 int frac = parse_int(dsf[2]);
60
61                 double r = deg + (min + frac / 10000.0) / 60.0;
62                 if (coord.endsWith("S") || coord.endsWith("W"))
63                         r = -r;
64                 return r;
65         }
66
67         public static String strip_suffix(String v, String suffix) {
68                 if (v.endsWith(suffix))
69                         return v.substring(0, v.length() - suffix.length());
70                 return v;
71         }
72
73         public static void word(String v, String m) throws ParseException {
74                 if (!v.equals(m)) {
75                         throw new ParseException("error matching '" + v + "' '" + m + "'", 0);
76                 }
77         }
78 }