[ghex/gtk4-port: 84/91] Avoid direct access to hex-document struct




commit d2b211eee9f373409ba29cea8c8316d96b2c65b0
Author: Logan Rathbone <poprocks gmail com>
Date:   Wed Aug 4 17:07:23 2021 -0400

    Avoid direct access to hex-document struct
    
    Preparation for converting it to a modern gobject final type.

 src/common-ui.c               |  5 +++--
 src/converter.c               |  4 +++-
 src/findreplace.c             | 21 ++++++++++++------
 src/ghex-application-window.c | 12 +++++------
 src/ghex-notebook-tab.c       |  3 ++-
 src/gtkhex.c                  |  8 +++----
 src/hex-document.c            | 50 ++++++++++++++++++++++++++++++++-----------
 src/hex-document.h            |  6 +++++-
 src/print.c                   | 21 +++++++++++-------
 9 files changed, 87 insertions(+), 43 deletions(-)
---
diff --git a/src/common-ui.c b/src/common-ui.c
index 740cabbd..a86886b8 100644
--- a/src/common-ui.c
+++ b/src/common-ui.c
@@ -291,15 +291,16 @@ common_print (GtkWindow *parent, GtkHex *gh, gboolean preview)
        HexDocument *doc;
        GtkPrintOperationResult result;
        GError *error = NULL;
-       char *basename;
        char *gtk_file_name;
+       char *basename;
 
        g_return_if_fail (GTK_IS_HEX (gh));
 
        doc = gtk_hex_get_document (gh);
        g_return_if_fail (HEX_IS_DOCUMENT (doc));
 
-       gtk_file_name = g_filename_to_utf8 (doc->file_name, -1, NULL, NULL, NULL);
+       gtk_file_name = g_filename_to_utf8 (hex_document_get_file_name (doc),
+                       -1, NULL, NULL, NULL);
        basename = g_filename_display_basename (gtk_file_name);
 
        job = ghex_print_job_info_new (doc, gtk_hex_get_group_type (gh));
diff --git a/src/converter.c b/src/converter.c
index 76a7b68c..f3eb8211 100644
--- a/src/converter.c
+++ b/src/converter.c
@@ -255,10 +255,12 @@ get_cursor_val_cb(GtkButton *button, Converter *conv)
        guint val, start;
        guint group_type;
        HexDocument *doc;
+       int file_size;
 
        g_return_if_fail (GTK_IS_HEX(conv->gh));
 
        doc = gtk_hex_get_document (conv->gh);
+       file_size = hex_document_get_file_size (doc);
        group_type = gtk_hex_get_group_type (conv->gh);
        start = gtk_hex_get_cursor (conv->gh);
        start = start - start % group_type;
@@ -269,7 +271,7 @@ get_cursor_val_cb(GtkButton *button, Converter *conv)
                val |= gtk_hex_get_byte(conv->gh, start);
                start++;
        } while((start % group_type != 0) &&
-                       (start < doc->file_size) );
+                       (start < file_size) );
 
        set_values(conv, val);
 }
diff --git a/src/findreplace.c b/src/findreplace.c
index 035a6b9d..5a92c201 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -123,12 +123,13 @@ create_hex_view (HexDocument *doc)
     return gh;
 }
 
-static gint get_search_string(HexDocument *doc, gchar **str)
+static int
+get_search_string (HexDocument *doc, char **str)
 {
-       guint size = doc->file_size;
+       int size = hex_document_get_file_size (doc);
        
        if (size > 0)
-               *str = (gchar *)hex_document_get_data(doc, 0, size);
+               *str = hex_document_get_data(doc, 0, size);
        else
                *str = NULL;
 
@@ -364,6 +365,7 @@ goto_byte_cb (GtkButton *button, gpointer user_data)
        gint is_relative = 0;
        gboolean is_hex;
        const gchar *byte_str;
+       int file_size;
        
        (void)button;   /* unused */
 
@@ -378,6 +380,7 @@ goto_byte_cb (GtkButton *button, gpointer user_data)
 
        doc = gtk_hex_get_document(priv->gh);
        cursor_pos = gtk_hex_get_cursor(priv->gh);
+       file_size = hex_document_get_file_size (doc);
 
        entry = GTK_ENTRY(self->int_entry);
        buffer = gtk_entry_get_buffer (entry);
@@ -428,7 +431,7 @@ goto_byte_cb (GtkButton *button, gpointer user_data)
                        }
                        byte = byte * is_relative + cursor_pos;
                }
