lib/utils: implement read_from_stdin

This commit is contained in:
Dirk-Jan C. Binnema 2023-04-29 22:55:54 +03:00
parent e4af910d04
commit 3a05dd8725
4 changed files with 40 additions and 6 deletions

View File

@ -1,4 +1,4 @@
## Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
## Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
##
## 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',

View File

@ -26,6 +26,7 @@
#include <glib.h>
#include <gio/gio.h>
#include <gio/gunixinputstream.h>
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<std::string>
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<GOutputStreamSpliceFlags>
(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<const char*>(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
/*

View File

@ -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<std::string> read_from_stdin();
} // namespace Mu
#endif /* MU_UTILS_FILE_HH__ */

View File

@ -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')