summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-02-25 12:18:36 -0700
committerAaron Ball <nullspoon@iohq.net>2015-02-25 12:18:36 -0700
commit53040a1b3551417c4146d47ca4324b7eb7ca8128 (patch)
tree73667397815d8d7e85622a43b307c60e9f7b61a2
parentd89446f3e2d495ff9b5ca6528ec48e7f4233bb90 (diff)
downloadnoteless-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.c18
-rw-r--r--src/note.c13
-rw-r--r--src/note_list.c27
-rw-r--r--src/note_list.h3
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 <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 );
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(&note, note_path);
+ return note_rm(&note);
+ }
+ 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 );

Generated by cgit