-               if (byte >= doc->file_size) {
+               if (byte >= file_size) {
                        display_error_dialog(parent,
                                                                 _("Can not position cursor beyond the "
                                                                   "end of file."));
@@ -457,6 +460,7 @@ replace_one_cb (GtkButton *button, gpointer user_data)
        int cursor_pos;
        char *find_str = NULL, *rep_str = NULL;
        int find_len, rep_len, offset;
+       int file_size;
 
        (void)button;   /* unused */
        g_return_if_fail (REPLACE_IS_DIALOG(self));
@@ -474,6 +478,7 @@ replace_one_cb (GtkButton *button, gpointer user_data)
 
        doc = gtk_hex_get_document (priv->gh);
        cursor_pos = gtk_hex_get_cursor (priv->gh);
+       file_size = hex_document_get_file_size (doc);
        
        if ((find_len = get_search_string(f_priv->f_doc, &find_str)) == 0)
        {
@@ -482,7 +487,7 @@ replace_one_cb (GtkButton *button, gpointer user_data)
        }
        rep_len = get_search_string(self->r_doc, &rep_str);
        
-       if (find_len > doc->file_size - cursor_pos)
+       if (find_len > file_size - cursor_pos)
                goto clean_up;
        
        if (hex_document_compare_data(doc, find_str, cursor_pos, find_len) == 0)
@@ -520,6 +525,7 @@ replace_all_cb (GtkButton *button, gpointer user_data)
        int cursor_pos;
        char *find_str = NULL, *rep_str = NULL;
        int find_len, rep_len, offset, count;
+       int file_size;
 
        (void)button;   /* unused */
        g_return_if_fail (REPLACE_IS_DIALOG (self));
@@ -536,6 +542,7 @@ replace_all_cb (GtkButton *button, gpointer user_data)
 
        doc = gtk_hex_get_document (priv->gh);
        cursor_pos = gtk_hex_get_cursor (priv->gh);
+       file_size = hex_document_get_file_size (doc);
 
        if ((find_len = get_search_string(f_priv->f_doc, &find_str)) == 0)
        {
@@ -544,7 +551,7 @@ replace_all_cb (GtkButton *button, gpointer user_data)
        }
        rep_len = get_search_string(self->r_doc, &rep_str);
 
-       if (find_len > doc->file_size - cursor_pos)
+       if (find_len > file_size - cursor_pos)
                goto clean_up;
        
        count = 0;
@@ -557,7 +564,7 @@ replace_all_cb (GtkButton *button, gpointer user_data)
                count++;
        }
        
-       gtk_hex_set_cursor(priv->gh, MIN(offset, doc->file_size));  
+       gtk_hex_set_cursor(priv->gh, MIN(offset, file_size));  
 
        if (count == 0) {
                display_info_dialog (parent, _("No occurrences were found."));
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index b8acec26..027192c7 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -494,7 +494,7 @@ close_doc_confirmation_dialog (GHexApplicationWindow *self)
                         * edited. */
                        _("<big><b>%s has been edited since opening.</b></big>\n\n"
                           "Would you like to save your changes?"),
-                       doc->path_end);
+                       hex_document_get_basename (doc));
 
        gtk_dialog_add_buttons (GTK_DIALOG(dialog),
                        _("_Save Changes"),             GTK_RESPONSE_ACCEPT,
@@ -622,7 +622,7 @@ assess_can_save (HexDocument *doc)
        gboolean can_save = FALSE;
 
        /* Can't save if we have a new document that is still untitled. */
-       if (doc->file_name)
+       if (hex_document_get_file_name (doc))
                can_save = hex_document_has_changed (doc);
 
        return can_save;
@@ -1012,7 +1012,7 @@ save_as_response_cb (GtkNativeDialog *dialog,
                                        "of a bug or programer error. Please file a bug report.",
                                        __func__);
                }
