summaryrefslogtreecommitdiff
path: root/gio/src/subprocess.ccg
blob: 73f1d9a3beca03a19cc4cfdfd479a829cab3e6e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Copyright (C) 2023 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <giomm/slot_async.h>
#include <gio/gio.h>

namespace Gio
{
Subprocess::Subprocess(const std::vector<std::string>& argv, Flags flags)
: _CONSTRUCT("argv", Glib::ArrayHandler<std::string>::vector_to_array(argv).data(), "flags", (GSubprocessFlags)flags)
{
  init();
}

std::pair<Glib::RefPtr<Glib::Bytes>, Glib::RefPtr<Glib::Bytes>>
Subprocess::communicate(const Glib::RefPtr<const Glib::Bytes>& stdin_buf,
  const Glib::RefPtr<Cancellable>& cancellable)
{
  GError* gerror = nullptr;
  GBytes* gstdout_buf = nullptr;
  GBytes* gstderr_buf = nullptr;
  (void)g_subprocess_communicate(gobj(),
    const_cast<GBytes*>(Glib::unwrap(stdin_buf)),
    Glib::unwrap(cancellable), &gstdout_buf, &gstderr_buf, &gerror);
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return {Glib::wrap(gstdout_buf), Glib::wrap(gstderr_buf)};
}

std::pair<Glib::RefPtr<Glib::Bytes>, Glib::RefPtr<Glib::Bytes>>
Subprocess::communicate_finish(const Glib::RefPtr<AsyncResult>& result)
{
  GError* gerror = nullptr;
  GBytes* gstdout_buf = nullptr;
  GBytes* gstderr_buf = nullptr;
  (void)g_subprocess_communicate_finish(gobj(), Glib::unwrap(result),
    &gstdout_buf, &gstderr_buf, &gerror);
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return {Glib::wrap(gstdout_buf), Glib::wrap(gstderr_buf)};
}

std::pair<Glib::ustring, Glib::ustring>
Subprocess::communicate_utf8(const Glib::ustring& stdin_buf,
  const Glib::RefPtr<Cancellable>& cancellable)
{
  GError* gerror = nullptr;
  char* gstdout_buf = nullptr;
  char* gstderr_buf = nullptr;
  (void)g_subprocess_communicate_utf8(gobj(), Glib::c_str_or_nullptr(stdin_buf),
    Glib::unwrap(cancellable), &gstdout_buf, &gstderr_buf, &gerror);
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return {Glib::convert_return_gchar_ptr_to_ustring(gstdout_buf),
          Glib::convert_return_gchar_ptr_to_ustring(gstderr_buf)};
}

std::pair<Glib::ustring, Glib::ustring>
Subprocess::communicate_utf8_finish(const Glib::RefPtr<AsyncResult>& result)
{
  GError* gerror = nullptr;
  char* gstdout_buf = nullptr;
  char* gstderr_buf = nullptr;
  (void)g_subprocess_communicate_utf8_finish(gobj(), Glib::unwrap(result),
    &gstdout_buf, &gstderr_buf, &gerror);
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return {Glib::convert_return_gchar_ptr_to_ustring(gstdout_buf),
          Glib::convert_return_gchar_ptr_to_ustring(gstderr_buf)};
}

} // namespace Gio