From 53040a1b3551417c4146d47ca4324b7eb7ca8128 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Wed, 25 Feb 2015 12:18:36 -0700 Subject: Implemented noteless rm command Now we can remove notes. Outputs proper status messages when file could not be deleted, when it doesn't exist for deletion, and when the deletion was successful. --- src/main.c | 18 ++++++++++++------ src/note.c | 13 ++++++++++--- src/note_list.c | 27 +++++++++++++++++---------- src/note_list.h | 3 ++- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main.c b/src/main.c index 393ade8..dea8ba6 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,7 @@ void get_help() { " cat Outputs the specified note's contents verbatim.\n" " new Creates the specified note and opens for editing.\n" " edit Opens the specified note for editing.\n" - " find Performs a case-insensitive search of all notes for the\n" + //" find Performs a case-insensitive search of all notes for the\n" " rm Deletes the specified note.\n" " given search term.\n" " help Displays this help text\n" @@ -178,11 +178,17 @@ int main(int argc, char* argv[]) { return create_status; } } else if(strcmp(argv[1], "edit") == 0) { - char* name = argv[2]; - return note_list_edit(&list, editor, name ); - // } else if( arg == "rm" ) { - // string name = argv[i + 1]; - // return list.rm( name ); + return note_list_edit(&list, editor, argv[2]); + } else if(strcmp(argv[1], "rm") == 0) { + int rm_status = note_list_rm_note(&list, argv[2]); + if(rm_status == 0) { + printf("Note matching \"%s\" deleted successfully\n", argv[2]); + } else if(rm_status == 1) { + printf("Error: There are no notes matching \"%s\".\n", argv[2]); + } else { + printf("Error: Failed deleting note matching \"%s\".\n", argv[2]); + } + return rm_status; // } else if( arg == "find" ) { // string term = argv[i + 1]; // return search_notes( list, term ); diff --git a/src/note.c b/src/note.c index 2bc828d..bb2ffb9 100644 --- a/src/note.c +++ b/src/note.c @@ -84,9 +84,16 @@ int note_cat(note_t* note) { * * @return int Success or failure */ -// int note::rm() { -// return remove( _path.out().c_str() ); -// } +int note_rm(note_t* note) { + // Verify file exists. + FILE* f = fopen(note->path, "r"); + if(f) { + return remove(note->path); + fclose(f); + } else { + return 1; + } +} /** * Gets the filename of the current note instance without its extension. diff --git a/src/note_list.c b/src/note_list.c index 2566b59..6ddb47a 100644 --- a/src/note_list.c +++ b/src/note_list.c @@ -261,16 +261,23 @@ int note_list_cat_note(note_list_t* list, char* note_name) { * * @return int Exit code (1 = error, 0 = success) */ -// int note_list::rm( string note_name ) { -// int id = find_note_id( note_name ); -// if( id == -1 ) { -// cout << "Note \"" << note_name << "\" does not exist." << endl; -// return 1; -// } else { -// notes[id].rm(); -// } -// return 0; -// } +int note_list_rm_note(note_list_t* list, char* note_name) { + int id = note_list_get_note_id(list, note_name); + if(id == -1) { + printf("Error: There is no note matching \"%s\".\n", note_name); + return 1; + } else { + char note_path[256] = ""; + strcpy(note_path, list->path); + strcat(note_path, "/"); + strcat(note_path, list->names[id]); + + note_t note; + note_new(¬e, note_path); + return note_rm(¬e); + } + return 0; +} /** * TODO diff --git a/src/note_list.h b/src/note_list.h index 227c9f5..a6822db 100644 --- a/src/note_list.h +++ b/src/note_list.h @@ -43,11 +43,12 @@ int note_list_cat_note(note_list_t*, char*); int note_list_create_note(note_list_t*, char*, char*); +int note_list_rm_note(note_list_t*, char*); + // void get_notes(note_list_t*); // note_list( string, string ); // int add( string, string ); -// int rm( string ); // vector enum_names(); // vector find( bool, string ); -- cgit v1.2.3