[ghex] Make some raw structs into boxed types for GI



commit 23568971b556702ad3ad03b6e1ceecb6c7536904
Author: Logan Rathbone <poprocks gmail com>
Date:   Tue Dec 28 01:00:55 2021 -0500

    Make some raw structs into boxed types for GI

 src/findreplace.c  |  2 +-
 src/gtkhex.c       | 76 ++++++++++++++++++++++++++++++++++++++++++------------
 src/gtkhex.h       | 12 ++++++---
 src/hex-document.c | 48 +++++++++++++++++++++++++++++-----
 src/hex-document.h | 25 +++++++++++-------
 5 files changed, 126 insertions(+), 37 deletions(-)
---
diff --git a/src/findreplace.c b/src/findreplace.c
index 758defc..1831561 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -55,7 +55,7 @@ static guint signals[LAST_SIGNAL];
 typedef struct
 {
        HexWidget *gh;
-       HexWidget_AutoHighlight *auto_highlight;
+       HexWidgetAutoHighlight *auto_highlight;
 
 } PaneDialogPrivate;
 
diff --git a/src/gtkhex.c b/src/gtkhex.c
index 3170f9e..c574087 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -97,12 +97,12 @@ struct _HexWidget_Highlight
 };
 
 /**
- * HexWidget_AutoHighlight:
+ * HexWidgetAutoHighlight:
  *
  * A structure used to automatically highlight all visible occurrences
  * of a given string.
  */
-struct _HexWidget_AutoHighlight
+struct _HexWidgetAutoHighlight
 {
        char *search_string;
        int search_len;
@@ -111,9 +111,41 @@ struct _HexWidget_AutoHighlight
        gint64 view_max;
 
        HexWidget_Highlight *highlights;
-       HexWidget_AutoHighlight *next, *prev;
+       HexWidgetAutoHighlight *next, *prev;
 };
 
+HexWidgetAutoHighlight *
+hex_widget_autohighlight_new (void)
+{
+       return g_slice_new0 (HexWidgetAutoHighlight);
+}
+
+void
+hex_widget_autohighlight_free (HexWidgetAutoHighlight *ahl)
+{
+       g_free (ahl->search_string);
+       g_free (ahl);
+}
+
+HexWidgetAutoHighlight *
+hex_widget_autohighlight_copy (HexWidgetAutoHighlight *ahl)
+{
+       HexWidgetAutoHighlight *new = g_slice_new0 (HexWidgetAutoHighlight);
+
+       new->search_string = g_strdup (ahl->search_string);
+       new->search_len = ahl->search_len;
+       new->view_min = ahl->view_min;
+       new->view_max = ahl->view_max;
+       new->highlights = ahl->highlights;
+       new->next = ahl->next;
+       new->prev = ahl->prev;
+
+       return new;
+}
+
+G_DEFINE_BOXED_TYPE (HexWidgetAutoHighlight, hex_widget_autohighlight,
+               hex_widget_autohighlight_copy, hex_widget_autohighlight_free)
+
 
 /* ------------------------------
  * Main HexWidget GObject definition
@@ -177,7 +209,7 @@ struct _HexWidget
        /* width of the hex display `xdisp` and ascii display `adisp` */
        int xdisp_width, adisp_width;
 
-       HexWidget_AutoHighlight *auto_highlight;
+       HexWidgetAutoHighlight *auto_highlight;
 
        /* scroll direction: 0 means no scrolling; a -ve number means we're
         * scrolling up, and a +ve number means we're scrolling down. */
@@ -219,12 +251,12 @@ static void hex_widget_invalidate_all_highlights (HexWidget *self);
 static void hex_widget_update_all_auto_highlights (HexWidget *self);
 
 static HexWidget_Highlight *hex_widget_insert_highlight (HexWidget *self,
-               HexWidget_AutoHighlight *ahl, gint64 start, gint64 end);
+               HexWidgetAutoHighlight *ahl, gint64 start, gint64 end);
 
