Implemented the INSERT command.
[pspp] / 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 {
75   struct syntax_string_source *sss = (struct syntax_string_source *) i;
76
77   size_t next;
78
79   if ( sss->posn == -1)
80     return false;
81
82   next = ss_find_char (ds_substr (&sss->buffer,
83                                   sss->posn, -1), '\n');
84
85   ds_assign_substring (line,
86                        ds_substr (&sss->buffer,
87                                   sss->posn,
88                                   next)
89                        );
90
91   if ( next != -1 )
92     sss->posn += next + 1; /* + 1 to skip newline */
93   else
94     sss->posn = -1; /* End of file encountered */
95
96   return true;
97 }
98
99 struct getl_interface *
100 create_syntax_string_source (const char *format, ...)
101 {
102   va_list args;
103
104   struct syntax_string_source *sss = xzalloc (sizeof *sss);
105
106   sss->posn = 0;
107
108   ds_init_empty (&sss->buffer);
109
110   va_start (args, format);
111   ds_put_vformat (&sss->buffer, format, args);
112   va_end (args);
113
114   sss->parent.interactive = always_false;
115   sss->parent.close = do_close;
116   sss->parent.read = read_single_line;
117
118   sss->parent.name = name;
119   sss->parent.location = location;
120
121
122   return (struct getl_interface *) sss;
123 }
124
125 /* Return the syntax currently contained in S.
126    Primarily usefull for debugging */
127 const char *
128 syntax_string_source_get_syntax (const struct syntax_string_source *s)
129 {
130   return ds_cstr (&s->buffer);
131 }