3860b8987cc805e6226fddbcb1cdcd8b01f4475f
[pspp] / src / language / syntax-string-source.c
1 /* PSPPIRE - a graphical interface for PSPP.
2    Copyright (C) 2007, 2009, 2010 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/cast.h>
21 #include <libpspp/getl.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 #include "xalloc.h"
30
31 struct syntax_string_source
32   {
33     struct getl_interface parent;
34     struct string buffer;
35     size_t posn;
36   };
37
38
39 static bool
40 always_false (const struct getl_interface *i UNUSED)
41 {
42   return false;
43 }
44
45 /* Returns the name of the source */
46 static const char *
47 name (const struct getl_interface *i UNUSED)
48 {
49   return NULL;
50 }
51
52
53 /* Returns the location within the source */
54 static int
55 location (const struct getl_interface *i UNUSED)
56 {
57   return -1;
58 }
59
60
61 static void
62 do_close (struct getl_interface *i )
63 {
64   struct syntax_string_source *sss = UP_CAST (i, struct syntax_string_source,
65                                               parent);
66
67   ds_destroy (&sss->buffer);
68
69   free (sss);
70 }
71
72
73
74 static bool
75 read_single_line (struct getl_interface *i,
76                   struct string *line)
77 {
78   struct syntax_string_source *sss = UP_CAST (i, struct syntax_string_source,
79                                               parent);
80
81   size_t next;
82
83   if ( sss->posn == -1)
84     return false;
85
86   next = ss_find_char (ds_substr (&sss->buffer,
87                                   sss->posn, -1), '\n');
88
89   ds_assign_substring (line,
90                        ds_substr (&sss->buffer,
91                                   sss->posn,
92                                   next)
93                        );
94
95   if ( next != -1 )
96     sss->posn += next + 1; /* + 1 to skip newline */
97   else
98     sss->posn = -1; /* End of file encountered */
99
100   return true;
101 }
102
103 static struct syntax_string_source *
104 create_syntax_string_source__ (void)
105 {
106   struct syntax_string_source *sss = xzalloc (sizeof *sss);
107
108   sss->posn = 0;
109
110   sss->parent.interactive = always_false;
111   sss->parent.close = do_close;
112   sss->parent.read = read_single_line;
113
114   sss->parent.name = name;
115   sss->parent.location = location;
116
117   return sss;
118 }
119
120 struct getl_interface *
121 create_syntax_string_source (const char *s)
122 {
123   struct syntax_string_source *sss = create_syntax_string_source__ ();
124   ds_init_cstr (&sss->buffer, s);
125   return &sss->parent;
126 }
127
128 struct getl_interface *
129 create_syntax_format_source (const char *format, ...)
130 {
131   struct syntax_string_source *sss;
132   va_list args;
133
134   sss = create_syntax_string_source__ ();
135
136   ds_init_empty (&sss->buffer);
137
138   va_start (args, format);
139   ds_put_vformat (&sss->buffer, format, args);
140   va_end (args);
141
142   return &sss->parent;
143 }
144
145 /* Return the syntax currently contained in S.
146    Primarily usefull for debugging */
147 const char *
148 syntax_string_source_get_syntax (const struct syntax_string_source *s)
149 {
150   return ds_cstr (&s->buffer);
151 }