405141cd50a7fd1734f387281b5b344d5a7e74f9
[pspp] / src / language / syntax-string-source.c
1 /* PSPPIRE - a graphical interface for PSPP.
2    Copyright (C) 2007, 2009 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 struct getl_interface *
104 create_syntax_string_source (const char *format, ...)
105 {
106   va_list args;
107
108   struct syntax_string_source *sss = xzalloc (sizeof *sss);
109
110   sss->posn = 0;
111
112   ds_init_empty (&sss->buffer);
113
114   va_start (args, format);
115   ds_put_vformat (&sss->buffer, format, args);
116   va_end (args);
117
118   sss->parent.interactive = always_false;
119   sss->parent.close = do_close;
120   sss->parent.read = read_single_line;
121
122   sss->parent.name = name;
123   sss->parent.location = location;
124
125
126   return &sss->parent;
127 }
128
129 /* Return the syntax currently contained in S.
130    Primarily usefull for debugging */
131 const char *
132 syntax_string_source_get_syntax (const struct syntax_string_source *s)
133 {
134   return ds_cstr (&s->buffer);
135 }