summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Monnerat <[email protected]>2015-10-08 18:00:51 +0200
committerraveit65 <[email protected]>2015-10-13 14:05:36 +0200
commit43903ad12b880e64a3d0551b90b27aa4621a2ab8 (patch)
tree011a52dca7980140982a6404b976b0c1f656267d
parent78101257f40bbf8a0d7b606bee36e527f9b0ea11 (diff)
downloadpluma-43903ad12b880e64a3d0551b90b27aa4621a2ab8.tar.bz2
pluma-43903ad12b880e64a3d0551b90b27aa4621a2ab8.tar.xz
CRLF across 8k boundary inserts an empty line.
fixes issue#100 This introduces buffering of a trailing, non-leading CR for each buffer passed to pluma_document_output_write(), for later possible recombination into a CRLF. Since this buffering has to be flushed at EOF, a flush method has been added to the class. It is implicitly called at document close time.
-rw-r--r--pluma/pluma-document-output-stream.c42
1 files changed, 35 insertions, 7 deletions
diff --git a/pluma/pluma-document-output-stream.c b/pluma/pluma-document-output-stream.c
index 06aa728d..ae9e66bf 100644
--- a/pluma/pluma-document-output-stream.c
+++ b/pluma/pluma-document-output-stream.c
@@ -66,6 +66,10 @@ static gssize pluma_document_output_stream_write (GOutputStream *stre
GCancellable *cancellable,
GError **error);
+static gboolean pluma_document_output_stream_flush (GOutputStream *stream,
+ GCancellable *cancellable,
+ GError **error);
+
static gboolean pluma_document_output_stream_close (GOutputStream *stream,
GCancellable *cancellable,
GError **error);
@@ -154,6 +158,7 @@ pluma_document_output_stream_class_init (PlumaDocumentOutputStreamClass *klass)
object_class->constructed = pluma_document_output_stream_constructed;
stream_class->write_fn = pluma_document_output_stream_write;
+ stream_class->flush = pluma_document_output_stream_flush;
stream_class->close_fn = pluma_document_output_stream_close;
g_object_class_install_property (object_class,
@@ -290,7 +295,6 @@ pluma_document_output_stream_write (GOutputStream *stream,
gsize len;
gboolean freetext = FALSE;
const gchar *end;
- gsize nvalid;
gboolean valid;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
@@ -328,16 +332,23 @@ pluma_document_output_stream_write (GOutputStream *stream,
/* validate */
valid = g_utf8_validate (text, len, &end);
- nvalid = end - text;
- if (!valid)
+ /* Avoid keeping a CRLF across two buffers. */
+ if (valid && len > 1 && end[-1] == '\r')
{
- gsize remainder;
+ valid = FALSE;
+ end--;
+ }
- remainder = len - nvalid;
+ if (!valid)
+ {
+ gsize nvalid = end - text;
+ gsize remainder = len - nvalid;
+ gunichar ch;
if ((remainder < MAX_UNICHAR_LEN) &&
- (g_utf8_get_char_validated (text + nvalid, remainder) == (gunichar)-2))
+ ((ch = g_utf8_get_char_validated (text + nvalid, remainder)) == (gunichar)-2 ||
+ ch == (gunichar)'\r'))
{
ostream->priv->buffer = g_strndup (end, remainder);
ostream->priv->buflen = remainder;
@@ -345,7 +356,7 @@ pluma_document_output_stream_write (GOutputStream *stream,
}
else
{
- /* TODO: we cuould escape invalid text and tag it in red
+ /* TODO: we could escape invalid text and tag it in red
* and make the doc readonly.
*/
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
@@ -368,6 +379,23 @@ pluma_document_output_stream_write (GOutputStream *stream,
}
static gboolean
+pluma_document_output_stream_flush (GOutputStream *stream,
+ GCancellable *cancellable,
+ GError **error)
+{
+ PlumaDocumentOutputStream *ostream = PLUMA_DOCUMENT_OUTPUT_STREAM (stream);
+
+ /* Flush deferred data if some. */
+ if (!ostream->priv->is_closed && ostream->priv->is_initialized &&
+ ostream->priv->buflen > 0 &&
+ pluma_document_output_stream_write (stream, "", 0, cancellable,
+ error) == -1)
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean
pluma_document_output_stream_close (GOutputStream *stream,
GCancellable *cancellable,
GError **error)