diff options
author | Aaron Ball <nullspoon@iohq.net> | 2015-02-25 11:30:13 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2015-02-25 11:30:13 -0700 |
commit | 83f7cbe3d162db0985c3e6daed068c0da73b5623 (patch) | |
tree | aa5dd8c3cc2235c0daed7e796ad0f72906cc464b | |
parent | 31d811a29587e93fe8efceefd1426c303b5c6117 (diff) | |
download | noteless-83f7cbe3d162db0985c3e6daed068c0da73b5623.tar.gz noteless-83f7cbe3d162db0985c3e6daed068c0da73b5623.tar.xz |
Reimplemented create/new note functionality
Now we can create new notes. Notifies user if note already exists and exists
with failure status code.
-rw-r--r-- | src/main.c | 10 | ||||
-rw-r--r-- | src/note_list.c | 32 | ||||
-rw-r--r-- | src/note_list.h | 3 |
3 files changed, 34 insertions, 11 deletions
@@ -170,9 +170,13 @@ int main(int argc, char* argv[]) { if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) { list_notes(&list); - // } else if( arg == "new" ) { - // string name = argv[i + 1]; - // return list.create( editor, name ); + } else if(strcmp(argv[1], "new") == 0) { + int create_status = note_list_create_note(&list, editor, argv[2]); + // Notify user that a note already exists by the specified name + if(create_status == 1) { + printf("A note by the name %s already exists.\n", argv[2]); + return create_status; + } } else if(strcmp(argv[1], "edit") == 0) { char* name = argv[2]; return note_list_edit(&list, editor, name ); diff --git a/src/note_list.c b/src/note_list.c index 0376508..79b9aa9 100644 --- a/src/note_list.c +++ b/src/note_list.c @@ -146,13 +146,31 @@ void note_list_free(note_list_t* list) { * * @return int Success or failure (always success for now) */ -// int note_list::create( string editor, string note_name ) { -// string spath = _dirname + '/' + note_name + '.' + _ext; -// path p( spath ); -// note n( p ); -// n.edit( editor ); -// return 0; -// } +int note_list_create_note(note_list_t* list, char* editor, char* note_name) { + char tmp_note_name[64]; + strcpy(tmp_note_name, note_name); + strcat(tmp_note_name, "."); + strcat(tmp_note_name, list->extension); + + // Check if note already exists + for(int i = 0; i < list->count; i++) { + if(strcmp(list->names[i], tmp_note_name) == 0) { + return 1; + } + } + + // Construct full note path + char note_path[256]; + strcpy(note_path, list->path); + strcat(note_path, "/"); + strcat(note_path, tmp_note_name); + + note_t note; + note_new(¬e, note_path); + note_edit(¬e, editor); + + return 0; +} /** diff --git a/src/note_list.h b/src/note_list.h index 11744a6..227c9f5 100644 --- a/src/note_list.h +++ b/src/note_list.h @@ -41,11 +41,12 @@ int note_list_get_note_id(note_list_t*, char*); int note_list_cat_note(note_list_t*, char*); +int note_list_create_note(note_list_t*, char*, char*); + // void get_notes(note_list_t*); // note_list( string, string ); // int add( string, string ); -// int create( string, string ); // int rm( string ); // vector<string> enum_names(); // vector<string> find( bool, string ); |