diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 131 |
1 files changed, 47 insertions, 84 deletions
@@ -1,25 +1,11 @@ -/** - * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net> - * - * Noteless is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Noteless is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with noteless. If not, see <http://www.gnu.org/licenses/>. - */ -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> +#include <dirent.h> #include <string.h> -#include "common.h" -#include "config.h" + #include "note_list.h" +#include "note.h" +#include "config.h" /** * Prints the standard help text @@ -49,90 +35,69 @@ void get_help() { * * @param list Note list whos names to enumerate */ -void list_notes(note_list_t* list) { - // List notes - for(int i = 0; i < list->count; i++) { - char name[128]; - strcpy(name, list->names[i]); - strip_extension(name); - printf("* %s\n", name); +void list_notes(note_list* list) { + note* n; + + while((n = note_list_read(list)) != NULL) { + printf("* %s\n", n->name); } } /** - * Ye olde main function + * Sets up environment variables. + * Handles loading in configuration defaults before loading the config file + * values if one is present. */ -int main(int argc, char* argv[]) { - // Print helptext if no commands specified - if(argc == 1) { - printf("\nNo command specified. Printing help text.\n"); - get_help(); - return 1; - } - +void config_setup(config_t* c) { // Get the home dir char home_path[strlen(getenv("HOME"))]; strcpy(home_path, getenv("HOME")); - /** - * Config file overrides - */ - config_t c; - // Set default note path char tmp_note_path[strlen(home_path) + 17]; strcpy(tmp_note_path, home_path); strcat(tmp_note_path, "/Documents/Notes"); - config_set(&c, "note_path", tmp_note_path); + config_set(c, "note_path", tmp_note_path); // Set the default note extension - config_set(&c, "extension", "mdown"); + config_set(c, "extension", "mdown"); // Set editor default char tmp_editor[128]; get_user_editor(tmp_editor); - config_set(&c, "editor", tmp_editor); + config_set(c, "editor", tmp_editor); // Assemble the path to the conf file char conf_path[strlen(home_path) + 23]; strcpy(conf_path, home_path); strcat(conf_path, "/.config/noteless.conf"); // Load the actual config file to override any defaults - config_load(&c, conf_path); - - // Just some literal code comments - // int config_status = config_load(&c, conf_path); - // if(config_status == -1) { - // printf("Config file could not be opened or does not exist.\n"); - // printf("Assuming defaults.\n"); - // } - - - // // If the init command was passed, we want to init before checking if the - // // note path exists, otherwise the error will display and the store will - // // never be initialized. - // if( string( argv[1] ) == "init" ) { - // path p( note_path ); - // return p.create(); - // } - - // // Check to make sure the note path exists - // path p( note_path ); - // if( ! p.exists() ) { - // string out = - // "\nThe note store path '" + p.out() + "' does not exist.\n\n" - // "If this is your first time running noteless, please run " - // "'[1mnoteless init[0m' to\n" - // "create the note store here.\n\n" - // "Otherwise, please verify the path variable in your configuration."; - // cout << out << endl; - // return 1; - // } - - note_list_t list; - - note_list_new(&list, config_get(&c, "note_path"), config_get(&c, "extension")); + config_load(c, conf_path); +} + + +int main(int argc, char* argv []) { + // Print helptext if no commands specified + if(argc == 1) { + printf("\nNo command specified. Printing help text.\n"); + get_help(); + return 1; + } + + config_t c; + note_list list; + + // + // Config setup + // + config_setup(&c); + + note_list_new( + &list, + config_get(&c, "note_path"), + config_get(&c, "extension") + ); if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) { list_notes(&list); @@ -161,16 +126,14 @@ int main(int argc, char* argv[]) { return note_list_cat_note(&list, argv[2]); } else if(strcmp(argv[1], "help") == 0) { get_help(); - } else if(note_list_get_note_id(&list, argv[1]) != -1 ) { - // Try to open the note if it exists - return note_list_edit(&list, config_get(&c, "editor"), argv[1]); } else { - printf("Error: Unknown command or note name '%s'.\n", argv[1]); - return 1; + int err = note_list_edit(&list, config_get(&c, "editor"), argv[1]); + if(err == 1) { + printf("%s is also not a valid command.\n", argv[1]); + return 1; + } } - // DANGER WILL ROBINSON!!! - // Clean up note_list_free(&list); config_free(&c); |