-static void hex_widget_delete_highlight (HexWidget *self, HexWidget_AutoHighlight *ahl,
+static void hex_widget_delete_highlight (HexWidget *self, HexWidgetAutoHighlight *ahl,
                HexWidget_Highlight *hl);
 
-static void hex_widget_update_auto_highlight (HexWidget *self, HexWidget_AutoHighlight *ahl,
+static void hex_widget_update_auto_highlight (HexWidget *self, HexWidgetAutoHighlight *ahl,
                gboolean delete, gboolean add);
 
 static void recalc_displays (HexWidget *self);
@@ -752,7 +784,7 @@ render_highlights (HexWidget *self,
 {
        /* Shorthand tracers to walk through highlight lists */
        HexWidget_Highlight *highlight = &self->selection;
-       HexWidget_AutoHighlight *auto_highlight = self->auto_highlight;
+       HexWidgetAutoHighlight *auto_highlight = self->auto_highlight;
        GtkWidget *widget;              /* shorthand for the hex or ascii drawing area */
        PangoLayout *layout;    /* shorthand for the hex or ascii pango layout */
        GtkStyleContext *context;
@@ -1906,7 +1938,7 @@ static void
 hex_widget_invalidate_all_highlights (HexWidget *self)
 {
        HexWidget_Highlight *cur = &self->selection;
-       HexWidget_AutoHighlight *nextList = self->auto_highlight;
+       HexWidgetAutoHighlight *nextList = self->auto_highlight;
 
        while (cur)
        {
@@ -1923,7 +1955,7 @@ hex_widget_invalidate_all_highlights (HexWidget *self)
 
 static HexWidget_Highlight *
 hex_widget_insert_highlight (HexWidget *self,
-               HexWidget_AutoHighlight *ahl,
+               HexWidgetAutoHighlight *ahl,
                gint64 start,
                gint64 end)
 {
@@ -1950,7 +1982,7 @@ hex_widget_insert_highlight (HexWidget *self,
 }
 
 static void
-hex_widget_delete_highlight (HexWidget *self, HexWidget_AutoHighlight *ahl,
+hex_widget_delete_highlight (HexWidget *self, HexWidgetAutoHighlight *ahl,
                HexWidget_Highlight *hl)
 {
        gint64 start, end;
@@ -2005,14 +2037,14 @@ hex_widget_find_limited (HexWidget *self, char *find, int findlen,
  * view_min () and view_max () value.
  */
 static inline void
-ahl_set_view_min_max (HexWidget *self, HexWidget_AutoHighlight *ahl)
+ahl_set_view_min_max (HexWidget *self, HexWidgetAutoHighlight *ahl)
 {
        ahl->view_min = self->top_line * self->cpl;
        ahl->view_max = (self->top_line + self->vis_lines) * self->cpl;
 }
 
 static void
-hex_widget_update_auto_highlight (HexWidget *self, HexWidget_AutoHighlight *ahl,
+hex_widget_update_auto_highlight (HexWidget *self, HexWidgetAutoHighlight *ahl,
                gboolean delete, gboolean add)
 {
        gint64 del_min, del_max;
@@ -2074,7 +2106,7 @@ hex_widget_update_auto_highlight (HexWidget *self, HexWidget_AutoHighlight *ahl,
 static void
 hex_widget_update_all_auto_highlights (HexWidget *self)
 {
-       HexWidget_AutoHighlight *cur = self->auto_highlight;
+       HexWidgetAutoHighlight *cur = self->auto_highlight;
 
        int mult = 10;
 
@@ -3348,12 +3380,21 @@ hex_widget_set_insert_mode (HexWidget *self, gboolean insert)
                        self->cursor_pos = payload_size - 1;
 }
 
-HexWidget_AutoHighlight *
+/**
+ * hex_widget_insert_autohighlight:
+ * @search: (array length=len) (transfer full): search string to auto-highlight
+ * @len: length of the @search string
+ *   
+ * Insert an auto-highlight of a given search string.
+ *
+ * Returns: a newly created [struct@Hex.WidgetAutoHighlight] structure
+ */
+HexWidgetAutoHighlight *
 hex_widget_insert_autohighlight (HexWidget *self,
                const char *search,
                int len)
 {
-       HexWidget_AutoHighlight *new = g_new0 (HexWidget_AutoHighlight, 1);
+       HexWidgetAutoHighlight *new = hex_widget_autohighlight_new ();
 
        new->search_string = g_memdup2 (search, len);
        new->search_len = len;
@@ -3372,6 +3413,7 @@ hex_widget_insert_autohighlight (HexWidget *self,
        return new;
 }
 
+/* FIXME - use _free func. */
 /**
  * hex_widget_delete_autohighlight:
  * @ahl: the autohighlight to be deleted
@@ -3379,7 +3421,7 @@ hex_widget_insert_autohighlight (HexWidget *self,
  * Delete the requested autohighlight.
  */
 void hex_widget_delete_autohighlight (HexWidget *self,
-               HexWidget_AutoHighlight *ahl)
+               HexWidgetAutoHighlight *ahl)
 {
        g_free (ahl->search_string);
 
diff --git a/src/gtkhex.h b/src/gtkhex.h
index 4e74800..07bcbb6 100644
--- a/src/gtkhex.h
+++ b/src/gtkhex.h
@@ -65,7 +65,13 @@ G_DECLARE_FINAL_TYPE(HexWidget, hex_widget, HEX, WIDGET, GtkWidget)
 
 /* OPAQUE DATATYPES */
 
-typedef struct _HexWidget_AutoHighlight HexWidget_AutoHighlight;
+#define HEX_TYPE_WIDGET_AUTOHIGHLIGHT (hex_widget_autohighlight_get_type ())
+GType hex_widget_autohighlight_get_type (void) G_GNUC_CONST;
+typedef struct _HexWidgetAutoHighlight HexWidgetAutoHighlight;
+
+HexWidgetAutoHighlight *       hex_widget_autohighlight_new (void);
+HexWidgetAutoHighlight *       hex_widget_autohighlight_copy (HexWidgetAutoHighlight *ahl);
+void   hex_widget_autohighlight_free (HexWidgetAutoHighlight *ahl);
 
 /* PUBLIC METHOD DECLARATIONS */
 
@@ -97,9 +103,9 @@ gboolean hex_widget_get_selection (HexWidget *gh, gint64 *start, gint64 *end);
 void hex_widget_clear_selection (HexWidget *gh);
 void hex_widget_delete_selection (HexWidget *gh);
 
-HexWidget_AutoHighlight *
+HexWidgetAutoHighlight *
 hex_widget_insert_autohighlight (HexWidget *gh, const char *search, int len);
-void hex_widget_delete_autohighlight (HexWidget *gh, HexWidget_AutoHighlight *ahl);
+void hex_widget_delete_autohighlight (HexWidget *gh, HexWidgetAutoHighlight *ahl);
 
 GtkAdjustment *hex_widget_get_adjustment(HexWidget *gh);
 HexDocument *hex_widget_get_document (HexWidget *gh);
diff --git a/src/hex-document.c b/src/hex-document.c
index c04b918..7c0fcb8 100644
--- a/src/hex-document.c
+++ b/src/hex-document.c
@@ -76,6 +76,42 @@ enum {
 
 static guint hex_signals[LAST_SIGNAL];
 
+
+/* TODO - come up with _new functions for the GTypes below and actually use
+ * them. */
+
+/* HexDocumentFindData GType Definitions */
+
+static HexDocumentFindData *
+hex_document_find_data_new (void)
+{
+       return g_slice_new0 (HexDocumentFindData);
+}
+
+/* FIXME - unused and could be unreliable. */
+static HexDocumentFindData *
+hex_document_find_data_copy (HexDocumentFindData *data)
+{
+       return g_slice_dup (HexDocumentFindData, data);
+}
+
+G_DEFINE_BOXED_TYPE (HexDocumentFindData, hex_document_find_data,
+               hex_document_find_data_copy, g_free)
+
+
+/* HexChangeData GType Definitions */
+
+/* FIXME - unused and could be unreliable */
+static HexChangeData *
+hex_change_data_copy (HexChangeData *data)
+{
+       return g_slice_dup (HexChangeData, data);
+}
+
+G_DEFINE_BOXED_TYPE (HexChangeData, hex_change_data,
+               hex_change_data_copy, g_free)
+
+
 /* GOBJECT DEFINITION */
 
 /**
@@ -949,7 +985,7 @@ hex_document_export_html (HexDocument *doc,
  */
 int
 hex_document_compare_data (HexDocument *doc,
-               char *what, gint64 pos, size_t len)
+               const char *what, gint64 pos, size_t len)
 {
        char c;
 
@@ -986,7 +1022,7 @@ hex_document_compare_data (HexDocument *doc,
  *   otherwise.
  */
 gboolean
-hex_document_find_forward (HexDocument *doc, gint64 start, char *what,
+hex_document_find_forward (HexDocument *doc, gint64 start, const char *what,
                                                  size_t len, gint64 *offset)
 {
        gint64 pos;
@@ -1065,7 +1101,7 @@ hex_document_find_forward_thread (GTask *task,
 void
 hex_document_find_forward_async (HexDocument *doc,
                gint64 start,
-               char *what,
+               const char *what,
                size_t len,
                gint64 *offset,
                const char *found_msg,
@@ -1075,7 +1111,7 @@ hex_document_find_forward_async (HexDocument *doc,
                gpointer user_data)
 {
        GTask *task;
-       HexDocumentFindData *find_data = g_new0 (HexDocumentFindData, 1);
+       HexDocumentFindData *find_data = hex_document_find_data_new ();
 
        find_data->start = start;
        find_data->what = what;
@@ -1109,7 +1145,7 @@ hex_document_find_forward_async (HexDocument *doc,
  *   otherwise.
  */
 gboolean
-hex_document_find_backward (HexDocument *doc, gint64 start, char *what,
+hex_document_find_backward (HexDocument *doc, gint64 start, const char *what,
                                                   size_t len, gint64 *offset)
 {
        gint64 pos = start;
@@ -1166,7 +1202,7 @@ hex_document_find_backward_thread (GTask *task,
 void
 hex_document_find_backward_async (HexDocument *doc,
                gint64 start,
-               char *what,
+               const char *what,
                size_t len,
                gint64 *offset,
                const char *found_msg,
diff --git a/src/hex-document.h b/src/hex-document.h
index 3ab1a6d..664c34a 100644
--- a/src/hex-document.h
+++ b/src/hex-document.h
@@ -71,11 +71,14 @@ typedef enum
  * A structure containing metadata about a find operation in a
  * [class@Hex.Document].
  */
+#define HEX_TYPE_DOCUMENT_FIND_DATA (hex_document_find_data_get_type ())
+GType hex_document_find_data_get_type (void) G_GNUC_CONST;
+
 typedef struct
 {
        gboolean found;
        gint64 start;
-       char *what;
+       const char *what;
        size_t len;
        gint64 offset;
        const char *found_msg;
@@ -100,8 +103,10 @@ typedef struct
  * A structure containing metadata about a change made to a
  * [class@Hex.Document].
  */
-typedef struct _HexChangeData HexChangeData;
-struct _HexChangeData
+#define HEX_TYPE_CHANGE_DATA (hex_change_data_get_type ())
+GType hex_change_data_get_type (void) G_GNUC_CONST;
+
+typedef struct
 {
        gint64 start, end;
        /* `replace length`: length to replace (overwrite); (0 to insert without
@@ -112,10 +117,10 @@ struct _HexChangeData
        HexChangeType type;
        char *v_string;
        char v_byte;
-};
+} HexChangeData;
 
 
-HexDocument    *hex_document_new(void);
+HexDocument    *hex_document_new (void);
 HexDocument    *hex_document_new_from_file (GFile *file);
 void           hex_document_set_data (HexDocument *doc, gint64 offset, size_t len,
                size_t rep_len, char *data, gboolean undoable);
@@ -143,20 +148,20 @@ void              hex_document_changed (HexDocument *doc,gpointer change_data,
 void           hex_document_set_max_undo (HexDocument *doc, int max_undo);
 gboolean       hex_document_undo (HexDocument *doc);
 gboolean       hex_document_redo (HexDocument *doc);
-int                    hex_document_compare_data (HexDocument *doc, char *what,
+int                    hex_document_compare_data (HexDocument *doc, const char *what,
                gint64 pos, size_t len);
 gboolean       hex_document_find_forward (HexDocument *doc, gint64 start,
-               char *what, size_t len, gint64 *offset);
+               const 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 *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 *offset);
+               const 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 *what, size_t len, gint64 *offset, const char *found_msg,
                const char *not_found_msg, GCancellable *cancellable,
                GAsyncReadyCallback callback, gpointer user_data);
 


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