-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_server.cpp
142 lines (125 loc) · 3.17 KB
/
todo_server.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "todo_server.h"
using namespace std;
class TodoDiskReader {
private:
string filename;
ofstream file_output;
public:
TodoDiskReader()
{
std::string homedir = getenv("HOME");
this->filename = homedir + "/.todo/todo_list.txt";
ifstream todofile(filename.c_str());
if (!todofile.good())
{
cout << "Error opening file!" << endl;
exit(1);
}
}
string read()
{
if (this->filename.size() == 0) {
return "";
}
// attempt to read data from ~/.todo/todos.txt
ifstream todo_file(this->filename.c_str());
stringstream result;
result << todo_file.rdbuf();
return result.str();
}
void start_save()
{
file_output = ofstream(this->filename.c_str());
}
void write(string s)
{
if (this->filename.size() == 0)
{
return;
}
// write data back to ~/.todo/todos.txt
this->file_output << s;
}
void finish()
{
file_output.close();
}
};
TodoServer::TodoServer()
{
debug_print("Initializing Server...");
TodoDiskReader reader;
string line;
stringstream myfile = stringstream(reader.read());
while (getline(myfile, line))
{
debug_print("Read line " + line + " from file");
string tokens[3];
istringstream ss(line);
int token_loc = 0;
while (getline(ss, tokens[token_loc], '|'))
{
token_loc++;
}
string title = tokens[0];
string description = tokens[1];
bool completed;
istringstream(tokens[2]) >> completed;
TodoObject *todo = new TodoObject(title, description, completed);
debug_print("Found todo item " + title);
// add it to the list of todos
add_todo_item(todo);
}
}
void TodoServer::remove_todo_item(std::string title)
{
debug_print("attempting to delete todo " + title);
std::vector<TodoObject *>::iterator todo_iterator = todos.begin();
for (auto todo : todos)
{
if (todo->get_title().compare(title) == 0)
{
debug_print("Found todo " + title);
todos.erase(todo_iterator);
std::cout << "Removed todo " << title << std::endl;
return;
}
todo_iterator++;
}
std::cout << "Couldn't find a todo with the title " << title << std::endl;
}
void TodoServer::add_todo_item(TodoObject *todo)
{
//todo check dups
debug_print("Adding todo item " + todo->get_title());
todos.push_back(todo);
}
void TodoServer::save_todo_list()
{
TodoDiskReader reader;
reader.start_save();
for (TodoObject *todo : todos)
{
std::stringstream stream;
stream << todo;
reader.write(stream.str());
debug_print("wrote " + stream.str());
delete todo;
}
reader.finish();
}
TodoObject *TodoServer::find_todo(std::string title)
{
for (auto todo : todos)
{
if (todo->get_title().compare(title) == 0)
{
return todo;
}
}
return nullptr;
}
const std::vector<TodoObject *>& TodoServer::get_todo_list()
{
return todos;
}