45ef7dac843cf265e1fb7309136d8a757e1c36e2
[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_11;
19
20 import java.util.*;
21 import java.text.*;
22
23 public class AltosParse {
24         public static boolean isdigit(char c) {
25                 return '0' <= c && c <= '9';
26         }
27
28         public static int parse_int(String v) throws ParseException {
29                 try {
30                         return (int) AltosLib.fromdec(v);
31                 } catch (NumberFormatException e) {
32                         throw new ParseException("error parsing int " + v, 0);
33                 }
34         }
35
36         public static long parse_long(String v) throws ParseException {
37                 try {
38                         return AltosLib.fromdec(v);
39                 } catch (NumberFormatException e) {
40                         throw new ParseException("error parsing int " + v, 0);
41                 }
42         }
43
44         public static int parse_hex(String v) throws ParseException {
45                 try {
46                         return AltosLib.fromhex(v);
47                 } catch (NumberFormatException e) {
48                         throw new ParseException("error parsing hex " + v, 0);
49                 }
50         }
51
52         static NumberFormat get_nf_locale() {
53                 NumberFormat nf = NumberFormat.getInstance();
54                 nf.setParseIntegerOnly(false);
55                 nf.setGroupingUsed(false);
56                 return nf;
57         }
58
59         static NumberFormat nf_locale = get_nf_locale();
60
61         static NumberFormat get_nf_net() {
62                 NumberFormat nf = NumberFormat.getInstance(Locale.ROOT);
63                 nf.setParseIntegerOnly(false);
64                 nf.setGroupingUsed(false);
65                 return nf;
66         }
67
68         static NumberFormat nf_net = get_nf_net();
69
70         public static double parse_double_locale(String str) throws ParseException {
71                 try {
72                         return nf_locale.parse(str.trim()).doubleValue();
73                 } catch (ParseException pe) {
74                         throw new ParseException("error parsing double " + str, 0);
75                 }
76         }
77
78         public static String format_double_locale(double number) {
79                 return nf_locale.format(number);
80         }
81
82         public static double parse_double_net(String str) throws ParseException {
83                 try {
84                         String t = str.trim();
85 //                      System.out.printf("Parse string \"%s\" trim \"%s\"\n", str, t);
86                         return nf_net.parse(t).doubleValue();
87                 } catch (ParseException pe) {
88                         throw new ParseException("error parsing double " + str, 0);
89                 }
90         }
91
92         public static String format_double_net(double number) {
93                 String ret = nf_net.format(number);
94 //              System.out.printf("format double %f \"%s\"\n", number, ret);
95                 return ret;
96         }
97
98         public static double parse_coord(String coord) throws ParseException {
99                 String[]        dsf = coord.split("\\D+");
100
101                 if (dsf.length != 3) {
102                         throw new ParseException("error parsing coord " + coord, 0);
103                 }
104                 int deg = parse_int(dsf[0]);
105                 int min = parse_int(dsf[1]);
106                 int frac = parse_int(dsf[2]);
107
108                 double r = deg + (min + frac / 10000.0) / 60.0;
109                 if (coord.endsWith("S") || coord.endsWith("W"))
110                         r = -r;
111                 return r;
112         }
113
114         public static String strip_suffix(String v, String suffix) {
115                 if (v.endsWith(suffix))
116                         return v.substring(0, v.length() - suffix.length());
117                 return v;
118         }
119
120         public static void word(String v, String m) throws ParseException {
121                 if (!v.equals(m)) {
122                         throw new ParseException("error matching '" + v + "' '" + m + "'", 0);
123                 }
124         }
125 }