-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.cpp
More file actions
54 lines (46 loc) · 1.88 KB
/
task.cpp
File metadata and controls
54 lines (46 loc) · 1.88 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
#include "Task.h"
#include "Utils.h"
#include <sstream>
#include <stdexcept>
Task::Task(int id, std::string title, std::string description,
int priority, std::string deadline, bool done)
: m_id(id),
m_title(std::move(title)),
m_description(std::move(description)),
m_priority(priority),
m_deadline(std::move(deadline)),
m_done(done) {}
int Task::id() const { return m_id; }
const std::string& Task::title() const { return m_title; }
const std::string& Task::description() const { return m_description; }
int Task::priority() const { return m_priority; }
const std::string& Task::deadline() const { return m_deadline; }
bool Task::done() const { return m_done; }
void Task::setTitle(const std::string& t) { m_title = t; }
void Task::setDescription(const std::string& d) { m_description = d; }
void Task::setPriority(int p) { m_priority = p; }
void Task::setDeadline(const std::string& d) { m_deadline = d; }
void Task::setDone(bool val) { m_done = val; }
std::string Task::toCSV() const {
std::ostringstream oss;
oss << m_id << ','
<< Utils::escapeCSV(m_title) << ','
<< Utils::escapeCSV(m_description) << ','
<< m_priority << ','
<< Utils::escapeCSV(m_deadline) << ','
<< (m_done ? "1" : "0");
return oss.str();
}
Task Task::fromCSV(const std::string& line) {
auto parts = Utils::splitCSV(line);
if (parts.size() != 6) {
throw std::runtime_error("Invalid CSV line: expected 6 fields, got " + std::to_string(parts.size()));
}
int id = Utils::stoiSafe(parts[0], "id");
std::string title = Utils::unescapeCSV(parts[1]);
std::string description = Utils::unescapeCSV(parts[2]);
int priority = Utils::stoiSafe(parts[3], "priority");
std::string deadline = Utils::unescapeCSV(parts[4]);
bool done = (parts[5] == "1");
return Task{id, title, description, priority, deadline, done};
}