1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
31 #include <data/case-sink.h>
32 #include <data/case-source.h>
33 #include <data/case.h>
34 #include <data/dictionary.h>
35 #include <data/procedure.h>
36 #include <data/settings.h>
37 #include <data/value.h>
38 #include <data/variable.h>
39 #include <language/command.h>
40 #include <language/lexer/lexer.h>
41 #include <language/lexer/variable-parser.h>
42 #include <libpspp/alloc.h>
43 #include <libpspp/array.h>
44 #include <libpspp/assertion.h>
45 #include <libpspp/message.h>
46 #include <libpspp/message.h>
47 #include <libpspp/misc.h>
48 #include <libpspp/pool.h>
49 #include <libpspp/str.h>
55 #define _(msgid) gettext (msgid)
57 /* List of variable names. */
61 char name[SHORT_NAME_LEN + 1];
64 /* Represents a FLIP input program. */
67 struct pool *pool; /* Pool containing FLIP data. */
68 struct variable **var; /* Variables to transpose. */
69 int *idx_to_fv; /* var[]->index to compacted sink case fv. */
70 size_t var_cnt; /* Number of elements in `var'. */
71 int case_cnt; /* Pre-flip case count. */
72 size_t case_size; /* Post-flip bytes per case. */
74 union value *output_buf; /* Case output buffer. */
76 struct variable *new_names; /* Variable containing new variable names. */
77 struct varname *new_names_head; /* First new variable. */
78 struct varname *new_names_tail; /* Last new variable. */
80 FILE *file; /* Temporary file containing data. */
83 static void destroy_flip_pgm (struct flip_pgm *);
84 static struct case_sink *flip_sink_create (struct dictionary *d, struct flip_pgm *);
85 static struct case_source *flip_source_create (struct flip_pgm *);
86 static bool flip_file (struct flip_pgm *);
87 static int build_dictionary (struct dictionary *, struct flip_pgm *);
89 static const struct case_source_class flip_source_class;
90 static const struct case_sink_class flip_sink_class;
92 /* Parses and executes FLIP. */
94 cmd_flip (struct lexer *lexer, struct dataset *ds)
96 struct flip_pgm *flip;
97 struct case_sink *sink;
98 struct dictionary *dict = dataset_dict (ds);
101 if (proc_make_temporary_transformations_permanent (ds))
102 msg (SW, _("FLIP ignores TEMPORARY. "
103 "Temporary transformations will be made permanent."));
105 flip = pool_create_container (struct flip_pgm, pool);
107 flip->idx_to_fv = dict_get_compacted_dict_index_to_case_index (dict);
108 pool_register (flip->pool, free, flip->idx_to_fv);
111 flip->new_names = NULL;
112 flip->new_names_head = NULL;
113 flip->new_names_tail = NULL;
116 lex_match (lexer, '/');
117 if (lex_match_id (lexer, "VARIABLES"))
119 lex_match (lexer, '=');
120 if (!parse_variables (lexer, dict, &flip->var, &flip->var_cnt,
123 lex_match (lexer, '/');
126 dict_get_vars (dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
127 pool_register (flip->pool, free, flip->var);
129 lex_match (lexer, '/');
130 if (lex_match_id (lexer, "NEWNAMES"))
132 lex_match (lexer, '=');
133 flip->new_names = parse_variable (lexer, dict);
134 if (!flip->new_names)
138 flip->new_names = dict_lookup_var (dict, "CASE_LBL");
144 for (i = 0; i < flip->var_cnt; i++)
145 if (flip->var[i] == flip->new_names)
147 remove_element (flip->var, flip->var_cnt, sizeof *flip->var, i);
153 /* Read the active file into a flip_sink. */
155 proc_make_temporary_transformations_permanent (ds);
156 sink = flip_sink_create (dict, flip);
159 proc_set_sink (ds, sink);
160 flip->new_names_tail = NULL;
161 ok = procedure (ds,NULL, NULL);
163 /* Flip the data we read. */
164 if (!flip_file (flip))
166 discard_variables (ds);
170 /* Flip the dictionary. */
172 if (!build_dictionary (dict, flip))
174 discard_variables (ds);
177 flip->case_size = dict_get_case_size (dict);
179 /* Set up flipped data for reading. */
180 proc_set_source (ds, flip_source_create (flip));
182 return ok ? lex_end_of_command (lexer) : CMD_CASCADING_FAILURE;
185 destroy_flip_pgm (flip);
186 return CMD_CASCADING_FAILURE;
191 destroy_flip_pgm (struct flip_pgm *flip)
194 pool_destroy (flip->pool);
197 /* Make a new variable with base name NAME, which is bowdlerized and
198 mangled until acceptable, and returns success. */
200 make_new_var (struct dictionary *dict, char name[])
204 /* Trim trailing spaces. */
205 cp = strchr (name, '\0');
206 while (cp > name && isspace ((unsigned char) cp[-1]))
209 /* Fix invalid characters. */
210 for (cp = name; *cp && cp < name + SHORT_NAME_LEN; cp++)
213 if (!lex_is_id1 (*cp) || *cp == '$')
218 if (!lex_is_idn (*cp))
222 str_uppercase (name);
224 if (dict_create_var (dict, name, 0))
227 /* Add numeric extensions until acceptable. */
229 const int len = (int) strlen (name);
230 char n[SHORT_NAME_LEN + 1];
233 for (i = 1; i < 10000000; i++)
235 int ofs = MIN (7 - intlog10 (i), len);
236 memcpy (n, name, ofs);
237 sprintf (&n[ofs], "%d", i);
239 if (dict_create_var (dict, n, 0))
244 msg (SE, _("Could not create acceptable variant for variable %s."), name);
248 /* Make a new dictionary for all the new variable names. */
250 build_dictionary (struct dictionary *dict, struct flip_pgm *flip)
252 dict_create_var_assert (dict, "CASE_LBL", 8);
254 if (flip->new_names_head == NULL)
258 if (flip->case_cnt > 99999)
260 msg (SE, _("Cannot create more than 99999 variable names."));
264 for (i = 0; i < flip->case_cnt; i++)
267 char s[SHORT_NAME_LEN + 1];
269 sprintf (s, "VAR%03d", i);
270 v = dict_create_var_assert (dict, s, 0);
277 for (v = flip->new_names_head; v; v = v->next)
278 if (!make_new_var (dict, v->name))
285 /* Creates a flip sink based on FLIP. */
286 static struct case_sink *
287 flip_sink_create (struct dictionary *dict, struct flip_pgm *flip)
291 flip->output_buf = pool_nalloc (flip->pool,
292 flip->var_cnt, sizeof *flip->output_buf);
294 flip->file = pool_tmpfile (flip->pool);
295 if (flip->file == NULL)
297 msg (SE, _("Could not create temporary file for FLIP."));
301 /* Write variable names as first case. */
302 for (i = 0; i < flip->var_cnt; i++)
303 buf_copy_str_rpad (flip->output_buf[i].s, MAX_SHORT_STRING,
304 var_get_name (flip->var[i]));
305 if (fwrite (flip->output_buf, sizeof *flip->output_buf,
306 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
308 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
314 return create_case_sink (&flip_sink_class, dict, flip);
317 /* Writes case C to the FLIP sink.
318 Returns true if successful, false if an I/O error occurred. */
320 flip_sink_write (struct case_sink *sink, const struct ccase *c)
322 struct flip_pgm *flip = sink->aux;
327 if (flip->new_names != NULL)
329 struct varname *v = pool_alloc (flip->pool, sizeof *v);
330 int fv = flip->idx_to_fv[var_get_dict_index (flip->new_names)];
332 if (var_is_numeric (flip->new_names))
334 double f = case_num_idx (c, fv);
337 strcpy (v->name, "VSYSMIS");
338 else if (f < INT_MIN)
339 strcpy (v->name, "VNEGINF");
340 else if (f > INT_MAX)
341 strcpy (v->name, "VPOSINF");
343 snprintf (v->name, sizeof v->name, "V%d", (int) f);
347 int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING);
348 memcpy (v->name, case_str_idx (c, fv), width);
352 if (flip->new_names_head == NULL)
353 flip->new_names_head = v;
355 flip->new_names_tail->next = v;
356 flip->new_names_tail = v;
359 /* Write to external file. */
360 for (i = 0; i < flip->var_cnt; i++)
364 if (var_is_numeric (flip->var[i]))
366 int fv = flip->idx_to_fv[var_get_dict_index (flip->var[i])];
367 out = case_num_idx (c, fv);
371 flip->output_buf[i].f = out;
374 if (fwrite (flip->output_buf, sizeof *flip->output_buf,
375 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
377 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
383 /* Transposes the external file into a new file. */
385 flip_file (struct flip_pgm *flip)
388 size_t case_capacity;
390 union value *input_buf, *output_buf;
391 FILE *input_file, *output_file;
393 /* Allocate memory for many cases. */
394 case_bytes = flip->var_cnt * sizeof *input_buf;
395 case_capacity = get_workspace () / case_bytes;
396 if (case_capacity > flip->case_cnt * 2)
397 case_capacity = flip->case_cnt * 2;
398 if (case_capacity < 2)
402 size_t bytes = case_bytes * case_capacity;
403 if (case_capacity > 2)
404 input_buf = malloc (bytes);
406 input_buf = xmalloc (bytes);
407 if (input_buf != NULL)
411 if (case_capacity < 2)
414 pool_register (flip->pool, free, input_buf);
416 /* Use half the allocated memory for input_buf, half for
419 output_buf = input_buf + flip->var_cnt * case_capacity;
421 input_file = flip->file;
422 if (fseek (input_file, 0, SEEK_SET) != 0)
424 msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
428 output_file = pool_tmpfile (flip->pool);
429 if (output_file == NULL)
431 msg (SE, _("Error creating FLIP source file."));
435 for (case_idx = 0; case_idx < flip->case_cnt; )
437 unsigned long read_cases = MIN (flip->case_cnt - case_idx,
441 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
443 msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
447 for (i = 0; i < flip->var_cnt; i++)
451 for (j = 0; j < read_cases; j++)
452 output_buf[j] = input_buf[i + j * flip->var_cnt];
459 #define off_t long int
462 if (fseeko (output_file,
463 sizeof *input_buf * (case_idx
464 + (off_t) i * flip->case_cnt),
467 msg (SE, _("Error seeking FLIP source file: %s."),
472 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
475 msg (SE, _("Error writing FLIP source file: %s."),
481 case_idx += read_cases;
484 if (pool_fclose (flip->pool, input_file) == EOF)
486 msg (SE, _("Error closing FLIP source file: %s."), strerror (errno));
489 pool_unregister (flip->pool, input_buf);
492 if (fseek (output_file, 0, SEEK_SET) != 0)
494 msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
497 flip->file = output_file;
502 /* FLIP sink class. */
503 static const struct case_sink_class flip_sink_class =
512 /* Creates and returns a FLIP source based on PGM,
513 which should have already been used as a sink. */
514 static struct case_source *
515 flip_source_create (struct flip_pgm *pgm)
517 return create_case_source (&flip_source_class, pgm);
520 /* Reads the FLIP stream. Copies each case into C and calls
521 WRITE_CASE passing WC_DATA.
522 Returns true if successful, false if an I/O error occurred. */
524 flip_source_read (struct case_source *source,
526 write_case_func *write_case, write_case_data wc_data)
528 struct flip_pgm *flip = source->aux;
529 union value *input_buf;
533 input_buf = xnmalloc (flip->case_cnt, sizeof *input_buf);
534 for (i = 0; ok && i < flip->var_cnt; i++)
538 if (fread (input_buf, sizeof *input_buf, flip->case_cnt,
539 flip->file) != flip->case_cnt)
541 if (ferror (flip->file))
542 msg (SE, _("Error reading FLIP temporary file: %s."),
544 else if (feof (flip->file))
545 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
552 for (j = 0; j < flip->case_cnt; j++)
553 case_data_rw_idx (c, j)->f = input_buf[j].f;
554 ok = write_case (wc_data);
561 /* Destroy internal data in SOURCE. */
563 flip_source_destroy (struct case_source *source)
565 struct flip_pgm *flip = source->aux;
567 destroy_flip_pgm (flip);
570 static const struct case_source_class flip_source_class =