[gtksourceview/wip/loader-saver: 11/23] Some code improvements
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/loader-saver: 11/23] Some code improvements
- Date: Fri, 27 Jun 2014 20:58:07 +0000 (UTC)
commit f8c0e4da3a46050ae3c9d60fee10509837f06014
Author: Sébastien Wilmet <swilmet gnome org>
Date: Wed Jun 4 22:24:58 2014 +0200
Some code improvements
gtksourceview/gtksourcebufferinputstream.c | 4 +-
gtksourceview/gtksourcebufferinputstream.h | 3 +-
gtksourceview/gtksourcebufferoutputstream.c | 4 +-
gtksourceview/gtksourcebufferoutputstream.h | 3 +-
gtksourceview/gtksourcefileloader.c | 29 ++++++++++---------------
gtksourceview/gtksourcefilesaver.c | 21 +++++++++---------
tests/test-buffer-input-stream.c | 6 ++--
tests/test-buffer-output-stream.c | 30 +++++++++++++-------------
8 files changed, 48 insertions(+), 52 deletions(-)
---
diff --git a/gtksourceview/gtksourcebufferinputstream.c b/gtksourceview/gtksourcebufferinputstream.c
index 8a7fcbf..7f4fc50 100644
--- a/gtksourceview/gtksourcebufferinputstream.c
+++ b/gtksourceview/gtksourcebufferinputstream.c
@@ -450,9 +450,9 @@ _gtk_source_buffer_input_stream_init (GtkSourceBufferInputStream *stream)
*
* Reads the data from @buffer.
*
- * Returns: a new #GInputStream to read @buffer
+ * Returns: a new input stream to read @buffer
*/
-GInputStream *
+GtkSourceBufferInputStream *
_gtk_source_buffer_input_stream_new (GtkTextBuffer *buffer,
GtkSourceNewlineType type,
gboolean add_trailing_newline)
diff --git a/gtksourceview/gtksourcebufferinputstream.h b/gtksourceview/gtksourcebufferinputstream.h
index b80c7c4..97c460b 100644
--- a/gtksourceview/gtksourcebufferinputstream.h
+++ b/gtksourceview/gtksourcebufferinputstream.h
@@ -56,7 +56,8 @@ G_GNUC_INTERNAL
GType _gtk_source_buffer_input_stream_get_type (void) G_GNUC_CONST;
G_GNUC_INTERNAL
-GInputStream *_gtk_source_buffer_input_stream_new (GtkTextBuffer *buffer,
+GtkSourceBufferInputStream
+ *_gtk_source_buffer_input_stream_new (GtkTextBuffer *buffer,
GtkSourceNewlineType type,
gboolean
add_trailing_newline);
diff --git a/gtksourceview/gtksourcebufferoutputstream.c b/gtksourceview/gtksourcebufferoutputstream.c
index b4e4f77..883238f 100644
--- a/gtksourceview/gtksourcebufferoutputstream.c
+++ b/gtksourceview/gtksourcebufferoutputstream.c
@@ -470,7 +470,7 @@ get_newline_type (GtkTextIter *end)
return res;
}
-GOutputStream *
+GtkSourceBufferOutputStream *
gtk_source_buffer_output_stream_new (GtkSourceBuffer *buffer,
GSList *candidate_encodings,
gboolean remove_trailing_newline)
@@ -484,7 +484,7 @@ gtk_source_buffer_output_stream_new (GtkSourceBuffer *buffer,
stream->priv->encodings = g_slist_copy (candidate_encodings);
- return G_OUTPUT_STREAM (stream);
+ return stream;
}
GtkSourceNewlineType
diff --git a/gtksourceview/gtksourcebufferoutputstream.h b/gtksourceview/gtksourcebufferoutputstream.h
index 62b2f5c..041c25e 100644
--- a/gtksourceview/gtksourcebufferoutputstream.h
+++ b/gtksourceview/gtksourcebufferoutputstream.h
@@ -56,7 +56,8 @@ G_GNUC_INTERNAL
GType gtk_source_buffer_output_stream_get_type (void) G_GNUC_CONST;
G_GNUC_INTERNAL
-GOutputStream *gtk_source_buffer_output_stream_new (GtkSourceBuffer *buffer,
+GtkSourceBufferOutputStream
+ *gtk_source_buffer_output_stream_new (GtkSourceBuffer *buffer,
GSList
*candidate_encodings,
gboolean
remove_trailing_newline);
diff --git a/gtksourceview/gtksourcefileloader.c b/gtksourceview/gtksourcefileloader.c
index d4426ca..de10f74 100644
--- a/gtksourceview/gtksourcefileloader.c
+++ b/gtksourceview/gtksourcefileloader.c
@@ -95,15 +95,16 @@ struct _GtkSourceFileLoaderPrivate
GFileProgressCallback progress_cb;
gpointer progress_cb_data;
- /* FIXME is it not better to allocate this field separately? I think the
- * private struct is allocated with GSlice by GObject, and GSlice is
- * better to use only for small struct, no?
- */
gchar chunk_buffer[READ_CHUNK_SIZE];
gssize chunk_bytes_read;
+ /* The two streams cannot be spliced directly, because
+ * (1) we need to call the progress callback
+ * (2) sync methods must be used for the output stream, and async
+ * methods for the input stream.
+ */
GInputStream *input_stream;
- GOutputStream *output_stream;
+ GtkSourceBufferOutputStream *output_stream;
guint guess_content_type_from_content : 1;
guint tried_mount : 1;
@@ -315,7 +316,6 @@ close_input_stream_cb (GInputStream *input_stream,
GtkSourceFileLoader *loader)
{
GError *error = NULL;
- GtkSourceBufferOutputStream *output_stream;
DEBUG ({
g_print ("%s\n", G_STRFUNC);
@@ -337,7 +337,7 @@ close_input_stream_cb (GInputStream *input_stream,
g_print ("Close output stream\n");
});
- g_output_stream_close (loader->priv->output_stream,
+ g_output_stream_close (G_OUTPUT_STREAM (loader->priv->output_stream),
g_task_get_cancellable (loader->priv->task),
&error);
@@ -350,8 +350,7 @@ close_input_stream_cb (GInputStream *input_stream,
/* Check if we needed some fallback char, if so, check if there was a
* previous error and if not set a fallback used error.
*/
- output_stream = GTK_SOURCE_BUFFER_OUTPUT_STREAM (loader->priv->output_stream);
- if (gtk_source_buffer_output_stream_get_num_fallbacks (output_stream) > 0)
+ if (gtk_source_buffer_output_stream_get_num_fallbacks (loader->priv->output_stream) > 0)
{
g_task_return_new_error (loader->priv->task,
GTK_SOURCE_FILE_LOADER_ERROR,
@@ -387,7 +386,7 @@ write_file_chunk (GtkSourceFileLoader *loader)
/* We use sync methods on the buffer stream since it is in memory. Using
* async would be racy and we can end up with invalidated iters.
*/
- bytes_written = g_output_stream_write (loader->priv->output_stream,
+ bytes_written = g_output_stream_write (G_OUTPUT_STREAM (loader->priv->output_stream),
loader->priv->chunk_buffer + chunk_bytes_written,
loader->priv->chunk_bytes_read - chunk_bytes_written,
g_task_get_cancellable (loader->priv->task),
@@ -479,18 +478,14 @@ read_cb (GInputStream *input_stream,
/* End of the file, we are done! */
if (loader->priv->chunk_bytes_read == 0)
{
- GtkSourceBufferOutputStream *output_stream;
-
- output_stream = GTK_SOURCE_BUFFER_OUTPUT_STREAM (loader->priv->output_stream);
-
/* Flush the stream to ensure proper line ending detection. */
- g_output_stream_flush (loader->priv->output_stream, NULL, NULL);
+ g_output_stream_flush (G_OUTPUT_STREAM (loader->priv->output_stream), NULL, NULL);
loader->priv->auto_detected_encoding =
- gtk_source_buffer_output_stream_get_guessed (output_stream);
+ gtk_source_buffer_output_stream_get_guessed (loader->priv->output_stream);
loader->priv->auto_detected_newline_type =
- gtk_source_buffer_output_stream_detect_newline_type (output_stream);
+ gtk_source_buffer_output_stream_detect_newline_type (loader->priv->output_stream);
write_complete (loader);
return;
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index ed76217..e89fbe2 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -104,11 +104,14 @@ struct _GtkSourceFileSaverPrivate
gssize chunk_bytes_written;
/* The output_stream contains the required converter(s) for the encoding
- * and the compression type. The input_stream is the
- * GtkSourceBufferInputStream (thus in UTF-8, without compression).
+ * and the compression type.
+ * The two streams cannot be spliced directly, because
+ * (1) we need to call the progress callback
+ * (2) sync methods must be used for the input stream, and async
+ * methods for the output stream.
*/
GOutputStream *output_stream;
- GInputStream *input_stream;
+ GtkSourceBufferInputStream *input_stream;
GFileInfo *info;
@@ -550,7 +553,7 @@ write_complete (GtkSourceFileSaver *saver)
g_print ("Close input stream\n");
});
- g_input_stream_close (saver->priv->input_stream,
+ g_input_stream_close (G_INPUT_STREAM (saver->priv->input_stream),
g_task_get_cancellable (saver->priv->task),
&error);
@@ -616,11 +619,9 @@ write_file_chunk_cb (GOutputStream *output_stream,
if (saver->priv->progress_cb != NULL)
{
- GtkSourceBufferInputStream *buffer_stream;
gsize total_chars_written;
- buffer_stream = GTK_SOURCE_BUFFER_INPUT_STREAM (saver->priv->input_stream);
- total_chars_written = _gtk_source_buffer_input_stream_tell (buffer_stream);
+ total_chars_written = _gtk_source_buffer_input_stream_tell (saver->priv->input_stream);
saver->priv->progress_cb (total_chars_written,
saver->priv->total_size,
@@ -663,7 +664,7 @@ read_file_chunk (GtkSourceFileSaver *saver)
/* We use sync methods on doc stream since it is in memory. Using async
* would be racy and we can end up with invalid iters.
*/
- saver->priv->chunk_bytes_read = g_input_stream_read (saver->priv->input_stream,
+ saver->priv->chunk_bytes_read = g_input_stream_read (G_INPUT_STREAM (saver->priv->input_stream),
saver->priv->chunk_buffer,
WRITE_CHUNK_SIZE,
g_task_get_cancellable (saver->priv->task),
@@ -693,7 +694,6 @@ replace_file_cb (GFile *file,
{
GFileOutputStream *file_output_stream;
GOutputStream *output_stream;
- GtkSourceBufferInputStream *buffer_stream;
GError *error = NULL;
DEBUG ({
@@ -768,8 +768,7 @@ replace_file_cb (GFile *file,
saver->priv->output_stream = G_OUTPUT_STREAM (output_stream);
}
- buffer_stream = GTK_SOURCE_BUFFER_INPUT_STREAM (saver->priv->input_stream);
- saver->priv->total_size = _gtk_source_buffer_input_stream_get_total_size (buffer_stream);
+ saver->priv->total_size = _gtk_source_buffer_input_stream_get_total_size (saver->priv->input_stream);
DEBUG ({
g_print ("Total number of characters: %" G_GINT64_FORMAT "\n", saver->priv->total_size);
diff --git a/tests/test-buffer-input-stream.c b/tests/test-buffer-input-stream.c
index 4f71466..218327e 100644
--- a/tests/test-buffer-input-stream.c
+++ b/tests/test-buffer-input-stream.c
@@ -32,7 +32,7 @@ test_consecutive_read (const gchar *inbuf,
gsize read_chunk_len)
{
GtkTextBuffer *buf;
- GInputStream *in;
+ GtkSourceBufferInputStream *in;
gsize outlen;
gssize n, r;
GError *err = NULL;
@@ -50,7 +50,7 @@ test_consecutive_read (const gchar *inbuf,
do
{
- r = g_input_stream_read (in, b + n, read_chunk_len, NULL, &err);
+ r = g_input_stream_read (G_INPUT_STREAM (in), b + n, read_chunk_len, NULL, &err);
g_assert_cmpint (r, >=, 0);
g_assert_no_error (err);
@@ -63,7 +63,7 @@ test_consecutive_read (const gchar *inbuf,
g_assert_cmpstr (b, ==, outbuf);
- close = g_input_stream_close (in, NULL, &err);
+ close = g_input_stream_close (G_INPUT_STREAM (in), NULL, &err);
g_assert (close);
g_assert_no_error (err);
diff --git a/tests/test-buffer-output-stream.c b/tests/test-buffer-output-stream.c
index 7227581..97e72c3 100644
--- a/tests/test-buffer-output-stream.c
+++ b/tests/test-buffer-output-stream.c
@@ -34,7 +34,7 @@ test_consecutive_write (const gchar *inbuf,
GtkSourceNewlineType newline_type)
{
GtkSourceBuffer *source_buffer;
- GOutputStream *out;
+ GtkSourceBufferOutputStream *out;
gsize len;
gssize n, w;
GError *err = NULL;
@@ -51,21 +51,21 @@ test_consecutive_write (const gchar *inbuf,
do
{
len = MIN (write_chunk_len, strlen (inbuf + n));
- w = g_output_stream_write (out, inbuf + n, len, NULL, &err);
+ w = g_output_stream_write (G_OUTPUT_STREAM (out), inbuf + n, len, NULL, &err);
g_assert_cmpint (w, >=, 0);
g_assert_no_error (err);
n += w;
} while (w != 0);
- g_output_stream_flush (out, NULL, &err);
+ g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err);
g_assert_no_error (err);
- type = gtk_source_buffer_output_stream_detect_newline_type (GTK_SOURCE_BUFFER_OUTPUT_STREAM (out));
+ type = gtk_source_buffer_output_stream_detect_newline_type (out);
g_assert (type == newline_type);
- g_output_stream_close (out, NULL, &err);
+ g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err);
g_assert_no_error (err);
g_object_get (G_OBJECT (source_buffer), "text", &b, NULL);
@@ -121,7 +121,7 @@ static void
test_boundary (void)
{
GtkSourceBuffer *source_buffer;
- GOutputStream *out;
+ GtkSourceBufferOutputStream *out;
gint line_count;
GError *err = NULL;
GSList *encodings = NULL;
@@ -130,17 +130,17 @@ test_boundary (void)
encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_utf8 ());
out = gtk_source_buffer_output_stream_new (source_buffer, encodings, TRUE);
- g_output_stream_write (out, "\r", 1, NULL, NULL);
- g_output_stream_write (out, "\n", 1, NULL, NULL);
+ g_output_stream_write (G_OUTPUT_STREAM (out), "\r", 1, NULL, NULL);
+ g_output_stream_write (G_OUTPUT_STREAM (out), "\n", 1, NULL, NULL);
- g_output_stream_flush (out, NULL, &err);
+ g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err);
g_assert_no_error (err);
line_count = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (source_buffer));
g_assert_cmpint (line_count, ==, 2);
- g_output_stream_close (out, NULL, &err);
+ g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err);
g_assert_no_error (err);
g_object_unref (source_buffer);
@@ -248,7 +248,7 @@ do_test (const gchar *inbuf,
const GtkSourceEncoding **guessed)
{
GtkSourceBuffer *source_buffer;
- GOutputStream *out;
+ GtkSourceBufferOutputStream *out;
GError *err = NULL;
GtkTextIter start, end;
gchar *text;
@@ -269,7 +269,7 @@ do_test (const gchar *inbuf,
do
{
to_write = MIN (len, write_chunk_len);
- w = g_output_stream_write (out, inbuf + n, to_write, NULL, &err);
+ w = g_output_stream_write (G_OUTPUT_STREAM (out), inbuf + n, to_write, NULL, &err);
g_assert_cmpint (w, >=, 0);
g_assert_no_error (err);
@@ -277,15 +277,15 @@ do_test (const gchar *inbuf,
n += w;
} while (len != 0);
- g_output_stream_flush (out, NULL, &err);
+ g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err);
g_assert_no_error (err);
- g_output_stream_close (out, NULL, &err);
+ g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err);
g_assert_no_error (err);
if (guessed != NULL)
{
- *guessed = gtk_source_buffer_output_stream_get_guessed (GTK_SOURCE_BUFFER_OUTPUT_STREAM
(out));
+ *guessed = gtk_source_buffer_output_stream_get_guessed (out);
}
gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (source_buffer), &start, &end);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]