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
37 /* List of variable names. */
44 /* Represents a FLIP input program. */
47 struct variable **var; /* Variables to transpose. */
48 int var_cnt; /* Number of elements in `var'. */
49 int case_cnt; /* Pre-flip case count. */
50 size_t case_size; /* Post-flip bytes per case. */
52 struct variable *new_names; /* Variable containing new variable names. */
53 struct varname *new_names_head; /* First new variable. */
54 struct varname *new_names_tail; /* Last new variable. */
56 FILE *file; /* Temporary file containing data. */
59 static void destroy_flip_pgm (struct flip_pgm *);
60 static struct case_sink *flip_sink_create (struct flip_pgm *);
61 static struct case_source *flip_source_create (struct flip_pgm *);
62 static void flip_file (struct flip_pgm *);
63 static int build_dictionary (struct flip_pgm *);
65 static const struct case_source_class flip_source_class;
66 static const struct case_sink_class flip_sink_class;
68 /* Parses and executes FLIP. */
72 struct flip_pgm *flip;
74 flip = xmalloc (sizeof *flip);
78 flip->new_names = NULL;
79 flip->new_names_head = NULL;
80 flip->new_names_tail = NULL;
83 lex_match_id ("FLIP");
85 if (lex_match_id ("VARIABLES"))
88 if (!parse_variables (default_dict, &flip->var, &flip->var_cnt, PV_NO_DUPLICATE))
93 dict_get_vars (default_dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
96 if (lex_match_id ("NEWNAMES"))
99 flip->new_names = parse_variable ();
100 if (!flip->new_names)
104 flip->new_names = dict_lookup_var (default_dict, "CASE_LBL");
110 for (i = 0; i < flip->var_cnt; i++)
111 if (flip->var[i] == flip->new_names)
113 memmove (&flip->var[i], &flip->var[i + 1], sizeof *flip->var * (flip->var_cnt - i - 1));
119 /* Read the active file into a flip_sink. */
121 temp_trns = temporary = 0;
122 vfm_sink = flip_sink_create (flip);
123 flip->new_names_tail = NULL;
124 procedure (NULL, NULL, NULL, NULL);
126 /* Flip the data we read. */
129 /* Flip the dictionary. */
130 dict_clear (default_dict);
131 if (!build_dictionary (flip))
133 discard_variables ();
136 flip->case_size = dict_get_case_size (default_dict);
138 /* Set up flipped data for reading. */
139 vfm_source = flip_source_create (flip);
141 return lex_end_of_command ();
144 destroy_flip_pgm (flip);
150 destroy_flip_pgm (struct flip_pgm *flip)
152 struct varname *iter, *next;
155 for (iter = flip->new_names_head; iter != NULL; iter = next)
160 if (flip->file != NULL)
165 /* Make a new variable with base name NAME, which is bowdlerized and
166 mangled until acceptable, and returns success. */
168 make_new_var (char name[])
170 /* Fix invalid characters. */
174 for (cp = name; *cp && !isspace (*cp); cp++)
176 *cp = toupper ((unsigned char) *cp);
177 if (!isalpha (*cp) && *cp != '@' && *cp != '#'
178 && (cp == name || (*cp != '.' && *cp != '$' && *cp != '_'
182 *cp = 'V'; /* _ not valid in first position. */
190 if (dict_create_var (default_dict, name, 0))
193 /* Add numeric extensions until acceptable. */
195 int len = (int) strlen (name);
199 for (i = 1; i < 10000000; i++)
201 int ofs = min (7 - intlog10 (i), len);
202 memcpy (n, name, ofs);
203 sprintf (&n[ofs], "%d", i);
205 if (dict_create_var (default_dict, n, 0))
210 msg (SE, _("Could not create acceptable variant for variable %s."), name);
214 /* Make a new dictionary for all the new variable names. */
216 build_dictionary (struct flip_pgm *flip)
218 dict_create_var_assert (default_dict, "CASE_LBL", 8);
220 if (flip->new_names_head == NULL)
224 if (flip->case_cnt > 99999)
226 msg (SE, _("Cannot create more than 99999 variable names."));
230 for (i = 0; i < flip->case_cnt; i++)
235 sprintf (s, "VAR%03d", i);
236 v = dict_create_var_assert (default_dict, s, 0);
243 for (v = flip->new_names_head; v; v = v->next)
244 if (!make_new_var (v->name))
251 /* Cases during transposition. */
252 struct flip_sink_info
254 struct flip_pgm *flip; /* FLIP program. */
255 union value *output_buf; /* Case output buffer. */
258 /* Creates a flip sink based on FLIP. */
259 static struct case_sink *
260 flip_sink_create (struct flip_pgm *flip)
262 struct flip_sink_info *info = xmalloc (sizeof *info);
266 info->output_buf = xmalloc (sizeof *info->output_buf * flip->var_cnt);
268 flip->file = tmpfile ();
270 msg (FE, _("Could not create temporary file for FLIP."));
272 /* Write variable names as first case. */
273 for (i = 0; i < flip->var_cnt; i++)
274 st_bare_pad_copy (info->output_buf[i].s, flip->var[i]->name, 8);
275 if (fwrite (info->output_buf, sizeof *info->output_buf,
276 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
277 msg (FE, _("Error writing FLIP file: %s."), strerror (errno));
281 return create_case_sink (&flip_sink_class, info);
284 /* Writes case C to the FLIP sink. */
286 flip_sink_write (struct case_sink *sink, const struct ccase *c)
288 struct flip_sink_info *info = sink->aux;
289 struct flip_pgm *flip = info->flip;
294 if (flip->new_names != NULL)
296 struct varname *v = xmalloc (sizeof (struct varname));
298 if (flip->new_names->type == NUMERIC)
300 double f = c->data[flip->new_names->fv].f;
303 strcpy (v->name, "VSYSMIS");
304 else if (f < INT_MIN)
305 strcpy (v->name, "VNEGINF");
306 else if (f > INT_MAX)
307 strcpy (v->name, "VPOSINF");
310 char name[INT_DIGITS + 2];
311 sprintf (name, "V%d", (int) f);
312 strncpy (v->name, name, 8);
318 int width = min (flip->new_names->width, 8);
319 memcpy (v->name, c->data[flip->new_names->fv].s, width);
323 if (flip->new_names_head == NULL)
324 flip->new_names_head = v;
326 flip->new_names_tail->next = v;
327 flip->new_names_tail = v;
330 /* Write to external file. */
331 for (i = 0; i < flip->var_cnt; i++)
332 if (flip->var[i]->type == NUMERIC)
333 info->output_buf[i].f = c->data[flip->var[i]->fv].f;
335 info->output_buf[i].f = SYSMIS;
337 if (fwrite (info->output_buf, sizeof *info->output_buf,
338 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
339 msg (FE, _("Error writing FLIP file: %s."), strerror (errno));
342 /* Transposes the external file into a new file. */
344 flip_file (struct flip_pgm *flip)
347 size_t case_capacity;
349 union value *input_buf, *output_buf;
350 FILE *input_file, *output_file;
352 /* Allocate memory for many cases. */
353 case_bytes = flip->var_cnt * sizeof *input_buf;
354 case_capacity = set_max_workspace / case_bytes;
355 if (case_capacity > flip->case_cnt * 2)
356 case_capacity = flip->case_cnt * 2;
357 if (case_capacity < 2)
361 size_t bytes = case_bytes * case_capacity;
362 if (case_capacity > 2)
363 input_buf = malloc (bytes);
365 input_buf = xmalloc (bytes);
366 if (input_buf != NULL)
370 if (case_capacity < 2)
374 /* Use half the allocated memory for input_buf, half for
377 output_buf = input_buf + flip->var_cnt * case_capacity;
379 input_file = flip->file;
380 if (fseek (input_file, 0, SEEK_SET) != 0)
381 msg (FE, _("Error rewinding FLIP file: %s."), strerror (errno));
383 output_file = tmpfile ();
384 if (output_file == NULL)
385 msg (FE, _("Error creating FLIP source file."));
387 for (case_idx = 0; case_idx < flip->case_cnt; )
389 unsigned long read_cases = min (flip->case_cnt - case_idx,
393 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
394 msg (FE, _("Error reading FLIP file: %s."), strerror (errno));
396 for (i = 0; i < flip->var_cnt; i++)
400 for (j = 0; j < read_cases; j++)
401 output_buf[j] = input_buf[i + j * flip->var_cnt];
403 if (fseek (output_file,
404 sizeof *input_buf * (case_idx + i * flip->case_cnt),
406 msg (FE, _("Error seeking FLIP source file: %s."),
409 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
411 msg (FE, _("Error writing FLIP source file: %s."),
415 case_idx += read_cases;
421 if (fseek (output_file, 0, SEEK_SET) != 0)
422 msg (FE, _("Error rewind FLIP source file: %s."), strerror (errno));
423 flip->file = output_file;
426 /* Destroy sink's internal data. */
428 flip_sink_destroy (struct case_sink *sink)
430 struct flip_sink_info *info = sink->aux;
432 free (info->output_buf);
436 /* FLIP sink class. */
437 static const struct case_sink_class flip_sink_class =
446 /* Creates and returns a FLIP source based on PGM,
447 which should have already been used as a sink. */
448 static struct case_source *
449 flip_source_create (struct flip_pgm *pgm)
451 return create_case_source (&flip_source_class, default_dict, pgm);
454 /* Reads the FLIP stream. Copies each case into C and calls
455 WRITE_CASE passing WC_DATA. */
457 flip_source_read (struct case_source *source,
459 write_case_func *write_case, write_case_data wc_data)
461 struct flip_pgm *flip = source->aux;
464 for (i = 0; i < flip->var_cnt; i++)
466 if (fread (c->data, sizeof *c->data, flip->case_cnt,
467 flip->file) != flip->case_cnt)
469 if (ferror (flip->file))
470 msg (SE, _("Error reading FLIP temporary file: %s."),
472 else if (feof (flip->file))
473 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
479 if (!write_case (wc_data))
484 /* Destroy internal data in SOURCE. */
486 flip_source_destroy (struct case_source *source)
488 struct flip_pgm *flip = source->aux;
490 destroy_flip_pgm (flip);
493 static const struct case_source_class flip_source_class =