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