From 19123186d739abe84fe49d590cc1b4db6635eead Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 2 Sep 2011 18:02:29 +0000 Subject: [PATCH] better exception handling in edit and write_buffer --- src/main.cc | 134 +++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/src/main.cc b/src/main.cc index 32870b52..9074536a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -82,70 +82,6 @@ void deinit_ncurses() endwin(); } -void do_insert(Window& window) -{ - std::string inserted; - LineAndColumn pos = window.cursor_position(); - while(true) - { - char c = getch(); - if (c == 27) - break; - - window.insert(std::string() + c); - draw_window(window); - } -} - -std::shared_ptr current_window; - -void edit(const std::string& filename) -{ - try - { - Buffer* buffer = create_buffer_from_file(filename); - if (buffer) - current_window = std::make_shared(std::shared_ptr(buffer)); - } - catch (open_file_error& what) - { - assert(false); - } -} - -void write_buffer(const std::string& filename) -{ - try - { - write_buffer_to_file(*current_window->buffer(), filename); - } - catch(open_file_error& what) - { - assert(false); - } - catch(write_file_error& what) - { - assert(false); - } -} - -bool quit_requested = false; - -void quit(const std::string&) -{ - quit_requested = true; -} - -std::unordered_map> cmdmap = -{ - { "e", edit }, - { "edit", edit }, - { "q", quit }, - { "quit", quit }, - { "w", write_buffer }, - { "write", write_buffer }, -}; - struct prompt_aborted {}; std::string prompt(const std::string& text) @@ -194,6 +130,76 @@ void print_status(const std::string& status) addstr(status.c_str()); } +void do_insert(Window& window) +{ + std::string inserted; + LineAndColumn pos = window.cursor_position(); + while(true) + { + char c = getch(); + if (c == 27) + break; + + window.insert(std::string() + c); + draw_window(window); + } +} + +std::shared_ptr current_window; + +void edit(const std::string& filename) +{ + try + { + std::shared_ptr buffer(create_buffer_from_file(filename)); + if (buffer) + current_window = std::make_shared(buffer); + } + catch (file_not_found& what) + { + current_window = std::make_shared(std::make_shared(filename)); + } + catch (open_file_error& what) + { + print_status("error opening '" + filename + "' (" + what.what() + ")"); + } +} + +void write_buffer(const std::string& filename) +{ + try + { + Buffer& buffer = *current_window->buffer(); + write_buffer_to_file(buffer, + filename.empty() ? buffer.name() : filename); + } + catch(open_file_error& what) + { + print_status("error opening " + filename + "(" + what.what() + ")"); + } + catch(write_file_error& what) + { + print_status("error writing " + filename + "(" + what.what() + ")"); + } +} + +bool quit_requested = false; + +void quit(const std::string&) +{ + quit_requested = true; +} + +std::unordered_map> cmdmap = +{ + { "e", edit }, + { "edit", edit }, + { "q", quit }, + { "quit", quit }, + { "w", write_buffer }, + { "write", write_buffer }, +}; + void do_command() { try