Android release v6.7_preview1
[xcsoar.git] / src / Waypoint / WaypointGlue.cpp
blob4edd7258d2868b7324214b4f3262cd5fb45dbd49
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 "WaypointGlue.hpp"
25 #include "Profile/Profile.hpp"
26 #include "Util/StringUtil.hpp"
27 #include "LogFile.hpp"
28 #include "Waypoint/Waypoints.hpp"
29 #include "WaypointReader.hpp"
30 #include "Language/Language.hpp"
31 #include "NMEA/Aircraft.hpp"
32 #include "Airspace/ProtectedAirspaceWarningManager.hpp"
33 #include "IO/TextWriter.hpp"
34 #include "OS/PathName.hpp"
35 #include "Waypoint/WaypointWriter.hpp"
36 #include "Operation/Operation.hpp"
37 #include "WaypointFileType.hpp"
39 #include <windef.h> /* for MAX_PATH */
41 namespace WaypointGlue {
42 bool GetPath(int file_number, TCHAR *value);
43 bool IsWritable(int file_number);
46 bool
47 WaypointGlue::GetPath(int file_number, TCHAR *value)
49 const char *key;
51 switch (file_number) {
52 case 1:
53 key = ProfileKeys::WaypointFile;
54 break;
55 case 2:
56 key = ProfileKeys::AdditionalWaypointFile;
57 break;
58 case 3:
59 key = ProfileKeys::WatchedWaypointFile;
60 break;
61 default:
62 return false;
65 return Profile::GetPath(key, value);
68 bool
69 WaypointGlue::IsWritable(int file_number)
71 TCHAR file[MAX_PATH];
72 if (!GetPath(file_number, file))
73 return false;
75 return (MatchesExtension(file, _T(".dat")) ||
76 MatchesExtension(file, _T(".cup")) ||
77 MatchesExtension(file, _T(".xcw")));
80 bool
81 WaypointGlue::IsWritable()
83 return IsWritable(1) || IsWritable(2) || IsWritable(3);
86 static bool
87 LoadWaypointFile(Waypoints &waypoints, const TCHAR *path, int file_num,
88 const RasterTerrain *terrain, OperationEnvironment &operation)
90 WaypointReader reader(path, file_num);
91 if (reader.Error()) {
92 LogFormat(_T("Failed to open waypoint file: %s"), path);
93 return false;
96 // parse the file
97 reader.SetTerrain(terrain);
98 if (!reader.Parse(waypoints, operation)) {
99 LogFormat(_T("Failed to parse waypoint file: %s"), path);
100 return false;
103 return true;
106 bool
107 WaypointGlue::LoadWaypoints(Waypoints &way_points,
108 const RasterTerrain *terrain,
109 OperationEnvironment &operation)
111 LogFormat("ReadWaypoints");
112 operation.SetText(_("Loading Waypoints..."));
114 bool found = false;
116 // Delete old waypoints
117 way_points.Clear();
119 TCHAR path[MAX_PATH];
121 // ### FIRST FILE ###
122 if (Profile::GetPath(ProfileKeys::WaypointFile, path))
123 found |= LoadWaypointFile(way_points, path, 1, terrain, operation);
125 // ### SECOND FILE ###
126 if (Profile::GetPath(ProfileKeys::AdditionalWaypointFile, path))
127 found |= LoadWaypointFile(way_points, path, 2, terrain, operation);
129 // ### WATCHED WAYPOINT/THIRD FILE ###
130 if (Profile::GetPath(ProfileKeys::WatchedWaypointFile, path))
131 found |= LoadWaypointFile(way_points, path, 3, terrain, operation);
133 // ### MAP/FOURTH FILE ###
135 // If no waypoint file found yet
136 if (!found && Profile::GetPath(ProfileKeys::MapFile, path)) {
137 TCHAR *tail = path + _tcslen(path);
139 _tcscpy(tail, _T("/waypoints.xcw"));
140 found |= LoadWaypointFile(way_points, path, 0, terrain, operation);
142 _tcscpy(tail, _T("/waypoints.cup"));
143 found |= LoadWaypointFile(way_points, path, 0, terrain, operation);
146 // Optimise the waypoint list after attaching new waypoints
147 way_points.Optimise();
149 // Return whether waypoints have been loaded into the waypoint list
150 return found;
153 bool
154 WaypointGlue::SaveWaypointFile(const Waypoints &way_points, int num)
156 if (!IsWritable(num)) {
157 LogFormat("Waypoint file %d can not be written", num);
158 return false;
161 TCHAR file[255];
162 GetPath(num, file);
164 TextWriter writer(file);
165 if (!writer.IsOpen()) {
166 LogFormat("Waypoint file %d can not be written", num);
167 return false;
170 WaypointWriter wp_writer(way_points, num);
171 wp_writer.Save(writer, DetermineWaypointFileType(file));
173 LogFormat("Waypoint file %d saved", num);
174 return true;
177 bool
178 WaypointGlue::SaveWaypoints(const Waypoints &way_points)
180 bool result = false;
182 // ### FIRST FILE ###
183 result |= SaveWaypointFile(way_points, 1);
185 // ### SECOND FILE ###
186 result |= SaveWaypointFile(way_points, 2);
188 // ### THIRD FILE ###
189 result |= SaveWaypointFile(way_points, 3);
191 return result;