xed-document-output-stream: Clean up code style

This commit is contained in:
JosephMcc 2017-01-19 01:38:13 -08:00
parent dfa7714cc5
commit 28ef771d15
1 changed files with 241 additions and 247 deletions

View File

@ -16,7 +16,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with xed; if not, write to the Free Software * along with xed; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, * Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
@ -35,213 +35,208 @@
* thread */ * thread */
#define XED_DOCUMENT_OUTPUT_STREAM_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object),\ #define XED_DOCUMENT_OUTPUT_STREAM_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object),\
XED_TYPE_DOCUMENT_OUTPUT_STREAM,\ XED_TYPE_DOCUMENT_OUTPUT_STREAM,\
XedDocumentOutputStreamPrivate)) XedDocumentOutputStreamPrivate))
#define MAX_UNICHAR_LEN 6 #define MAX_UNICHAR_LEN 6
struct _XedDocumentOutputStreamPrivate struct _XedDocumentOutputStreamPrivate
{ {
XedDocument *doc; XedDocument *doc;
GtkTextIter pos; GtkTextIter pos;
gchar *buffer; gchar *buffer;
gsize buflen; gsize buflen;
guint is_initialized : 1; guint is_initialized : 1;
guint is_closed : 1; guint is_closed : 1;
}; };
enum enum
{ {
PROP_0, PROP_0,
PROP_DOCUMENT PROP_DOCUMENT
}; };
G_DEFINE_TYPE (XedDocumentOutputStream, xed_document_output_stream, G_TYPE_OUTPUT_STREAM) G_DEFINE_TYPE (XedDocumentOutputStream, xed_document_output_stream, G_TYPE_OUTPUT_STREAM)
static gssize xed_document_output_stream_write (GOutputStream *stream, static gssize xed_document_output_stream_write (GOutputStream *stream,
const void *buffer, const void *buffer,
gsize count, gsize count,
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);
static gboolean xed_document_output_stream_flush (GOutputStream *stream, static gboolean xed_document_output_stream_flush (GOutputStream *stream,
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);
static gboolean xed_document_output_stream_close (GOutputStream *stream, static gboolean xed_document_output_stream_close (GOutputStream *stream,
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);
static void static void
xed_document_output_stream_set_property (GObject *object, xed_document_output_stream_set_property (GObject *object,
guint prop_id, guint prop_id,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object); XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object);
switch (prop_id) switch (prop_id)
{ {
case PROP_DOCUMENT: case PROP_DOCUMENT:
stream->priv->doc = XED_DOCUMENT (g_value_get_object (value)); stream->priv->doc = XED_DOCUMENT (g_value_get_object (value));
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
} }
} }
static void static void
xed_document_output_stream_get_property (GObject *object, xed_document_output_stream_get_property (GObject *object,
guint prop_id, guint prop_id,
GValue *value, GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object); XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object);
switch (prop_id) switch (prop_id)
{ {
case PROP_DOCUMENT: case PROP_DOCUMENT:
g_value_set_object (value, stream->priv->doc); g_value_set_object (value, stream->priv->doc);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
} }
} }
static void static void
xed_document_output_stream_finalize (GObject *object) xed_document_output_stream_finalize (GObject *object)
{ {
XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object); XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object);
g_free (stream->priv->buffer); g_free (stream->priv->buffer);
G_OBJECT_CLASS (xed_document_output_stream_parent_class)->finalize (object); G_OBJECT_CLASS (xed_document_output_stream_parent_class)->finalize (object);
} }
static void static void
xed_document_output_stream_constructed (GObject *object) xed_document_output_stream_constructed (GObject *object)
{ {
XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object); XedDocumentOutputStream *stream = XED_DOCUMENT_OUTPUT_STREAM (object);
if (!stream->priv->doc) if (!stream->priv->doc)
{ {
g_critical ("This should never happen, a problem happened constructing the Document Output Stream!"); g_critical ("This should never happen, a problem happened constructing the Document Output Stream!");
return; return;
} }
/* Init the undoable action */ /* Init the undoable action */
gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (stream->priv->doc)); gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (stream->priv->doc));
/* clear the buffer */ /* clear the buffer */
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (stream->priv->doc), gtk_text_buffer_set_text (GTK_TEXT_BUFFER (stream->priv->doc), "", 0);
"", 0); gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (stream->priv->doc), FALSE);
gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (stream->priv->doc),
FALSE);
gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (stream->priv->doc)); gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (stream->priv->doc));
} }
static void static void
xed_document_output_stream_class_init (XedDocumentOutputStreamClass *klass) xed_document_output_stream_class_init (XedDocumentOutputStreamClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass); GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
object_class->get_property = xed_document_output_stream_get_property; object_class->get_property = xed_document_output_stream_get_property;
object_class->set_property = xed_document_output_stream_set_property; object_class->set_property = xed_document_output_stream_set_property;
object_class->finalize = xed_document_output_stream_finalize; object_class->finalize = xed_document_output_stream_finalize;
object_class->constructed = xed_document_output_stream_constructed; object_class->constructed = xed_document_output_stream_constructed;
stream_class->write_fn = xed_document_output_stream_write; stream_class->write_fn = xed_document_output_stream_write;
stream_class->flush = xed_document_output_stream_flush; stream_class->flush = xed_document_output_stream_flush;
stream_class->close_fn = xed_document_output_stream_close; stream_class->close_fn = xed_document_output_stream_close;
g_object_class_install_property (object_class, g_object_class_install_property (object_class,
PROP_DOCUMENT, PROP_DOCUMENT,
g_param_spec_object ("document", g_param_spec_object ("document",
"Document", "Document",
"The document which is written", "The document which is written",
XED_TYPE_DOCUMENT, XED_TYPE_DOCUMENT,
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY)); G_PARAM_CONSTRUCT_ONLY));
g_type_class_add_private (object_class, sizeof (XedDocumentOutputStreamPrivate)); g_type_class_add_private (object_class, sizeof (XedDocumentOutputStreamPrivate));
} }
static void static void
xed_document_output_stream_init (XedDocumentOutputStream *stream) xed_document_output_stream_init (XedDocumentOutputStream *stream)
{ {
stream->priv = XED_DOCUMENT_OUTPUT_STREAM_GET_PRIVATE (stream); stream->priv = XED_DOCUMENT_OUTPUT_STREAM_GET_PRIVATE (stream);
stream->priv->buffer = NULL; stream->priv->buffer = NULL;
stream->priv->buflen = 0; stream->priv->buflen = 0;
stream->priv->is_initialized = FALSE; stream->priv->is_initialized = FALSE;
stream->priv->is_closed = FALSE; stream->priv->is_closed = FALSE;
} }
static XedDocumentNewlineType static XedDocumentNewlineType
get_newline_type (GtkTextIter *end) get_newline_type (GtkTextIter *end)
{ {
XedDocumentNewlineType res; XedDocumentNewlineType res;
GtkTextIter copy; GtkTextIter copy;
gunichar c; gunichar c;
copy = *end; copy = *end;
c = gtk_text_iter_get_char (&copy); c = gtk_text_iter_get_char (&copy);
if (g_unichar_break_type (c) == G_UNICODE_BREAK_CARRIAGE_RETURN) if (g_unichar_break_type (c) == G_UNICODE_BREAK_CARRIAGE_RETURN)
{ {
if (gtk_text_iter_forward_char (&copy) && if (gtk_text_iter_forward_char (&copy) &&
g_unichar_break_type (gtk_text_iter_get_char (&copy)) == G_UNICODE_BREAK_LINE_FEED) g_unichar_break_type (gtk_text_iter_get_char (&copy)) == G_UNICODE_BREAK_LINE_FEED)
{ {
res = XED_DOCUMENT_NEWLINE_TYPE_CR_LF; res = XED_DOCUMENT_NEWLINE_TYPE_CR_LF;
} }
else else
{ {
res = XED_DOCUMENT_NEWLINE_TYPE_CR; res = XED_DOCUMENT_NEWLINE_TYPE_CR;
} }
} }
else else
{ {
res = XED_DOCUMENT_NEWLINE_TYPE_LF; res = XED_DOCUMENT_NEWLINE_TYPE_LF;
} }
return res; return res;
} }
GOutputStream * GOutputStream *
xed_document_output_stream_new (XedDocument *doc) xed_document_output_stream_new (XedDocument *doc)
{ {
return G_OUTPUT_STREAM (g_object_new (XED_TYPE_DOCUMENT_OUTPUT_STREAM, return G_OUTPUT_STREAM (g_object_new (XED_TYPE_DOCUMENT_OUTPUT_STREAM, "document", doc, NULL));
"document", doc, NULL));
} }
XedDocumentNewlineType XedDocumentNewlineType
xed_document_output_stream_detect_newline_type (XedDocumentOutputStream *stream) xed_document_output_stream_detect_newline_type (XedDocumentOutputStream *stream)
{ {
XedDocumentNewlineType type; XedDocumentNewlineType type;
GtkTextIter iter; GtkTextIter iter;
g_return_val_if_fail (XED_IS_DOCUMENT_OUTPUT_STREAM (stream), g_return_val_if_fail (XED_IS_DOCUMENT_OUTPUT_STREAM (stream), XED_DOCUMENT_NEWLINE_TYPE_DEFAULT);
XED_DOCUMENT_NEWLINE_TYPE_DEFAULT);
type = XED_DOCUMENT_NEWLINE_TYPE_DEFAULT; type = XED_DOCUMENT_NEWLINE_TYPE_DEFAULT;
gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (stream->priv->doc), gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (stream->priv->doc), &iter);
&iter);
if (gtk_text_iter_ends_line (&iter) || gtk_text_iter_forward_to_line_end (&iter)) if (gtk_text_iter_ends_line (&iter) || gtk_text_iter_forward_to_line_end (&iter))
{ {
type = get_newline_type (&iter); type = get_newline_type (&iter);
} }
return type; return type;
} }
/* If the last char is a newline, remove it from the buffer (otherwise /* If the last char is a newline, remove it from the buffer (otherwise
@ -249,171 +244,170 @@ xed_document_output_stream_detect_newline_type (XedDocumentOutputStream *stream)
static void static void
remove_ending_newline (XedDocumentOutputStream *stream) remove_ending_newline (XedDocumentOutputStream *stream)
{ {
GtkTextIter end; GtkTextIter end;
GtkTextIter start; GtkTextIter start;
gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (stream->priv->doc), &end); gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (stream->priv->doc), &end);
start = end; start = end;
gtk_text_iter_set_line_offset (&start, 0); gtk_text_iter_set_line_offset (&start, 0);
if (gtk_text_iter_ends_line (&start) && if (gtk_text_iter_ends_line (&start) && gtk_text_iter_backward_line (&start))
gtk_text_iter_backward_line (&start)) {
{ if (!gtk_text_iter_ends_line (&start))
if (!gtk_text_iter_ends_line (&start)) {
{ gtk_text_iter_forward_to_line_end (&start);
gtk_text_iter_forward_to_line_end (&start); }
}
/* Delete the empty line which is from 'start' to 'end' */ /* Delete the empty line which is from 'start' to 'end' */
gtk_text_buffer_delete (GTK_TEXT_BUFFER (stream->priv->doc), gtk_text_buffer_delete (GTK_TEXT_BUFFER (stream->priv->doc), &start, &end);
&start, }
&end);
}
} }
static void static void
end_append_text_to_document (XedDocumentOutputStream *stream) end_append_text_to_document (XedDocumentOutputStream *stream)
{ {
remove_ending_newline (stream); remove_ending_newline (stream);
gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (stream->priv->doc), gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (stream->priv->doc), FALSE);
FALSE);
gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (stream->priv->doc)); gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (stream->priv->doc));
} }
static gssize static gssize
xed_document_output_stream_write (GOutputStream *stream, xed_document_output_stream_write (GOutputStream *stream,
const void *buffer, const void *buffer,
gsize count, gsize count,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
XedDocumentOutputStream *ostream; XedDocumentOutputStream *ostream;
gchar *text; gchar *text;
gsize len; gsize len;
gboolean freetext = FALSE; gboolean freetext = FALSE;
const gchar *end; const gchar *end;
gboolean valid; gboolean valid;
if (g_cancellable_set_error_if_cancelled (cancellable, error)) if (g_cancellable_set_error_if_cancelled (cancellable, error))
return -1; {
return -1;
}
ostream = XED_DOCUMENT_OUTPUT_STREAM (stream); ostream = XED_DOCUMENT_OUTPUT_STREAM (stream);
if (!ostream->priv->is_initialized) if (!ostream->priv->is_initialized)
{ {
/* Init the undoable action */ /* Init the undoable action */
gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (ostream->priv->doc)); gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (ostream->priv->doc));
gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (ostream->priv->doc), gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (ostream->priv->doc), &ostream->priv->pos);
&ostream->priv->pos); ostream->priv->is_initialized = TRUE;
ostream->priv->is_initialized = TRUE; }
}
if (ostream->priv->buflen > 0) if (ostream->priv->buflen > 0)
{ {
len = ostream->priv->buflen + count; len = ostream->priv->buflen + count;
text = g_new (gchar , len + 1); text = g_new (gchar , len + 1);
memcpy (text, ostream->priv->buffer, ostream->priv->buflen); memcpy (text, ostream->priv->buffer, ostream->priv->buflen);
memcpy (text + ostream->priv->buflen, buffer, count); memcpy (text + ostream->priv->buflen, buffer, count);
text[len] = '\0'; text[len] = '\0';
g_free (ostream->priv->buffer); g_free (ostream->priv->buffer);
ostream->priv->buffer = NULL; ostream->priv->buffer = NULL;
ostream->priv->buflen = 0; ostream->priv->buflen = 0;
freetext = TRUE; freetext = TRUE;
} }
else else
{ {
text = (gchar *) buffer; text = (gchar *) buffer;
len = count; len = count;
} }
/* validate */ /* validate */
valid = g_utf8_validate (text, len, &end); valid = g_utf8_validate (text, len, &end);
/* Avoid keeping a CRLF across two buffers. */ /* Avoid keeping a CRLF across two buffers. */
if (valid && len > 1 && end[-1] == '\r') if (valid && len > 1 && end[-1] == '\r')
{ {
valid = FALSE; valid = FALSE;
end--; end--;
} }
if (!valid) if (!valid)
{ {
gsize nvalid = end - text; gsize nvalid = end - text;
gsize remainder = len - nvalid; gsize remainder = len - nvalid;
gunichar ch; gunichar ch;
if ((remainder < MAX_UNICHAR_LEN) && if ((remainder < MAX_UNICHAR_LEN) &&
((ch = g_utf8_get_char_validated (text + nvalid, remainder)) == (gunichar)-2 || ((ch = g_utf8_get_char_validated (text + nvalid, remainder)) == (gunichar)-2 ||
ch == (gunichar)'\r')) ch == (gunichar)'\r'))
{ {
ostream->priv->buffer = g_strndup (end, remainder); ostream->priv->buffer = g_strndup (end, remainder);
ostream->priv->buflen = remainder; ostream->priv->buflen = remainder;
len -= remainder; len -= remainder;
} }
else else
{ {
/* TODO: we could escape invalid text and tag it in red /* TODO: we could escape invalid text and tag it in red
* and make the doc readonly. * and make the doc readonly.
*/ */
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, _("Invalid UTF-8 sequence in input"));
_("Invalid UTF-8 sequence in input"));
if (freetext) if (freetext)
g_free (text); {
g_free (text);
}
return -1; return -1;
} }
} }
gtk_text_buffer_insert (GTK_TEXT_BUFFER (ostream->priv->doc), gtk_text_buffer_insert (GTK_TEXT_BUFFER (ostream->priv->doc), &ostream->priv->pos, text, len);
&ostream->priv->pos, text, len);
if (freetext) if (freetext)
g_free (text); {
g_free (text);
}
return count; return count;
} }
static gboolean static gboolean
xed_document_output_stream_flush (GOutputStream *stream, xed_document_output_stream_flush (GOutputStream *stream,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
XedDocumentOutputStream *ostream = XED_DOCUMENT_OUTPUT_STREAM (stream); XedDocumentOutputStream *ostream = XED_DOCUMENT_OUTPUT_STREAM (stream);
/* Flush deferred data if some. */ /* Flush deferred data if some. */
if (!ostream->priv->is_closed && ostream->priv->is_initialized && if (!ostream->priv->is_closed && ostream->priv->is_initialized &&
ostream->priv->buflen > 0 && ostream->priv->buflen > 0 &&
xed_document_output_stream_write (stream, "", 0, cancellable, xed_document_output_stream_write (stream, "", 0, cancellable, error) == -1)
error) == -1) {
return FALSE; return FALSE;
}
return TRUE; return TRUE;
} }
static gboolean static gboolean
xed_document_output_stream_close (GOutputStream *stream, xed_document_output_stream_close (GOutputStream *stream,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
XedDocumentOutputStream *ostream = XED_DOCUMENT_OUTPUT_STREAM (stream); XedDocumentOutputStream *ostream = XED_DOCUMENT_OUTPUT_STREAM (stream);
if (!ostream->priv->is_closed && ostream->priv->is_initialized) if (!ostream->priv->is_closed && ostream->priv->is_initialized)
{ {
end_append_text_to_document (ostream); end_append_text_to_document (ostream);
ostream->priv->is_closed = TRUE; ostream->priv->is_closed = TRUE;
} }
if (ostream->priv->buflen > 0) if (ostream->priv->buflen > 0)
{ {
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, _("Incomplete UTF-8 sequence in input"));
_("Incomplete UTF-8 sequence in input")); return FALSE;
return FALSE; }
}
return TRUE; return TRUE;
} }