OpenCPN Partial API docs
Loading...
Searching...
No Matches
position_parser.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2010 by David S. Register *
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, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <wx/tokenzr.h>
25#include <wx/string.h>
26#include <wx/regex.h>
27
29#include "model/navutil_base.h"
30
31PositionParser::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
40bool PositionParser::FindSeparator(const wxString& src) {
41 // Used when format is similar to "12 34.56 N 12 34.56 E"
42 wxString posPartOfSeparator = "";
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 "<[a-z,A-Z]*\\s*[a-z,A-Z]*=\"([0-9,.]*)\"\\s*[a-z,A-Z]*=\"([-,0-9,.]*)"
57 "\"\\s*/*>",
58 re_compile_flags);
59
60 if (regex.IsValid()) {
61 if (regex.Matches(src)) {
62 int n = regex.GetMatchCount();
63 latitudeString = regex.GetMatch(src, 1);
64 longitudeString = regex.GetMatch(src, 2);
65 latitudeString.Trim(true);
66 latitudeString.Trim(false);
67 longitudeString.Trim(true);
68 longitudeString.Trim(false);
69 return true;
70 }
71 }
72
73 // Now try various separators.
74
75 separator = ", ";
76 wxStringTokenizer tk1(src, separator);
77 if (tk1.CountTokens() == 2) {
78 latitudeString = tk1.GetNextToken();
79 latitudeString.Trim(true);
80 latitudeString.Trim(false);
81 longitudeString = tk1.GetNextToken();
82 longitudeString.Trim(true);
83 longitudeString.Trim(false);
84
85 return true;
86 }
87
88 separator = ",";
89 wxStringTokenizer tk2(src, separator);
90 if (tk2.CountTokens() == 2) {
91 latitudeString = tk2.GetNextToken();
92 latitudeString.Trim(true);
93 latitudeString.Trim(false);
94 longitudeString = tk2.GetNextToken();
95 longitudeString.Trim(true);
96 longitudeString.Trim(false);
97
98 return true;
99 }
100
101 separator = " ";
102 wxStringTokenizer tk3(src, separator);
103 if (tk3.CountTokens() == 2) {
104 latitudeString = tk3.GetNextToken();
105 latitudeString.Trim(true);
106 latitudeString.Trim(false);
107 longitudeString = tk3.GetNextToken();
108 longitudeString.Trim(true);
109 longitudeString.Trim(false);
110
111 return true;
112 }
113
114 separator = "\t";
115 wxStringTokenizer tk4(src, separator);
116 if (tk4.CountTokens() == 2) {
117 latitudeString = tk4.GetNextToken();
118 latitudeString.Trim(true);
119 latitudeString.Trim(false);
120 longitudeString = tk4.GetNextToken();
121 longitudeString.Trim(true);
122 longitudeString.Trim(false);
123
124 return true;
125 }
126
127 separator = "\n";
128 wxStringTokenizer tk5(src, separator);
129 if (tk5.CountTokens() == 2) {
130 latitudeString = tk5.GetNextToken();
131 latitudeString.Trim(true);
132 latitudeString.Trim(false);
133 longitudeString = tk5.GetNextToken();
134 longitudeString.Trim(true);
135 longitudeString.Trim(false);
136
137 return true;
138 }
139
140 separator = "N";
141 posPartOfSeparator = "N";
142 wxStringTokenizer tk6(src, separator);
143 if (tk6.CountTokens() == 2) {
144 latitudeString = tk6.GetNextToken() << posPartOfSeparator;
145 latitudeString.Trim(true);
146 latitudeString.Trim(false);
147 longitudeString = tk6.GetNextToken();
148 longitudeString.Trim(true);
149 longitudeString.Trim(false);
150
151 return true;
152 }
153
154 separator = "S";
155 posPartOfSeparator = "S";
156 wxStringTokenizer tk7(src, separator);
157 if (tk7.CountTokens() == 2) {
158 latitudeString = tk7.GetNextToken() << posPartOfSeparator;
159 latitudeString.Trim(true);
160 latitudeString.Trim(false);
161 longitudeString = tk7.GetNextToken();
162 longitudeString.Trim(true);
163 longitudeString.Trim(false);
164
165 return true;
166 }
167
168 // Give up.
169 return false;
170}
Navigation Utility Functions without GUI dependencies.
String positions parsing.