OpenCPN Partial API docs
position_parser.cpp
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  *
5  ***************************************************************************
6  * Copyright (C) 2010 by David S. Register *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  **************************************************************************/
23 
24 #include <wx/tokenzr.h>
25 #include <wx/string.h>
26 #include <wx/regex.h>
27 
28 #include "model/position_parser.h"
29 #include "model/navutil_base.h"
30 
31 PositionParser::PositionParser(const wxString& src) {
32  parsedOk = false;
33  if (FindSeparator(src)) {
34  latitude = fromDMM(latitudeString);
35  longitude = fromDMM(longitudeString);
36  if ((latitude != 0.0) && (longitude != 0.0)) parsedOk = true;
37  }
38 }
39 
40 bool PositionParser::FindSeparator(const wxString& src) {
41  // Used when format is similar to "12 34.56 N 12 34.56 E"
42  wxString posPartOfSeparator = _T("");
43 
44  // First the XML case:
45  // Generalized XML tag format, accepts anything like <XXX yyy="<lat>"
46  // zzz="<lon>" > GPX format <wpt lat="<lat>" lon="<lon>" /> tag among others.
47 
48  wxRegEx regex;
49 
50  int re_compile_flags = wxRE_ICASE;
51 #ifdef wxHAS_REGEX_ADVANCED
52  re_compile_flags |= wxRE_ADVANCED;
53 #endif
54 
55  regex.Compile(
56  _T( "<[a-z,A-Z]*\\s*[a-z,A-Z]*=\"([0-9,.]*)\"\\s*[a-z,A-Z]*=\"([-,0-9,.]*)\"\\s*/*>" ),
57  re_compile_flags);
58 
59  if (regex.IsValid()) {
60  if (regex.Matches(src)) {
61  int n = regex.GetMatchCount();
62  latitudeString = regex.GetMatch(src, 1);
63  longitudeString = regex.GetMatch(src, 2);
64  latitudeString.Trim(true);
65  latitudeString.Trim(false);
66  longitudeString.Trim(true);
67  longitudeString.Trim(false);
68  return true;
69  }
70  }
71 
72  // Now try various separators.
73 
74  separator = _T(", ");
75  wxStringTokenizer tk1(src, separator);
76  if (tk1.CountTokens() == 2) {
77  latitudeString = tk1.GetNextToken();
78  latitudeString.Trim(true);
79  latitudeString.Trim(false);
80  longitudeString = tk1.GetNextToken();
81  longitudeString.Trim(true);
82  longitudeString.Trim(false);
83 
84  return true;
85  }
86 
87  separator = _T(",");
88  wxStringTokenizer tk2(src, separator);
89  if (tk2.CountTokens() == 2) {
90  latitudeString = tk2.GetNextToken();
91  latitudeString.Trim(true);
92  latitudeString.Trim(false);
93  longitudeString = tk2.GetNextToken();
94  longitudeString.Trim(true);
95  longitudeString.Trim(false);
96 
97  return true;
98  }
99 
100  separator = _T(" ");
101  wxStringTokenizer tk3(src, separator);
102  if (tk3.CountTokens() == 2) {
103  latitudeString = tk3.GetNextToken();
104  latitudeString.Trim(true);
105  latitudeString.Trim(false);
106  longitudeString = tk3.GetNextToken();
107  longitudeString.Trim(true);
108  longitudeString.Trim(false);
109 
110  return true;
111  }
112 
113  separator = _T("\t");
114  wxStringTokenizer tk4(src, separator);
115  if (tk4.CountTokens() == 2) {
116  latitudeString = tk4.GetNextToken();
117  latitudeString.Trim(true);
118  latitudeString.Trim(false);
119  longitudeString = tk4.GetNextToken();
120  longitudeString.Trim(true);
121  longitudeString.Trim(false);
122 
123  return true;
124  }
125 
126  separator = _T("\n");
127  wxStringTokenizer tk5(src, separator);
128  if (tk5.CountTokens() == 2) {
129  latitudeString = tk5.GetNextToken();
130  latitudeString.Trim(true);
131  latitudeString.Trim(false);
132  longitudeString = tk5.GetNextToken();
133  longitudeString.Trim(true);
134  longitudeString.Trim(false);
135 
136  return true;
137  }
138 
139  separator = _T("N");
140  posPartOfSeparator = _T("N");
141  wxStringTokenizer tk6(src, separator);
142  if (tk6.CountTokens() == 2) {
143  latitudeString = tk6.GetNextToken() << posPartOfSeparator;
144  latitudeString.Trim(true);
145  latitudeString.Trim(false);
146  longitudeString = tk6.GetNextToken();
147  longitudeString.Trim(true);
148  longitudeString.Trim(false);
149 
150  return true;
151  }
152 
153  separator = _T("S");
154  posPartOfSeparator = _T("S");
155  wxStringTokenizer tk7(src, separator);
156  if (tk7.CountTokens() == 2) {
157  latitudeString = tk7.GetNextToken() << posPartOfSeparator;
158  latitudeString.Trim(true);
159  latitudeString.Trim(false);
160  longitudeString = tk7.GetNextToken();
161  longitudeString.Trim(true);
162  longitudeString.Trim(false);
163 
164  return true;
165  }
166 
167  // Give up.
168  return false;
169 }