c3e8d4ffd1713718ea3a0a3e7a27d9258c9a5e94
[pspp-builds.git] / src / language / syntax-string-source.c
1 /*
2   PSPPIRE --- A Graphical User Interface for PSPP
3   Copyright (C) 2007  Free Software Foundation
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18   02110-1301, USA. */
19
20
21 #include <config.h>
22
23 #include <libpspp/getl.h>
24 #include <libpspp/alloc.h>
25 #include <libpspp/compiler.h>
26 #include <libpspp/str.h>
27
28 #include <stdlib.h>
29
30 #include "syntax-string-source.h"
31
32 struct syntax_string_source
33   {
34     struct getl_interface parent;
35     struct string buffer;
36     size_t posn;
37   };
38
39
40 static bool
41 always_false (const struct getl_interface *i UNUSED)
42 {
43   return false;
44 }
45
46 /* Returns the name of the source */
47 static const char *
48 name (const struct getl_interface *i UNUSED)
49 {
50   return NULL;
51 }
52
53
54 /* Returns the location within the source */
55 static int
56 location (const struct getl_interface *i UNUSED)
57 {
58   return -1;
59 }
60
61
62 static void
63 do_close (struct getl_interface *i )
64 {
65   struct syntax_string_source *sss = (struct syntax_string_source *) i;
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                   enum getl_syntax *syntax_rules UNUSED)
78 {
79   struct syntax_string_source *sss = (struct syntax_string_source *) i;
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 (struct getl_interface *) sss;
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 }