-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo.cpp
70 lines (65 loc) · 1.59 KB
/
todo.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
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
int main(int argc, char* const argv[])
{
std::string todoFilePath = std::string(getenv("HOME")) + "/.todo.db";
std::vector<std::string> todos;
std::fstream todoFileStream(todoFilePath, std::ios::in);
if (todoFileStream)
{
std::string todo;
while (std::getline(todoFileStream, todo))
{
todos.push_back(todo);
}
todoFileStream.close();
}
if (argc > 2 && argv[1] == std::string("add"))
{
std::string todo(argv[2]);
for (int i = 3; i < argc; i++)
{
todo += " " + std::string(argv[i]);
}
todos.push_back(todo);
}
else if (argc == 4 && argv[1] == std::string("swap"))
{
std::istringstream swapArg(argv[2]);
size_t from;
if (swapArg >> from && swapArg.eof() && from > 0 && from < todos.size() + 1)
{
swapArg.str(argv[3]);
swapArg.clear();
size_t to;
if (swapArg >> to && swapArg.eof() && to > 0 && to < todos.size() + 1)
{
std::swap(todos[from - 1], todos[to - 1]);
}
}
}
else if (argc == 3 && argv[1] == std::string("done"))
{
std::istringstream idArg(argv[2]);
size_t id;
if (idArg >> id && idArg.eof() && id > 0 && id < todos.size() + 1)
{
todos.erase(todos.begin() + id - 1);
}
}
todoFileStream.open(todoFilePath, std::ios::out | std::ios::trunc);
if (!todoFileStream)
{
std::cerr << "Couldn't open " + todoFilePath << std::endl;
return EXIT_FAILURE;
}
for (size_t i = 0; i < todos.size(); i++)
{
todoFileStream << todos.at(i) << std::endl;
std::cout << i + 1 << ": " << todos.at(i) << std::endl;
}
todoFileStream.close();
return EXIT_SUCCESS;
}