Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / syntax-string-source.c
1 /* PSPPIRE - a graphical interface for PSPP.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include <libpspp/getl.h>
21 #include <libpspp/alloc.h>
22 #include <libpspp/compiler.h>
23 #include <libpspp/str.h>
24
25 #include <stdlib.h>
26
27 #include "syntax-string-source.h"
28
29 struct syntax_string_source
30   {
31     struct getl_interface parent;
32     struct string buffer;
33     size_t posn;
34   };
35
36
37 static bool
38 always_false (const struct getl_interface *i UNUSED)
39 {
40   return false;
41 }
42
43 /* Returns the name of the source */
44 static const char *
45 name (const struct getl_interface *i UNUSED)
46 {
47   return NULL;
48 }
49
50
51 /* Returns the location within the source */
52 static int
53 location (const struct getl_interface *i UNUSED)
54 {
55   return -1;
56 }
57
58
59 static void
60 do_close (struct getl_interface *i )
61 {
62   struct syntax_string_source *sss = (struct syntax_string_source *) i;
63
64   ds_destroy (&sss->buffer);
65
66   free (sss);
67 }
68
69
70
71 static bool
72 read_single_line (struct getl_interface *i,
73                   struct string *line,
74                   enum getl_syntax *syntax_rules UNUSED)
75 {
76   struct syntax_string_source *sss = (struct syntax_string_source *) i;
77
78   size_t next;
79
80   if ( sss->posn == -1)
81     return false;
82
83   next = ss_find_char (ds_substr (&sss->buffer,
84                                   sss->posn, -1), '\n');
85
86   ds_assign_substring (line,
87                        ds_substr (&sss->buffer,
88                                   sss->posn,
89                                   next)
90                        );
91
92   if ( next != -1 )
93     sss->posn += next + 1; /* + 1 to skip newline */
94   else
95     sss->posn = -1; /* End of file encountered */
96
97   return true;
98 }
99
100 struct getl_interface *
101 create_syntax_string_source (const char *format, ...)
102 {
103   va_list args;
104
105   struct syntax_string_source *sss = xzalloc (sizeof *sss);
106
107   sss->posn = 0;
108
109   ds_init_empty (&sss->buffer);
110
111   va_start (args, format);
112   ds_put_vformat (&sss->buffer, format, args);
113   va_end (args);
114
115   sss->parent.interactive = always_false;
116   sss->parent.close = do_close;
117   sss->parent.read = read_single_line;
118
119   sss->parent.name = name;
120   sss->parent.location = location;
121
122
123   return (struct getl_interface *) sss;
124 }
125
126 /* Return the syntax currently contained in S.
127    Primarily usefull for debugging */
128 const char *
129 syntax_string_source_get_syntax (const struct syntax_string_source *s)
130 {
131   return ds_cstr (&s->buffer);
132 }