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., 59 Temple Place - Suite 330, Boston, MA
30 #include "dictionary.h"
39 /* List of variable names. */
46 /* Represents a FLIP input program. */
49 struct variable **var; /* Variables to transpose. */
50 int var_cnt; /* Number of elements in `var'. */
51 int case_cnt; /* Pre-flip case count. */
52 size_t case_size; /* Post-flip bytes per case. */
54 struct variable *new_names; /* Variable containing new variable names. */
55 struct varname *new_names_head; /* First new variable. */
56 struct varname *new_names_tail; /* Last new variable. */
58 FILE *file; /* Temporary file containing data. */
61 static void destroy_flip_pgm (struct flip_pgm *);
62 static struct case_sink *flip_sink_create (struct flip_pgm *);
63 static struct case_source *flip_source_create (struct flip_pgm *);
64 static void flip_file (struct flip_pgm *);
65 static int build_dictionary (struct flip_pgm *);
67 static const struct case_source_class flip_source_class;
68 static const struct case_sink_class flip_sink_class;
70 /* Parses and executes FLIP. */
74 struct flip_pgm *flip;
78 msg (SM, _("FLIP ignores TEMPORARY. "
79 "Temporary transformations will be made permanent."));
83 flip = xmalloc (sizeof *flip);
87 flip->new_names = NULL;
88 flip->new_names_head = NULL;
89 flip->new_names_tail = NULL;
93 if (lex_match_id ("VARIABLES"))
96 if (!parse_variables (default_dict, &flip->var, &flip->var_cnt, PV_NO_DUPLICATE))
101 dict_get_vars (default_dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
104 if (lex_match_id ("NEWNAMES"))
107 flip->new_names = parse_variable ();
108 if (!flip->new_names)
112 flip->new_names = dict_lookup_var (default_dict, "CASE_LBL");
118 for (i = 0; i < flip->var_cnt; i++)
119 if (flip->var[i] == flip->new_names)
121 memmove (&flip->var[i], &flip->var[i + 1], sizeof *flip->var * (flip->var_cnt - i - 1));
127 /* Read the active file into a flip_sink. */
129 temp_trns = temporary = 0;
130 vfm_sink = flip_sink_create (flip);
131 flip->new_names_tail = NULL;
132 procedure (NULL, NULL);
134 /* Flip the data we read. */
137 /* Flip the dictionary. */
138 dict_clear (default_dict);
139 if (!build_dictionary (flip))
141 discard_variables ();
144 flip->case_size = dict_get_case_size (default_dict);
146 /* Set up flipped data for reading. */
147 vfm_source = flip_source_create (flip);
149 return lex_end_of_command ();
152 destroy_flip_pgm (flip);
158 destroy_flip_pgm (struct flip_pgm *flip)
160 struct varname *iter, *next;
163 for (iter = flip->new_names_head; iter != NULL; iter = next)
168 if (flip->file != NULL)
173 /* Make a new variable with base name NAME, which is bowdlerized and
174 mangled until acceptable, and returns success. */
176 make_new_var (char name[])
178 /* Fix invalid characters. */
182 for (cp = name; *cp && !isspace (*cp); cp++)
184 *cp = toupper ((unsigned char) *cp);
185 if (!isalpha (*cp) && *cp != '@' && *cp != '#'
186 && (cp == name || (*cp != '.' && *cp != '$' && *cp != '_'
190 *cp = 'V'; /* _ not valid in first position. */
198 if (dict_create_var (default_dict, name, 0))
201 /* Add numeric extensions until acceptable. */
203 int len = (int) strlen (name);
207 for (i = 1; i < 10000000; i++)
209 int ofs = min (7 - intlog10 (i), len);
210 memcpy (n, name, ofs);
211 sprintf (&n[ofs], "%d", i);
213 if (dict_create_var (default_dict, n, 0))
218 msg (SE, _("Could not create acceptable variant for variable %s."), name);
222 /* Make a new dictionary for all the new variable names. */
224 build_dictionary (struct flip_pgm *flip)
226 dict_create_var_assert (default_dict, "CASE_LBL", 8);
228 if (flip->new_names_head == NULL)
232 if (flip->case_cnt > 99999)
234 msg (SE, _("Cannot create more than 99999 variable names."));
238 for (i = 0; i < flip->case_cnt; i++)
243 sprintf (s, "VAR%03d", i);
244 v = dict_create_var_assert (default_dict, s, 0);
251 for (v = flip->new_names_head; v; v = v->next)
252 if (!make_new_var (v->name))
259 /* Cases during transposition. */
260 struct flip_sink_info
262 struct flip_pgm *flip; /* FLIP program. */
263 union value *output_buf; /* Case output buffer. */
266 /* Creates a flip sink based on FLIP. */
267 static struct case_sink *
268 flip_sink_create (struct flip_pgm *flip)
270 struct flip_sink_info *info = xmalloc (sizeof *info);
274 info->output_buf = xmalloc (sizeof *info->output_buf * flip->var_cnt);
276 flip->file = tmpfile ();
278 msg (FE, _("Could not create temporary file for FLIP."));
280 /* Write variable names as first case. */
281 for (i = 0; i < flip->var_cnt; i++)
282 st_bare_pad_copy (info->output_buf[i].s, flip->var[i]->name, 8);
283 if (fwrite (info->output_buf, sizeof *info->output_buf,
284 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
285 msg (FE, _("Error writing FLIP file: %s."), strerror (errno));
289 return create_case_sink (&flip_sink_class, default_dict, info);
292 /* Writes case C to the FLIP sink. */
294 flip_sink_write (struct case_sink *sink, const struct ccase *c)
296 struct flip_sink_info *info = sink->aux;
297 struct flip_pgm *flip = info->flip;
302 if (flip->new_names != NULL)
304 struct varname *v = xmalloc (sizeof (struct varname));
306 if (flip->new_names->type == NUMERIC)
308 double f = case_num (c, sink->idx_to_fv[flip->new_names->index]);
311 strcpy (v->name, "VSYSMIS");
312 else if (f < INT_MIN)
313 strcpy (v->name, "VNEGINF");
314 else if (f > INT_MAX)
315 strcpy (v->name, "VPOSINF");
318 char name[INT_DIGITS + 2];
319 sprintf (name, "V%d", (int) f);
320 strncpy (v->name, name, 8);
326 int width = min (flip->new_names->width, 8);
327 memcpy (v->name, case_str (c, sink->idx_to_fv[flip->new_names->index]),
332 if (flip->new_names_head == NULL)
333 flip->new_names_head = v;
335 flip->new_names_tail->next = v;
336 flip->new_names_tail = v;
339 /* Write to external file. */
340 for (i = 0; i < flip->var_cnt; i++)
344 if (flip->var[i]->type == NUMERIC)
345 out = case_num (c, sink->idx_to_fv[flip->var[i]->index]);
348 info->output_buf[i].f = out;
351 if (fwrite (info->output_buf, sizeof *info->output_buf,
352 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
353 msg (FE, _("Error writing FLIP file: %s."), strerror (errno));
356 /* Transposes the external file into a new file. */
358 flip_file (struct flip_pgm *flip)
361 size_t case_capacity;
363 union value *input_buf, *output_buf;
364 FILE *input_file, *output_file;
366 /* Allocate memory for many cases. */
367 case_bytes = flip->var_cnt * sizeof *input_buf;
368 case_capacity = get_max_workspace() / case_bytes;
369 if (case_capacity > flip->case_cnt * 2)
370 case_capacity = flip->case_cnt * 2;
371 if (case_capacity < 2)
375 size_t bytes = case_bytes * case_capacity;
376 if (case_capacity > 2)
377 input_buf = malloc (bytes);
379 input_buf = xmalloc (bytes);
380 if (input_buf != NULL)
384 if (case_capacity < 2)
388 /* Use half the allocated memory for input_buf, half for
391 output_buf = input_buf + flip->var_cnt * case_capacity;
393 input_file = flip->file;
394 if (fseek (input_file, 0, SEEK_SET) != 0)
395 msg (FE, _("Error rewinding FLIP file: %s."), strerror (errno));
397 output_file = tmpfile ();
398 if (output_file == NULL)
399 msg (FE, _("Error creating FLIP source file."));
401 for (case_idx = 0; case_idx < flip->case_cnt; )
403 unsigned long read_cases = min (flip->case_cnt - case_idx,
407 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
408 msg (FE, _("Error reading FLIP file: %s."), strerror (errno));
410 for (i = 0; i < flip->var_cnt; i++)
414 for (j = 0; j < read_cases; j++)
415 output_buf[j] = input_buf[i + j * flip->var_cnt];
421 if (fseeko (output_file,
422 sizeof *input_buf * (case_idx
423 + (off_t) i * flip->case_cnt),
425 msg (FE, _("Error seeking FLIP source file: %s."),
428 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
430 msg (FE, _("Error writing FLIP source file: %s."),
434 case_idx += read_cases;
440 if (fseek (output_file, 0, SEEK_SET) != 0)
441 msg (FE, _("Error rewind FLIP source file: %s."), strerror (errno));
442 flip->file = output_file;
445 /* Destroy sink's internal data. */
447 flip_sink_destroy (struct case_sink *sink)
449 struct flip_sink_info *info = sink->aux;
451 free (info->output_buf);
455 /* FLIP sink class. */
456 static const struct case_sink_class flip_sink_class =
465 /* Creates and returns a FLIP source based on PGM,
466 which should have already been used as a sink. */
467 static struct case_source *
468 flip_source_create (struct flip_pgm *pgm)
470 return create_case_source (&flip_source_class, default_dict, pgm);
473 /* Reads the FLIP stream. Copies each case into C and calls
474 WRITE_CASE passing WC_DATA. */
476 flip_source_read (struct case_source *source,
478 write_case_func *write_case, write_case_data wc_data)
480 struct flip_pgm *flip = source->aux;
481 union value *input_buf;
484 input_buf = xmalloc (sizeof *input_buf * flip->case_cnt);
485 for (i = 0; i < flip->var_cnt; i++)
489 if (fread (input_buf, sizeof *input_buf, flip->case_cnt,
490 flip->file) != flip->case_cnt)
492 if (ferror (flip->file))
493 msg (SE, _("Error reading FLIP temporary file: %s."),
495 else if (feof (flip->file))
496 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
502 for (j = 0; j < flip->case_cnt; j++)
503 case_data_rw (c, j)->f = input_buf[j].f;
504 if (!write_case (wc_data))
510 /* Destroy internal data in SOURCE. */
512 flip_source_destroy (struct case_source *source)
514 struct flip_pgm *flip = source->aux;
516 destroy_flip_pgm (flip);
519 static const struct case_source_class flip_source_class =