SectorZone: add attribute arc_boundary
[xcsoar.git] / src / Task / TaskStore.cpp
blobae83f4070e5179e1dcf96f86b8d5b2b927ec797e
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 "Task/TaskStore.hpp"
25 #include "Task/TaskFile.hpp"
26 #include "Task/ProtectedTaskManager.hpp"
27 #include "Engine/Task/Ordered/OrderedTask.hpp"
28 #include "Components.hpp"
29 #include "OS/FileUtil.hpp"
30 #include "LocalPath.hpp"
31 #include "Language/Language.hpp"
33 #include <cstdio>
34 #include <algorithm>
35 #include <memory>
37 class TaskFileVisitor: public File::Visitor
39 private:
40 TaskStore::ItemVector &store;
42 public:
43 TaskFileVisitor(TaskStore::ItemVector &_store):
44 store(_store) {}
46 void Visit(const TCHAR *path, const TCHAR *base_name) {
47 // Create a TaskFile instance to determine how many
48 // tasks are inside of this task file
49 std::unique_ptr<TaskFile> task_file(TaskFile::Create(path));
50 if (!task_file)
51 return;
53 // Count the tasks in the task file
54 unsigned count = task_file->Count();
55 // For each task in the task file
56 for (unsigned i = 0; i < count; i++) {
57 // Copy base name of the file into task name
58 StaticString<256> name(base_name);
60 // If the task file holds more than one task
61 const TCHAR *saved_name = task_file->GetName(i);
62 if (saved_name != NULL) {
63 name += _T(": ");
64 name += saved_name;
65 } else if (count > 1) {
66 // .. append " - Task #[n]" suffix to the task name
67 name.AppendFormat(_T(": %s #%d"), _("Task"), i + 1);
70 // Add the task to the TaskStore
71 store.emplace_back(path, name.empty() ? path : name, i);
76 void
77 TaskStore::Clear()
79 // clear entries first
80 store.erase(store.begin(), store.end());
83 void
84 TaskStore::Scan(bool extra)
86 Clear();
88 // scan files
89 TaskFileVisitor tfv(store);
90 VisitDataFiles(_T("*.tsk"), tfv);
92 if (extra) {
93 VisitDataFiles(_T("*.cup"), tfv);
94 VisitDataFiles(_T("*.igc"), tfv);
97 std::sort(store.begin(), store.end());
100 TaskStore::Item::~Item()
102 if (!filename.empty())
103 delete task;
106 OrderedTask*
107 TaskStore::Item::GetTask(const TaskBehaviour &task_behaviour)
109 if (task != NULL)
110 return task;
112 if (valid)
113 task = TaskFile::GetTask(filename.c_str(), task_behaviour,
114 &way_points, task_index);
116 if (task == NULL)
117 valid = false;
119 return task;
122 const TCHAR *
123 TaskStore::GetName(unsigned index) const
125 return store[index].GetName();
128 const TCHAR *
129 TaskStore::GetPath(unsigned index) const
131 return store[index].GetPath();
134 OrderedTask*
135 TaskStore::GetTask(unsigned index, const TaskBehaviour &task_behaviour)
137 return store[index].GetTask(task_behaviour);