diff options
author | Aaron Ball <nullspoon@iohq.net> | 2015-02-25 12:18:36 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2015-02-25 12:18:36 -0700 |
commit | 53040a1b3551417c4146d47ca4324b7eb7ca8128 (patch) | |
tree | 73667397815d8d7e85622a43b307c60e9f7b61a2 | |
parent | d89446f3e2d495ff9b5ca6528ec48e7f4233bb90 (diff) | |
download | noteless-53040a1b3551417c4146d47ca4324b7eb7ca8128.tar.gz noteless-53040a1b3551417c4146d47ca4324b7eb7ca8128.tar.xz |
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.
-rw-r--r-- | src/main.c | 18 | ||||
-rw-r--r-- | src/note.c | 13 | ||||
-rw-r--r-- | src/note_list.c | 27 | ||||
-rw-r--r-- | src/note_list.h | 3 |
4 files changed, 41 insertions, 20 deletions
@@ -34,7 +34,7 @@ void get_help() { " cat <note> Outputs the specified note's contents verbatim.\n" " new <note> Creates the specified note and opens for editing.\n" " edit <note> Opens the specified note for editing.\n" - " find <term> Performs a case-insensitive search of all notes for the\n" + //" find <term> Performs a case-insensitive search of all notes for the\n" " rm <note> 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 ); @@ -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<string> enum_names(); // vector<string> find( bool, string ); |