#include "libpspp/cast.h"
+#include "gl/minmax.h"
+
static const struct lex_reader_class lex_gtk_text_buffer_reader_class ;
struct lex_gtk_text_buffer_reader
{
struct lex_reader reader;
+
+ /* The GtkTextBuffer from which we are reading. */
GtkTextBuffer *buffer;
GtkTextIter start;
GtkTextIter stop;
+
+ /* Text pulled from part of the GtkTextBuffer. */
+ gchar *part;
+ gsize part_len; /* Number of bytes in 'part'. */
+ gsize part_ofs; /* Current offset into 'part'. */
};
static struct lex_gtk_text_buffer_reader *
r->start = start;
r->stop = stop;
+ r->part = NULL;
+ r->part_len = 0;
+ r->part_ofs = 0;
+
return &r->reader;
}
enum prompt_style prompt_style UNUSED)
{
struct lex_gtk_text_buffer_reader *r = lex_gtk_text_buffer_reader_cast (r_);
- int n_chars = n;
- char *s;
+ gsize chunk;
+
+ if (r->part_ofs == r->part_len)
+ {
+ /* Read up to N characters into r->part. N characters might be more than
+ N bytes, but that's OK: we'll just buffer up some of those bytes for
+ the next read. */
+ int n_chars = n;
+
+ GtkTextIter iter = r->start ;
+
+ int offset = gtk_text_iter_get_offset (&iter);
+ int end_offset = gtk_text_iter_get_offset (&r->stop);
- GtkTextIter iter = r->start ;
-
- int offset = gtk_text_iter_get_offset (&iter);
- int end_offset = gtk_text_iter_get_offset (&r->stop);
+ if ( end_offset - offset < n)
+ n_chars = end_offset - offset;
- if ( end_offset - offset < n)
- n_chars = end_offset - offset;
-
- gtk_text_iter_set_offset (&iter, offset + n_chars);
+ gtk_text_iter_set_offset (&iter, offset + n_chars);
- s = gtk_text_iter_get_text (&r->start, &iter);
+ g_free (r->part);
+ r->part = gtk_text_iter_get_text (&r->start, &iter);
+ r->part_len = strlen (r->part);
+ r->part_ofs = 0;
- strncpy (buf, s, n_chars);
+ r->start = iter;
+ }
- r->start = iter;
+ chunk = MIN (r->part_len - r->part_ofs, n);
+ memcpy (buf, r->part + r->part_ofs, chunk);
+ r->part_ofs += chunk;
- return strlen (s);
+ return chunk;
}
struct lex_gtk_text_buffer_reader *r = lex_gtk_text_buffer_reader_cast (r_);
g_object_unref (r->buffer);
+ g_free (r->part);
}