1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
30 #include <data/case-sink.h>
31 #include <data/case-source.h>
32 #include <data/case.h>
33 #include <data/dictionary.h>
34 #include <data/procedure.h>
35 #include <data/settings.h>
36 #include <data/value.h>
37 #include <data/variable.h>
38 #include <language/command.h>
39 #include <language/lexer/lexer.h>
40 #include <language/lexer/variable-parser.h>
41 #include <libpspp/alloc.h>
42 #include <libpspp/array.h>
43 #include <libpspp/assertion.h>
44 #include <libpspp/message.h>
45 #include <libpspp/message.h>
46 #include <libpspp/misc.h>
47 #include <libpspp/pool.h>
48 #include <libpspp/str.h>
54 #define _(msgid) gettext (msgid)
56 /* List of variable names. */
60 char name[SHORT_NAME_LEN + 1];
63 /* Represents a FLIP input program. */
66 struct pool *pool; /* Pool containing FLIP data. */
67 struct variable **var; /* Variables to transpose. */
68 int *idx_to_fv; /* var[]->index to compacted sink case fv. */
69 size_t var_cnt; /* Number of elements in `var'. */
70 int case_cnt; /* Pre-flip case count. */
71 size_t case_size; /* Post-flip bytes per case. */
73 union value *output_buf; /* Case output buffer. */
75 struct variable *new_names; /* Variable containing new variable names. */
76 struct varname *new_names_head; /* First new variable. */
77 struct varname *new_names_tail; /* Last new variable. */
79 FILE *file; /* Temporary file containing data. */
82 static void destroy_flip_pgm (struct flip_pgm *);
83 static struct case_sink *flip_sink_create (struct dictionary *d, struct flip_pgm *);
84 static struct case_source *flip_source_create (struct flip_pgm *);
85 static bool flip_file (struct flip_pgm *);
86 static int build_dictionary (struct dictionary *, struct flip_pgm *);
88 static const struct case_source_class flip_source_class;
89 static const struct case_sink_class flip_sink_class;
91 /* Parses and executes FLIP. */
93 cmd_flip (struct lexer *lexer, struct dataset *ds)
95 struct flip_pgm *flip;
96 struct case_sink *sink;
97 struct dictionary *dict = dataset_dict (ds);
100 if (proc_make_temporary_transformations_permanent (ds))
101 msg (SW, _("FLIP ignores TEMPORARY. "
102 "Temporary transformations will be made permanent."));
104 flip = pool_create_container (struct flip_pgm, pool);
106 flip->idx_to_fv = dict_get_compacted_dict_index_to_case_index (dict);
107 pool_register (flip->pool, free, flip->idx_to_fv);
110 flip->new_names = NULL;
111 flip->new_names_head = NULL;
112 flip->new_names_tail = NULL;
115 lex_match (lexer, '/');
116 if (lex_match_id (lexer, "VARIABLES"))
118 lex_match (lexer, '=');
119 if (!parse_variables (lexer, dict, &flip->var, &flip->var_cnt,
122 lex_match (lexer, '/');
125 dict_get_vars (dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
126 pool_register (flip->pool, free, flip->var);
128 lex_match (lexer, '/');
129 if (lex_match_id (lexer, "NEWNAMES"))
131 lex_match (lexer, '=');
132 flip->new_names = parse_variable (lexer, dict);
133 if (!flip->new_names)
137 flip->new_names = dict_lookup_var (dict, "CASE_LBL");
143 for (i = 0; i < flip->var_cnt; i++)
144 if (flip->var[i] == flip->new_names)
146 remove_element (flip->var, flip->var_cnt, sizeof *flip->var, i);
152 /* Read the active file into a flip_sink. */
154 proc_make_temporary_transformations_permanent (ds);
155 sink = flip_sink_create (dict, flip);
158 proc_set_sink (ds, sink);
159 flip->new_names_tail = NULL;
160 ok = procedure (ds,NULL, NULL);
162 /* Flip the data we read. */
163 if (!flip_file (flip))
165 discard_variables (ds);
169 /* Flip the dictionary. */
171 if (!build_dictionary (dict, flip))
173 discard_variables (ds);
176 flip->case_size = dict_get_case_size (dict);
178 /* Set up flipped data for reading. */
179 proc_set_source (ds, flip_source_create (flip));
181 return ok ? lex_end_of_command (lexer) : CMD_CASCADING_FAILURE;
184 destroy_flip_pgm (flip);
185 return CMD_CASCADING_FAILURE;
190 destroy_flip_pgm (struct flip_pgm *flip)
193 pool_destroy (flip->pool);
196 /* Make a new variable with base name NAME, which is bowdlerized and
197 mangled until acceptable, and returns success. */
199 make_new_var (struct dictionary *dict, char name[])
203 /* Trim trailing spaces. */
204 cp = strchr (name, '\0');
205 while (cp > name && isspace ((unsigned char) cp[-1]))
208 /* Fix invalid characters. */
209 for (cp = name; *cp && cp < name + SHORT_NAME_LEN; cp++)
212 if (!lex_is_id1 (*cp) || *cp == '$')
217 if (!lex_is_idn (*cp))
221 str_uppercase (name);
223 if (dict_create_var (dict, name, 0))
226 /* Add numeric extensions until acceptable. */
228 const int len = (int) strlen (name);
229 char n[SHORT_NAME_LEN + 1];
232 for (i = 1; i < 10000000; i++)
234 int ofs = MIN (7 - intlog10 (i), len);
235 memcpy (n, name, ofs);
236 sprintf (&n[ofs], "%d", i);
238 if (dict_create_var (dict, n, 0))
243 msg (SE, _("Could not create acceptable variant for variable %s."), name);
247 /* Make a new dictionary for all the new variable names. */
249 build_dictionary (struct dictionary *dict, struct flip_pgm *flip)
251 dict_create_var_assert (dict, "CASE_LBL", 8);
253 if (flip->new_names_head == NULL)
257 if (flip->case_cnt > 99999)
259 msg (SE, _("Cannot create more than 99999 variable names."));
263 for (i = 0; i < flip->case_cnt; i++)
266 char s[SHORT_NAME_LEN + 1];
268 sprintf (s, "VAR%03d", i);
269 v = dict_create_var_assert (dict, s, 0);
276 for (v = flip->new_names_head; v; v = v->next)
277 if (!make_new_var (dict, v->name))
284 /* Creates a flip sink based on FLIP. */
285 static struct case_sink *
286 flip_sink_create (struct dictionary *dict, struct flip_pgm *flip)
290 flip->output_buf = pool_nalloc (flip->pool,
291 flip->var_cnt, sizeof *flip->output_buf);
293 flip->file = pool_tmpfile (flip->pool);
294 if (flip->file == NULL)
296 msg (SE, _("Could not create temporary file for FLIP."));
300 /* Write variable names as first case. */
301 for (i = 0; i < flip->var_cnt; i++)
302 buf_copy_str_rpad (flip->output_buf[i].s, MAX_SHORT_STRING,
303 var_get_name (flip->var[i]));
304 if (fwrite (flip->output_buf, sizeof *flip->output_buf,
305 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
307 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
313 return create_case_sink (&flip_sink_class, dict, flip);
316 /* Writes case C to the FLIP sink.
317 Returns true if successful, false if an I/O error occurred. */
319 flip_sink_write (struct case_sink *sink, const struct ccase *c)
321 struct flip_pgm *flip = sink->aux;
326 if (flip->new_names != NULL)
328 struct varname *v = pool_alloc (flip->pool, sizeof *v);
329 int fv = flip->idx_to_fv[var_get_dict_index (flip->new_names)];
331 if (var_is_numeric (flip->new_names))
333 double f = case_num_idx (c, fv);
336 strcpy (v->name, "VSYSMIS");
337 else if (f < INT_MIN)
338 strcpy (v->name, "VNEGINF");
339 else if (f > INT_MAX)
340 strcpy (v->name, "VPOSINF");
342 snprintf (v->name, sizeof v->name, "V%d", (int) f);
346 int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING);
347 memcpy (v->name, case_str_idx (c, fv), width);
351 if (flip->new_names_head == NULL)
352 flip->new_names_head = v;
354 flip->new_names_tail->next = v;
355 flip->new_names_tail = v;
358 /* Write to external file. */
359 for (i = 0; i < flip->var_cnt; i++)
363 if (var_is_numeric (flip->var[i]))
365 int fv = flip->idx_to_fv[var_get_dict_index (flip->var[i])];
366 out = case_num_idx (c, fv);
370 flip->output_buf[i].f = out;
373 if (fwrite (flip->output_buf, sizeof *flip->output_buf,
374 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
376 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
382 /* Transposes the external file into a new file. */
384 flip_file (struct flip_pgm *flip)
387 size_t case_capacity;
389 union value *input_buf, *output_buf;
390 FILE *input_file, *output_file;
392 /* Allocate memory for many cases. */
393 case_bytes = flip->var_cnt * sizeof *input_buf;
394 case_capacity = get_workspace () / case_bytes;
395 if (case_capacity > flip->case_cnt * 2)
396 case_capacity = flip->case_cnt * 2;
397 if (case_capacity < 2)
401 size_t bytes = case_bytes * case_capacity;
402 if (case_capacity > 2)
403 input_buf = malloc (bytes);
405 input_buf = xmalloc (bytes);
406 if (input_buf != NULL)
410 if (case_capacity < 2)
413 pool_register (flip->pool, free, input_buf);
415 /* Use half the allocated memory for input_buf, half for
418 output_buf = input_buf + flip->var_cnt * case_capacity;
420 input_file = flip->file;
421 if (fseek (input_file, 0, SEEK_SET) != 0)
423 msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
427 output_file = pool_tmpfile (flip->pool);
428 if (output_file == NULL)
430 msg (SE, _("Error creating FLIP source file."));
434 for (case_idx = 0; case_idx < flip->case_cnt; )
436 unsigned long read_cases = MIN (flip->case_cnt - case_idx,
440 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
442 msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
446 for (i = 0; i < flip->var_cnt; i++)
450 for (j = 0; j < read_cases; j++)
451 output_buf[j] = input_buf[i + j * flip->var_cnt];
458 #define off_t long int
461 if (fseeko (output_file,
462 sizeof *input_buf * (case_idx
463 + (off_t) i * flip->case_cnt),
466 msg (SE, _("Error seeking FLIP source file: %s."),
471 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
474 msg (SE, _("Error writing FLIP source file: %s."),
480 case_idx += read_cases;
483 if (pool_fclose (flip->pool, input_file) == EOF)
485 msg (SE, _("Error closing FLIP source file: %s."), strerror (errno));
488 pool_unregister (flip->pool, input_buf);
491 if (fseek (output_file, 0, SEEK_SET) != 0)
493 msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
496 flip->file = output_file;
501 /* FLIP sink class. */
502 static const struct case_sink_class flip_sink_class =
511 /* Creates and returns a FLIP source based on PGM,
512 which should have already been used as a sink. */
513 static struct case_source *
514 flip_source_create (struct flip_pgm *pgm)
516 return create_case_source (&flip_source_class, pgm);
519 /* Reads the FLIP stream. Copies each case into C and calls
520 WRITE_CASE passing WC_DATA.
521 Returns true if successful, false if an I/O error occurred. */
523 flip_source_read (struct case_source *source,
525 write_case_func *write_case, write_case_data wc_data)
527 struct flip_pgm *flip = source->aux;
528 union value *input_buf;
532 input_buf = xnmalloc (flip->case_cnt, sizeof *input_buf);
533 for (i = 0; ok && i < flip->var_cnt; i++)
537 if (fread (input_buf, sizeof *input_buf, flip->case_cnt,
538 flip->file) != flip->case_cnt)
540 if (ferror (flip->file))
541 msg (SE, _("Error reading FLIP temporary file: %s."),
543 else if (feof (flip->file))
544 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
551 for (j = 0; j < flip->case_cnt; j++)
552 case_data_rw_idx (c, j)->f = input_buf[j].f;
553 ok = write_case (wc_data);
560 /* Destroy internal data in SOURCE. */
562 flip_source_destroy (struct case_source *source)
564 struct flip_pgm *flip = source->aux;
566 destroy_flip_pgm (flip);
569 static const struct case_source_class flip_source_class =