1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* A lex_reader object to read characters directly from a GtkTextBuffer */
21 #include "psppire-lex-reader.h"
22 #include "src/language/lexer/lexer.h"
27 #include "libpspp/cast.h"
29 #include "gl/minmax.h"
31 static const struct lex_reader_class lex_gtk_text_buffer_reader_class ;
34 struct lex_gtk_text_buffer_reader
36 struct lex_reader reader;
38 /* The GtkTextBuffer from which we are reading. */
39 GtkTextBuffer *buffer;
43 /* Text pulled from part of the GtkTextBuffer. */
45 gsize part_len; /* Number of bytes in 'part'. */
46 gsize part_ofs; /* Current offset into 'part'. */
49 static struct lex_gtk_text_buffer_reader *
50 lex_gtk_text_buffer_reader_cast (struct lex_reader *r)
52 return UP_CAST (r, struct lex_gtk_text_buffer_reader, reader);
57 lex_reader_for_gtk_text_buffer (GtkTextBuffer *buffer, GtkTextIter start, GtkTextIter stop)
59 struct lex_gtk_text_buffer_reader *r = xmalloc (sizeof *r);
61 lex_reader_init (&r->reader, &lex_gtk_text_buffer_reader_class);
64 g_object_ref (buffer);
78 lex_gtk_text_buffer_read (struct lex_reader *r_, char *buf, size_t n,
79 enum prompt_style prompt_style UNUSED)
81 struct lex_gtk_text_buffer_reader *r = lex_gtk_text_buffer_reader_cast (r_);
84 if (r->part_ofs == r->part_len)
86 /* Read up to N characters into r->part. N characters might be more than
87 N bytes, but that's OK: we'll just buffer up some of those bytes for
91 GtkTextIter iter = r->start ;
93 int offset = gtk_text_iter_get_offset (&iter);
94 int end_offset = gtk_text_iter_get_offset (&r->stop);
96 if ( end_offset - offset < n)
97 n_chars = end_offset - offset;
99 gtk_text_iter_set_offset (&iter, offset + n_chars);
102 r->part = gtk_text_iter_get_text (&r->start, &iter);
103 r->part_len = strlen (r->part);
109 chunk = MIN (r->part_len - r->part_ofs, n);
110 memcpy (buf, r->part + r->part_ofs, chunk);
111 r->part_ofs += chunk;
119 lex_gtk_text_buffer_close (struct lex_reader *r_)
121 struct lex_gtk_text_buffer_reader *r = lex_gtk_text_buffer_reader_cast (r_);
123 g_object_unref (r->buffer);
128 static const struct lex_reader_class lex_gtk_text_buffer_reader_class =
130 lex_gtk_text_buffer_read,
131 lex_gtk_text_buffer_close