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
21 #include <libpspp/message.h>
27 #include <libpspp/array.h>
28 #include <libpspp/alloc.h>
29 #include <data/case.h>
30 #include <language/command.h>
31 #include <data/dictionary.h>
32 #include <libpspp/message.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/misc.h>
36 #include <libpspp/pool.h>
37 #include <data/settings.h>
38 #include <libpspp/str.h>
39 #include <data/value.h>
40 #include <data/variable.h>
41 #include <procedure.h>
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
48 #define _(msgid) gettext (msgid)
50 /* List of variable names. */
54 char name[SHORT_NAME_LEN + 1];
57 /* Represents a FLIP input program. */
60 struct pool *pool; /* Pool containing FLIP data. */
61 struct variable **var; /* Variables to transpose. */
62 int *idx_to_fv; /* var[]->index to compacted sink case fv. */
63 size_t var_cnt; /* Number of elements in `var'. */
64 int case_cnt; /* Pre-flip case count. */
65 size_t case_size; /* Post-flip bytes per case. */
67 union value *output_buf; /* Case output buffer. */
69 struct variable *new_names; /* Variable containing new variable names. */
70 struct varname *new_names_head; /* First new variable. */
71 struct varname *new_names_tail; /* Last new variable. */
73 FILE *file; /* Temporary file containing data. */
76 static void destroy_flip_pgm (struct flip_pgm *);
77 static struct case_sink *flip_sink_create (struct flip_pgm *);
78 static struct case_source *flip_source_create (struct flip_pgm *);
79 static bool flip_file (struct flip_pgm *);
80 static int build_dictionary (struct flip_pgm *);
82 static const struct case_source_class flip_source_class;
83 static const struct case_sink_class flip_sink_class;
85 /* Parses and executes FLIP. */
89 struct flip_pgm *flip;
94 msg (SW, _("FLIP ignores TEMPORARY. "
95 "Temporary transformations will be made permanent."));
99 flip = pool_create_container (struct flip_pgm, pool);
101 flip->idx_to_fv = dict_get_compacted_idx_to_fv (default_dict);
102 pool_register (flip->pool, free, flip->idx_to_fv);
105 flip->new_names = NULL;
106 flip->new_names_head = NULL;
107 flip->new_names_tail = NULL;
111 if (lex_match_id ("VARIABLES"))
114 if (!parse_variables (default_dict, &flip->var, &flip->var_cnt,
120 dict_get_vars (default_dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
121 pool_register (flip->pool, free, flip->var);
124 if (lex_match_id ("NEWNAMES"))
127 flip->new_names = parse_variable ();
128 if (!flip->new_names)
132 flip->new_names = dict_lookup_var (default_dict, "CASE_LBL");
138 for (i = 0; i < flip->var_cnt; i++)
139 if (flip->var[i] == flip->new_names)
141 remove_element (flip->var, flip->var_cnt, sizeof *flip->var, i);
147 /* Read the active file into a flip_sink. */
149 temp_trns = temporary = 0;
150 vfm_sink = flip_sink_create (flip);
151 if (vfm_sink == NULL)
153 flip->new_names_tail = NULL;
154 ok = procedure (NULL, NULL);
156 /* Flip the data we read. */
157 if (!flip_file (flip))
159 discard_variables ();
163 /* Flip the dictionary. */
164 dict_clear (default_dict);
165 if (!build_dictionary (flip))
167 discard_variables ();
170 flip->case_size = dict_get_case_size (default_dict);
172 /* Set up flipped data for reading. */
173 vfm_source = flip_source_create (flip);
175 return ok ? lex_end_of_command () : CMD_CASCADING_FAILURE;
178 destroy_flip_pgm (flip);
179 return CMD_CASCADING_FAILURE;
184 destroy_flip_pgm (struct flip_pgm *flip)
187 pool_destroy (flip->pool);
190 /* Make a new variable with base name NAME, which is bowdlerized and
191 mangled until acceptable, and returns success. */
193 make_new_var (char name[])
197 /* Trim trailing spaces. */
198 cp = strchr (name, '\0');
199 while (cp > name && isspace ((unsigned char) cp[-1]))
202 /* Fix invalid characters. */
203 for (cp = name; *cp && cp < name + SHORT_NAME_LEN; cp++)
206 if (!lex_is_id1 (*cp) || *cp == '$')
211 if (!lex_is_idn (*cp))
215 str_uppercase (name);
217 if (dict_create_var (default_dict, name, 0))
220 /* Add numeric extensions until acceptable. */
222 const int len = (int) strlen (name);
223 char n[SHORT_NAME_LEN + 1];
226 for (i = 1; i < 10000000; i++)
228 int ofs = min (7 - intlog10 (i), len);
229 memcpy (n, name, ofs);
230 sprintf (&n[ofs], "%d", i);
232 if (dict_create_var (default_dict, n, 0))
237 msg (SE, _("Could not create acceptable variant for variable %s."), name);
241 /* Make a new dictionary for all the new variable names. */
243 build_dictionary (struct flip_pgm *flip)
245 dict_create_var_assert (default_dict, "CASE_LBL", 8);
247 if (flip->new_names_head == NULL)
251 if (flip->case_cnt > 99999)
253 msg (SE, _("Cannot create more than 99999 variable names."));
257 for (i = 0; i < flip->case_cnt; i++)
260 char s[SHORT_NAME_LEN + 1];
262 sprintf (s, "VAR%03d", i);
263 v = dict_create_var_assert (default_dict, s, 0);
270 for (v = flip->new_names_head; v; v = v->next)
271 if (!make_new_var (v->name))
278 /* Creates a flip sink based on FLIP. */
279 static struct case_sink *
280 flip_sink_create (struct flip_pgm *flip)
284 flip->output_buf = pool_nalloc (flip->pool,
285 flip->var_cnt, sizeof *flip->output_buf);
287 flip->file = pool_tmpfile (flip->pool);
288 if (flip->file == NULL)
290 msg (SE, _("Could not create temporary file for FLIP."));
294 /* Write variable names as first case. */
295 for (i = 0; i < flip->var_cnt; i++)
296 buf_copy_str_rpad (flip->output_buf[i].s, MAX_SHORT_STRING,
298 if (fwrite (flip->output_buf, sizeof *flip->output_buf,
299 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
301 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
307 return create_case_sink (&flip_sink_class, default_dict, flip);
310 /* Writes case C to the FLIP sink.
311 Returns true if successful, false if an I/O error occurred. */
313 flip_sink_write (struct case_sink *sink, const struct ccase *c)
315 struct flip_pgm *flip = sink->aux;
320 if (flip->new_names != NULL)
322 struct varname *v = pool_alloc (flip->pool, sizeof *v);
324 if (flip->new_names->type == NUMERIC)
326 double f = case_num (c, flip->idx_to_fv[flip->new_names->index]);
329 strcpy (v->name, "VSYSMIS");
330 else if (f < INT_MIN)
331 strcpy (v->name, "VNEGINF");
332 else if (f > INT_MAX)
333 strcpy (v->name, "VPOSINF");
336 char name[INT_STRLEN_BOUND (int) + 2];
337 sprintf (name, "V%d", (int) f);
338 str_copy_trunc (v->name, sizeof v->name, name);
343 int width = min (flip->new_names->width, MAX_SHORT_STRING);
344 memcpy (v->name, case_str (c, flip->idx_to_fv[flip->new_names->index]),
349 if (flip->new_names_head == NULL)
350 flip->new_names_head = v;
352 flip->new_names_tail->next = v;
353 flip->new_names_tail = v;
356 /* Write to external file. */
357 for (i = 0; i < flip->var_cnt; i++)
361 if (flip->var[i]->type == NUMERIC)
362 out = case_num (c, flip->idx_to_fv[flip->var[i]->index]);
365 flip->output_buf[i].f = out;
368 if (fwrite (flip->output_buf, sizeof *flip->output_buf,
369 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
371 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
377 /* Transposes the external file into a new file. */
379 flip_file (struct flip_pgm *flip)
382 size_t case_capacity;
384 union value *input_buf, *output_buf;
385 FILE *input_file, *output_file;
387 /* Allocate memory for many cases. */
388 case_bytes = flip->var_cnt * sizeof *input_buf;
389 case_capacity = get_workspace () / case_bytes;
390 if (case_capacity > flip->case_cnt * 2)
391 case_capacity = flip->case_cnt * 2;
392 if (case_capacity < 2)
396 size_t bytes = case_bytes * case_capacity;
397 if (case_capacity > 2)
398 input_buf = malloc (bytes);
400 input_buf = xmalloc (bytes);
401 if (input_buf != NULL)
405 if (case_capacity < 2)
408 pool_register (flip->pool, free, input_buf);
410 /* Use half the allocated memory for input_buf, half for
413 output_buf = input_buf + flip->var_cnt * case_capacity;
415 input_file = flip->file;
416 if (fseek (input_file, 0, SEEK_SET) != 0)
418 msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
422 output_file = pool_tmpfile (flip->pool);
423 if (output_file == NULL)
425 msg (SE, _("Error creating FLIP source file."));
429 for (case_idx = 0; case_idx < flip->case_cnt; )
431 unsigned long read_cases = min (flip->case_cnt - case_idx,
435 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
437 msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
441 for (i = 0; i < flip->var_cnt; i++)
445 for (j = 0; j < read_cases; j++)
446 output_buf[j] = input_buf[i + j * flip->var_cnt];
453 #define off_t long int
456 if (fseeko (output_file,
457 sizeof *input_buf * (case_idx
458 + (off_t) i * flip->case_cnt),
461 msg (SE, _("Error seeking FLIP source file: %s."),
466 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
469 msg (SE, _("Error writing FLIP source file: %s."),
475 case_idx += read_cases;
478 pool_fclose (flip->pool, input_file);
479 pool_unregister (flip->pool, input_buf);
482 if (fseek (output_file, 0, SEEK_SET) != 0)
484 msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
487 flip->file = output_file;
492 /* FLIP sink class. */
493 static const struct case_sink_class flip_sink_class =
502 /* Creates and returns a FLIP source based on PGM,
503 which should have already been used as a sink. */
504 static struct case_source *
505 flip_source_create (struct flip_pgm *pgm)
507 return create_case_source (&flip_source_class, pgm);
510 /* Reads the FLIP stream. Copies each case into C and calls
511 WRITE_CASE passing WC_DATA.
512 Returns true if successful, false if an I/O error occurred. */
514 flip_source_read (struct case_source *source,
516 write_case_func *write_case, write_case_data wc_data)
518 struct flip_pgm *flip = source->aux;
519 union value *input_buf;
523 input_buf = xnmalloc (flip->case_cnt, sizeof *input_buf);
524 for (i = 0; ok && i < flip->var_cnt; i++)
528 if (fread (input_buf, sizeof *input_buf, flip->case_cnt,
529 flip->file) != flip->case_cnt)
531 if (ferror (flip->file))
532 msg (SE, _("Error reading FLIP temporary file: %s."),
534 else if (feof (flip->file))
535 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
542 for (j = 0; j < flip->case_cnt; j++)
543 case_data_rw (c, j)->f = input_buf[j].f;
544 ok = write_case (wc_data);
551 /* Destroy internal data in SOURCE. */
553 flip_source_destroy (struct case_source *source)
555 struct flip_pgm *flip = source->aux;
557 destroy_flip_pgm (flip);
560 static const struct case_source_class flip_source_class =