doc: Add 1.9 release notes
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_13;
20
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosParse {
25         public static boolean isdigit(char c) {
26                 return '0' <= c && c <= '9';
27         }
28
29         public static int parse_int(String v) throws ParseException {
30                 try {
31                         return (int) AltosLib.fromdec(v);
32                 } catch (NumberFormatException e) {
33                         throw new ParseException("error parsing int " + v, 0);
34                 }
35         }
36
37         public static long parse_long(String v) throws ParseException {
38                 try {
39                         return AltosLib.fromdec(v);
40                 } catch (NumberFormatException e) {
41                         throw new ParseException("error parsing int " + v, 0);
42                 }
43         }
44
45         public static int parse_hex(String v) throws ParseException {
46                 try {
47                         return AltosLib.fromhex(v);
48                 } catch (NumberFormatException e) {
49                         throw new ParseException("error parsing hex " + v, 0);
50                 }
51         }
52
53         static NumberFormat get_nf_locale() {
54                 NumberFormat nf = NumberFormat.getInstance();
55                 nf.setParseIntegerOnly(false);
56                 nf.setGroupingUsed(false);
57                 return nf;
58         }
59
60         static NumberFormat nf_locale = get_nf_locale();
61
62         static NumberFormat get_nf_net() {
63                 NumberFormat nf = NumberFormat.getInstance(Locale.ROOT);
64                 nf.setParseIntegerOnly(false);
65                 nf.setGroupingUsed(false);
66                 return nf;
67         }
68
69         static NumberFormat nf_net = get_nf_net();
70
71         public static double parse_double_locale(String str) throws ParseException {
72                 try {
73                         return nf_locale.parse(str.trim()).doubleValue();
74                 } catch (ParseException pe) {
75                         throw new ParseException("error parsing double " + str, 0);
76                 }
77         }
78
79         public static String format_double_locale(double number) {
80                 return nf_locale.format(number);
81         }
82
83         public static double parse_double_net(String str) throws ParseException {
84                 try {
85                         String t = str.trim();
86 //                      System.out.printf("Parse string \"%s\" trim \"%s\"\n", str, t);
87                         return nf_net.parse(t).doubleValue();
88                 } catch (ParseException pe) {
89                         throw new ParseException("error parsing double " + str, 0);
90                 }
91         }
92
93         public static String format_double_net(double number) {
94                 String ret = nf_net.format(number);
95 //              System.out.printf("format double %f \"%s\"\n", number, ret);
96                 return ret;
97         }
98
99         public static double parse_coord(String coord) throws ParseException {
100                 String[]        dsf = coord.split("\\D+");
101
102                 if (dsf.length != 3) {
103                         throw new ParseException("error parsing coord " + coord, 0);
104                 }
105                 int deg = parse_int(dsf[0]);
106                 int min = parse_int(dsf[1]);
107                 int frac = parse_int(dsf[2]);
108
109                 double r = deg + (min + frac / 10000.0) / 60.0;
110                 if (coord.endsWith("S") || coord.endsWith("W"))
111                         r = -r;
112                 return r;
113         }
114
115         public static String strip_suffix(String v, String suffix) {
116                 if (v.endsWith(suffix))
117                         return v.substring(0, v.length() - suffix.length());
118                 return v;
119         }
120
121         public static void word(String v, String m) throws ParseException {
122                 if (!v.equals(m)) {
123                         throw new ParseException("error matching '" + v + "' '" + m + "'", 0);
124                 }
125         }
126 }