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
27 #include "algorithm.h"
31 #include "dictionary.h"
42 /* List of variable names. */
46 char name[SHORT_NAME_LEN + 1];
49 /* Represents a FLIP input program. */
52 struct variable **var; /* Variables to transpose. */
53 int *idx_to_fv; /* var[]->index to compacted sink case fv. */
54 int var_cnt; /* Number of elements in `var'. */
55 int case_cnt; /* Pre-flip case count. */
56 size_t case_size; /* Post-flip bytes per case. */
58 struct variable *new_names; /* Variable containing new variable names. */
59 struct varname *new_names_head; /* First new variable. */
60 struct varname *new_names_tail; /* Last new variable. */
62 FILE *file; /* Temporary file containing data. */
65 static void destroy_flip_pgm (struct flip_pgm *);
66 static struct case_sink *flip_sink_create (struct flip_pgm *);
67 static struct case_source *flip_source_create (struct flip_pgm *);
68 static void flip_file (struct flip_pgm *);
69 static int build_dictionary (struct flip_pgm *);
71 static const struct case_source_class flip_source_class;
72 static const struct case_sink_class flip_sink_class;
74 /* Parses and executes FLIP. */
78 struct flip_pgm *flip;
82 msg (SM, _("FLIP ignores TEMPORARY. "
83 "Temporary transformations will be made permanent."));
87 flip = xmalloc (sizeof *flip);
89 flip->idx_to_fv = dict_get_compacted_idx_to_fv (default_dict);
92 flip->new_names = NULL;
93 flip->new_names_head = NULL;
94 flip->new_names_tail = NULL;
98 if (lex_match_id ("VARIABLES"))
101 if (!parse_variables (default_dict, &flip->var, &flip->var_cnt, PV_NO_DUPLICATE))
106 dict_get_vars (default_dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
109 if (lex_match_id ("NEWNAMES"))
112 flip->new_names = parse_variable ();
113 if (!flip->new_names)
117 flip->new_names = dict_lookup_var (default_dict, "CASE_LBL");
123 for (i = 0; i < flip->var_cnt; i++)
124 if (flip->var[i] == flip->new_names)
126 remove_element (flip->var, flip->var_cnt, sizeof *flip->var, i);
132 /* Read the active file into a flip_sink. */
134 temp_trns = temporary = 0;
135 vfm_sink = flip_sink_create (flip);
136 flip->new_names_tail = NULL;
137 procedure (NULL, NULL);
139 /* Flip the data we read. */
142 /* Flip the dictionary. */
143 dict_clear (default_dict);
144 if (!build_dictionary (flip))
146 discard_variables ();
149 flip->case_size = dict_get_case_size (default_dict);
151 /* Set up flipped data for reading. */
152 vfm_source = flip_source_create (flip);
154 return lex_end_of_command ();
157 destroy_flip_pgm (flip);
163 destroy_flip_pgm (struct flip_pgm *flip)
165 struct varname *iter, *next;
168 free (flip->idx_to_fv);
169 for (iter = flip->new_names_head; iter != NULL; iter = next)
174 if (flip->file != NULL)
179 /* Make a new variable with base name NAME, which is bowdlerized and
180 mangled until acceptable, and returns success. */
182 make_new_var (char name[])
184 /* Fix invalid characters. */
188 for (cp = name; *cp && !isspace (*cp); cp++)
190 *cp = toupper ((unsigned char) *cp);
191 if (!isalpha (*cp) && *cp != '@' && *cp != '#'
192 && (cp == name || (*cp != '.' && *cp != '$' && *cp != '_'
196 *cp = 'V'; /* _ not valid in first position. */
204 if (dict_create_var (default_dict, name, 0))
207 /* Add numeric extensions until acceptable. */
209 const int len = (int) strlen (name);
210 char n[SHORT_NAME_LEN + 1];
213 for (i = 1; i < 10000000; i++)
215 int ofs = min (7 - intlog10 (i), len);
216 memcpy (n, name, ofs);
217 sprintf (&n[ofs], "%d", i);
219 if (dict_create_var (default_dict, n, 0))
224 msg (SE, _("Could not create acceptable variant for variable %s."), name);
228 /* Make a new dictionary for all the new variable names. */
230 build_dictionary (struct flip_pgm *flip)
232 dict_create_var_assert (default_dict, "CASE_LBL", 8);
234 if (flip->new_names_head == NULL)
238 if (flip->case_cnt > 99999)
240 msg (SE, _("Cannot create more than 99999 variable names."));
244 for (i = 0; i < flip->case_cnt; i++)
247 char s[SHORT_NAME_LEN + 1];
249 sprintf (s, "VAR%03d", i);
250 v = dict_create_var_assert (default_dict, s, 0);
257 for (v = flip->new_names_head; v; v = v->next)
258 if (!make_new_var (v->name))
265 /* Cases during transposition. */
266 struct flip_sink_info
268 struct flip_pgm *flip; /* FLIP program. */
269 union value *output_buf; /* Case output buffer. */
272 /* Creates a flip sink based on FLIP. */
273 static struct case_sink *
274 flip_sink_create (struct flip_pgm *flip)
276 struct flip_sink_info *info = xmalloc (sizeof *info);
280 info->output_buf = xmalloc (sizeof *info->output_buf * flip->var_cnt);
282 flip->file = tmpfile ();
284 msg (FE, _("Could not create temporary file for FLIP."));
286 /* Write variable names as first case. */
287 for (i = 0; i < flip->var_cnt; i++)
288 st_bare_pad_copy (info->output_buf[i].s, flip->var[i]->name, MAX_SHORT_STRING);
289 if (fwrite (info->output_buf, sizeof *info->output_buf,
290 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
291 msg (FE, _("Error writing FLIP file: %s."), strerror (errno));
295 return create_case_sink (&flip_sink_class, default_dict, info);
298 /* Writes case C to the FLIP sink. */
300 flip_sink_write (struct case_sink *sink, const struct ccase *c)
302 struct flip_sink_info *info = sink->aux;
303 struct flip_pgm *flip = info->flip;
308 if (flip->new_names != NULL)
310 struct varname *v = xmalloc (sizeof (struct varname));
312 if (flip->new_names->type == NUMERIC)
314 double f = case_num (c, flip->idx_to_fv[flip->new_names->index]);
317 strcpy (v->name, "VSYSMIS");
318 else if (f < INT_MIN)
319 strcpy (v->name, "VNEGINF");
320 else if (f > INT_MAX)
321 strcpy (v->name, "VPOSINF");
324 char name[INT_DIGITS + 2];
325 sprintf (name, "V%d", (int) f);
326 strncpy (v->name, name, SHORT_NAME_LEN);
327 name[SHORT_NAME_LEN] = 0;
332 int width = min (flip->new_names->width, MAX_SHORT_STRING);
333 memcpy (v->name, case_str (c, flip->idx_to_fv[flip->new_names->index]),
338 if (flip->new_names_head == NULL)
339 flip->new_names_head = v;
341 flip->new_names_tail->next = v;
342 flip->new_names_tail = v;
345 /* Write to external file. */
346 for (i = 0; i < flip->var_cnt; i++)
350 if (flip->var[i]->type == NUMERIC)
351 out = case_num (c, flip->idx_to_fv[flip->var[i]->index]);
354 info->output_buf[i].f = out;
357 if (fwrite (info->output_buf, sizeof *info->output_buf,
358 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
359 msg (FE, _("Error writing FLIP file: %s."), strerror (errno));
362 /* Transposes the external file into a new file. */
364 flip_file (struct flip_pgm *flip)
367 size_t case_capacity;
369 union value *input_buf, *output_buf;
370 FILE *input_file, *output_file;
372 /* Allocate memory for many cases. */
373 case_bytes = flip->var_cnt * sizeof *input_buf;
374 case_capacity = get_max_workspace() / case_bytes;
375 if (case_capacity > flip->case_cnt * 2)
376 case_capacity = flip->case_cnt * 2;
377 if (case_capacity < 2)
381 size_t bytes = case_bytes * case_capacity;
382 if (case_capacity > 2)
383 input_buf = malloc (bytes);
385 input_buf = xmalloc (bytes);
386 if (input_buf != NULL)
390 if (case_capacity < 2)
394 /* Use half the allocated memory for input_buf, half for
397 output_buf = input_buf + flip->var_cnt * case_capacity;
399 input_file = flip->file;
400 if (fseek (input_file, 0, SEEK_SET) != 0)
401 msg (FE, _("Error rewinding FLIP file: %s."), strerror (errno));
403 output_file = tmpfile ();
404 if (output_file == NULL)
405 msg (FE, _("Error creating FLIP source file."));
407 for (case_idx = 0; case_idx < flip->case_cnt; )
409 unsigned long read_cases = min (flip->case_cnt - case_idx,
413 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
414 msg (FE, _("Error reading FLIP file: %s."), strerror (errno));
416 for (i = 0; i < flip->var_cnt; i++)
420 for (j = 0; j < read_cases; j++)
421 output_buf[j] = input_buf[i + j * flip->var_cnt];
428 #define off_t long int
431 if (fseeko (output_file,
432 sizeof *input_buf * (case_idx
433 + (off_t) i * flip->case_cnt),
435 msg (FE, _("Error seeking FLIP source file: %s."),
438 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
440 msg (FE, _("Error writing FLIP source file: %s."),
444 case_idx += read_cases;
450 if (fseek (output_file, 0, SEEK_SET) != 0)
451 msg (FE, _("Error rewind FLIP source file: %s."), strerror (errno));
452 flip->file = output_file;
455 /* Destroy sink's internal data. */
457 flip_sink_destroy (struct case_sink *sink)
459 struct flip_sink_info *info = sink->aux;
461 free (info->output_buf);
465 /* FLIP sink class. */
466 static const struct case_sink_class flip_sink_class =
475 /* Creates and returns a FLIP source based on PGM,
476 which should have already been used as a sink. */
477 static struct case_source *
478 flip_source_create (struct flip_pgm *pgm)
480 return create_case_source (&flip_source_class, pgm);
483 /* Reads the FLIP stream. Copies each case into C and calls
484 WRITE_CASE passing WC_DATA. */
486 flip_source_read (struct case_source *source,
488 write_case_func *write_case, write_case_data wc_data)
490 struct flip_pgm *flip = source->aux;
491 union value *input_buf;
494 input_buf = xmalloc (sizeof *input_buf * flip->case_cnt);
495 for (i = 0; i < flip->var_cnt; i++)
499 if (fread (input_buf, sizeof *input_buf, flip->case_cnt,
500 flip->file) != flip->case_cnt)
502 if (ferror (flip->file))
503 msg (SE, _("Error reading FLIP temporary file: %s."),
505 else if (feof (flip->file))
506 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
512 for (j = 0; j < flip->case_cnt; j++)
513 case_data_rw (c, j)->f = input_buf[j].f;
514 if (!write_case (wc_data))
520 /* Destroy internal data in SOURCE. */
522 flip_source_destroy (struct case_source *source)
524 struct flip_pgm *flip = source->aux;
526 destroy_flip_pgm (flip);
529 static const struct case_source_class flip_source_class =