From a81dbd90a10887d173f423bdc21c0b39c2be6b22 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 26 Nov 2015 23:40:17 +0000 Subject: [PATCH] Consolidate writing to fd --- src/buffer_utils.cc | 4 ++-- src/file.cc | 2 +- src/file.hh | 4 ++++ src/main.cc | 8 -------- src/ncurses_ui.cc | 3 ++- src/shell_manager.cc | 2 +- 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index 9dd16abe..5d1b6d5e 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -162,8 +162,8 @@ void write_to_debug_buffer(StringView str) { if (not BufferManager::has_instance()) { - write(2, str.data(), (int)str.length()); - write(2, "\n", 1); + write_stderr(str); + write_stderr("\n"); return; } diff --git a/src/file.cc b/src/file.cc index 1daa298b..73f50ca7 100644 --- a/src/file.cc +++ b/src/file.cc @@ -197,7 +197,7 @@ bool file_exists(StringView filename) return stat(real_filename.c_str(), &st) == 0; } -static void write(int fd, StringView data) +void write(int fd, StringView data) { const char* ptr = data.data(); ssize_t count = (int)data.length(); diff --git a/src/file.hh b/src/file.hh index 98431adc..9a17c2b1 100644 --- a/src/file.hh +++ b/src/file.hh @@ -42,6 +42,10 @@ String get_kak_binary_path(); String read_fd(int fd, bool text = false); String read_file(StringView filename, bool text = false); +void write(int fd, StringView data); +inline void write_stdout(StringView str) { write(1, str); } +inline void write_stderr(StringView str) { write(2, str); } + struct MappedFile { diff --git a/src/main.cc b/src/main.cc index e3b0098e..5dbd908a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -44,14 +44,6 @@ String runtime_directory() return "/usr/share/kak"; } -static void write(int fd, StringView str) -{ - write(fd, str.data(), (size_t)(int)str.length()); -} - -static void write_stdout(StringView str) { write(1, str); } -static void write_stderr(StringView str) { write(2, str); } - void register_env_vars() { static const struct { diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index 933c3eb3..432f4700 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -3,6 +3,7 @@ #include "containers.hh" #include "display_buffer.hh" #include "event_manager.hh" +#include "file.hh" #include "keys.hh" #include "register_manager.hh" #include "utf8_iterator.hh" @@ -405,7 +406,7 @@ void NCursesUI::draw_status(const DisplayLine& status_line, for (auto& atom : mode_line) title += atom.content(); title += " - Kakoune\007"; - write(1, title.data(), (int)title.length()); + write_stdout(title); } m_dirty = true; diff --git a/src/shell_manager.cc b/src/shell_manager.cc index 861b9a63..100c2185 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -132,7 +132,7 @@ std::pair ShellManager::eval( child_stdout.close_write_fd(); child_stderr.close_write_fd(); - write(child_stdin.write_fd(), input.data(), (int)input.length()); + write(child_stdin.write_fd(), input); child_stdin.close_write_fd(); auto wait_time = profile ? Clock::now() : TimePoint{};