[ghex/expand-search-options: 12/14] findrep: Replace all with expanded search options
- From: Logan Rathbone <larathbone src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ghex/expand-search-options: 12/14] findrep: Replace all with expanded search options
- Date: Tue, 19 Apr 2022 22:04:08 +0000 (UTC)
commit 5597fc8b626b84a8a16c95bcde87c83bc9e27972
Author: Logan Rathbone <poprocks gmail com>
Date: Tue Apr 19 03:39:04 2022 -0400
findrep: Replace all with expanded search options
src/findreplace.c | 80 +++++++++++++++++++++++++++----------------------------
1 file changed, 40 insertions(+), 40 deletions(-)
---
diff --git a/src/findreplace.c b/src/findreplace.c
index 41d4536..26054ee 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -302,7 +302,6 @@ find_common (FindDialog *self, enum FindDirection direction,
HexDocument *doc;
gint64 cursor_pos;
gint64 str_len;
- gint64 offset;
char *str;
HexDocumentFindData *find_data;
@@ -527,19 +526,14 @@ replace_one_cb (GtkButton *button, gpointer user_data)
HexDocument *doc;
gint64 cursor_pos;
char *find_str = NULL, *rep_str = NULL;
- size_t find_len, rep_len;
- gint64 offset;
+ size_t find_len, rep_str_len;
gint64 payload;
+ HexDocumentFindData *find_data = NULL;
- (void)button; /* unused */
g_return_if_fail (REPLACE_IS_DIALOG(self));
priv = pane_dialog_get_instance_private (PANE_DIALOG(self));
- g_return_if_fail (HEX_IS_WIDGET (priv->gh));
-
f_priv = find_dialog_get_instance_private (FIND_DIALOG(self));
- g_return_if_fail (HEX_IS_DOCUMENT (f_priv->f_doc));
- g_return_if_fail (HEX_IS_DOCUMENT (self->r_doc));
parent = GTK_WINDOW(gtk_widget_get_native (widget));
if (! GTK_IS_WINDOW(parent))
@@ -549,34 +543,38 @@ replace_one_cb (GtkButton *button, gpointer user_data)
cursor_pos = hex_widget_get_cursor (priv->gh);
payload = hex_buffer_get_payload_size (hex_document_get_buffer (doc));
- if ((find_len = get_search_string(f_priv->f_doc, &find_str)) == 0)
+ if ((find_len = get_search_string (f_priv->f_doc, &find_str)) == 0 ||
+ (rep_str_len = get_search_string (self->r_doc, &rep_str)) == 0)
{
no_string_dialog (parent);
- return;
+ goto clean_up;
}
- rep_len = get_search_string (self->r_doc, &rep_str);
if (find_len > payload - cursor_pos)
goto clean_up;
- if (hex_document_compare_data(doc, find_str, cursor_pos, find_len) == 0)
+ find_data = g_new0 (HexDocumentFindData, 1);
+ find_data->start = cursor_pos;
+ find_data->what = find_str;
+ find_data->len = find_len;
+ find_data->flags = search_flags_from_checkboxes (f_priv);
+
+ if (hex_document_find_forward_full (doc, find_data))
{
- hex_document_set_data(doc, cursor_pos,
- rep_len, find_len, rep_str, TRUE);
+ hex_widget_set_cursor (priv->gh, find_data->offset);
+ cursor_pos = hex_widget_get_cursor (priv->gh);
+ hex_document_set_data (doc,
+ cursor_pos, rep_str_len, find_data->found_len, rep_str, TRUE);
}
-
- if (hex_document_find_forward(doc, cursor_pos + rep_len,
- find_str, find_len, &offset))
+ else
{
- hex_widget_set_cursor(priv->gh, offset);
- }
- else {
display_info_dialog (parent, _("String was not found."));
}
clean_up:
g_free (find_str);
g_free (rep_str);
+ g_free (find_data);
}
static void
@@ -590,19 +588,15 @@ replace_all_cb (GtkButton *button, gpointer user_data)
HexDocument *doc;
gint64 cursor_pos;
char *find_str = NULL, *rep_str = NULL;
- size_t find_len, rep_len;
- int count;
- gint64 offset;
+ size_t find_len, rep_str_len;
gint64 payload;
+ HexDocumentFindData *find_data = NULL;
+ int count;
- (void)button; /* unused */
g_return_if_fail (REPLACE_IS_DIALOG (self));
priv = pane_dialog_get_instance_private (PANE_DIALOG(self));
- g_return_if_fail (HEX_IS_WIDGET (priv->gh));
f_priv = find_dialog_get_instance_private (FIND_DIALOG(self));
- g_return_if_fail (HEX_IS_DOCUMENT (f_priv->f_doc));
- g_return_if_fail (HEX_IS_DOCUMENT (self->r_doc));
parent = GTK_WINDOW(gtk_widget_get_native (widget));
if (! GTK_IS_WINDOW(parent))
@@ -612,35 +606,41 @@ replace_all_cb (GtkButton *button, gpointer user_data)
cursor_pos = hex_widget_get_cursor (priv->gh);
payload = hex_buffer_get_payload_size (hex_document_get_buffer (doc));
- if ((find_len = get_search_string(f_priv->f_doc, &find_str)) == 0)
+ if ((find_len = get_search_string (f_priv->f_doc, &find_str)) == 0 ||
+ (rep_str_len = get_search_string (self->r_doc, &rep_str)) == 0)
{
no_string_dialog (parent);
- return;
+ goto clean_up;
}
- rep_len = get_search_string(self->r_doc, &rep_str);
- if (find_len > payload - (unsigned)cursor_pos)
+ if (find_len > payload - cursor_pos)
goto clean_up;
+
+ find_data = g_new0 (HexDocumentFindData, 1);
+ find_data->start = 0;
+ find_data->what = find_str;
+ find_data->len = find_len;
+ find_data->flags = search_flags_from_checkboxes (f_priv);
+
count = 0;
- cursor_pos = 0;
+ while (hex_document_find_forward_full (doc, find_data))
+ {
- while(hex_document_find_forward(doc, cursor_pos, find_str, find_len,
- &offset)) {
- hex_document_set_data (doc, offset, rep_len, find_len, rep_str, TRUE);
- cursor_pos = offset + rep_len;
+ hex_document_set_data (doc,
+ find_data->offset, rep_str_len, find_data->found_len, rep_str,
+ TRUE);
count++;
}
- hex_widget_set_cursor(priv->gh, MIN(offset, payload));
-
if (count == 0) {
display_info_dialog (parent, _("No occurrences were found."));
}
clean_up:
- g_free(find_str);
- g_free(rep_str);
+ g_free (find_str);
+ g_free (rep_str);
+ g_free (find_data);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]