Add MCONVERT file
[pspp] / src / language / data-io / mconvert.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2021 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 #include <config.h>
18
19 #include "data/dataset.h"
20 #include "language/data-io/file-handle.h"
21 #include "language/lexer/lexer.h"
22 #include "language/command.h"
23
24 #include "gettext.h"
25 #define _(msgid) gettext (msgid)
26
27 int
28 cmd_mconvert (struct lexer *lexer, struct dataset *ds)
29 {
30   bool append = false;
31   struct file_handle *in = NULL;
32   struct file_handle *out = NULL;
33   while (lex_token (lexer) != T_ENDCMD)
34     {
35       if (lex_match_id (lexer, "APPEND"))
36         append = true;
37       else if (lex_match_id (lexer, "REPLACE"))
38         append = false;
39       else
40         {
41           if (lex_match_id (lexer, "MATRIX"))
42             lex_match (lexer, T_EQUALS);
43
44           struct file_handle **fhp = (lex_match_id (lexer, "IN") ? &in
45                                       : lex_match_id (lexer, "OUT") ? &out
46                                       : NULL);
47           if (!fhp)
48             {
49               lex_error_expecting (lexer, "IN", "OUT", "APPEND", "REPLACE");
50               goto error;
51             }
52           if (!lex_force_match (lexer, T_LPAREN))
53             goto error;
54
55           fh_unref (*fhp);
56           if (lex_match (lexer, T_ASTERISK))
57             *fhp = NULL;
58           else
59             {
60               *fhp = fh_parse (lexer, FH_REF_FILE, dataset_session (ds));
61               if (!*fhp)
62                 goto error;
63             }
64
65           if (!lex_force_match (lexer, T_RPAREN))
66             goto error;
67         }
68
69       lex_match (lexer, T_SLASH);
70     }
71
72   if (!in && !dataset_has_source (ds))
73     {
74       msg (SE, _("No active file is defined and no external file is "
75                  "specified on MATRIX=IN."));
76       goto error;
77     }
78
79   /* XXX */
80
81 error:
82   fh_unref (in);
83   fh_unref (out);
84   return CMD_FAILURE;
85 }
86