011deed39d3cc5ef0e9e33058766c9bd4e378808
[pspp] / src / data / dataset-writer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2009-2011 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-writer.h"
20
21 #include <stdlib.h>
22
23 #include "data/case.h"
24 #include "data/case-map.h"
25 #include "data/casereader.h"
26 #include "data/casewriter-provider.h"
27 #include "data/casewriter.h"
28 #include "data/dataset.h"
29 #include "data/dictionary.h"
30 #include "data/file-handle-def.h"
31 #include "data/variable.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/taint.h"
34
35 #include "gl/xalloc.h"
36
37 #define N_(msgid) (msgid)
38
39 /* A dataset file writer. */
40 struct dataset_writer
41   {
42     struct dataset *ds;                 /* Underlying dataset. */
43     struct fh_lock *lock;               /* Exclusive access to file handle. */
44     struct dictionary *dict;            /* Dictionary for subwriter. */
45     struct case_map *compactor;         /* Compacts into dictionary. */
46     struct casewriter *subwriter;       /* Data output. */
47   };
48
49 static const struct casewriter_class dataset_writer_casewriter_class;
50
51 /* Opens FH, which must have referent type FH_REF_DATASET, and
52    returns a dataset_writer for it, or a null pointer on
53    failure.  Cases stored in the dataset_writer will be expected
54    to be drawn from DICTIONARY. */
55 struct casewriter *
56 dataset_writer_open (struct file_handle *fh,
57                      const struct dictionary *dictionary)
58 {
59   struct dataset_writer *writer;
60   struct casewriter *casewriter;
61   struct fh_lock *lock;
62
63   /* Get exclusive write access to handle. */
64   /* TRANSLATORS: this fragment will be interpolated into
65      messages in fh_lock() that identify types of files. */
66   lock = fh_lock (fh, FH_REF_DATASET, N_("dataset"), FH_ACC_WRITE, true);
67   if (lock == NULL)
68     return NULL;
69
70   /* Create writer. */
71   writer = xmalloc (sizeof *writer);
72   writer->lock = lock;
73   writer->ds = fh_get_dataset (fh);
74
75   writer->dict = dict_clone (dictionary);
76   dict_delete_scratch_vars (writer->dict);
77   if (dict_count_values (writer->dict, 0) < dict_get_n_vars (writer->dict))
78     {
79       writer->compactor = case_map_to_compact_dict (writer->dict, 0);
80       dict_compact_values (writer->dict);
81     }
82   else
83     writer->compactor = NULL;
84   writer->subwriter = autopaging_writer_create (dict_get_proto (writer->dict));
85
86   casewriter = casewriter_create (dict_get_proto (writer->dict),
87                                   &dataset_writer_casewriter_class, writer);
88   taint_propagate (casewriter_get_taint (writer->subwriter),
89                    casewriter_get_taint (casewriter));
90   return casewriter;
91 }
92
93 /* Writes case C to WRITER. */
94 static void
95 dataset_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_,
96                                  struct ccase *c)
97 {
98   struct dataset_writer *writer = writer_;
99   casewriter_write (writer->subwriter,
100                     case_map_execute (writer->compactor, c));
101 }
102
103 /* Closes WRITER. */
104 static void
105 dataset_writer_casewriter_destroy (struct casewriter *w UNUSED, void *writer_)
106 {
107   struct dataset_writer *writer = writer_;
108   struct casereader *reader = casewriter_make_reader (writer->subwriter);
109   if (!casereader_error (reader))
110     {
111       dataset_set_dict (writer->ds, writer->dict);
112       dataset_set_source (writer->ds, reader);
113     }
114   else
115     {
116       casereader_destroy (reader);
117       dict_unref (writer->dict);
118     }
119
120   fh_unlock (writer->lock);
121   free (writer);
122 }
123
124 static const struct casewriter_class dataset_writer_casewriter_class =
125   {
126     dataset_writer_casewriter_write,
127     dataset_writer_casewriter_destroy,
128     NULL,
129   };