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
36 /* Variables to transpose. */
37 static struct variable **var;
40 /* Variable containing new variable names. */
41 static struct variable *newnames;
43 /* List of variable names. */
50 /* New variable names. */
51 static struct varname *new_names_head, *new_names_tail;
52 static int case_count;
54 static int build_dictionary (void);
56 /* Parses and executes FLIP. */
60 lex_match_id ("FLIP");
62 if (lex_match_id ("VARIABLES"))
65 if (!parse_variables (default_dict, &var, &nvar, PV_NO_DUPLICATE))
70 dict_get_vars (default_dict, &var, &nvar, 1u << DC_SYSTEM);
73 if (lex_match_id ("NEWNAMES"))
76 newnames = parse_variable ();
84 newnames = dict_lookup_var (default_dict, "CASE_LBL");
90 for (i = 0; i < nvar; i++)
91 if (var[i] == newnames)
93 memmove (&var[i], &var[i + 1], sizeof *var * (nvar - i - 1));
100 temp_trns = temporary = 0;
101 vfm_sink = &flip_stream;
102 new_names_tail = NULL;
103 procedure (NULL, NULL, NULL);
105 dict_clear (default_dict);
106 if (!build_dictionary ())
108 discard_variables ();
114 return lex_end_of_command ();
117 /* Make a new variable with base name NAME, which is bowdlerized and
118 mangled until acceptable, and returns success. */
120 make_new_var (char name[])
122 /* Fix invalid characters. */
126 for (cp = name; *cp && !isspace (*cp); cp++)
128 *cp = toupper ((unsigned char) *cp);
129 if (!isalpha (*cp) && *cp != '@' && *cp != '#'
130 && (cp == name || (*cp != '.' && *cp != '$' && *cp != '_'
134 *cp = 'V'; /* _ not valid in first position. */
142 if (dict_create_var (default_dict, name, 0))
145 /* Add numeric extensions until acceptable. */
147 int len = (int) strlen (name);
151 for (i = 1; i < 10000000; i++)
153 int ofs = min (7 - intlog10 (i), len);
154 memcpy (n, name, ofs);
155 sprintf (&n[ofs], "%d", i);
157 if (dict_create_var (default_dict, n, 0))
162 msg (SE, _("Could not create acceptable variant for variable %s."), name);
166 /* Make a new dictionary for all the new variable names. */
168 build_dictionary (void)
170 if (!dict_create_var (default_dict, "CASE_LBL", 8))
177 if (case_count > 99999)
179 msg (SE, _("Cannot create more than 99999 variable names."));
183 for (i = 0; i < case_count; i++)
188 sprintf (s, "VAR%03d", i);
189 v = dict_create_var (default_dict, s, 0);
195 struct varname *v, *n;
197 new_names_tail->next = NULL;
198 for (v = new_names_head; v; v = n)
201 if (!make_new_var (v->name))
218 /* Each case to be transposed. */
221 struct flip_case *next;
225 /* Sink: Cases during transposition. */
226 static int internal; /* Internal vs. external flipping. */
227 static char *sink_old_names; /* Old variable names. */
228 static unsigned long sink_cases; /* Number of cases. */
229 static struct flip_case *head, *tail; /* internal == 1: Cases. */
230 static FILE *sink_file; /* internal == 0: Temporary file. */
232 /* Source: Cases after transposition. */
233 static struct flip_case *src; /* Internal transposition records. */
234 static char *src_old_names; /* Old variable names. */
235 static unsigned long src_cases; /* Number of cases. */
236 static FILE *src_file; /* src == NULL: Temporary file. */
238 /* Initialize the FLIP stream. */
240 flip_stream_init (void)
251 for (i = 0; i < nvar; i++)
252 n += strlen (var[i]->name);
253 p = sink_old_names = xmalloc (n);
254 for (i = 0; i < nvar; i++)
255 p = stpcpy (p, var[i]->name) + 1;
259 /* Reads the FLIP stream and passes it to write_case(). */
261 flip_stream_read (void)
263 if (src || (src == NULL && src_file == NULL))
265 /* Internal transposition, or empty file. */
267 char *p = src_old_names;
269 for (i = 0; i < nvar; i++)
271 struct flip_case *iter;
273 st_bare_pad_copy (temp_case->data[0].s, p, 8);
274 p = strchr (p, 0) + 1;
276 for (iter = src, j = 1; iter; iter = iter->next, j++)
277 temp_case->data[j].f = iter->v[i];
286 char *p = src_old_names;
288 for (i = 0; i < nvar; i++)
290 st_bare_pad_copy (temp_case->data[0].s, p, 8);
291 p = strchr (p, 0) + 1;
293 if (fread (&temp_case->data[1], sizeof (double), src_cases,
294 src_file) != src_cases)
295 msg (FE, _("Error reading FLIP source file: %s."),
304 /* Writes temp_case to the FLIP stream. */
306 flip_stream_write (void)
312 struct varname *v = xmalloc (sizeof (struct varname));
313 if (newnames->type == NUMERIC)
315 double f = temp_case->data[newnames->fv].f;
318 strcpy (v->name, "VSYSMIS");
319 else if (f < INT_MIN)
320 strcpy (v->name, "VNEGINF");
321 else if (f > INT_MAX)
322 strcpy (v->name, "VPOSINF");
325 char name[INT_DIGITS + 2];
326 sprintf (name, "V%d", (int) f);
327 strncpy (v->name, name, 8);
333 int width = min (newnames->width, 8);
334 memcpy (v->name, temp_case->data[newnames->fv].s, width);
338 if (new_names_tail == NULL)
341 new_names_tail->next = v;
350 flip_case *c = malloc (sizeof (flip_case)
351 + sizeof (double) * (nvar - 1));
355 /* Write to internal file. */
358 for (i = 0; i < nvar; i++)
359 if (var[i]->type == NUMERIC)
360 c->v[i] = temp_case->data[var[i]->fv].f;
375 /* Initialize external file. */
376 struct flip_case *iter, *next;
380 sink_file = tmpfile ();
382 msg (FE, _("Could not create temporary file for FLIP."));
386 for (iter = head; iter; iter = next)
390 if (fwrite (iter->v, sizeof (double), nvar, sink_file)
392 msg (FE, _("Error writing FLIP file: %s."),
399 /* Write to external file. */
401 double *d = local_alloc (sizeof *d * nvar);
404 for (i = 0; i < nvar; i++)
405 if (var[i]->type == NUMERIC)
406 d[i] = temp_case->data[var[i]->fv].f;
410 if (fwrite (d, sizeof *d, nvar, sink_file) != (size_t) nvar)
411 msg (FE, _("Error writing FLIP file: %s."),
418 /* Transpose the external file. */
420 transpose_external_file (void)
422 unsigned long n_cases;
423 unsigned long cur_case;
424 double *case_buf, *temp_buf;
426 n_cases = 4 * 1024 * 1024 / ((nvar + 1) * sizeof *case_buf);
431 assert (n_cases >= 2 /* 1 */);
432 case_buf = ((n_cases <= 2 ? xmalloc : (void *(*)(size_t)) malloc)
433 ((nvar + 1) * sizeof *case_buf * n_cases));
442 /* A temporary buffer that holds n_cases elements. */
443 temp_buf = &case_buf[nvar * n_cases];
445 src_file = tmpfile ();
447 msg (FE, _("Error creating FLIP source file."));
449 if (fseek (sink_file, 0, SEEK_SET) != 0)
450 msg (FE, _("Error rewinding FLIP file: %s."), strerror (errno));
452 for (cur_case = 0; cur_case < sink_cases; )
454 unsigned long read_cases = min (sink_cases - cur_case, n_cases);
457 if (read_cases != fread (case_buf, sizeof *case_buf * nvar,
458 read_cases, sink_file))
459 msg (FE, _("Error reading FLIP file: %s."), strerror (errno));
461 for (i = 0; i < nvar; i++)
465 for (j = 0; j < read_cases; j++)
466 temp_buf[j] = case_buf[i + j * nvar];
469 sizeof *case_buf * (cur_case + i * sink_cases),
471 msg (FE, _("Error seeking FLIP source file: %s."),
474 if (fwrite (temp_buf, sizeof *case_buf, read_cases, src_file)
476 msg (FE, _("Error writing FLIP source file: %s."),
480 cur_case += read_cases;
483 if (fseek (src_file, 0, SEEK_SET) != 0)
484 msg (FE, _("Error rewind FLIP source file: %s."), strerror (errno));
491 /* Change the FLIP stream from sink to source mode. */
493 flip_stream_mode (void)
495 src_cases = sink_cases;
496 src_old_names = sink_old_names;
497 sink_old_names = NULL;
515 transpose_external_file ();
519 /* Destroy source's internal data. */
521 flip_stream_destroy_source (void)
523 free (src_old_names);
526 struct flip_case *iter, *next;
528 for (iter = src; iter; iter = next)
538 /* Destroy sink's internal data. */
540 flip_stream_destroy_sink (void)
542 struct flip_case *iter, *next;
544 free (sink_old_names);
549 for (iter = head; iter; iter = next)
556 struct case_stream flip_stream =
562 flip_stream_destroy_source,
563 flip_stream_destroy_sink,