1 /* PSPPIRE - a graphical interface for PSPP.
2 Copyright (C) 2007 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/>. */
20 #include <libpspp/getl.h>
21 #include <libpspp/alloc.h>
22 #include <libpspp/compiler.h>
23 #include <libpspp/str.h>
27 #include "syntax-string-source.h"
29 struct syntax_string_source
31 struct getl_interface parent;
38 always_false (const struct getl_interface *i UNUSED)
43 /* Returns the name of the source */
45 name (const struct getl_interface *i UNUSED)
51 /* Returns the location within the source */
53 location (const struct getl_interface *i UNUSED)
60 do_close (struct getl_interface *i )
62 struct syntax_string_source *sss = (struct syntax_string_source *) i;
64 ds_destroy (&sss->buffer);
72 read_single_line (struct getl_interface *i,
75 struct syntax_string_source *sss = (struct syntax_string_source *) i;
82 next = ss_find_char (ds_substr (&sss->buffer,
83 sss->posn, -1), '\n');
85 ds_assign_substring (line,
86 ds_substr (&sss->buffer,
92 sss->posn += next + 1; /* + 1 to skip newline */
94 sss->posn = -1; /* End of file encountered */
99 struct getl_interface *
100 create_syntax_string_source (const char *format, ...)
104 struct syntax_string_source *sss = xzalloc (sizeof *sss);
108 ds_init_empty (&sss->buffer);
110 va_start (args, format);
111 ds_put_vformat (&sss->buffer, format, args);
114 sss->parent.interactive = always_false;
115 sss->parent.close = do_close;
116 sss->parent.read = read_single_line;
118 sss->parent.name = name;
119 sss->parent.location = location;
122 return (struct getl_interface *) sss;
125 /* Return the syntax currently contained in S.
126 Primarily usefull for debugging */
128 syntax_string_source_get_syntax (const struct syntax_string_source *s)
130 return ds_cstr (&s->buffer);