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.h>
31 #include <data/casereader.h>
32 #include <data/casereader-provider.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/misc.h>
46 #include <libpspp/pool.h>
47 #include <libpspp/str.h>
53 #define _(msgid) gettext (msgid)
55 /* List of variable names. */
59 char name[SHORT_NAME_LEN + 1];
62 /* Represents a FLIP input program. */
65 struct pool *pool; /* Pool containing FLIP data. */
66 const struct variable **var; /* Variables to transpose. */
67 int *idx_to_fv; /* var[]->index to compacted sink case fv. */
68 size_t var_cnt; /* Number of elements in `var'. */
69 int case_cnt; /* Pre-flip case count. */
70 size_t case_size; /* Post-flip bytes per case. */
72 struct variable *new_names; /* Variable containing new variable names. */
73 struct varname *new_names_head; /* First new variable. */
74 struct varname *new_names_tail; /* Last new variable. */
76 FILE *file; /* Temporary file containing data. */
77 union value *input_buf; /* Input buffer for temporary file. */
78 size_t cases_read; /* Number of cases already read. */
79 bool error; /* Error reading temporary file? */
82 static const struct casereader_class flip_casereader_class;
84 static void destroy_flip_pgm (struct flip_pgm *);
85 static bool flip_file (struct flip_pgm *);
86 static bool build_dictionary (struct dictionary *, struct flip_pgm *);
87 static bool write_flip_case (struct flip_pgm *, const struct ccase *);
89 /* Parses and executes FLIP. */
91 cmd_flip (struct lexer *lexer, struct dataset *ds)
93 struct dictionary *dict = dataset_dict (ds);
94 struct flip_pgm *flip;
95 struct casereader *input, *reader;
96 union value *output_buf;
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;
115 flip->input_buf = NULL;
116 flip->cases_read = 0;
119 lex_match (lexer, '/');
120 if (lex_match_id (lexer, "VARIABLES"))
122 lex_match (lexer, '=');
123 if (!parse_variables_const (lexer, dict, &flip->var, &flip->var_cnt,
126 lex_match (lexer, '/');
129 dict_get_vars (dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
130 pool_register (flip->pool, free, flip->var);
132 lex_match (lexer, '/');
133 if (lex_match_id (lexer, "NEWNAMES"))
135 lex_match (lexer, '=');
136 flip->new_names = parse_variable (lexer, dict);
137 if (!flip->new_names)
141 flip->new_names = dict_lookup_var (dict, "CASE_LBL");
145 for (i = 0; i < flip->var_cnt; i++)
146 if (flip->var[i] == flip->new_names)
148 remove_element (flip->var, flip->var_cnt, sizeof *flip->var, i);
154 output_buf = pool_nalloc (flip->pool,
155 flip->var_cnt, sizeof *output_buf);
157 flip->file = pool_tmpfile (flip->pool);
158 if (flip->file == NULL)
160 msg (SE, _("Could not create temporary file for FLIP."));
164 /* Write variable names as first case. */
165 for (i = 0; i < flip->var_cnt; i++)
166 buf_copy_str_rpad (output_buf[i].s, MAX_SHORT_STRING,
167 var_get_name (flip->var[i]));
168 if (fwrite (output_buf, sizeof *output_buf,
169 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
171 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
177 /* Read the active file into a flip_sink. */
178 proc_make_temporary_transformations_permanent (ds);
179 proc_discard_output (ds);
181 input = proc_open (ds);
182 while (casereader_read (input, &c))
184 write_flip_case (flip, &c);
187 ok = casereader_destroy (input);
188 ok = proc_commit (ds) && ok;
190 /* Flip the data we read. */
191 if (!ok || !flip_file (flip))
193 proc_discard_active_file (ds);
197 /* Flip the dictionary. */
199 if (!build_dictionary (dict, flip))
201 proc_discard_active_file (ds);
204 flip->case_size = dict_get_case_size (dict);
206 /* Set up flipped data for reading. */
207 reader = casereader_create_sequential (NULL, dict_get_next_value_idx (dict),
209 &flip_casereader_class, flip);
210 proc_set_active_file_data (ds, reader);
211 return lex_end_of_command (lexer);
214 destroy_flip_pgm (flip);
215 return CMD_CASCADING_FAILURE;
220 destroy_flip_pgm (struct flip_pgm *flip)
223 pool_destroy (flip->pool);
226 /* Make a new variable with base name NAME, which is bowdlerized and
227 mangled until acceptable, and returns success. */
229 make_new_var (struct dictionary *dict, char name[])
233 /* Trim trailing spaces. */
234 cp = strchr (name, '\0');
235 while (cp > name && isspace ((unsigned char) cp[-1]))
238 /* Fix invalid characters. */
239 for (cp = name; *cp && cp < name + SHORT_NAME_LEN; cp++)
242 if (!lex_is_id1 (*cp) || *cp == '$')
247 if (!lex_is_idn (*cp))
251 str_uppercase (name);
253 if (dict_create_var (dict, name, 0))
256 /* Add numeric extensions until acceptable. */
258 const int len = (int) strlen (name);
259 char n[SHORT_NAME_LEN + 1];
262 for (i = 1; i < 10000000; i++)
264 int ofs = MIN (7 - intlog10 (i), len);
265 memcpy (n, name, ofs);
266 sprintf (&n[ofs], "%d", i);
268 if (dict_create_var (dict, n, 0))
273 msg (SE, _("Could not create acceptable variant for variable %s."), name);
277 /* Make a new dictionary for all the new variable names. */
279 build_dictionary (struct dictionary *dict, struct flip_pgm *flip)
281 dict_create_var_assert (dict, "CASE_LBL", 8);
283 if (flip->new_names_head == NULL)
287 if (flip->case_cnt > 99999)
289 msg (SE, _("Cannot create more than 99999 variable names."));
293 for (i = 0; i < flip->case_cnt; i++)
296 char s[SHORT_NAME_LEN + 1];
298 sprintf (s, "VAR%03d", i);
299 v = dict_create_var_assert (dict, s, 0);
306 for (v = flip->new_names_head; v; v = v->next)
307 if (!make_new_var (dict, v->name))
314 /* Writes case C to the FLIP sink.
315 Returns true if successful, false if an I/O error occurred. */
317 write_flip_case (struct flip_pgm *flip, const struct ccase *c)
323 if (flip->new_names != NULL)
325 struct varname *v = pool_alloc (flip->pool, sizeof *v);
326 int fv = flip->idx_to_fv[var_get_dict_index (flip->new_names)];
328 if (var_is_numeric (flip->new_names))
330 double f = case_num_idx (c, fv);
333 strcpy (v->name, "VSYSMIS");
334 else if (f < INT_MIN)
335 strcpy (v->name, "VNEGINF");
336 else if (f > INT_MAX)
337 strcpy (v->name, "VPOSINF");
339 snprintf (v->name, sizeof v->name, "V%d", (int) f);
343 int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING);
344 memcpy (v->name, case_str_idx (c, fv), width);
348 if (flip->new_names_head == NULL)
349 flip->new_names_head = v;
351 flip->new_names_tail->next = v;
352 flip->new_names_tail = v;
355 /* Write to external file. */
356 for (i = 0; i < flip->var_cnt; i++)
360 if (var_is_numeric (flip->var[i]))
362 int fv = flip->idx_to_fv[var_get_dict_index (flip->var[i])];
363 out = case_num_idx (c, fv);
367 fwrite (&out, sizeof out, 1, flip->file);
372 /* Transposes the external file into a new file. */
374 flip_file (struct flip_pgm *flip)
377 size_t case_capacity;
379 union value *input_buf, *output_buf;
380 FILE *input_file, *output_file;
382 /* Allocate memory for many cases. */
383 case_bytes = flip->var_cnt * sizeof *input_buf;
384 case_capacity = get_workspace () / case_bytes;
385 if (case_capacity > flip->case_cnt * 2)
386 case_capacity = flip->case_cnt * 2;
387 if (case_capacity < 2)
391 size_t bytes = case_bytes * case_capacity;
392 if (case_capacity > 2)
393 input_buf = malloc (bytes);
395 input_buf = xmalloc (bytes);
396 if (input_buf != NULL)
400 if (case_capacity < 2)
403 pool_register (flip->pool, free, input_buf);
405 /* Use half the allocated memory for input_buf, half for
408 output_buf = input_buf + flip->var_cnt * case_capacity;
410 input_file = flip->file;
411 if (fseek (input_file, 0, SEEK_SET) != 0)
413 msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
417 output_file = pool_tmpfile (flip->pool);
418 if (output_file == NULL)
420 msg (SE, _("Error creating FLIP source file."));
424 for (case_idx = 0; case_idx < flip->case_cnt; )
426 unsigned long read_cases = MIN (flip->case_cnt - case_idx,
430 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
432 if (ferror (input_file))
433 msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
435 msg (SE, _("Unexpected end of file reading FLIP file."));
439 for (i = 0; i < flip->var_cnt; i++)
443 for (j = 0; j < read_cases; j++)
444 output_buf[j] = input_buf[i + j * flip->var_cnt];
451 #define off_t long int
454 if (fseeko (output_file,
455 sizeof *input_buf * (case_idx
456 + (off_t) i * flip->case_cnt),
459 msg (SE, _("Error seeking FLIP source file: %s."),
464 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
467 msg (SE, _("Error writing FLIP source file: %s."),
473 case_idx += read_cases;
476 if (pool_fclose (flip->pool, input_file) == EOF)
478 msg (SE, _("Error closing FLIP source file: %s."), strerror (errno));
481 pool_unregister (flip->pool, input_buf);
484 if (fseek (output_file, 0, SEEK_SET) != 0)
486 msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
489 flip->file = output_file;
494 /* Reads one case into C.
495 Returns true if successful, false at end of file or if an
496 I/O error occurred. */
498 flip_casereader_read (struct casereader *reader UNUSED, void *flip_,
501 struct flip_pgm *flip = flip_;
504 if (flip->error || flip->cases_read >= flip->var_cnt)
507 case_create (c, flip->case_cnt);
508 for (i = 0; i < flip->case_cnt; i++)
511 if (fread (&in, sizeof in, 1, flip->file) != 1)
514 if (ferror (flip->file))
515 msg (SE, _("Error reading FLIP temporary file: %s."),
517 else if (feof (flip->file))
518 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
524 case_data_rw_idx (c, i)->f = in;
532 /* Destroys the source.
533 Returns true if successful read, false if an I/O occurred
534 during destruction or previously. */
536 flip_casereader_destroy (struct casereader *reader UNUSED, void *flip_)
538 struct flip_pgm *flip = flip_;
540 casereader_force_error (reader);
541 destroy_flip_pgm (flip);
544 static const struct casereader_class flip_casereader_class =
546 flip_casereader_read,
547 flip_casereader_destroy,