From 5825b0b870739af3760c07138bed50e99db788ae Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 23 Oct 2011 19:44:42 +0200 Subject: [PATCH] New object: psppire-lex-reader This object reads lexer input direct from a GtkBuffer instead of converting to a string first. --- src/ui/gui/automake.mk | 2 + src/ui/gui/psppire-lex-reader.c | 106 +++++++++++++++++++++++++++++ src/ui/gui/psppire-lex-reader.h | 26 +++++++ src/ui/gui/psppire-syntax-window.c | 6 +- 4 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 src/ui/gui/psppire-lex-reader.c create mode 100644 src/ui/gui/psppire-lex-reader.h diff --git a/src/ui/gui/automake.mk b/src/ui/gui/automake.mk index 116e2be3..81010218 100644 --- a/src/ui/gui/automake.mk +++ b/src/ui/gui/automake.mk @@ -217,6 +217,8 @@ src_ui_gui_psppire_SOURCES = \ src/ui/gui/psppire-encoding-selector.h \ src/ui/gui/psppire-hbuttonbox.h \ src/ui/gui/psppire-keypad.h \ + src/ui/gui/psppire-lex-reader.c \ + src/ui/gui/psppire-lex-reader.h \ src/ui/gui/psppire-output-window.c \ src/ui/gui/psppire-output-window.h \ src/ui/gui/psppire-var-view.c \ diff --git a/src/ui/gui/psppire-lex-reader.c b/src/ui/gui/psppire-lex-reader.c new file mode 100644 index 00000000..5afd1015 --- /dev/null +++ b/src/ui/gui/psppire-lex-reader.c @@ -0,0 +1,106 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* A lex_reader object to read characters directly from a GtkTextBuffer */ + +#include + +#include "psppire-lex-reader.h" +#include "src/language/lexer/lexer.h" + +#include + + +#include "libpspp/cast.h" + +static const struct lex_reader_class lex_gtk_text_buffer_reader_class ; + + +struct lex_gtk_text_buffer_reader +{ + struct lex_reader reader; + GtkTextBuffer *buffer; + GtkTextIter start; + GtkTextIter stop; +}; + +static struct lex_gtk_text_buffer_reader * +lex_gtk_text_buffer_reader_cast (struct lex_reader *r) +{ + return UP_CAST (r, struct lex_gtk_text_buffer_reader, reader); +} + + +struct lex_reader * +lex_reader_for_gtk_text_buffer (GtkTextBuffer *buffer, GtkTextIter start, GtkTextIter stop) +{ + struct lex_gtk_text_buffer_reader *r = xmalloc (sizeof *r); + + lex_reader_init (&r->reader, &lex_gtk_text_buffer_reader_class); + + r->buffer = buffer; + g_object_ref (buffer); + + r->start = start; + r->stop = stop; + + return &r->reader; +} + + +static size_t +lex_gtk_text_buffer_read (struct lex_reader *r_, char *buf, size_t n, + 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; + + 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; + + gtk_text_iter_set_offset (&iter, offset + n_chars); + + s = gtk_text_iter_get_text (&r->start, &iter); + + strcpy (buf, s); + + r->start = iter; + + return strlen (s); +} + + + +static void +lex_gtk_text_buffer_close (struct lex_reader *r_) +{ + struct lex_gtk_text_buffer_reader *r = lex_gtk_text_buffer_reader_cast (r_); + + g_object_unref (r->buffer); +} + + +static const struct lex_reader_class lex_gtk_text_buffer_reader_class = + { + lex_gtk_text_buffer_read, + lex_gtk_text_buffer_close + }; diff --git a/src/ui/gui/psppire-lex-reader.h b/src/ui/gui/psppire-lex-reader.h new file mode 100644 index 00000000..954d78c4 --- /dev/null +++ b/src/ui/gui/psppire-lex-reader.h @@ -0,0 +1,26 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef LEX_READER_H +#define LEX_READER_H + +#include + +struct lex_reader; + +struct lex_reader *lex_reader_for_gtk_text_buffer (GtkTextBuffer *buffer, GtkTextIter start, GtkTextIter stop); + +#endif diff --git a/src/ui/gui/psppire-syntax-window.c b/src/ui/gui/psppire-syntax-window.c index cd4b62da..1ad906a1 100644 --- a/src/ui/gui/psppire-syntax-window.c +++ b/src/ui/gui/psppire-syntax-window.c @@ -33,6 +33,7 @@ #include "ui/gui/helper.h" #include "ui/gui/psppire-data-window.h" #include "ui/gui/psppire-encoding-selector.h" +#include "ui/gui/psppire-lex-reader.h" #include "ui/gui/psppire-syntax-window.h" #include "ui/gui/psppire-window-register.h" #include "ui/gui/psppire.h" @@ -248,10 +249,7 @@ editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start, GtkTextIter stop) { PsppireWindow *win = PSPPIRE_WINDOW (sw); - struct lex_reader *reader; - gchar *text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (sw->buffer), &start, &stop, FALSE); - reader = lex_reader_for_string (text); - g_free (text); + struct lex_reader *reader = lex_reader_for_gtk_text_buffer (GTK_TEXT_BUFFER (sw->buffer), start, stop); lex_reader_set_file_name (reader, psppire_window_get_filename (win)); -- 2.30.2