-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.cpp
More file actions
122 lines (108 loc) · 3.63 KB
/
Copy pathactions.cpp
File metadata and controls
122 lines (108 loc) · 3.63 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
119
120
121
122
#include "actions.h"
#include "utils.h"
void printList(std::vector<std::string> *list, std::unordered_set<int> * doneList) {
printf("\n\n");
printf("========= TODO LIST ========= \n");
int index = 1;
if (list->size() <= 0) {
printf(" List is empty! \n");
} else {
for (const std::string c : *list) {
printf("%d - %s", index, c.c_str());
if (doneList->count(index) > 0) {
printf(" - DONE");
}
printf("\n");
++index;
}
}
printf("============================ \n\n");
}
void addItemToList(std::vector<std::string>* list) {
std::string input;
printf("Please insert what you want inside the list: ");
std::getline(std::cin, input);
list->push_back(input);
}
void deleteListElement(std::vector<std::string>* list, std::unordered_set<int> * doneList) {
if (checkListIsEmpty(list)) return;
int index = checkActionIndex(list, "remove from the list", true);
if (index == -1) return;
list->erase(list->begin() + (index - 1));
if(doneList->count(index) > 0) {
doneList->erase(doneList->find(index));
}
return;
}
void changeListElement(std::vector<std::string>* list) {
if (checkListIsEmpty(list)) return;
int index = checkActionIndex(list, "change", true);
if (index == -1) return;
std::string edit = list->at(index - 1);
printf("Edit entry %s: \n", edit.c_str());
std::getline(std::cin, edit);
list->erase(list->begin() + index - 1);
list->insert(list->begin() + index - 1, edit);
}
void markElementAsDone(std::unordered_set<int> * doneList, const std::vector<std::string>* list) {
if (checkListIsEmpty(list)) return;
int index = -2;
while(!checkIntInterval(1, list->size(), index) || doneList->count(index) > 0) {
index = checkActionIndex(list, "mark as done", true);
printf("The element is already marked as done.\n");
if (index == -1) return;
}
doneList->insert(index);
}
void loadList(std::vector<std::string> * list, std::unordered_set<int> * done) {
std::string option = "";
std::ifstream file;
std::string line;
bool gotFirstLine = false;
printf("Continue with this operation will delete the current TODO list, are you ok with that? [Y]es/[N]o: ");
std::cin >> option;
if (option == "Yes" || option == "yes" || option == "Y" || option == "y") {
list->clear();
done->clear();
file.open("list.txt");
if(file.is_open()) {
while(!file.eof()) {
getline(file, line);
if(!gotFirstLine) {
gotFirstLine = true;
// convert each number in the first line for the done set
for(const char val: line) {
if(val != ',') {
done->insert((int)val - '0');
}
}
continue;
}
if (!line.empty()){
list->push_back(line);
}
}
file.close();
}
return;
}
if (option == "NO" || option == "no" || option == "N" || option == "n"){
printf("Load aborted.\n");
return;
}
printf("Instruction not clear. Load aborted.\n");
return;
}
void saveList(std::vector<std::string> * list, std::unordered_set<int> * done) {
std::ofstream file;
file.open("list.txt");
for(const int d : *done) {
file << d << ',';
}
file << "\n";
for(const std::string item : *list) {
file << item << "\n";
}
file.close();
return;
}