Skip to content

Commit

Permalink
src files added
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinkum0009 committed Sep 5, 2021
1 parent 0f00814 commit 3c7d57d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main.cpp
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;
}
60 changes: 60 additions & 0 deletions src/todo.cpp
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";
}

}

}
27 changes: 27 additions & 0 deletions src/todo.hpp
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;




};
}

0 comments on commit 3c7d57d

Please sign in to comment.