Revert "Device/Driver/LX: Add small delay after baud rate change"
[xcsoar.git] / test / src / NearestWaypoints.cpp
blob577df314422ed22b3b109d290f00cc4d8e1aeabc
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Waypoint/WaypointReader.hpp"
25 #include "Waypoint/Waypoints.hpp"
26 #include "Engine/Waypoint/WaypointVisitor.hpp"
27 #include "OS/ConvertPathName.hpp"
28 #include "OS/Args.hpp"
29 #include "Operation/Operation.hpp"
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <tchar.h>
35 static bool
36 LoadWaypoints(const char *_path, Waypoints &waypoints)
38 PathName path(_path);
39 WaypointReader parser(path, 0);
40 if (parser.Error()) {
41 fprintf(stderr, "WayPointParser::SetFile() has failed\n");
42 return false;
45 NullOperationEnvironment operation;
46 if (!parser.Parse(waypoints, operation)) {
47 fprintf(stderr, "WayPointParser::Parse() has failed\n");
48 return false;
51 waypoints.Optimise();
52 return true;
55 static bool
56 ParseGeopoint(const char *line, GeoPoint &location)
58 double value;
59 char *endptr;
61 value = strtod(line, &endptr);
62 if (line == endptr)
63 return false;
65 location.latitude = Angle::Degrees(value);
66 line = endptr;
68 value = strtod(line, &endptr);
69 if (line == endptr)
70 return false;
72 location.longitude = Angle::Degrees(value);
74 return true;
77 enum class WaypointType: uint8_t {
78 ALL,
79 LANDABLE,
80 AIRPORT,
83 static bool
84 AlwaysTrue(const Waypoint &waypoint)
86 return true;
89 static bool
90 IsLandable(const Waypoint &waypoint)
92 return waypoint.IsLandable();
95 static bool
96 IsAirport(const Waypoint &waypoint)
98 return waypoint.IsAirport();
101 static const Waypoint *
102 GetNearestWaypoint(const GeoPoint &location, const Waypoints &waypoints,
103 fixed range, WaypointType type)
105 bool (*predicate)(const Waypoint &);
106 switch (type) {
107 case WaypointType::AIRPORT:
108 predicate = IsAirport;
109 break;
110 case WaypointType::LANDABLE:
111 predicate = IsLandable;
112 break;
113 default:
114 predicate = AlwaysTrue;
115 break;
118 return waypoints.GetNearestIf(location, range, predicate);
121 static void
122 PrintWaypoint(const Waypoint *waypoint)
124 if (!waypoint)
125 printf("\n");
126 else
127 _ftprintf(stdout, _T("%f %f %.0f %s\n"),
128 (double)waypoint->location.latitude.Degrees(),
129 (double)waypoint->location.longitude.Degrees(),
130 (double)waypoint->elevation,
131 waypoint->name.c_str());
134 int main(int argc, char **argv)
136 WaypointType type = WaypointType::ALL;
137 fixed range = fixed(100000);
139 Args args(argc, argv,
140 "PATH\n\nPATH is expected to be any compatible waypoint file.\n"
141 "Stdin expects a list of coordinates at floating point values\n"
142 "in the format: LAT LON\n\ne.g.\n"
143 "-23.49858 123.45838\n"
144 "2.12343 34.38432\n"
145 "65.18234 -173.48307\n\n"
146 "Output is in the format: LAT LON ELEV (in m) NAME\n\ne.g.\n"
147 "50.823055 6.186384 189 Aachen Merzbruc");
149 const char *arg;
150 while ((arg = args.PeekNext()) != NULL && *arg == '-') {
151 args.Skip();
153 const char *value;
154 if ((value = StringAfterPrefix(arg, "--range=")) != NULL) {
155 double _range = strtod(value, NULL);
156 if (_range > 0)
157 range = fixed(_range);
158 } else if (StringStartsWith(arg, "--airports-only")) {
159 type = WaypointType::AIRPORT;
160 } else if (StringStartsWith(arg, "--landables-only")) {
161 type = WaypointType::LANDABLE;
162 } else {
163 args.UsageError();
167 const char *path = args.ExpectNext();
168 args.ExpectEnd();
170 Waypoints waypoints;
171 if (!LoadWaypoints(path, waypoints))
172 return EXIT_FAILURE;
174 char buffer[1024];
175 const char *line;
176 while ((line = fgets(buffer, sizeof(buffer) - 3, stdin)) != NULL) {
177 GeoPoint location;
178 if (!ParseGeopoint(line, location))
179 continue;
181 const Waypoint *waypoint = GetNearestWaypoint(location, waypoints,
182 range, type);
183 PrintWaypoint(waypoint);
186 return EXIT_SUCCESS;