[ghex] hex-doc/find-repl: async API
- From: Logan Rathbone <larathbone src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ghex] hex-doc/find-repl: async API
- Date: Tue, 21 Dec 2021 06:17:21 +0000 (UTC)
commit bc0010e3b552bd53554790de75811aa8e541a9d0
Author: Logan Rathbone <poprocks gmail com>
Date: Tue Dec 21 01:16:26 2021 -0500
hex-doc/find-repl: async API
src/findreplace.c | 74 +++++++++++++++++++++++++++++++++++++-
src/hex-document.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++---
src/hex-document.h | 31 ++++++++++++++--
3 files changed, 200 insertions(+), 9 deletions(-)
---
diff --git a/src/findreplace.c b/src/findreplace.c
index da9c423..bfc4867 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -69,6 +69,7 @@ typedef struct {
GtkWidget *hbox;
GtkWidget *f_next, *f_prev, *f_clear;
GtkWidget *close;
+ gboolean found;
} FindDialogPrivate;
@@ -176,6 +177,45 @@ no_string_dialog (GtkWindow *parent)
_("No string provided."));
}
+static void
+find_ready_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ HexDocument *doc = HEX_DOCUMENT (source_object);
+ FindDialog *self = FIND_DIALOG (user_data);
+ HexDocumentFindData *find_data;
+ PaneDialogPrivate *priv = pane_dialog_get_instance_private (PANE_DIALOG(self));
+ FindDialogPrivate *f_priv = find_dialog_get_instance_private (self);
+ GtkWindow *parent = GTK_WINDOW(gtk_widget_get_native (GTK_WIDGET(self)));
+
+ find_data = hex_document_find_finish (doc, res);
+
+ if (find_data->found)
+ {
+ f_priv->found = TRUE;
+ gtk_hex_set_cursor (priv->gh, find_data->offset);
+
+ /* If string found, insert auto-highlights of search string */
+
+ if (priv->auto_highlight)
+ gtk_hex_delete_autohighlight (priv->gh, priv->auto_highlight);
+
+ priv->auto_highlight = NULL;
+ priv->auto_highlight = gtk_hex_insert_autohighlight (priv->gh,
+ find_data->what, find_data->len);
+
+ gtk_widget_grab_focus (GTK_WIDGET(priv->gh));
+ }
+ else
+ {
+ display_info_dialog (parent,
+ f_priv->found ? find_data->found_msg : find_data->not_found_msg);
+
+ f_priv->found = FALSE;
+ }
+}
+
static void
find_common (FindDialog *self, enum FindDirection direction,
const char *found_msg, const char *not_found_msg)
@@ -189,7 +229,6 @@ find_common (FindDialog *self, enum FindDirection direction,
gint64 str_len;
gint64 offset;
char *str;
- static gboolean found = FALSE;
g_return_if_fail (FIND_IS_DIALOG(self));
@@ -212,6 +251,35 @@ find_common (FindDialog *self, enum FindDirection direction,
/* Search for requested string */
+ if (direction == FIND_FORWARD)
+ {
+ hex_document_find_forward_async (doc,
+ f_priv->found == FALSE ? cursor_pos : cursor_pos + 1,
+ str,
+ str_len,
+ &offset,
+ found_msg,
+ not_found_msg,
+ NULL, /* cancellable */
+ find_ready_cb,
+ self);
+ }
+ else /* FIND_BACKWARD */
+ {
+ hex_document_find_backward_async (doc,
+ cursor_pos,
+ str,
+ str_len,
+ &offset,
+ found_msg,
+ not_found_msg,
+ NULL,
+ find_ready_cb,
+ self);
+ }
+
+
+#if 0
if (direction == FIND_FORWARD
?
hex_document_find_forward (doc,
@@ -222,6 +290,9 @@ find_common (FindDialog *self, enum FindDirection direction,
hex_document_find_backward (doc,
cursor_pos, str, str_len, &offset)
)
+#endif
+
+#if 0
{
found = TRUE;
gtk_hex_set_cursor (priv->gh, offset);
@@ -243,6 +314,7 @@ find_common (FindDialog *self, enum FindDirection direction,
found = FALSE;
}
+#endif
}
static void
diff --git a/src/hex-document.c b/src/hex-document.c
index 24c9885..16d0c18 100644
--- a/src/hex-document.c
+++ b/src/hex-document.c
@@ -77,7 +77,6 @@ enum {
static guint hex_signals[LAST_SIGNAL];
-
/* GOBJECT DEFINITION */
struct _HexDocument
@@ -776,7 +775,7 @@ hex_document_compare_data (HexDocument *doc,
gboolean
hex_document_find_forward (HexDocument *doc, gint64 start, char *what,
- size_t len, gint64 *found)
+ size_t len, gint64 *offset)
{
gint64 pos;
gint64 payload = hex_buffer_get_payload_size (
@@ -787,7 +786,7 @@ hex_document_find_forward (HexDocument *doc, gint64 start, char *what,
{
if (hex_document_compare_data (doc, what, pos, len) == 0)
{
- *found = pos;
+ *offset = pos;
return TRUE;
}
pos++;
@@ -796,9 +795,61 @@ hex_document_find_forward (HexDocument *doc, gint64 start, char *what,
return FALSE;
}
+HexDocumentFindData *
+hex_document_find_finish (HexDocument *doc,
+ GAsyncResult *result)
+{
+ g_return_val_if_fail (g_task_is_valid (result, G_OBJECT(doc)), FALSE);
+
+ return g_task_propagate_pointer (G_TASK(result), NULL);
+}
+
+static void
+hex_document_find_forward_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ HexDocument *doc = HEX_DOCUMENT (source_object);
+ HexDocumentFindData *find_data = task_data;
+
+ find_data->found = hex_document_find_forward (doc,
+ find_data->start, find_data->what,
+ find_data->len, &find_data->offset);
+
+ g_task_return_pointer (task, find_data, g_free);
+}
+
+void
+hex_document_find_forward_async (HexDocument *doc,
+ gint64 start,
+ char *what,
+ size_t len,
+ gint64 *offset,
+ const char *found_msg,
+ const char *not_found_msg,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GTask *task;
+ HexDocumentFindData *find_data = g_new0 (HexDocumentFindData, 1);
+
+ find_data->start = start;
+ find_data->what = what;
+ find_data->len = len;
+ find_data->found_msg = found_msg;
+ find_data->not_found_msg = not_found_msg;
+
+ task = g_task_new (doc, cancellable, callback, user_data);
+ g_task_set_task_data (task, find_data, g_free);
+ g_task_run_in_thread (task, hex_document_find_forward_thread);
+ g_object_unref (task); /* _run_in_thread takes a ref */
+}
+
gboolean
hex_document_find_backward (HexDocument *doc, gint64 start, char *what,
- size_t len, gint64 *found)
+ size_t len, gint64 *offset)
{
gint64 pos = start;
@@ -808,7 +859,7 @@ hex_document_find_backward (HexDocument *doc, gint64 start, char *what,
do {
pos--;
if (hex_document_compare_data (doc, what, pos, len) == 0) {
- *found = pos;
+ *offset = pos;
return TRUE;
}
} while (pos > 0);
@@ -816,6 +867,49 @@ hex_document_find_backward (HexDocument *doc, gint64 start, char *what,
return FALSE;
}
+static void
+hex_document_find_backward_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ HexDocument *doc = HEX_DOCUMENT (source_object);
+ HexDocumentFindData *find_data = task_data;
+
+ find_data->found = hex_document_find_backward (doc,
+ find_data->start, find_data->what,
+ find_data->len, &find_data->offset);
+
+ g_task_return_pointer (task, find_data, g_free);
+}
+
+void
+hex_document_find_backward_async (HexDocument *doc,
+ gint64 start,
+ char *what,
+ size_t len,
+ gint64 *offset,
+ const char *found_msg,
+ const char *not_found_msg,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GTask *task;
+ HexDocumentFindData *find_data = g_new0 (HexDocumentFindData, 1);
+
+ find_data->start = start;
+ find_data->what = what;
+ find_data->len = len;
+ find_data->found_msg = found_msg;
+ find_data->not_found_msg = not_found_msg;
+
+ task = g_task_new (doc, cancellable, callback, user_data);
+ g_task_set_task_data (task, find_data, g_free);
+ g_task_run_in_thread (task, hex_document_find_backward_thread);
+ g_object_unref (task); /* _run_in_thread takes a ref */
+}
+
gboolean
hex_document_undo (HexDocument *doc)
{
diff --git a/src/hex-document.h b/src/hex-document.h
index 0226ffc..b8587e7 100644
--- a/src/hex-document.h
+++ b/src/hex-document.h
@@ -50,6 +50,17 @@ typedef enum
HEX_CHANGE_BYTE
} HexChangeType;
+typedef struct
+{
+ gboolean found;
+ gint64 start;
+ char *what;
+ size_t len;
+ gint64 offset;
+ const char *found_msg;
+ const char *not_found_msg;
+} HexDocumentFindData;
+
typedef struct _HexChangeData HexChangeData;
struct _HexChangeData
{
@@ -75,7 +86,7 @@ void hex_document_set_nibble (HexDocument *doc, char val, gint64 offset,
gboolean lower_nibble, gboolean insert, gboolean undoable);
void hex_document_delete_data (HexDocument *doc, gint64 offset,
size_t len, gboolean undoable);
-//void hex_document_read (HexDocument *doc);
+/* TODO - Reimplement void hex_document_read (HexDocument *doc); */
void hex_document_read_async (HexDocument *doc, GCancellable *cancellable,
GAsyncReadyCallback callback, gpointer user_data);
@@ -96,9 +107,23 @@ gboolean hex_document_redo (HexDocument *doc);
int hex_document_compare_data (HexDocument *doc, char *what,
gint64 pos, size_t len);
gboolean hex_document_find_forward (HexDocument *doc, gint64 start,
- char *what, size_t len, gint64 *found);
+ char *what, size_t len, gint64 *offset);
+
+void hex_document_find_forward_async (HexDocument *doc, gint64 start,
+ char *what, size_t len, gint64 *offset, const char *found_msg,
+ const char *not_found_msg, GCancellable *cancellable,
+ GAsyncReadyCallback callback, gpointer user_data);
+
gboolean hex_document_find_backward (HexDocument *doc, gint64 start,
- char *what, size_t len, gint64 *found);
+ char *what, size_t len, gint64 *offset);
+void hex_document_find_backward_async (HexDocument *doc, gint64 start,
+ char *what, size_t len, gint64 *offset, const char *found_msg,
+ const char *not_found_msg, GCancellable *cancellable,
+ GAsyncReadyCallback callback, gpointer user_data);
+
+HexDocumentFindData *
+hex_document_find_finish (HexDocument *doc, GAsyncResult *result);
+
gboolean hex_document_can_undo (HexDocument *doc);
gboolean hex_document_can_redo (HexDocument *doc);
gint64 hex_document_get_file_size (HexDocument *doc);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]