-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f00814
commit 3c7d57d
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <cstdio> | ||
#include <string> | ||
#include "todo.hpp" | ||
#include <cstring> | ||
|
||
#define HELP_MSG "press -a to add the task\npress -d to delete the task\npress -l for list of tasks\npress -h for help\n" | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
// printf("You entered %d arguments\n", argc -1); | ||
|
||
|
||
if (argc < 2) { | ||
printf(HELP_MSG); | ||
// exit(1); | ||
} | ||
else { | ||
for (int i = 1; i < argc; ++i){ | ||
if (strcmp(argv[i],"-h")==0){ | ||
|
||
printf(HELP_MSG); | ||
} | ||
else if (strcmp(argv[i],"-a")==0){ | ||
todo::Todo obj(todo::FileType::APPEND); | ||
|
||
|
||
printf("adding the todo to file\n"); | ||
obj.saveTodo(argv[i+1]); | ||
} | ||
else if (strcmp(argv[i],"-l")==0){ | ||
todo::Todo obj(todo::FileType::READ); | ||
printf("list of todos\n"); | ||
obj.listTodo(); | ||
} | ||
|
||
else if (strcmp(argv[i],"-d")==0){ | ||
|
||
int index = static_cast<int>(*argv[i+1] - '0'); | ||
|
||
printf("todo removed\n"); | ||
todo::Todo obj(todo::FileType::WRITE); | ||
obj.deleteTodo(index); | ||
} | ||
|
||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "todo.hpp" | ||
todo::Todo::Todo(FileType fileType) { | ||
switch (fileType) | ||
{ | ||
case APPEND: | ||
file.open(FILE_NAME,std::ios_base::app); | ||
break; | ||
|
||
case READ: | ||
file.open(FILE_NAME,std::ios_base::in); | ||
break; | ||
|
||
case WRITE: | ||
file.open(FILE_NAME,std::ios_base::out); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
if(!file){ | ||
printf("failed to open file\n"); | ||
exit(1); | ||
} | ||
}; | ||
todo::Todo::~Todo() { | ||
file.close(); | ||
}; | ||
|
||
void todo::Todo::saveTodo(const char* value) { | ||
file << value << "\n"; | ||
}; | ||
|
||
void todo::Todo::listTodo(){ | ||
if (file.is_open()){ | ||
index=0; | ||
while (std::getline(file,line)){ | ||
++index; | ||
printf("%d. %s\n",index, line.c_str()); | ||
} | ||
} | ||
} | ||
|
||
void todo::Todo::deleteTodo(int index){ | ||
std::string line; | ||
|
||
|
||
int _index = 0; | ||
while (std::getline(file,line)){ | ||
++_index; | ||
if (_index == index){ | ||
line.replace(line.begin(),line.end(), "ok"); | ||
printf("%s\n", line.c_str()); | ||
printf("done\n"); | ||
|
||
// file << line << "\n"; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
#define FILE_NAME "/home/hunter/zzzzz/cpp/learn/applications/todo/todo.txt" | ||
|
||
namespace todo { | ||
enum FileType {READ, WRITE, APPEND}; | ||
class Todo { | ||
public: | ||
Todo(FileType fileType); | ||
~Todo(); | ||
void saveTodo(const char* value); | ||
void listTodo(); | ||
void deleteTodo(int index); | ||
private: | ||
std::fstream file; | ||
std::string line; | ||
int index; | ||
|
||
|
||
|
||
|
||
}; | ||
} |