pxd: initial work
[pspp] / src / language / data-io / session.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2012 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/case.h"
20 #include "data/dataset.h"
21 #include "data/dictionary.h"
22 #include "language/command.h"
23 #include "language/data-io/file-handle.h"
24 #include "language/lexer/lexer.h"
25 #include "libpspp/pxd.h"
26
27 #include "gl/xalloc.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 int
33 cmd_save_session (struct lexer *lexer, struct dataset *ds)
34 {
35   struct file_handle *handle;
36   struct pxd_object *obj;
37   struct pxd_id root;
38   struct pxd *pxd;
39
40   if (!lex_force_match_id (lexer, "OUTFILE"))
41     return CMD_FAILURE;
42   lex_match (lexer, '=');
43
44   handle = fh_parse (lexer, FH_REF_FILE, dataset_session (ds));
45   if (handle == NULL)
46     return CMD_FAILURE;
47
48   pxd = pxd_open (fh_get_file_name (handle), true);
49   obj = dict_save (dataset_dict (ds), pxd);
50   pxd_get_root (pxd, &root);
51   pxd_swap_root (pxd, &root, obj);
52   pxd_close (pxd);
53
54   return CMD_SUCCESS;
55 }
56
57 int
58 cmd_get_session (struct lexer *lexer, struct dataset *ds)
59 {
60   struct file_handle *handle;
61   struct pxd_object *obj;
62   struct pxd_id root;
63   struct pxd *pxd;
64
65   if (lex_match_id (lexer, "FILE"))
66     lex_match (lexer, '=');
67
68   handle = fh_parse (lexer, FH_REF_FILE, dataset_session (ds));
69   if (handle == NULL)
70     return CMD_FAILURE;
71
72   pxd = pxd_open (fh_get_file_name (handle), false);
73   pxd_get_root (pxd, &root);
74   obj = pxd_fetch (pxd, &root);
75   dict_destroy (dict_load (obj, pxd));
76   pxd_close (pxd);
77
78   return CMD_SUCCESS;
79 }