Update GPG key for signing flatpaks.
[pspp] / data / scratch-writer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 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 "scratch-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/dictionary.h>
29 #include <data/file-handle-def.h>
30 #include <data/scratch-handle.h>
31 #include <data/variable.h>
32 #include <libpspp/compiler.h>
33 #include <libpspp/taint.h>
34
35 #include "xalloc.h"
36
37 #define N_(msgid) (msgid)
38
39 /* A scratch file writer. */
40 struct scratch_writer
41   {
42     struct file_handle *fh;             /* Underlying file handle. */
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 scratch_writer_casewriter_class;
50
51 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
52    returns a scratch_writer for it, or a null pointer on
53    failure.  Cases stored in the scratch_writer will be expected
54    to be drawn from DICTIONARY. */
55 struct casewriter *
56 scratch_writer_open (struct file_handle *fh,
57                      const struct dictionary *dictionary)
58 {
59   struct scratch_writer *writer;
60   struct casewriter *casewriter;
61   struct fh_lock *lock;
62   size_t dict_value_cnt;
63
64   /* Get exclusive write access to handle. */
65   /* TRANSLATORS: this fragment will be interpolated into
66      messages in fh_lock() that identify types of files. */
67   lock = fh_lock (fh, FH_REF_SCRATCH, N_("scratch file"), FH_ACC_WRITE, true);
68   if (lock == NULL)
69     return NULL;
70
71   /* Create writer. */
72   writer = xmalloc (sizeof *writer);
73   writer->lock = lock;
74   writer->fh = fh_ref (fh);
75
76   writer->dict = dict_clone (dictionary);
77   dict_delete_scratch_vars (writer->dict);
78   if (dict_count_values (writer->dict, 0)
79       < dict_get_next_value_idx (writer->dict))
80     {
81       writer->compactor = case_map_to_compact_dict (writer->dict, 0);
82       dict_compact_values (writer->dict);
83     }
84   else
85     writer->compactor = NULL;
86   dict_value_cnt = dict_get_next_value_idx (writer->dict);
87   writer->subwriter = autopaging_writer_create (dict_value_cnt);
88
89   casewriter = casewriter_create (dict_value_cnt,
90                                   &scratch_writer_casewriter_class, writer);
91   taint_propagate (casewriter_get_taint (writer->subwriter),
92                    casewriter_get_taint (casewriter));
93   return casewriter;
94 }
95
96 /* Writes case C to WRITER. */
97 static void
98 scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_,
99                                  struct ccase *c)
100 {
101   struct scratch_writer *writer = writer_;
102   struct ccase tmp;
103   if (writer->compactor)
104     {
105       case_map_execute (writer->compactor, c, &tmp);
106       case_destroy (c);
107     }
108   else
109     case_move (&tmp, c);
110   casewriter_write (writer->subwriter, &tmp);
111 }
112
113 /* Closes WRITER. */
114 static void
115 scratch_writer_casewriter_destroy (struct casewriter *w UNUSED, void *writer_)
116 {
117   static unsigned int next_unique_id = 0x12345678;
118
119   struct scratch_writer *writer = writer_;
120   struct casereader *reader = casewriter_make_reader (writer->subwriter);
121   if (!casereader_error (reader))
122     {
123       /* Destroy previous contents of handle. */
124       struct scratch_handle *sh = fh_get_scratch_handle (writer->fh);
125       if (sh != NULL)
126         scratch_handle_destroy (sh);
127
128       /* Create new contents. */
129       sh = xmalloc (sizeof *sh);
130       sh->unique_id = ++next_unique_id;
131       sh->dictionary = writer->dict;
132       sh->casereader = reader;
133       fh_set_scratch_handle (writer->fh, sh);
134     }
135   else
136     {
137       casereader_destroy (reader);
138       dict_destroy (writer->dict);
139     }
140
141   fh_unlock (writer->lock);
142   fh_unref (writer->fh);
143   free (writer);
144 }
145
146 static const struct casewriter_class scratch_writer_casewriter_class =
147   {
148     scratch_writer_casewriter_write,
149     scratch_writer_casewriter_destroy,
150     NULL,
151   };