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 fill_all_vars (&var, &nvar, FV_NO_SYSTEM);
72 if (lex_match_id ("NEWNAMES"))
75 newnames = parse_variable ();
83 newnames = find_variable ("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 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 (create_variable (&default_dict, name, NUMERIC, 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 (create_variable (&default_dict, n, NUMERIC, 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 force_create_variable (&default_dict, "CASE_LBL", ALPHA, 8);
174 if (case_count > 99999)
176 msg (SE, _("Cannot create more than 99999 variable names."));
180 for (i = 0; i < case_count; i++)
184 sprintf (s, "VAR%03d", i);
185 force_create_variable (&default_dict, s, NUMERIC, 0);
190 struct varname *v, *n;
192 new_names_tail->next = NULL;
193 for (v = new_names_head; v; v = n)
196 if (!make_new_var (v->name))
213 /* Each case to be transposed. */
216 struct flip_case *next;
220 /* Sink: Cases during transposition. */
221 static int internal; /* Internal vs. external flipping. */
222 static char *sink_old_names; /* Old variable names. */
223 static unsigned long sink_cases; /* Number of cases. */
224 static struct flip_case *head, *tail; /* internal == 1: Cases. */
225 static FILE *sink_file; /* internal == 0: Temporary file. */
227 /* Source: Cases after transposition. */
228 static struct flip_case *src; /* Internal transposition records. */
229 static char *src_old_names; /* Old variable names. */
230 static unsigned long src_cases; /* Number of cases. */
231 static FILE *src_file; /* src == NULL: Temporary file. */
233 /* Initialize the FLIP stream. */
235 flip_stream_init (void)
246 for (i = 0; i < nvar; i++)
247 n += strlen (var[i]->name);
248 p = sink_old_names = xmalloc (n);
249 for (i = 0; i < nvar; i++)
250 p = stpcpy (p, var[i]->name) + 1;
254 /* Reads the FLIP stream and passes it to write_case(). */
256 flip_stream_read (void)
258 if (src || (src == NULL && src_file == NULL))
260 /* Internal transposition, or empty file. */
262 char *p = src_old_names;
264 for (i = 0; i < nvar; i++)
266 struct flip_case *iter;
268 st_bare_pad_copy (temp_case->data[0].s, p, 8);
269 p = strchr (p, 0) + 1;
271 for (iter = src, j = 1; iter; iter = iter->next, j++)
272 temp_case->data[j].f = iter->v[i];
281 char *p = src_old_names;
283 for (i = 0; i < nvar; i++)
285 st_bare_pad_copy (temp_case->data[0].s, p, 8);
286 p = strchr (p, 0) + 1;
288 if (fread (&temp_case->data[1], sizeof (double), src_cases,
289 src_file) != src_cases)
290 msg (FE, _("Error reading FLIP source file: %s."),
299 /* Writes temp_case to the FLIP stream. */
301 flip_stream_write (void)
308 char name[INT_DIGITS + 2];
310 if (newnames->type == NUMERIC)
311 sprintf (name, "V%d", (int) temp_case->data[newnames->fv].f);
314 int width = min (newnames->width, 8);
315 memcpy (name, temp_case->data[newnames->fv].s, width);
319 v = xmalloc (sizeof (struct varname) + strlen (name) - 1);
320 strcpy (v->name, name);
322 if (new_names_tail == NULL)
325 new_names_tail->next = v;
334 flip_case *c = malloc (sizeof (flip_case)
335 + sizeof (double) * (nvar - 1));
339 /* Write to internal file. */
342 for (i = 0; i < nvar; i++)
343 if (var[i]->type == NUMERIC)
344 c->v[i] = temp_case->data[var[i]->fv].f;
359 /* Initialize external file. */
360 struct flip_case *iter, *next;
364 sink_file = tmpfile ();
366 msg (FE, _("Could not create temporary file for FLIP."));
370 for (iter = head; iter; iter = next)
374 if (fwrite (iter->v, sizeof (double), nvar, sink_file)
376 msg (FE, _("Error writing FLIP file: %s."),
383 /* Write to external file. */
385 double *d = local_alloc (sizeof *d * nvar);
388 for (i = 0; i < nvar; i++)
389 if (var[i]->type == NUMERIC)
390 d[i] = temp_case->data[var[i]->fv].f;
394 if (fwrite (d, sizeof *d, nvar, sink_file) != (size_t) nvar)
395 msg (FE, _("Error writing FLIP file: %s."),
402 /* Transpose the external file. */
404 transpose_external_file (void)
406 unsigned long n_cases;
407 unsigned long cur_case;
408 double *case_buf, *temp_buf;
410 n_cases = 4 * 1024 * 1024 / ((nvar + 1) * sizeof *case_buf);
415 assert (n_cases >= 2 /* 1 */);
416 case_buf = ((n_cases <= 2 ? xmalloc : (void *(*)(size_t)) malloc)
417 ((nvar + 1) * sizeof *case_buf * n_cases));
426 /* A temporary buffer that holds n_cases elements. */
427 temp_buf = &case_buf[nvar * n_cases];
429 src_file = tmpfile ();
431 msg (FE, _("Error creating FLIP source file."));
433 if (fseek (sink_file, 0, SEEK_SET) != 0)
434 msg (FE, _("Error rewinding FLIP file: %s."), strerror (errno));
436 for (cur_case = 0; cur_case < sink_cases; )
438 unsigned long read_cases = min (sink_cases - cur_case, n_cases);
441 if (read_cases != fread (case_buf, sizeof *case_buf * nvar,
442 read_cases, sink_file))
443 msg (FE, _("Error reading FLIP file: %s."), strerror (errno));
445 for (i = 0; i < nvar; i++)
449 for (j = 0; j < read_cases; j++)
450 temp_buf[j] = case_buf[i + j * nvar];
453 sizeof *case_buf * (cur_case + i * sink_cases),
455 msg (FE, _("Error seeking FLIP source file: %s."),
458 if (fwrite (temp_buf, sizeof *case_buf, read_cases, src_file)
460 msg (FE, _("Error writing FLIP source file: %s."),
464 cur_case += read_cases;
467 if (fseek (src_file, 0, SEEK_SET) != 0)
468 msg (FE, _("Error rewind FLIP source file: %s."), strerror (errno));
475 /* Change the FLIP stream from sink to source mode. */
477 flip_stream_mode (void)
479 src_cases = sink_cases;
480 src_old_names = sink_old_names;
481 sink_old_names = NULL;
499 transpose_external_file ();
503 /* Destroy source's internal data. */
505 flip_stream_destroy_source (void)
507 free (src_old_names);
510 struct flip_case *iter, *next;
512 for (iter = src; iter; iter = next)
522 /* Destroy sink's internal data. */
524 flip_stream_destroy_sink (void)
526 struct flip_case *iter, *next;
528 free (sink_old_names);
533 for (iter = head; iter; iter = next)
540 struct case_stream flip_stream =
546 flip_stream_destroy_source,
547 flip_stream_destroy_sink,