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
);
47 WaypointGlue::GetPath(int file_number
, TCHAR
*value
)
51 switch (file_number
) {
53 key
= ProfileKeys::WaypointFile
;
56 key
= ProfileKeys::AdditionalWaypointFile
;
59 key
= ProfileKeys::WatchedWaypointFile
;
65 return Profile::GetPath(key
, value
);
69 WaypointGlue::IsWritable(int file_number
)
72 if (!GetPath(file_number
, file
))
75 return (MatchesExtension(file
, _T(".dat")) ||
76 MatchesExtension(file
, _T(".cup")) ||
77 MatchesExtension(file
, _T(".xcw")));
81 WaypointGlue::IsWritable()
83 return IsWritable(1) || IsWritable(2) || IsWritable(3);
87 LoadWaypointFile(Waypoints
&waypoints
, const TCHAR
*path
, int file_num
,
88 const RasterTerrain
*terrain
, OperationEnvironment
&operation
)
90 WaypointReader
reader(path
, file_num
);
92 LogFormat(_T("Failed to open waypoint file: %s"), path
);
97 reader
.SetTerrain(terrain
);
98 if (!reader
.Parse(waypoints
, operation
)) {
99 LogFormat(_T("Failed to parse waypoint file: %s"), path
);
107 WaypointGlue::LoadWaypoints(Waypoints
&way_points
,
108 const RasterTerrain
*terrain
,
109 OperationEnvironment
&operation
)
111 LogFormat("ReadWaypoints");
112 operation
.SetText(_("Loading Waypoints..."));
116 // Delete old waypoints
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
154 WaypointGlue::SaveWaypointFile(const Waypoints
&way_points
, int num
)
156 if (!IsWritable(num
)) {
157 LogFormat("Waypoint file %d can not be written", num
);
164 TextWriter
writer(file
);
165 if (!writer
.IsOpen()) {
166 LogFormat("Waypoint file %d can not be written", num
);
170 WaypointWriter
wp_writer(way_points
, num
);
171 wp_writer
.Save(writer
, DetermineWaypointFileType(file
));
173 LogFormat("Waypoint file %d saved", num
);
178 WaypointGlue::SaveWaypoints(const Waypoints
&way_points
)
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);