diff options
author | Nullspoon <nullspoon@iohq.net> | 2014-08-26 19:53:03 -0600 |
---|---|---|
committer | Nullspoon <nullspoon@iohq.net> | 2014-08-26 19:53:03 -0600 |
commit | 908183f39980affff6d79df68014ad709c1170ae (patch) | |
tree | cb03d66fe0bf9e369e6ea595d1ec9ac026ef9082 | |
parent | 1a9f6dcff93845f365685a9fd13e7180d7bf9397 (diff) | |
download | noteless-908183f39980affff6d79df68014ad709c1170ae.tar.gz noteless-908183f39980affff6d79df68014ad709c1170ae.tar.xz |
Modified most main-called functions to return int
With the new default of edit if a command doesn't match but a note with that
name exists, noteless doesn't stop parsing arguments until it hits the end of
argv. Having all the of main-called functions return int allows it to return
status codes, thus exiting at the appropriate time with the added benefit of
being able to programatically work with most of these functions now.
-rw-r--r-- | src/lib/note_list.cpp | 16 | ||||
-rw-r--r-- | src/lib/note_list.h | 6 | ||||
-rw-r--r-- | src/main.cpp | 35 |
3 files changed, 31 insertions, 26 deletions
diff --git a/src/lib/note_list.cpp b/src/lib/note_list.cpp index a5f22a2..6e81082 100644 --- a/src/lib/note_list.cpp +++ b/src/lib/note_list.cpp @@ -82,13 +82,15 @@ int note_list::add( path p ) { * * @param string editor Path to the editor to use for editing * @param string note_name Name of the note to be created and edited + * + * @return int Success or failure (always success for now) */ -void note_list::create( string editor, string note_name ) { +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; + return 0; } /** @@ -97,13 +99,15 @@ void note_list::create( string editor, string note_name ) { * @param string editor Path to the editor to use for editing * @param string note_name Name of the note to be edited */ -void note_list::edit( string editor, string note_name ) { +int note_list::edit( string editor, string note_name ) { int id = find_note_id( note_name ); if( id != -1 ) { notes[id].edit( editor ); } else { cout << "Note \"" << note_name << "\" does not exist." << endl; + return 1; } + return 0; } /** @@ -142,17 +146,17 @@ vector<string> note_list::enum_names() { * * @return vector<string> Contents of the note, broken per line into a vector. */ -bool note_list::cat_note( string note_name, vector<string>* pbody ) { +int note_list::cat_note( string note_name, vector<string>* pbody ) { // Check for the requested note int id = find_note_id( note_name ); if( id != -1 ) { // Read in the note's contents notes[id].read(); *pbody = notes[id].body; - return true; } else { - return false; + return 1; } + return 0; } /** diff --git a/src/lib/note_list.h b/src/lib/note_list.h index e1c3282..3a8d563 100644 --- a/src/lib/note_list.h +++ b/src/lib/note_list.h @@ -38,11 +38,11 @@ class note_list { // Methods note_list( string, string ); int add( path ); - void create( string, string ); - void edit( string, string ); + int create( string, string ); + int edit( string, string ); vector<string> enum_names(); vector<string> find( bool, string ); - bool cat_note( string, vector<string>* ); + int cat_note( string, vector<string>* ); int find_note_id( string ); }; #endif diff --git a/src/main.cpp b/src/main.cpp index f70cee3..e7eeec7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ using namespace std; #include "lib/note_list.h" #include "lib/path.h" -void get_help() { +int get_help() { string out = "\nNoteless provides a simple command line interface for note " "taking.\n\n" "What makes this different than simply using a command line text editor is\n" @@ -39,6 +39,7 @@ void get_help() { " help Displays this help text\n" " ls,list Lists all notes in note directory.\n"; cout << out << endl; + return 0; } /** @@ -57,16 +58,20 @@ void list_notes( note_list list ) { /** * Searches all note contents for the specified text */ -void search_notes( note_list list, string term ) { +int search_notes( note_list list, string term ) { vector<string> matches = list.find( true, term ); for( int i = 0; i < matches.size(); i++ ) { cout << matches[i] << endl; } + if( matches.size() == 0 ) { + return 1; + } + return 0; } -void cat_note( note_list list, string name ) { +int cat_note( note_list list, string name ) { vector<string> body; - if( list.cat_note( name, &body ) ) { + if( list.cat_note( name, &body ) == 0 ) { // If list returns true, the note exists for( int i = 0; i < body.size(); i++ ) { cout << body[i] << endl; @@ -74,7 +79,9 @@ void cat_note( note_list list, string name ) { } else { // List cat_note returned false. Note doesn't exist cout << "Note " << name << " could not be found." << endl; + return 1; } + return 0; } /** @@ -137,29 +144,23 @@ int main( int argc, char** argv ) { list_notes( list ); } else if( arg == "new" ) { string name = argv[i + 1]; - list.create( editor, name ); - break; + return list.create( editor, name ); } else if( arg == "edit" ) { string name = argv[i + 1]; - list.edit( editor, name ); - break; + return list.edit( editor, name ); } else if( arg == "find" ) { string term = argv[i + 1]; - search_notes( list, term ); - break; + return search_notes( list, term ); } else if( arg == "cat" ) { string name = argv[i + 1]; - cat_note( list, name ); - break; + return cat_note( list, name ); } else if( arg == "help" ) { - get_help(); - break; + return get_help(); } else if( list.find_note_id( arg ) != -1 ) { // Try to open the note if it exists - list.edit( editor, arg ); - break; + return list.edit( editor, arg ); } else { - cout << "Error: Unknown command or note name'" << arg << "'." << endl; + cout << "Error: Unknown command or note name '" << arg << "'." << endl; } } return 0; |