This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo.cpp
140 lines (131 loc) · 3.08 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <ctime>
#include <sstream>
using namespace std;
//Function to print date in yyyy-mm-dd format
string printDate()
{
time_t rawtime;
tm *timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = std::localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d", timeinfo);
return buffer;
}
void readWriteTodo()
{
//Opening Input file = todo.txt & Output file = done.txt
ifstream inputTodo("todo.txt");
ofstream outputTodo("done.txt");
//String to store the data of the input file
string line;
string formatedDate;
//writing output to the done.txt file
if (inputTodo.is_open())
{
while (getline(inputTodo, line))
{
// outputTodo;
formatedDate = printDate();
outputTodo << "x " << formatedDate << " " << line << endl;
}
inputTodo.close();
outputTodo.close();
}
else
{
cout << "Error! Unable to open file :(" << endl;
}
}
void showList()
{
//Opening Input file = todo.txt & Output file = done.txt
ifstream inputTodo("todo.txt");
//String to store the data of the input file
string line;
int x = 1;
//writing output to the done.txt file
if (inputTodo.is_open())
{
while (getline(inputTodo, line))
{
cout << "[" << x << "]"
<< " " << line << endl;
x++;
}
inputTodo.close();
}
else
{
cout << "Error! Unable to open file :(" << endl;
}
}
void deleteTodoItem(int n = 1)
{
//code
}
//Adding item in the todo list
void addItemInTodoList(string todoString)
{
ofstream inputTodo("todo.txt", ios::app);
string line;
string formatedDate;
formatedDate = printDate();
inputTodo
<< todoString << endl;
inputTodo.close();
}
//Driver Code
int main(int argc, char *argv[])
{
readWriteTodo();
string argument, number, todoString;
for (int i = 1; i < argc; i++)
{
argument = argv[i];
break;
}
if (argument == "help")
{
cout << "\nUsage :-" << endl;
cout << "$ ./todo add \"todo item\"\t# Add a new todo\n";
cout << "$ ./todo ls\t\t\t# Show remaining todos\n";
cout << "$ ./todo del NUMBER\t\t# Delete a todo\n";
cout << "$ ./todo done NUMBER\t\t# Complete a todo\n";
cout << "$ ./todo help\t\t\t# Show usage\n";
cout << "$ ./todo report\t\t\t# Statistics\n\n";
return 0;
}
else if (argument == "ls")
{
showList();
}
else if (argument == "del")
{
number = argv[2];
//Converting String argument into Integer
stringstream parsingNumber(number);
int x = 0;
parsingNumber >> x;
deleteTodoItem(x);
}
else if (argument == "done")
{
/* code */
}
else if (argument == "report")
{
/* code */
}
else if (argument == "add")
{
todoString = argv[2];
addItemInTodoList(todoString);
}
return 0;
}
//Code By: Avinash Kumar