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