-               gtk_file_name = g_filename_to_utf8 (doc->file_name,
+               gtk_file_name = g_filename_to_utf8 (hex_document_get_file_name (doc),
                                -1, NULL, NULL, NULL);
 
                g_free(gtk_file_name);
@@ -1047,7 +1047,7 @@ save_as (GtkWidget *widget,
        doc = gtk_hex_get_document (self->gh);
        g_return_if_fail (HEX_IS_DOCUMENT (doc));
 
-       existing_file = g_file_new_for_path (doc->file_name);
+       existing_file = g_file_new_for_path (hex_document_get_file_name (doc));
 
        file_sel =
                gtk_file_chooser_native_new (_("Select a file to save buffer as"),
@@ -1083,7 +1083,7 @@ revert_response_cb (GtkDialog *dialog,
                goto end;
 
        doc = gtk_hex_get_document (self->gh);
-       gtk_file_name = g_filename_to_utf8 (doc->file_name,
+       gtk_file_name = g_filename_to_utf8 (hex_document_get_file_name (doc),
                        -1, NULL, NULL, NULL);
 
        hex_document_read (doc);
@@ -1122,7 +1122,7 @@ revert (GtkWidget *widget,
                        _("<big><b>Are you sure you want to revert %s?</b></big>\n\n"
                        "Your changes will be lost.\n\n"
                        "This action cannot be undone."),
-                       doc->path_end);
+                       hex_document_get_basename (doc));
 
        gtk_dialog_add_buttons (GTK_DIALOG(dialog),
                        _("_Revert"), GTK_RESPONSE_ACCEPT,
diff --git a/src/ghex-notebook-tab.c b/src/ghex-notebook-tab.c
index d6f93e4b..0a7560a9 100644
--- a/src/ghex-notebook-tab.c
+++ b/src/ghex-notebook-tab.c
@@ -188,7 +188,8 @@ refresh_file_name (GHexNotebookTab *self)
 
        doc = gtk_hex_get_document (self->gh);
 
-       gtk_label_set_markup (GTK_LABEL(self->label), doc->path_end);
+       gtk_label_set_markup (GTK_LABEL(self->label),
+                       hex_document_get_basename (doc));
        tab_bold_label (self, hex_document_has_changed (doc));
 }
 
diff --git a/src/gtkhex.c b/src/gtkhex.c
index 434fdfa8..17241dda 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -287,10 +287,10 @@ redo_action (GtkWidget *widget,
        /* shorthand. */
        doc = gh->document;
 
-       if (doc->undo_stack && doc->undo_top != doc->undo_stack) {
+       if (hex_document_can_redo (doc)) {
                hex_document_redo(doc);
 
-               cd = doc->undo_top->data;
+               cd = hex_document_get_undo_data (doc);
 
                gtk_hex_set_cursor(gh, cd->start);
                gtk_hex_set_nibble(gh, cd->lower_nibble);
@@ -326,8 +326,8 @@ undo_action (GtkWidget *widget,
        /* shorthand */
        doc = gh->document;
 
-       if (doc->undo_top) {
-               cd = doc->undo_top->data;
+       if (hex_document_can_undo (doc)) {
+               cd = hex_document_get_undo_data (doc);
 
                hex_document_undo(doc);
 
diff --git a/src/hex-document.c b/src/hex-document.c
index 8768d107..075686bc 100644
--- a/src/hex-document.c
+++ b/src/hex-document.c
@@ -302,8 +302,8 @@ hex_document_finalize(GObject *obj)
        if(hex->file_name)
                g_free(hex->file_name);
 
-       if(hex->path_end)
-               g_free(hex->path_end);
+       if(hex->basename)
+               g_free(hex->basename);
 
        undo_stack_free(hex);
 
@@ -459,7 +459,7 @@ hex_document_new()
        doc->buffer_size = doc->file_size + doc->gap_size;
        doc->gap_pos = doc->buffer = g_malloc(doc->buffer_size);
 
-       doc->path_end = g_strdup(_("New document"));
+       doc->basename = g_strdup(_("New document"));
 
        doc_list = g_list_append(doc_list, doc);
        return doc;
@@ -469,7 +469,7 @@ HexDocument *
 hex_document_new_from_file(const gchar *name)
 {
        HexDocument *doc;
-       gchar *path_end;
+       char *basename;
 
        doc = HEX_DOCUMENT (g_object_new (hex_document_get_type(), NULL));
        g_return_val_if_fail (doc != NULL, NULL);
@@ -481,9 +481,9 @@ hex_document_new_from_file(const gchar *name)
                doc->buffer = g_malloc(doc->buffer_size);
 
                /* find the start of the filename without path */
-               path_end = g_path_get_basename (doc->file_name);
-               doc->path_end = g_filename_to_utf8 (path_end, -1, NULL, NULL, NULL);
-               g_free (path_end);
+               basename = g_path_get_basename (doc->file_name);
+               doc->basename = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
+               g_free (basename);
 
                if(hex_document_read(doc)) {
                        doc_list = g_list_append(doc_list, doc);
@@ -812,7 +812,7 @@ hex_document_export_html (HexDocument *doc, char *html_path, char *base_name,
        fprintf(file, "<CENTER>");
        fprintf(file, "<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">\n");
        fprintf(file, "<TR>\n<TD COLSPAN=\"3\"><B>%s</B></TD>\n</TR>\n",
-                       doc->file_name?doc->file_name:doc->path_end);
+                       doc->file_name?doc->file_name:doc->basename);
        fprintf(file, "<TR>\n<TD COLSPAN=\"3\">&nbsp;</TD>\n</TR>\n");
        for(page = 0; page < pages; page++) {
                fprintf(file, "<TR>\n<TD>\n<A HREF=\"%s%08d.html\"><PRE>", base_name, page);
@@ -884,7 +884,7 @@ hex_document_export_html (HexDocument *doc, char *html_path, char *base_name,
                fprintf(file, "\n</TD>\n");
                fprintf(file, "<TD WIDTH=\"33%%\" ALIGN=\"CENTER\">\n");
                fprintf(file, "<A HREF=\"%s.html\">", base_name);
-               fprintf(file, "%s:", doc->path_end);
+               fprintf(file, "%s:", doc->basename);
                fprintf(file, "</A>");
                fprintf(file, " %d/%d", page+1, pages);
                fprintf(file, "\n</TD>\n");
@@ -1145,19 +1145,19 @@ hex_document_change_file_name (HexDocument *doc, const char *new_file_name)
 
        if(doc->file_name)
                g_free(doc->file_name);
-       if(doc->path_end)
-               g_free(doc->path_end);
+       if(doc->basename)
+               g_free(doc->basename);
 
        doc->file_name = g_strdup(new_file_name);
        doc->changed = FALSE;
 
        new_path_end = g_path_get_basename (doc->file_name);
-       doc->path_end = g_filename_to_utf8 (new_path_end, -1, NULL, NULL, NULL);
+       doc->basename = g_filename_to_utf8 (new_path_end, -1, NULL, NULL, NULL);
 
        if (new_path_end)
                g_free (new_path_end);
 
-       if (doc->file_name && doc->path_end) {
+       if (doc->file_name && doc->basename) {
                g_signal_emit (G_OBJECT(doc), hex_signals[FILE_NAME_CHANGED], 0);
                return TRUE;
        } else {
@@ -1186,3 +1186,27 @@ hex_document_can_redo (HexDocument *doc)
        else
                return FALSE;
 }
+
+const char *
+hex_document_get_file_name (HexDocument *doc)
+{
+       return doc->file_name;
+}
+
+const char *
+hex_document_get_basename (HexDocument *doc)
+{
+       return doc->basename;
+}
+
+int
+hex_document_get_file_size (HexDocument *doc)
+{
+       return doc->file_size;
+}
+
+HexChangeData *
+hex_document_get_undo_data (HexDocument *doc)
+{
+       return doc->undo_top->data;
+}
diff --git a/src/hex-document.h b/src/hex-document.h
index 7e7107ba..f86334db 100644
--- a/src/hex-document.h
+++ b/src/hex-document.h
@@ -73,7 +73,7 @@ struct _HexDocument
        GList *views;      /* a list of GtkHex widgets showing this document */
        
        char *file_name;
-       char *path_end;
+       char *basename;
 
        char *buffer;    /* data buffer */
        char *gap_pos;   /* pointer to the start of insertion gap */
@@ -139,6 +139,10 @@ gboolean    hex_document_change_file_name (HexDocument *doc,
                const char *new_file_name);
 gboolean    hex_document_can_undo (HexDocument *doc);
 gboolean    hex_document_can_redo (HexDocument *doc);
+const char     *hex_document_get_file_name (HexDocument *doc);
+const char     *hex_document_get_basename (HexDocument *doc);
+int                    hex_document_get_file_size (HexDocument *doc);
+HexChangeData *hex_document_get_undo_data (HexDocument *doc);
 
 G_END_DECLS
 
diff --git a/src/print.c b/src/print.c
index 37505d95..557dd0fe 100644
--- a/src/print.c
+++ b/src/print.c
@@ -46,8 +46,10 @@ static void print_shaded_box( GHexPrintJobInfo *pji, guint row, guint rows);
 static void print_header(GHexPrintJobInfo *pji, unsigned int page)
 {
        PangoLayout *layout;
+
+       const char *file_name = hex_document_get_file_name (pji->doc);
        cairo_t *cr = gtk_print_context_get_cairo_context (pji->pc);
-       char *text1 = g_filename_to_utf8 (pji->doc->file_name, -1, NULL,
+       char *text1 = g_filename_to_utf8 (file_name, -1, NULL,
                                                                           NULL, NULL);
        char *text2 = g_strdup_printf (_("Page: %i/%i"), page, pji->pages);
        char *pagetext = g_strdup_printf ("%d", page);
@@ -290,6 +292,7 @@ begin_print (GtkPrintOperation *operation,
     pji->pc = context;
     int font_width, font_height;
     int printable_width, printable_height;
+       int file_size = hex_document_get_file_size (pji->doc);
 
     layout = gtk_print_context_create_pango_layout (context);
     pango_layout_set_text (layout, " ", -1);
@@ -318,7 +321,7 @@ begin_print (GtkPrintOperation *operation,
     pji->bytes_per_row *= pji->gt;
     pji->rows_per_page = (printable_height - pji->header_height) /
                           pji->font_char_height - 2;
-    pji->pages = (((pji->doc->file_size/pji->bytes_per_row) + 1)/
+    pji->pages = (((file_size/pji->bytes_per_row) + 1)/
                    pji->rows_per_page) + 1;
     gtk_print_operation_set_n_pages (pji->master, pji->pages);
 }
@@ -329,7 +332,7 @@ print_page (GtkPrintOperation *operation,
             int               page_nr,
             gpointer           data)
 {
-       int j, max_row;
+       int j, max_row, file_size;
 
        GHexPrintJobInfo *pji = (GHexPrintJobInfo *)data;
        g_return_if_fail(pji != NULL);
@@ -337,10 +340,12 @@ print_page (GtkPrintOperation *operation,
        pji->pc = context;
        g_return_if_fail(pji->pc != NULL);
 
+       file_size = hex_document_get_file_size (pji->doc);
+
        print_header (pji, page_nr+1);
        max_row = (pji->bytes_per_row*pji->rows_per_page*(page_nr+1) >
-                       pji->doc->file_size ?
-                       (int)((pji->doc->file_size-1)-
+                       file_size ?
+                       (int)((file_size-1)-
                              (pji->bytes_per_row *
                               pji->rows_per_page*(page_nr))) /
                               pji->bytes_per_row + 1:
@@ -350,10 +355,10 @@ print_page (GtkPrintOperation *operation,
                int file_offset = pji->bytes_per_row*(j - 1) +
                        pji->bytes_per_row*pji->rows_per_page*(page_nr);
                int length = (file_offset + pji->bytes_per_row >
-                       pji->doc->file_size ?
-                       pji->doc->file_size - file_offset :
+                       file_size ?
+                       file_size - file_offset :
                        pji->bytes_per_row);
-               if (file_offset >= pji->doc->file_size)
+               if (file_offset >= file_size)
                        break;
                print_row (pji, file_offset, length, j);
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]