1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
25 #include "data/case.h"
26 #include "data/casereader.h"
27 #include "data/casereader-provider.h"
28 #include "data/dictionary.h"
29 #include "data/procedure.h"
30 #include "data/settings.h"
31 #include "data/short-names.h"
32 #include "data/value.h"
33 #include "data/variable.h"
34 #include "language/command.h"
35 #include "language/lexer/lexer.h"
36 #include "language/lexer/variable-parser.h"
37 #include "libpspp/array.h"
38 #include "libpspp/assertion.h"
39 #include "libpspp/message.h"
40 #include "libpspp/misc.h"
41 #include "libpspp/pool.h"
42 #include "libpspp/str.h"
43 #include "data/data-in.h"
44 #include "data/data-out.h"
46 #include "gl/intprops.h"
47 #include "gl/minmax.h"
48 #include "gl/xalloc.h"
51 #define _(msgid) gettext (msgid)
53 /* List of variable names. */
57 size_t n_names, allocated_names;
60 static void var_names_init (struct var_names *);
61 static void var_names_add (struct pool *, struct var_names *, const char *);
63 /* Represents a FLIP input program. */
66 struct pool *pool; /* Pool containing FLIP data. */
67 size_t n_vars; /* Pre-flip number of variables. */
68 int n_cases; /* Pre-flip number of cases. */
70 struct variable *new_names_var; /* Variable with new variable names. */
71 struct dictionary *dict; /* Dictionary of the names */
72 struct var_names old_names; /* Variable names before FLIP. */
73 struct var_names new_names; /* Variable names after FLIP. */
75 FILE *file; /* Temporary file containing data. */
76 size_t cases_read; /* Number of cases already read. */
77 bool error; /* Error reading temporary file? */
80 static const struct casereader_class flip_casereader_class;
82 static void destroy_flip_pgm (struct flip_pgm *);
83 static bool flip_file (struct flip_pgm *);
84 static void make_new_var (struct dictionary *, const char *name);
86 /* Parses and executes FLIP. */
88 cmd_flip (struct lexer *lexer, struct dataset *ds)
90 struct dictionary *dict = dataset_dict (ds);
91 const struct variable **vars;
92 struct flip_pgm *flip;
93 struct casereader *input, *reader;
98 if (proc_make_temporary_transformations_permanent (ds))
99 msg (SW, _("FLIP ignores TEMPORARY. "
100 "Temporary transformations will be made permanent."));
102 flip = pool_create_container (struct flip_pgm, pool);
105 flip->new_names_var = NULL;
106 var_names_init (&flip->old_names);
107 var_names_init (&flip->new_names);
109 flip->cases_read = 0;
113 lex_match (lexer, T_SLASH);
114 if (lex_match_id (lexer, "VARIABLES"))
116 lex_match (lexer, T_EQUALS);
117 if (!parse_variables_const (lexer, dict, &vars, &flip->n_vars,
120 lex_match (lexer, T_SLASH);
123 dict_get_vars (dict, &vars, &flip->n_vars, DC_SYSTEM);
124 pool_register (flip->pool, free, vars);
126 lex_match (lexer, T_SLASH);
127 if (lex_match_id (lexer, "NEWNAMES"))
129 lex_match (lexer, T_EQUALS);
130 flip->new_names_var = parse_variable (lexer, dict);
131 if (!flip->new_names_var)
135 flip->new_names_var = dict_lookup_var (dict, "CASE_LBL");
137 if (flip->new_names_var)
139 for (i = 0; i < flip->n_vars; i++)
140 if (vars[i] == flip->new_names_var)
142 remove_element (vars, flip->n_vars, sizeof *vars, i);
148 flip->file = pool_create_temp_file (flip->pool);
149 if (flip->file == NULL)
151 msg (SE, _("Could not create temporary file for FLIP."));
155 /* Save old variable names for use as values of CASE_LBL
156 variable in flipped file. */
157 for (i = 0; i < flip->n_vars; i++)
158 var_names_add (flip->pool, &flip->old_names,
159 pool_strdup (flip->pool, var_get_name (vars[i])));
161 /* Read the active file into a flip_sink. */
162 proc_discard_output (ds);
164 input = proc_open (ds);
165 while ((c = casereader_read (input)) != NULL)
168 for (i = 0; i < flip->n_vars; i++)
170 const struct variable *v = vars[i];
171 double out = var_is_numeric (v) ? case_num (c, v) : SYSMIS;
172 fwrite (&out, sizeof out, 1, flip->file);
174 if (flip->new_names_var != NULL)
176 const union value *value = case_data (c, flip->new_names_var);
178 if (var_is_numeric (flip->new_names_var))
181 name = (f == SYSMIS ? "VSYSMIS"
182 : f < INT_MIN ? "VNEGINF"
183 : f > INT_MAX ? "VPOSINF"
184 : pool_asprintf (flip->pool, "V%d", (int) f));
188 name = data_out_pool (value, dict_get_encoding (flip->dict), var_get_write_format (flip->new_names_var),
191 var_names_add (flip->pool, &flip->new_names, name);
195 ok = casereader_destroy (input);
196 ok = proc_commit (ds) && ok;
198 /* Flip the data we read. */
199 if (!ok || !flip_file (flip))
201 proc_discard_active_file (ds);
205 /* Flip the dictionary. */
207 dict_create_var_assert (dict, "CASE_LBL", 8);
208 for (i = 0; i < flip->n_cases; i++)
209 if (flip->new_names.n_names)
210 make_new_var (dict, flip->new_names.names[i]);
213 char s[3 + INT_STRLEN_BOUND (i) + 1];
214 sprintf (s, "VAR%03zu", i);
215 dict_create_var_assert (dict, s, 0);
218 /* Set up flipped data for reading. */
219 reader = casereader_create_sequential (NULL, dict_get_proto (dict),
221 &flip_casereader_class, flip);
222 proc_set_active_file_data (ds, reader);
226 destroy_flip_pgm (flip);
227 return CMD_CASCADING_FAILURE;
232 destroy_flip_pgm (struct flip_pgm *flip)
235 pool_destroy (flip->pool);
238 /* Make a new variable with base name NAME, which is bowdlerized and
239 mangled until acceptable. */
241 make_new_var (struct dictionary *dict, const char *name_)
243 char *name = xstrdup (name_);
246 /* Trim trailing spaces. */
247 cp = strchr (name, '\0');
248 while (cp > name && isspace ((unsigned char) cp[-1]))
251 /* Fix invalid characters. */
252 for (cp = name; *cp && cp < name + ID_MAX_LEN; cp++)
255 if (!lex_is_id1 (*cp) || *cp == '$')
260 if (!lex_is_idn (*cp))
265 /* Use the mangled name, if it is available, or add numeric
266 extensions until we find one that is. */
267 if (!dict_create_var (dict, name, 0))
269 int len = strlen (name);
273 char n[ID_MAX_LEN + 1];
274 int ofs = MIN (ID_MAX_LEN - 1 - intlog10 (i), len);
275 strncpy (n, name, ofs);
276 sprintf (&n[ofs], "%d", i);
278 if (dict_create_var (dict, n, 0))
285 /* Transposes the external file into a new file. */
287 flip_file (struct flip_pgm *flip)
290 size_t case_capacity;
292 double *input_buf, *output_buf;
293 FILE *input_file, *output_file;
295 /* Allocate memory for many cases. */
296 case_bytes = flip->n_vars * sizeof *input_buf;
297 case_capacity = settings_get_workspace () / case_bytes;
298 if (case_capacity > flip->n_cases * 2)
299 case_capacity = flip->n_cases * 2;
300 if (case_capacity < 2)
304 size_t bytes = case_bytes * case_capacity;
305 if (case_capacity > 2)
306 input_buf = malloc (bytes);
308 input_buf = xmalloc (bytes);
309 if (input_buf != NULL)
313 if (case_capacity < 2)
316 pool_register (flip->pool, free, input_buf);
318 /* Use half the allocated memory for input_buf, half for
321 output_buf = input_buf + flip->n_vars * case_capacity;
323 input_file = flip->file;
324 if (fseeko (input_file, 0, SEEK_SET) != 0)
326 msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
330 output_file = pool_create_temp_file (flip->pool);
331 if (output_file == NULL)
333 msg (SE, _("Error creating FLIP source file."));
337 for (case_idx = 0; case_idx < flip->n_cases; )
339 unsigned long read_cases = MIN (flip->n_cases - case_idx,
343 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
345 if (ferror (input_file))
346 msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
348 msg (SE, _("Unexpected end of file reading FLIP file."));
352 for (i = 0; i < flip->n_vars; i++)
356 for (j = 0; j < read_cases; j++)
357 output_buf[j] = input_buf[i + j * flip->n_vars];
359 if (fseeko (output_file,
360 sizeof *input_buf * (case_idx
361 + (off_t) i * flip->n_cases),
364 msg (SE, _("Error seeking FLIP source file: %s."),
369 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
372 msg (SE, _("Error writing FLIP source file: %s."),
378 case_idx += read_cases;
381 pool_fclose_temp_file (flip->pool, input_file);
382 pool_unregister (flip->pool, input_buf);
385 if (fseeko (output_file, 0, SEEK_SET) != 0)
387 msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
390 flip->file = output_file;
395 /* Reads and returns one case.
396 Returns a null pointer at end of file or if an I/O error occurred. */
397 static struct ccase *
398 flip_casereader_read (struct casereader *reader, void *flip_)
400 struct flip_pgm *flip = flip_;
401 const char *encoding;
405 if (flip->error || flip->cases_read >= flip->n_vars)
408 c = case_create (casereader_get_proto (reader));
409 encoding = dict_get_encoding (flip->dict);
410 data_in (ss_cstr (flip->old_names.names[flip->cases_read]), encoding,
411 FMT_A, case_data_rw_idx (c, 0), 8, encoding);
413 for (i = 0; i < flip->n_cases; i++)
416 if (fread (&in, sizeof in, 1, flip->file) != 1)
419 if (ferror (flip->file))
420 msg (SE, _("Error reading FLIP temporary file: %s."),
422 else if (feof (flip->file))
423 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
429 case_data_rw_idx (c, i + 1)->f = in;
437 /* Destroys the source.
438 Returns true if successful read, false if an I/O occurred
439 during destruction or previously. */
441 flip_casereader_destroy (struct casereader *reader UNUSED, void *flip_)
443 struct flip_pgm *flip = flip_;
445 casereader_force_error (reader);
446 destroy_flip_pgm (flip);
449 static const struct casereader_class flip_casereader_class =
451 flip_casereader_read,
452 flip_casereader_destroy,
458 var_names_init (struct var_names *vn)
462 vn->allocated_names = 0;
466 var_names_add (struct pool *pool, struct var_names *vn, const char *name)
468 if (vn->n_names >= vn->allocated_names)
469 vn->names = pool_2nrealloc (pool, vn->names, &vn->allocated_names,
471 vn->names[vn->n_names++] = name;