From 3c7d57d448e2698fc8b6a56e2330016e0a884077 Mon Sep 17 00:00:00 2001 From: sachinkum0009 Date: Sun, 5 Sep 2021 15:13:37 +0530 Subject: [PATCH] src files added --- src/main.cpp | 48 +++++++++++++++++++++++++++++++++++++++++ src/todo.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/todo.hpp | 27 +++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 src/main.cpp create mode 100644 src/todo.cpp create mode 100644 src/todo.hpp diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..7ea0564 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,48 @@ +#include +#include +#include "todo.hpp" +#include + +#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(*argv[i+1] - '0'); + + printf("todo removed\n"); + todo::Todo obj(todo::FileType::WRITE); + obj.deleteTodo(index); + } + + } +} + return 0; +} diff --git a/src/todo.cpp b/src/todo.cpp new file mode 100644 index 0000000..abb29e2 --- /dev/null +++ b/src/todo.cpp @@ -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"; + } + + } + +} \ No newline at end of file diff --git a/src/todo.hpp b/src/todo.hpp new file mode 100644 index 0000000..d1efc80 --- /dev/null +++ b/src/todo.hpp @@ -0,0 +1,27 @@ +#pragma once + + +#include +#include + +#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; + + + + + }; +} \ No newline at end of file