From 3caf96211085254ab3c42a59ce2c80a4ade3d2d8 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 9 Sep 2011 18:40:59 +0000 Subject: [PATCH] exception: refactoring --- src/buffer_manager.cc | 7 ++++--- src/command_manager.cc | 6 +++--- src/command_manager.hh | 8 ++++---- src/exception.cc | 14 ++++++++++++++ src/exception.hh | 32 ++++++++++++++++++++++++++++++++ src/file.cc | 14 ++++++++++---- src/file.hh | 23 ++++++++++------------- src/main.cc | 30 ++++++++++++++++-------------- src/regex_selector.cc | 5 ++--- 9 files changed, 95 insertions(+), 44 deletions(-) create mode 100644 src/exception.cc create mode 100644 src/exception.hh diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index 9d4cd5062..db355ae55 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -1,14 +1,15 @@ #include "buffer_manager.hh" +#include + #include "buffer.hh" #include "window.hh" -#include +#include "exception.hh" namespace Kakoune { -struct name_not_unique {}; - +struct name_not_unique : logic_error {}; BufferManager* BufferManager::ms_instance = nullptr; diff --git a/src/command_manager.cc b/src/command_manager.cc index aa1237d54..0cf2cff96 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -37,10 +37,10 @@ static std::vector split(const std::string& line) return result; } -struct command_not_found : public std::runtime_error +struct command_not_found : runtime_error { - command_not_found(const std::string& what) - : std::runtime_error("command not found: " + what) {} + command_not_found(const std::string& command) + : runtime_error(command + " : no such command") {} }; void CommandManager::execute(const std::string& command_line) diff --git a/src/command_manager.hh b/src/command_manager.hh index 9656b8e83..2f014838c 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -4,17 +4,17 @@ #include #include #include -#include #include +#include "exception.hh" + namespace Kakoune { -struct wrong_argument_count : public std::runtime_error +struct wrong_argument_count : runtime_error { - wrong_argument_count() - : std::runtime_error("wrong argument count") {} + wrong_argument_count() : runtime_error("wrong argument count") {} }; typedef std::vector CommandParameters; diff --git a/src/exception.cc b/src/exception.cc new file mode 100644 index 000000000..0d8160628 --- /dev/null +++ b/src/exception.cc @@ -0,0 +1,14 @@ +#include "exception.hh" + +#include +#include + +namespace Kakoune +{ + +std::string exception::description() const +{ + return typeid(*this).name(); +} + +} diff --git a/src/exception.hh b/src/exception.hh new file mode 100644 index 000000000..f341e724b --- /dev/null +++ b/src/exception.hh @@ -0,0 +1,32 @@ +#ifndef exception_hh_INCLUDED +#define exception_hh_INCLUDED + +#include + +namespace Kakoune +{ + +struct exception +{ + virtual ~exception() {} + virtual std::string description() const; +}; + +struct runtime_error : exception +{ + runtime_error(const std::string description) + : m_description(description) {} + + std::string description() const { return m_description; } + +private: + std::string m_description; +}; + +struct logic_error : exception +{ +}; + +} + +#endif // exception_hh_INCLUDED diff --git a/src/file.cc b/src/file.cc index bf3e31f24..a7907897f 100644 --- a/src/file.cc +++ b/src/file.cc @@ -1,5 +1,7 @@ #include "file.hh" + #include "buffer.hh" +#include "buffer_manager.hh" #include #include @@ -16,9 +18,9 @@ Buffer* create_buffer_from_file(const std::string& filename) if (fd == -1) { if (errno == ENOENT) - throw file_not_found(strerror(errno)); + throw file_not_found(filename); - throw open_file_error(strerror(errno)); + throw file_access_error(filename, strerror(errno)); } std::string content; @@ -32,6 +34,10 @@ Buffer* create_buffer_from_file(const std::string& filename) content += std::string(buf, size); } close(fd); + + if (Buffer* buffer = BufferManager::instance().get_buffer(filename)) + BufferManager::instance().delete_buffer(buffer); + return new Buffer(filename, content); } @@ -39,7 +45,7 @@ void write_buffer_to_file(const Buffer& buffer, const std::string& filename) { int fd = open(filename.c_str(), O_CREAT | O_WRONLY, 0644); if (fd == -1) - throw open_file_error(strerror(errno)); + throw file_access_error(filename, strerror(errno)); const BufferString& content = buffer.content(); ssize_t count = content.length() * sizeof(BufferChar); @@ -52,7 +58,7 @@ void write_buffer_to_file(const Buffer& buffer, const std::string& filename) count -= written; if (written == -1) - throw write_file_error(strerror(errno)); + throw file_access_error(filename, strerror(errno)); } close(fd); } diff --git a/src/file.hh b/src/file.hh index fa91e347a..54bb654e9 100644 --- a/src/file.hh +++ b/src/file.hh @@ -2,27 +2,24 @@ #define file_hh_INCLUDED #include -#include + +#include "exception.hh" namespace Kakoune { -struct open_file_error : public std::runtime_error +struct file_access_error : runtime_error { - open_file_error(const std::string& what) - : std::runtime_error(what) {} +public: + file_access_error(const std::string& filename, + const std::string& error_desc) + : runtime_error(filename + ": " + error_desc) {} }; -struct file_not_found : public open_file_error +struct file_not_found : file_access_error { - file_not_found(const std::string& what) - : open_file_error(what) {} -}; - -struct write_file_error : public std::runtime_error -{ - write_file_error(const std::string& what) - : std::runtime_error(what) {} + file_not_found(const std::string& filename) + : file_access_error(filename, "file not found") {} }; class Buffer; diff --git a/src/main.cc b/src/main.cc index e0a6f8771..1ec5de6fc 100644 --- a/src/main.cc +++ b/src/main.cc @@ -216,10 +216,6 @@ void do_command() command_manager.execute(prompt(":")); } catch (prompt_aborted&) {} - catch (std::runtime_error& err) - { - print_status(err.what()); - } } bool is_blank(char c) @@ -270,7 +266,6 @@ void do_search(Window& window) std::string ex = prompt("/"); window.select(false, RegexSelector(ex)); } - catch (boost::regex_error&) {} catch (prompt_aborted&) {} } @@ -313,18 +308,25 @@ int main() int count = 0; while(not quit_requested) { - char c = getch(); - - if (isdigit(c)) - count = count * 10 + c - '0'; - else + try { - if (keymap.find(c) != keymap.end()) + char c = getch(); + + if (isdigit(c)) + count = count * 10 + c - '0'; + else { - keymap[c](*current_window, count); - draw_window(*current_window); + if (keymap.find(c) != keymap.end()) + { + keymap[c](*current_window, count); + draw_window(*current_window); + } + count = 0; } - count = 0; + } + catch (Kakoune::runtime_error& error) + { + print_status(error.description()); } } deinit_ncurses(); diff --git a/src/regex_selector.cc b/src/regex_selector.cc index dcdae64ce..5b9cb0416 100644 --- a/src/regex_selector.cc +++ b/src/regex_selector.cc @@ -1,6 +1,5 @@ #include "regex_selector.hh" - -void print_status(const std::string&); +#include "exception.hh" namespace Kakoune { @@ -21,7 +20,7 @@ Selection RegexSelector::operator()(const BufferIterator& cursor) const } catch (boost::regex_error& err) { - print_status("regex error"); + throw runtime_error("regex error"); } return Selection(cursor, cursor);