[gtksourceview/wip/chergert/gsv-gtk4: 47/259] buffer: modernize GtkSourceBufferInputStream




commit 1a689fc597f004b6dac32656f1339fb5d024876f
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jan 9 11:23:09 2020 -0800

    buffer: modernize GtkSourceBufferInputStream
    
     - Use G_DECLARE_
     - Make final
     - Use private header

 docs/reference/meson.build                         |   2 +-
 gtksourceview/gtksourcebufferinputstream-private.h |  47 ++++++++
 gtksourceview/gtksourcebufferinputstream.c         | 121 +++++++++++----------
 gtksourceview/gtksourcebufferinputstream.h         |  69 ------------
 gtksourceview/gtksourcefilesaver.c                 |   2 +-
 gtksourceview/gtksourcetypes-private.h             |   4 +-
 testsuite/test-buffer-input-stream.c               |   3 +-
 7 files changed, 115 insertions(+), 133 deletions(-)
---
diff --git a/docs/reference/meson.build b/docs/reference/meson.build
index 4f3a3619..372262b4 100644
--- a/docs/reference/meson.build
+++ b/docs/reference/meson.build
@@ -8,7 +8,7 @@ reference_private_h = [
   'config.h',
   'gtksource.h',
   'gtksourcebuffer-private.h',
-  'gtksourcebufferinputstream.h',
+  'gtksourcebufferinputstream-private.h',
   'gtksourcebufferinternal.h',
   'gtksourcebufferoutputstream.h',
   'gtksourcecompletioncontainer.h',
diff --git a/gtksourceview/gtksourcebufferinputstream-private.h 
b/gtksourceview/gtksourcebufferinputstream-private.h
new file mode 100644
index 00000000..571c947e
--- /dev/null
+++ b/gtksourceview/gtksourcebufferinputstream-private.h
@@ -0,0 +1,47 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/*
+ * This file is part of GtkSourceView
+ *
+ * Copyright 2010 - Ignacio Casal Quinteiro
+ * Copyright 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+
+#include "gtksourcetypes-private.h"
+#include "gtksourcebuffer.h"
+#include "gtksourcefile.h"
+
+G_BEGIN_DECLS
+
+#define GTK_SOURCE_TYPE_BUFFER_INPUT_STREAM (_gtk_source_buffer_input_stream_get_type())
+
+GTK_SOURCE_INTERNAL
+G_DECLARE_FINAL_TYPE (GtkSourceBufferInputStream, _gtk_source_buffer_input_stream, GTK_SOURCE, 
BUFFER_INPUT_STREAM, GInputStream)
+
+GTK_SOURCE_INTERNAL
+GtkSourceBufferInputStream *_gtk_source_buffer_input_stream_new            (GtkTextBuffer              
*buffer,
+                                                                            GtkSourceNewlineType        type,
+                                                                            gboolean                    
add_trailing_newline);
+GTK_SOURCE_INTERNAL
+gsize                       _gtk_source_buffer_input_stream_get_total_size (GtkSourceBufferInputStream 
*stream);
+GTK_SOURCE_INTERNAL
+gsize                       _gtk_source_buffer_input_stream_tell           (GtkSourceBufferInputStream 
*stream);
+
+G_END_DECLS
diff --git a/gtksourceview/gtksourcebufferinputstream.c b/gtksourceview/gtksourcebufferinputstream.c
index ec984607..a8d9be86 100644
--- a/gtksourceview/gtksourcebufferinputstream.c
+++ b/gtksourceview/gtksourcebufferinputstream.c
@@ -24,7 +24,8 @@
 #include <glib.h>
 #include <gio/gio.h>
 #include <string.h>
-#include "gtksourcebufferinputstream.h"
+
+#include "gtksourcebufferinputstream-private.h"
 #include "gtksource-enumtypes.h"
 
 /* NOTE: never use async methods on this stream, the stream is just
@@ -34,8 +35,10 @@
  * thread.
  */
 
-struct _GtkSourceBufferInputStreamPrivate
+struct _GtkSourceBufferInputStream
 {
+       GInputStream parent_instance;
+
        GtkTextBuffer *buffer;
        GtkTextMark *pos;
        gint bytes_partial;
@@ -55,12 +58,12 @@ enum
        PROP_ADD_TRAILING_NEWLINE
 };
 
-G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceBufferInputStream, _gtk_source_buffer_input_stream, 
G_TYPE_INPUT_STREAM);
+G_DEFINE_TYPE (GtkSourceBufferInputStream, _gtk_source_buffer_input_stream, G_TYPE_INPUT_STREAM)
 
 static gsize
 get_new_line_size (GtkSourceBufferInputStream *stream)
 {
-       switch (stream->priv->newline_type)
+       switch (stream->newline_type)
        {
                case GTK_SOURCE_NEWLINE_TYPE_CR:
                case GTK_SOURCE_NEWLINE_TYPE_LF:
@@ -80,7 +83,7 @@ get_new_line_size (GtkSourceBufferInputStream *stream)
 static const gchar *
 get_new_line (GtkSourceBufferInputStream *stream)
 {
-       switch (stream->priv->newline_type)
+       switch (stream->newline_type)
        {
                case GTK_SOURCE_NEWLINE_TYPE_LF:
                        return "\n";
@@ -101,8 +104,8 @@ get_new_line (GtkSourceBufferInputStream *stream)
 
 static gsize
 read_line (GtkSourceBufferInputStream *stream,
-          gchar                      *outbuf,
-          gsize                       space_left)
+           gchar                      *outbuf,
+           gsize                       space_left)
 {
        GtkTextIter start, next, end;
        gchar *buf;
@@ -111,14 +114,14 @@ read_line (GtkSourceBufferInputStream *stream,
        const gchar *newline;
        gboolean is_last;
 
-       if (stream->priv->buffer == NULL)
+       if (stream->buffer == NULL)
        {
                return 0;
        }
 
-       gtk_text_buffer_get_iter_at_mark (stream->priv->buffer,
+       gtk_text_buffer_get_iter_at_mark (stream->buffer,
                                          &start,
-                                         stream->priv->pos);
+                                         stream->pos);
 
        if (gtk_text_iter_is_end (&start))
        {
@@ -140,7 +143,7 @@ read_line (GtkSourceBufferInputStream *stream,
 
        /* the bytes of a line includes also the newline, so with the
           offsets we remove the newline and we add the new newline size */
-       bytes = gtk_text_iter_get_bytes_in_line (&start) - stream->priv->bytes_partial;
+       bytes = gtk_text_iter_get_bytes_in_line (&start) - stream->bytes_partial;
 
        /* bytes_in_line includes the newlines, so we remove that assuming that
           they are single byte characters */
@@ -198,7 +201,7 @@ read_line (GtkSourceBufferInputStream *stream,
 
                /* Note: offset is one past what we wrote */
                gtk_text_iter_forward_chars (&start, char_offset);
-               stream->priv->bytes_partial += written;
+               stream->bytes_partial += written;
                read = written;
        }
        else
@@ -213,12 +216,12 @@ read_line (GtkSourceBufferInputStream *stream,
                }
 
                start = next;
-               stream->priv->bytes_partial = 0;
+               stream->bytes_partial = 0;
                read = bytes_to_write;
        }
 
-       gtk_text_buffer_move_mark (stream->priv->buffer,
-                                  stream->priv->pos,
+       gtk_text_buffer_move_mark (stream->buffer,
+                                  stream->pos,
                                   &start);
 
        g_free (buf);
@@ -227,10 +230,10 @@ read_line (GtkSourceBufferInputStream *stream,
 
 static gssize
 _gtk_source_buffer_input_stream_read (GInputStream  *input_stream,
-                                     void          *buffer,
-                                     gsize          count,
-                                     GCancellable  *cancellable,
-                                     GError       **error)
+                                      void          *buffer,
+                                      gsize          count,
+                                      GCancellable  *cancellable,
+                                      GError       **error)
 {
        GtkSourceBufferInputStream *stream;
        GtkTextIter iter;
@@ -250,21 +253,21 @@ _gtk_source_buffer_input_stream_read (GInputStream  *input_stream,
                return -1;
        }
 
-       if (stream->priv->buffer == NULL)
+       if (stream->buffer == NULL)
        {
                return 0;
        }
 
        /* Initialize the mark to the first char in the text buffer */
-       if (!stream->priv->is_initialized)
+       if (!stream->is_initialized)
        {
-               gtk_text_buffer_get_start_iter (stream->priv->buffer, &iter);
-               stream->priv->pos = gtk_text_buffer_create_mark (stream->priv->buffer,
+               gtk_text_buffer_get_start_iter (stream->buffer, &iter);
+               stream->pos = gtk_text_buffer_create_mark (stream->buffer,
                                                                 NULL,
                                                                 &iter,
                                                                 FALSE);
 
-               stream->priv->is_initialized = TRUE;
+               stream->is_initialized = TRUE;
        }
 
        space_left = count;
@@ -275,24 +278,24 @@ _gtk_source_buffer_input_stream_read (GInputStream  *input_stream,
                n = read_line (stream, (gchar *)buffer + read, space_left);
                read += n;
                space_left -= n;
-       } while (space_left > 0 && n != 0 && stream->priv->bytes_partial == 0);
+       } while (space_left > 0 && n != 0 && stream->bytes_partial == 0);
 
        /* Make sure that non-empty files are always terminated with \n (see bug #95676).
         * Note that we strip the trailing \n when loading the file */
-       gtk_text_buffer_get_iter_at_mark (stream->priv->buffer,
+       gtk_text_buffer_get_iter_at_mark (stream->buffer,
                                          &iter,
-                                         stream->priv->pos);
+                                         stream->pos);
 
        if (gtk_text_iter_is_end (&iter) &&
            !gtk_text_iter_is_start (&iter) &&
-           stream->priv->add_trailing_newline)
+           stream->add_trailing_newline)
        {
                gssize newline_size;
 
                newline_size = get_new_line_size (stream);
 
                if (space_left >= newline_size &&
-                   !stream->priv->newline_added)
+                   !stream->newline_added)
                {
                        const gchar *newline;
 
@@ -301,7 +304,7 @@ _gtk_source_buffer_input_stream_read (GInputStream  *input_stream,
                        memcpy ((gchar *)buffer + read, newline, newline_size);
 
                        read += newline_size;
-                       stream->priv->newline_added = TRUE;
+                       stream->newline_added = TRUE;
                }
        }
 
@@ -310,17 +313,17 @@ _gtk_source_buffer_input_stream_read (GInputStream  *input_stream,
 
 static gboolean
 _gtk_source_buffer_input_stream_close (GInputStream  *input_stream,
-                                      GCancellable  *cancellable,
-                                      GError       **error)
+                                       GCancellable  *cancellable,
+                                       GError       **error)
 {
        GtkSourceBufferInputStream *stream = GTK_SOURCE_BUFFER_INPUT_STREAM (input_stream);
 
-       stream->priv->newline_added = FALSE;
+       stream->newline_added = FALSE;
 
-       if (stream->priv->is_initialized &&
-           stream->priv->buffer != NULL)
+       if (stream->is_initialized &&
+           stream->buffer != NULL)
        {
-               gtk_text_buffer_delete_mark (stream->priv->buffer, stream->priv->pos);
+               gtk_text_buffer_delete_mark (stream->buffer, stream->pos);
        }
 
        return TRUE;
@@ -328,25 +331,25 @@ _gtk_source_buffer_input_stream_close (GInputStream  *input_stream,
 
 static void
 _gtk_source_buffer_input_stream_set_property (GObject      *object,
-                                             guint         prop_id,
-                                             const GValue *value,
-                                             GParamSpec   *pspec)
+                                              guint         prop_id,
+                                              const GValue *value,
+                                              GParamSpec   *pspec)
 {
        GtkSourceBufferInputStream *stream = GTK_SOURCE_BUFFER_INPUT_STREAM (object);
 
        switch (prop_id)
        {
                case PROP_BUFFER:
-                       g_assert (stream->priv->buffer == NULL);
-                       stream->priv->buffer = g_value_dup_object (value);
+                       g_assert (stream->buffer == NULL);
+                       stream->buffer = g_value_dup_object (value);
                        break;
 
                case PROP_NEWLINE_TYPE:
-                       stream->priv->newline_type = g_value_get_enum (value);
+                       stream->newline_type = g_value_get_enum (value);
                        break;
 
                case PROP_ADD_TRAILING_NEWLINE:
-                       stream->priv->add_trailing_newline = g_value_get_boolean (value);
+                       stream->add_trailing_newline = g_value_get_boolean (value);
                        break;
 
                default:
@@ -357,24 +360,24 @@ _gtk_source_buffer_input_stream_set_property (GObject      *object,
 
 static void
 _gtk_source_buffer_input_stream_get_property (GObject    *object,
-                                         guint       prop_id,
-                                         GValue     *value,
-                                         GParamSpec *pspec)
+                                              guint       prop_id,
+                                              GValue     *value,
+                                              GParamSpec *pspec)
 {
        GtkSourceBufferInputStream *stream = GTK_SOURCE_BUFFER_INPUT_STREAM (object);
 
        switch (prop_id)
        {
                case PROP_BUFFER:
-                       g_value_set_object (value, stream->priv->buffer);
+                       g_value_set_object (value, stream->buffer);
                        break;
 
                case PROP_NEWLINE_TYPE:
-                       g_value_set_enum (value, stream->priv->newline_type);
+                       g_value_set_enum (value, stream->newline_type);
                        break;
 
                case PROP_ADD_TRAILING_NEWLINE:
-                       g_value_set_boolean (value, stream->priv->add_trailing_newline);
+                       g_value_set_boolean (value, stream->add_trailing_newline);
                        break;
 
                default:
@@ -388,7 +391,7 @@ _gtk_source_buffer_input_stream_dispose (GObject *object)
 {
        GtkSourceBufferInputStream *stream = GTK_SOURCE_BUFFER_INPUT_STREAM (object);
 
-       g_clear_object (&stream->priv->buffer);
+       g_clear_object (&stream->buffer);
 
        G_OBJECT_CLASS (_gtk_source_buffer_input_stream_parent_class)->dispose (object);
 }
@@ -453,7 +456,7 @@ _gtk_source_buffer_input_stream_class_init (GtkSourceBufferInputStreamClass *kla
 static void
 _gtk_source_buffer_input_stream_init (GtkSourceBufferInputStream *stream)
 {
-       stream->priv = _gtk_source_buffer_input_stream_get_instance_private (stream);
+       stream = _gtk_source_buffer_input_stream_get_instance_private (stream);
 }
 
 /**
@@ -466,8 +469,8 @@ _gtk_source_buffer_input_stream_init (GtkSourceBufferInputStream *stream)
  */
 GtkSourceBufferInputStream *
 _gtk_source_buffer_input_stream_new (GtkTextBuffer        *buffer,
-                                    GtkSourceNewlineType  type,
-                                    gboolean              add_trailing_newline)
+                                     GtkSourceNewlineType  type,
+                                     gboolean              add_trailing_newline)
 {
        g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
 
@@ -483,12 +486,12 @@ _gtk_source_buffer_input_stream_get_total_size (GtkSourceBufferInputStream *stre
 {
        g_return_val_if_fail (GTK_SOURCE_IS_BUFFER_INPUT_STREAM (stream), 0);
 
-       if (stream->priv->buffer == NULL)
+       if (stream->buffer == NULL)
        {
                return 0;
        }
 
-       return gtk_text_buffer_get_char_count (stream->priv->buffer);
+       return gtk_text_buffer_get_char_count (stream->buffer);
 }
 
 gsize
@@ -499,8 +502,8 @@ _gtk_source_buffer_input_stream_tell (GtkSourceBufferInputStream *stream)
        /* FIXME: is this potentially inefficient? If yes, we could keep
           track of the offset internally, assuming the mark doesn't move
           during the operation */
-       if (!stream->priv->is_initialized ||
-           stream->priv->buffer == NULL)
+       if (!stream->is_initialized ||
+           stream->buffer == NULL)
        {
                return 0;
        }
@@ -508,9 +511,9 @@ _gtk_source_buffer_input_stream_tell (GtkSourceBufferInputStream *stream)
        {
                GtkTextIter iter;
 
-               gtk_text_buffer_get_iter_at_mark (stream->priv->buffer,
+               gtk_text_buffer_get_iter_at_mark (stream->buffer,
                                                  &iter,
-                                                 stream->priv->pos);
+                                                 stream->pos);
                return gtk_text_iter_get_offset (&iter);
        }
 }
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index a4f2e87d..dda555f9 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -26,7 +26,7 @@
 #include "gtksourcefilesaver.h"
 #include <glib/gi18n-lib.h>
 #include "gtksourcefile.h"
-#include "gtksourcebufferinputstream.h"
+#include "gtksourcebufferinputstream-private.h"
 #include "gtksourceencoding.h"
 #include "gtksourcebuffer.h"
 #include "gtksourcebuffer-private.h"
diff --git a/gtksourceview/gtksourcetypes-private.h b/gtksourceview/gtksourcetypes-private.h
index 2b947c69..dbafbd83 100644
--- a/gtksourceview/gtksourcetypes-private.h
+++ b/gtksourceview/gtksourcetypes-private.h
@@ -38,9 +38,9 @@ typedef struct _GtkSourceUndoManagerDefault   GtkSourceUndoManagerDefault;
 
 #ifdef _MSC_VER
 /* For Visual Studio, we need to export the symbols used by the unit tests */
-#define GTK_SOURCE_INTERNAL __declspec(dllexport)
+# define GTK_SOURCE_INTERNAL __declspec(dllexport)
 #else
-#define GTK_SOURCE_INTERNAL G_GNUC_INTERNAL
+# define GTK_SOURCE_INTERNAL G_GNUC_INTERNAL
 #endif
 
 G_END_DECLS
diff --git a/testsuite/test-buffer-input-stream.c b/testsuite/test-buffer-input-stream.c
index 1ae3cfed..bc8bcc01 100644
--- a/testsuite/test-buffer-input-stream.c
+++ b/testsuite/test-buffer-input-stream.c
@@ -23,7 +23,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <gtksourceview/gtksource.h>
-#include "gtksourceview/gtksourcebufferinputstream.h"
+
+#include "gtksourceview/gtksourcebufferinputstream-private.h"
 
 static void
 test_consecutive_read (const gchar          *inbuf,


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