[gnome-builder] subprocess: use virtual method for async utf8 APIs
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] subprocess: use virtual method for async utf8 APIs
- Date: Sun, 19 Mar 2017 13:04:23 +0000 (UTC)
commit 450bf05dcba3063a38ec8932a3622043ebad9296
Author: Christian Hergert <chergert redhat com>
Date: Sun Mar 19 06:04:09 2017 -0700
subprocess: use virtual method for async utf8 APIs
We should be using the per-backend specific APIs for this rather than
using a default base-class implementation.
libide/subprocess/ide-breakout-subprocess.c | 6 ++-
libide/subprocess/ide-simple-subprocess.c | 73 ++++++++++++++++++++++++++-
libide/subprocess/ide-subprocess.c | 43 +---------------
libide/subprocess/ide-subprocess.h | 10 ++++
4 files changed, 88 insertions(+), 44 deletions(-)
---
diff --git a/libide/subprocess/ide-breakout-subprocess.c b/libide/subprocess/ide-breakout-subprocess.c
index 33f6753..2eb2d77 100644
--- a/libide/subprocess/ide-breakout-subprocess.c
+++ b/libide/subprocess/ide-breakout-subprocess.c
@@ -331,7 +331,7 @@ ide_breakout_subprocess_wait_finish (IdeSubprocess *subprocess,
return g_task_propagate_boolean (G_TASK (result), error);
}
-void
+static void
ide_breakout_subprocess_communicate_utf8_async (IdeSubprocess *subprocess,
const char *stdin_buf,
GCancellable *cancellable,
@@ -395,7 +395,7 @@ communicate_result_validate_utf8 (const char *stream_name,
IDE_RETURN (TRUE);
}
-gboolean
+static gboolean
ide_breakout_subprocess_communicate_utf8_finish (IdeSubprocess *subprocess,
GAsyncResult *result,
char **stdout_buf,
@@ -931,6 +931,8 @@ subprocess_iface_init (IdeSubprocessInterface *iface)
iface->communicate_utf8 = ide_breakout_subprocess_communicate_utf8;
iface->communicate_async = ide_breakout_subprocess_communicate_async;
iface->communicate_finish = ide_breakout_subprocess_communicate_finish;
+ iface->communicate_utf8_async = ide_breakout_subprocess_communicate_utf8_async;
+ iface->communicate_utf8_finish = ide_breakout_subprocess_communicate_utf8_finish;
}
static gboolean
diff --git a/libide/subprocess/ide-simple-subprocess.c b/libide/subprocess/ide-simple-subprocess.c
index 50452cb..aea986e 100644
--- a/libide/subprocess/ide-simple-subprocess.c
+++ b/libide/subprocess/ide-simple-subprocess.c
@@ -271,7 +271,10 @@ ide_simple_subprocess_communicate_cb (GObject *object,
gpointer *data;
if (!g_subprocess_communicate_finish (subprocess, result, &stdout_buf, &stderr_buf, &error))
- g_task_return_error (task, g_steal_pointer (&error));
+ {
+ g_task_return_error (task, g_steal_pointer (&error));
+ return;
+ }
data = g_new0 (gpointer, 2);
data[0] = g_steal_pointer (&stdout_buf);
@@ -320,6 +323,72 @@ ide_simple_subprocess_communicate_finish (IdeSubprocess *subprocess,
}
static void
+ide_simple_subprocess_communicate_utf8_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GSubprocess *subprocess = (GSubprocess *)object;
+ g_autoptr(GTask) task = user_data;
+ g_autoptr(GError) error = NULL;
+ g_autofree gchar *stdout_buf = NULL;
+ g_autofree gchar *stderr_buf = NULL;
+ gpointer *data;
+
+ if (!g_subprocess_communicate_utf8_finish (subprocess, result, &stdout_buf, &stderr_buf, &error))
+ {
+ g_task_return_error (task, g_steal_pointer (&error));
+ return;
+ }
+
+ data = g_new0 (gpointer, 2);
+ data[0] = g_steal_pointer (&stdout_buf);
+ data[1] = g_steal_pointer (&stderr_buf);
+
+ g_task_return_pointer (task, data, free_object_pair);
+}
+
+static void
+ide_simple_subprocess_communicate_utf8_async (IdeSubprocess *subprocess,
+ const gchar *stdin_buf,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ IdeSimpleSubprocess *self = (IdeSimpleSubprocess *)subprocess;
+ GTask *task = g_task_new (self, cancellable, callback, user_data);
+ g_subprocess_communicate_utf8_async (self->subprocess, stdin_buf, cancellable,
ide_simple_subprocess_communicate_utf8_cb, task);
+}
+
+static gboolean
+ide_simple_subprocess_communicate_utf8_finish (IdeSubprocess *subprocess,
+ GAsyncResult *result,
+ gchar **stdout_buf,
+ gchar **stderr_buf,
+ GError **error)
+{
+ gpointer *pair;
+
+ pair = g_task_propagate_pointer (G_TASK (result), error);
+
+ if (pair != NULL)
+ {
+ if (stdout_buf != NULL)
+ *stdout_buf = g_steal_pointer (&pair[0]);
+
+ if (stderr_buf != NULL)
+ *stderr_buf = g_steal_pointer (&pair[1]);
+
+ g_free (pair[0]);
+ g_free (pair[1]);
+ g_free (pair);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static void
subprocess_iface_init (IdeSubprocessInterface *iface)
{
iface->get_identifier = ide_simple_subprocess_get_identifier;
@@ -341,6 +410,8 @@ subprocess_iface_init (IdeSubprocessInterface *iface)
iface->communicate_utf8 = ide_simple_subprocess_communicate_utf8;
iface->communicate_async = ide_simple_subprocess_communicate_async;
iface->communicate_finish = ide_simple_subprocess_communicate_finish;
+ iface->communicate_utf8_async = ide_simple_subprocess_communicate_utf8_async;
+ iface->communicate_utf8_finish = ide_simple_subprocess_communicate_utf8_finish;
}
/**
diff --git a/libide/subprocess/ide-subprocess.c b/libide/subprocess/ide-subprocess.c
index c097647..1e23536 100644
--- a/libide/subprocess/ide-subprocess.c
+++ b/libide/subprocess/ide-subprocess.c
@@ -379,29 +379,10 @@ ide_subprocess_communicate_utf8_async (IdeSubprocess *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
- g_autoptr(GBytes) stdin_bytes = NULL;
-
g_return_if_fail (IDE_IS_SUBPROCESS (self));
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
- if (stdin_buf)
- stdin_bytes = g_bytes_new (stdin_buf, strlen (stdin_buf));
-
- ide_subprocess_communicate_async (self, stdin_bytes, cancellable, callback, user_data);
-}
-
-static gchar *
-get_utf8_bytes (GBytes *bytes)
-{
- const gchar *data;
- gsize size;
-
- data = g_bytes_get_data (bytes, &size);
-
- if (size < G_MAXSIZE && g_utf8_validate (data, size, NULL))
- return g_strndup (data, size);
-
- return NULL;
+ WRAP_INTERFACE_METHOD (self, communicate_utf8_async, NULL, stdin_buf, cancellable, callback, user_data);
}
/**
@@ -421,30 +402,10 @@ ide_subprocess_communicate_utf8_finish (IdeSubprocess *self,
gchar **stderr_buf,
GError **error)
{
- g_autoptr(GBytes) stdout_bytes = NULL;
- g_autoptr(GBytes) stderr_bytes = NULL;
-
g_return_val_if_fail (IDE_IS_SUBPROCESS (self), FALSE);
g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
- if (ide_subprocess_communicate_finish (self, result, &stdout_bytes, &stderr_bytes, error))
- {
- if (stdout_bytes && stdout_buf)
- *stdout_buf = get_utf8_bytes (stdout_bytes);
-
- if (stderr_bytes && stderr_buf)
- *stderr_buf = get_utf8_bytes (stderr_bytes);
-
- return TRUE;
- }
-
- if (stdout_buf)
- *stdout_buf = NULL;
-
- if (stderr_buf)
- *stderr_buf = NULL;
-
- return FALSE;
+ return WRAP_INTERFACE_METHOD (self, communicate_utf8_finish, FALSE, result, stdout_buf, stderr_buf, error);
}
gboolean
diff --git a/libide/subprocess/ide-subprocess.h b/libide/subprocess/ide-subprocess.h
index a1a138e..a5bdacd 100644
--- a/libide/subprocess/ide-subprocess.h
+++ b/libide/subprocess/ide-subprocess.h
@@ -76,6 +76,16 @@ struct _IdeSubprocessInterface
GBytes **stdout_buf,
GBytes **stderr_buf,
GError **error);
+ void (*communicate_utf8_async) (IdeSubprocess *self,
+ const gchar *stdin_buf,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+ gboolean (*communicate_utf8_finish) (IdeSubprocess *self,
+ GAsyncResult *result,
+ gchar **stdout_buf,
+ gchar **stderr_buf,
+ GError **error);
};
const gchar *ide_subprocess_get_identifier (IdeSubprocess *self);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]