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
35 /* Variables to transpose. */
36 static struct variable **var;
39 /* Variable containing new variable names. */
40 static struct variable *newnames;
42 /* List of variable names. */
49 /* New variable names. */
50 static struct varname *new_names_head, *new_names_tail;
51 static int case_count;
53 static int build_dictionary (void);
55 /* Parses and executes FLIP. */
59 lex_match_id ("FLIP");
61 if (lex_match_id ("VARIABLES"))
64 if (!parse_variables (default_dict, &var, &nvar, PV_NO_DUPLICATE))
69 dict_get_vars (default_dict, &var, &nvar, 1u << DC_SYSTEM);
72 if (lex_match_id ("NEWNAMES"))
75 newnames = parse_variable ();
83 newnames = dict_lookup_var (default_dict, "CASE_LBL");
89 for (i = 0; i < nvar; i++)
90 if (var[i] == newnames)
92 memmove (&var[i], &var[i + 1], sizeof *var * (nvar - i - 1));
99 temp_trns = temporary = 0;
100 vfm_sink = &flip_stream;
101 new_names_tail = NULL;
102 procedure (NULL, NULL, NULL);
104 dict_clear (default_dict);
105 if (!build_dictionary ())
107 discard_variables ();
113 return lex_end_of_command ();
116 /* Make a new variable with base name NAME, which is bowdlerized and
117 mangled until acceptable, and returns success. */
119 make_new_var (char name[])
121 /* Fix invalid characters. */
125 for (cp = name; *cp && !isspace (*cp); cp++)
127 *cp = toupper ((unsigned char) *cp);
128 if (!isalpha (*cp) && *cp != '@' && *cp != '#'
129 && (cp == name || (*cp != '.' && *cp != '$' && *cp != '_')))
132 *cp = 'V'; /* _ not valid in first position. */
140 if (dict_create_var (default_dict, name, 0))
143 /* Add numeric extensions until acceptable. */
145 int len = (int) strlen (name);
149 for (i = 1; i < 10000000; i++)
151 int ofs = min (7 - intlog10 (i), len);
152 memcpy (n, name, ofs);
153 sprintf (&n[ofs], "%d", i);
155 if (dict_create_var (default_dict, n, 0))
160 msg (SE, _("Could not create acceptable variant for variable %s."), name);
164 /* Make a new dictionary for all the new variable names. */
166 build_dictionary (void)
168 if (!dict_create_var (default_dict, "CASE_LBL", 8))
175 if (case_count > 99999)
177 msg (SE, _("Cannot create more than 99999 variable names."));
181 for (i = 0; i < case_count; i++)
186 sprintf (s, "VAR%03d", i);
187 v = dict_create_var (default_dict, s, 0);
193 struct varname *v, *n;
195 new_names_tail->next = NULL;
196 for (v = new_names_head; v; v = n)
199 if (!make_new_var (v->name))
216 /* Each case to be transposed. */
219 struct flip_case *next;
223 /* Sink: Cases during transposition. */
224 static int internal; /* Internal vs. external flipping. */
225 static char *sink_old_names; /* Old variable names. */
226 static unsigned long sink_cases; /* Number of cases. */
227 static struct flip_case *head, *tail; /* internal == 1: Cases. */
228 static FILE *sink_file; /* internal == 0: Temporary file. */
230 /* Source: Cases after transposition. */
231 static struct flip_case *src; /* Internal transposition records. */
232 static char *src_old_names; /* Old variable names. */
233 static unsigned long src_cases; /* Number of cases. */
234 static FILE *src_file; /* src == NULL: Temporary file. */
236 /* Initialize the FLIP stream. */
238 flip_stream_init (void)
249 for (i = 0; i < nvar; i++)
250 n += strlen (var[i]->name);
251 p = sink_old_names = xmalloc (n);
252 for (i = 0; i < nvar; i++)
253 p = stpcpy (p, var[i]->name) + 1;
257 /* Reads the FLIP stream and passes it to write_case(). */
259 flip_stream_read (void)
261 if (src || (src == NULL && src_file == NULL))
263 /* Internal transposition, or empty file. */
265 char *p = src_old_names;
267 for (i = 0; i < nvar; i++)
269 struct flip_case *iter;
271 st_bare_pad_copy (temp_case->data[0].s, p, 8);
272 p = strchr (p, 0) + 1;
274 for (iter = src, j = 1; iter; iter = iter->next, j++)
275 temp_case->data[j].f = iter->v[i];
284 char *p = src_old_names;
286 for (i = 0; i < nvar; i++)
288 st_bare_pad_copy (temp_case->data[0].s, p, 8);
289 p = strchr (p, 0) + 1;
291 if (fread (&temp_case->data[1], sizeof (double), src_cases,
292 src_file) != src_cases)
293 msg (FE, _("Error reading FLIP source file: %s."),
302 /* Writes temp_case to the FLIP stream. */
304 flip_stream_write (void)
311 char name[INT_DIGITS + 2];
313 if (newnames->type == NUMERIC)
314 sprintf (name, "V%d", (int) temp_case->data[newnames->fv].f);
317 int width = min (newnames->width, 8);
318 memcpy (name, temp_case->data[newnames->fv].s, width);
322 v = xmalloc (sizeof (struct varname) + strlen (name) - 1);
323 strcpy (v->name, name);
325 if (new_names_tail == NULL)
328 new_names_tail->next = v;
337 flip_case *c = malloc (sizeof (flip_case)
338 + sizeof (double) * (nvar - 1));
342 /* Write to internal file. */
345 for (i = 0; i < nvar; i++)
346 if (var[i]->type == NUMERIC)
347 c->v[i] = temp_case->data[var[i]->fv].f;
362 /* Initialize external file. */
363 struct flip_case *iter, *next;
367 sink_file = tmpfile ();
369 msg (FE, _("Could not create temporary file for FLIP."));
373 for (iter = head; iter; iter = next)
377 if (fwrite (iter->v, sizeof (double), nvar, sink_file)
379 msg (FE, _("Error writing FLIP file: %s."),
386 /* Write to external file. */
388 double *d = local_alloc (sizeof *d * nvar);
391 for (i = 0; i < nvar; i++)
392 if (var[i]->type == NUMERIC)
393 d[i] = temp_case->data[var[i]->fv].f;
397 if (fwrite (d, sizeof *d, nvar, sink_file) != (size_t) nvar)
398 msg (FE, _("Error writing FLIP file: %s."),
405 /* Transpose the external file. */
407 transpose_external_file (void)
409 unsigned long n_cases;
410 unsigned long cur_case;
411 double *case_buf, *temp_buf;
413 n_cases = 4 * 1024 * 1024 / ((nvar + 1) * sizeof *case_buf);
418 assert (n_cases >= 2 /* 1 */);
419 case_buf = ((n_cases <= 2 ? xmalloc : (void *(*)(size_t)) malloc)
420 ((nvar + 1) * sizeof *case_buf * n_cases));
429 /* A temporary buffer that holds n_cases elements. */
430 temp_buf = &case_buf[nvar * n_cases];
432 src_file = tmpfile ();
434 msg (FE, _("Error creating FLIP source file."));
436 if (fseek (sink_file, 0, SEEK_SET) != 0)
437 msg (FE, _("Error rewinding FLIP file: %s."), strerror (errno));
439 for (cur_case = 0; cur_case < sink_cases; )
441 unsigned long read_cases = min (sink_cases - cur_case, n_cases);
444 if (read_cases != fread (case_buf, sizeof *case_buf * nvar,
445 read_cases, sink_file))
446 msg (FE, _("Error reading FLIP file: %s."), strerror (errno));
448 for (i = 0; i < nvar; i++)
452 for (j = 0; j < read_cases; j++)
453 temp_buf[j] = case_buf[i + j * nvar];
456 sizeof *case_buf * (cur_case + i * sink_cases),
458 msg (FE, _("Error seeking FLIP source file: %s."),
461 if (fwrite (temp_buf, sizeof *case_buf, read_cases, src_file)
463 msg (FE, _("Error writing FLIP source file: %s."),
467 cur_case += read_cases;
470 if (fseek (src_file, 0, SEEK_SET) != 0)
471 msg (FE, _("Error rewind FLIP source file: %s."), strerror (errno));
478 /* Change the FLIP stream from sink to source mode. */
480 flip_stream_mode (void)
482 src_cases = sink_cases;
483 src_old_names = sink_old_names;
484 sink_old_names = NULL;
502 transpose_external_file ();
506 /* Destroy source's internal data. */
508 flip_stream_destroy_source (void)
510 free (src_old_names);
513 struct flip_case *iter, *next;
515 for (iter = src; iter; iter = next)
525 /* Destroy sink's internal data. */
527 flip_stream_destroy_sink (void)
529 struct flip_case *iter, *next;
531 free (sink_old_names);
536 for (iter = head; iter; iter = next)
543 struct case_stream flip_stream =
549 flip_stream_destroy_source,
550 flip_stream_destroy_sink,