From 3a05dd8725c3e14a9a5fd9381131f42df10113c3 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 29 Apr 2023 22:55:54 +0300 Subject: [PATCH] lib/utils: implement read_from_stdin --- lib/utils/meson.build | 5 +++-- lib/utils/mu-utils-file.cc | 26 +++++++++++++++++++++++++- lib/utils/mu-utils-file.hh | 8 ++++++++ meson.build | 7 ++++--- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/utils/meson.build b/lib/utils/meson.build index 0d3938fa..efb9ffc2 100644 --- a/lib/utils/meson.build +++ b/lib/utils/meson.build @@ -1,4 +1,4 @@ -## Copyright (C) 2022 Dirk-Jan C. Binnema +## Copyright (C) 2022-2023 Dirk-Jan C. Binnema ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -27,6 +27,7 @@ lib_mu_utils=static_library('mu-utils', [ ], dependencies: [ glib_dep, gio_dep, + gio_unix_dep, config_h_dep, readline_dep ], include_directories: @@ -63,7 +64,7 @@ test('test-utils-file', executable('test-utils-file', 'mu-utils-file.cc', install: false, cpp_args: ['-DBUILD_TESTS'], - dependencies: [glib_dep, config_h_dep, lib_mu_utils_dep])) + dependencies: [glib_dep, gio_unix_dep,config_h_dep, lib_mu_utils_dep])) test('test-logger', executable('test-logger', 'mu-logger.cc', diff --git a/lib/utils/mu-utils-file.cc b/lib/utils/mu-utils-file.cc index e1b9a10d..f8468859 100644 --- a/lib/utils/mu-utils-file.cc +++ b/lib/utils/mu-utils-file.cc @@ -26,6 +26,7 @@ #include #include +#include using namespace Mu; @@ -223,11 +224,34 @@ Mu::g_cancellable_new_with_timeout(guint timeout) g_thread_new("cancel-wait", cancel_wait, cancel)); g_object_set_data_full(G_OBJECT(cancel), "cancel", cancel, cancel_wait_free); - return cancel; } +Result +Mu::read_from_stdin() +{ + g_autoptr(GOutputStream) outmem = g_memory_output_stream_new_resizable(); + g_autoptr(GInputStream) input = g_unix_input_stream_new(STDIN_FILENO, TRUE); + //g_autoptr(GCancellable) cancel{maybe_cancellable_timeout(timeout)}; + + GError *err{}; + auto bytes = g_output_stream_splice(outmem, input, + static_cast + (G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | + G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET), + {}, &err); + + if (bytes < 0) + return Err(Error::Code::File, &err, "error reading from pipe"); + + return Ok(std::string{ + static_cast(g_memory_output_stream_get_data( + G_MEMORY_OUTPUT_STREAM(outmem))), + g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(outmem))}); +} + + #ifdef BUILD_TESTS /* diff --git a/lib/utils/mu-utils-file.hh b/lib/utils/mu-utils-file.hh index 5fae29c7..df70d892 100644 --- a/lib/utils/mu-utils-file.hh +++ b/lib/utils/mu-utils-file.hh @@ -169,6 +169,14 @@ std::string join_paths(S&& s, Args...args) { */ GCancellable* g_cancellable_new_with_timeout(guint timeout); +/** + * Read for standard input + * + * @return data from standard input or an error. + */ +Result read_from_stdin(); + + } // namespace Mu #endif /* MU_UTILS_FILE_HH__ */ diff --git a/meson.build b/meson.build index d841e46f..6b26cdfc 100644 --- a/meson.build +++ b/meson.build @@ -121,9 +121,10 @@ config_h_data.set_quoted('MU_TESTMAILDIR_CJK', join_paths(testmaildir, 'cjk')) ################################################################################ # hard dependencies # -glib_dep = dependency('glib-2.0', version: '>= 2.58') -gobject_dep = dependency('gobject-2.0', version: '>= 2.58') -gio_dep = dependency('gio-2.0', version: '>= 2.50') +glib_dep = dependency('glib-2.0', version: '>= 2.60') +gobject_dep = dependency('gobject-2.0', version: '>= 2.60') +gio_dep = dependency('gio-2.0', version: '>= 2.60') +gio_unix_dep = dependency('gio-unix-2.0', version: '>= 2.60') gmime_dep = dependency('gmime-3.0', version: '>= 3.2') xapian_dep = dependency('xapian-core', version:'>= 1.4') thread_dep = dependency('threads')