-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.cpp
More file actions
118 lines (101 loc) · 3.05 KB
/
task_manager.cpp
File metadata and controls
118 lines (101 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "TaskManager.h"
#include "Utils.h"
#include <fstream>
#include <sstream>
#include <filesystem>
#include <iostream>
TaskManager::TaskManager(const std::string& storagePath)
: m_storagePath(storagePath), m_nextId(1) {
load();
}
TaskManager::~TaskManager() {
save();
}
void TaskManager::ensureDataDir() const {
std::filesystem::path p(m_storagePath);
auto dir = p.parent_path();
if (dir.empty()) return;
std::error_code ec;
if (!std::filesystem::create_directories(dir, ec) && ec) {
std::cerr << "Error creating directories for storage: " << ec.message() << "\n";
}
}
void TaskManager::load() {
m_tasks.clear();
std::ifstream in(m_storagePath);
if (!in.is_open()) {
std::cerr << "Warning: could not open storage file: " << m_storagePath << "\n";
return;
}
std::string line;
while (std::getline(in, line)) {
if (line.empty()) continue;
try {
Task t = Task::fromCSV(line);
m_tasks.push_back(t);
if (t.id() >= m_nextId) m_nextId = t.id() + 1;
} catch (const std::exception& e) {
std::cerr << "Failed to parse task line: \"" << line << "\" (" << e.what() << ")\n";
}
}
}
void TaskManager::save() const {
ensureDataDir();
std::ofstream out(m_storagePath, std::ios::trunc);
if (!out.is_open()) {
std::cerr << "Warning: could not open storage file for saving: " << m_storagePath << "\n";
return;
}
for (const auto& t : m_tasks) {
out << t.toCSV() << '\n';
}
}
Task& TaskManager::addTask(const std::string& title,
const std::string& description,
int priority,
const std::string& deadline) {
m_tasks.emplace_back(m_nextId++, title, description, priority, deadline, false);
save();
return m_tasks.back();
}
std::vector<Task> TaskManager::listAll() const {
return m_tasks;
}
bool TaskManager::markDone(int id, bool done) {
for (auto& t : m_tasks) {
if (t.id() == id) {
t.setDone(done);
save();
return true;
}
}
return false;
}
bool TaskManager::removeTask(int id) {
auto it = std::find_if(m_tasks.begin(), m_tasks.end(),
[id](const Task& t) { return t.id() == id; });
if (it != m_tasks.end()) {
m_tasks.erase(it);
save();
return true;
}
return false;
}
Task* TaskManager::findTask(int id) {
for (auto& t : m_tasks) {
if (t.id() == id) return &t;
}
return nullptr;
}
bool TaskManager::editTask(int id, const std::string& title,
const std::string& description,
int priority, const std::string& deadline) {
Task* t = findTask(id);
if (!t) return false;
if (!title.empty()) t->setTitle(title);
if (!description.empty()) t->setDescription(description);
if (priority >= 0) t->setPriority(priority);
if (!deadline.empty()) t->setDeadline(deadline);
save();
return true;
}